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/structure/exception.hxx>
17 #include <simgear/threads/SGThread.hxx>
18 #include <simgear/threads/SGGuard.hxx>
19 #include <simgear/debug/logstream.hxx>
22 ////////////////////////////////////////////////////////////////////////
23 // Implementation of SGCommandMgr class.
24 ////////////////////////////////////////////////////////////////////////
27 SGCommandMgr::SGCommandMgr ()
32 SGCommandMgr::~SGCommandMgr ()
37 SGMutex SGCommandMgr::_instanceMutex;
40 SGCommandMgr::instance()
42 static std::auto_ptr<SGCommandMgr> mgr;
46 SGGuard<SGMutex> lock(_instanceMutex);
50 mgr = std::auto_ptr<SGCommandMgr>(new SGCommandMgr);
55 SGCommandMgr::addCommandObject (const std::string &name, Command* command)
57 if (_commands.find(name) != _commands.end())
58 throw sg_exception("duplicate command name:" + name);
60 _commands[name] = command;
63 SGCommandMgr::Command*
64 SGCommandMgr::getCommand (const std::string &name) const
66 const command_map::const_iterator it = _commands.find(name);
67 return (it != _commands.end() ? it->second : 0);
71 SGCommandMgr::getCommandNames () const
74 command_map::const_iterator it = _commands.begin();
75 command_map::const_iterator last = _commands.end();
77 names.push_back(it->first);
84 SGCommandMgr::execute (const std::string &name, const SGPropertyNode * arg) const
86 Command* command = getCommand(name);
89 SG_LOG(SG_GENERAL, SG_WARN, "command not found: '" << name << "'");
95 return (*command)(arg);
97 catch(sg_exception& e)
103 "command '" << name << "' failed with exception\n"
104 "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")"
107 catch(std::exception& ex)
113 "command '" << name << "' failed with exception: " << ex.what()
122 "command '" << name << "' failed with unknown exception."
129 // end of commands.cxx