00001
00002
00003
00004
00005
00006
00007 #ifdef HAVE_CONFIG_H
00008 # include <simgear_config.h>
00009 #endif
00010
00011 #include <memory>
00012 #include <simgear/props/props_io.hxx>
00013
00014 #include <OpenThreads/Mutex>
00015 #include <OpenThreads/ScopedLock>
00016
00017 #include "commands.hxx"
00018
00019 #include <simgear/math/SGMath.hxx>
00020
00021
00022
00024
00026
00027
00028 SGCommandMgr::SGCommandMgr ()
00029 {
00030
00031 }
00032
00033 SGCommandMgr::~SGCommandMgr ()
00034 {
00035
00036 }
00037
00038 OpenThreads::Mutex SGCommandMgr::_instanceMutex;
00039
00040 SGCommandMgr*
00041 SGCommandMgr::instance()
00042 {
00043 static std::auto_ptr<SGCommandMgr> mgr;
00044 if (mgr.get())
00045 return mgr.get();
00046
00047 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_instanceMutex);
00048 if (mgr.get())
00049 return mgr.get();
00050
00051 mgr = std::auto_ptr<SGCommandMgr>(new SGCommandMgr);
00052 return mgr.get();
00053 }
00054
00055 void
00056 SGCommandMgr::addCommand (const string &name, command_t command)
00057 {
00058 _commands[name] = command;
00059 }
00060
00061 SGCommandMgr::command_t
00062 SGCommandMgr::getCommand (const string &name) const
00063 {
00064 const command_map::const_iterator it = _commands.find(name);
00065 return (it != _commands.end() ? it->second : 0);
00066 }
00067
00068 vector<string>
00069 SGCommandMgr::getCommandNames () const
00070 {
00071 vector<string> names;
00072 command_map::const_iterator it = _commands.begin();
00073 command_map::const_iterator last = _commands.end();
00074 while (it != last) {
00075 names.push_back(it->first);
00076 it++;
00077 }
00078 return names;
00079 }
00080
00081 bool
00082 SGCommandMgr::execute (const string &name, const SGPropertyNode * arg) const
00083 {
00084 command_t command = getCommand(name);
00085 if (command == 0)
00086 return false;
00087 else
00088 return (*command)(arg);
00089 }
00090
00091