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>
14 #include <simgear/props/props_io.hxx>
16 #include "commands.hxx"
18 #include <simgear/structure/exception.hxx>
19 #include <simgear/threads/SGThread.hxx>
20 #include <simgear/threads/SGGuard.hxx>
21 #include <simgear/debug/logstream.hxx>
24 ////////////////////////////////////////////////////////////////////////
25 // Implementation of SGCommandMgr class.
26 ////////////////////////////////////////////////////////////////////////
28 static SGCommandMgr* static_instance = NULL;
30 SGCommandMgr::SGCommandMgr ()
32 assert(static_instance == NULL);
33 static_instance = this;
36 SGCommandMgr::~SGCommandMgr ()
38 assert(static_instance == this);
39 static_instance = NULL;
43 SGCommandMgr::instance()
45 return static_instance;
49 SGCommandMgr::addCommandObject (const std::string &name, Command* command)
51 if (_commands.find(name) != _commands.end())
52 throw sg_exception("duplicate command name:" + name);
54 _commands[name] = command;
57 SGCommandMgr::Command*
58 SGCommandMgr::getCommand (const std::string &name) const
60 const command_map::const_iterator it = _commands.find(name);
61 return (it != _commands.end() ? it->second : 0);
65 SGCommandMgr::getCommandNames () const
68 command_map::const_iterator it = _commands.begin();
69 command_map::const_iterator last = _commands.end();
71 names.push_back(it->first);
78 SGCommandMgr::execute (const std::string &name, const SGPropertyNode * arg) const
80 Command* command = getCommand(name);
83 SG_LOG(SG_GENERAL, SG_WARN, "command not found: '" << name << "'");
89 return (*command)(arg);
91 catch(sg_exception& e)
97 "command '" << name << "' failed with exception\n"
98 "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")"
101 catch(std::exception& ex)
107 "command '" << name << "' failed with exception: " << ex.what()
116 "command '" << name << "' failed with unknown exception."
123 bool SGCommandMgr::removeCommand(const std::string& name)
125 command_map::iterator it = _commands.find(name);
126 if (it == _commands.end())
134 // end of commands.cxx