1 // commands.cxx - encapsulated commands.
2 // Started Spring 2001 by David Megginson, david@megginson.com
3 // This code is released into the Public Domain.
8 # include <simgear_config.h>
12 #include <simgear/props/props_io.hxx>
14 #include <OpenThreads/Mutex>
15 #include <OpenThreads/ScopedLock>
17 #include "commands.hxx"
19 #include <simgear/math/SGMath.hxx>
20 #include <simgear/structure/exception.hxx>
21 #include <simgear/debug/logstream.hxx>
24 ////////////////////////////////////////////////////////////////////////
25 // Implementation of SGCommandMgr class.
26 ////////////////////////////////////////////////////////////////////////
29 SGCommandMgr::SGCommandMgr ()
34 SGCommandMgr::~SGCommandMgr ()
39 OpenThreads::Mutex SGCommandMgr::_instanceMutex;
42 SGCommandMgr::instance()
44 static std::auto_ptr<SGCommandMgr> mgr;
48 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_instanceMutex);
52 mgr = std::auto_ptr<SGCommandMgr>(new SGCommandMgr);
57 SGCommandMgr::addCommand (const std::string &name, command_t command)
59 _commands[name] = command;
62 SGCommandMgr::command_t
63 SGCommandMgr::getCommand (const std::string &name) const
65 const command_map::const_iterator it = _commands.find(name);
66 return (it != _commands.end() ? it->second : 0);
70 SGCommandMgr::getCommandNames () const
73 command_map::const_iterator it = _commands.begin();
74 command_map::const_iterator last = _commands.end();
76 names.push_back(it->first);
83 SGCommandMgr::execute (const std::string &name, const SGPropertyNode * arg) const
85 command_t command = getCommand(name);
91 return (*command)(arg);
92 } catch (sg_exception& e) {
93 SG_LOG(SG_GENERAL, SG_ALERT, "command '" << name << "' failed with exception\n"
94 << "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")");
99 // end of commands.cxx