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::addCommand (const std::string &name, command_t command)
57 _commands[name] = command;
60 SGCommandMgr::command_t
61 SGCommandMgr::getCommand (const std::string &name) const
63 const command_map::const_iterator it = _commands.find(name);
64 return (it != _commands.end() ? it->second : 0);
68 SGCommandMgr::getCommandNames () const
71 command_map::const_iterator it = _commands.begin();
72 command_map::const_iterator last = _commands.end();
74 names.push_back(it->first);
81 SGCommandMgr::execute (const std::string &name, const SGPropertyNode * arg) const
83 command_t command = getCommand(name);
89 return (*command)(arg);
90 } catch (sg_exception& e) {
91 SG_LOG(SG_GENERAL, SG_ALERT, "command '" << name << "' failed with exception\n"
92 << "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")");
97 // end of commands.cxx