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 "commands.hxx"
16 #include <simgear/math/SGMath.hxx>
17 #include <simgear/structure/exception.hxx>
18 #include <simgear/threads/SGThread.hxx>
19 #include <simgear/threads/SGGuard.hxx>
20 #include <simgear/debug/logstream.hxx>
23 ////////////////////////////////////////////////////////////////////////
24 // Implementation of SGCommandMgr class.
25 ////////////////////////////////////////////////////////////////////////
28 SGCommandMgr::SGCommandMgr ()
33 SGCommandMgr::~SGCommandMgr ()
38 SGMutex SGCommandMgr::_instanceMutex;
41 SGCommandMgr::instance()
43 static std::auto_ptr<SGCommandMgr> mgr;
47 SGGuard<SGMutex> lock(_instanceMutex);
51 mgr = std::auto_ptr<SGCommandMgr>(new SGCommandMgr);
56 SGCommandMgr::addCommand (const std::string &name, command_t command)
58 _commands[name] = command;
61 SGCommandMgr::command_t
62 SGCommandMgr::getCommand (const std::string &name) const
64 const command_map::const_iterator it = _commands.find(name);
65 return (it != _commands.end() ? it->second : 0);
69 SGCommandMgr::getCommandNames () const
72 command_map::const_iterator it = _commands.begin();
73 command_map::const_iterator last = _commands.end();
75 names.push_back(it->first);
82 SGCommandMgr::execute (const std::string &name, const SGPropertyNode * arg) const
84 command_t command = getCommand(name);
90 return (*command)(arg);
91 } catch (sg_exception& e) {
92 SG_LOG(SG_GENERAL, SG_ALERT, "command '" << name << "' failed with exception\n"
93 << "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")");
98 // end of commands.cxx