]> git.mxchange.org Git - simgear.git/blob - simgear/structure/commands.cxx
Reset: commands can be removed
[simgear.git] / simgear / structure / commands.cxx
1 // commands.cxx - encapsulated commands.
2 // Started Spring 2001 by David Megginson, david@megginson.com
3 // This code is released into the Public Domain.
4 //
5 // $Id$
6
7 #ifdef HAVE_CONFIG_H
8 #  include <simgear_config.h>
9 #endif
10
11 #include <memory>
12 #include <simgear/props/props_io.hxx>
13
14 #include "commands.hxx"
15
16 #include <simgear/structure/exception.hxx>
17 #include <simgear/threads/SGThread.hxx>
18 #include <simgear/threads/SGGuard.hxx>
19 #include <simgear/debug/logstream.hxx>
20
21 \f
22 ////////////////////////////////////////////////////////////////////////
23 // Implementation of SGCommandMgr class.
24 ////////////////////////////////////////////////////////////////////////
25
26
27 SGCommandMgr::SGCommandMgr ()
28 {
29   // no-op
30 }
31
32 SGCommandMgr::~SGCommandMgr ()
33 {
34   // no-op
35 }
36
37 SGMutex SGCommandMgr::_instanceMutex;
38
39 SGCommandMgr*
40 SGCommandMgr::instance()
41 {
42   static std::auto_ptr<SGCommandMgr> mgr;
43   if (mgr.get())
44     return mgr.get();
45
46   SGGuard<SGMutex> lock(_instanceMutex);
47   if (mgr.get())
48     return mgr.get();
49
50   mgr = std::auto_ptr<SGCommandMgr>(new SGCommandMgr);
51   return mgr.get();
52 }
53
54 void
55 SGCommandMgr::addCommandObject (const std::string &name, Command* command)
56 {
57     if (_commands.find(name) != _commands.end())
58         throw sg_exception("duplicate command name:" + name);
59     
60   _commands[name] = command;
61 }
62
63 SGCommandMgr::Command*
64 SGCommandMgr::getCommand (const std::string &name) const
65 {
66   const command_map::const_iterator it = _commands.find(name);
67   return (it != _commands.end() ? it->second : 0);
68 }
69
70 string_list
71 SGCommandMgr::getCommandNames () const
72 {
73   string_list names;
74   command_map::const_iterator it = _commands.begin();
75   command_map::const_iterator last = _commands.end();
76   while (it != last) {
77     names.push_back(it->first);
78     ++it;
79   }
80   return names;
81 }
82
83 bool
84 SGCommandMgr::execute (const std::string &name, const SGPropertyNode * arg) const
85 {
86   Command* command = getCommand(name);
87   if (command == 0)
88   {
89     SG_LOG(SG_GENERAL, SG_WARN, "command not found: '" << name << "'");
90     return false;
91   }
92
93   try
94   {
95     return (*command)(arg);
96   }
97   catch(sg_exception& e)
98   {
99     SG_LOG
100     (
101       SG_GENERAL,
102       SG_ALERT,
103       "command '" << name << "' failed with exception\n"
104       "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")"
105     );
106   }
107   catch(std::exception& ex)
108   {
109     SG_LOG
110     (
111       SG_GENERAL,
112       SG_ALERT,
113       "command '" << name << "' failed with exception: " << ex.what()
114     );
115   }
116   catch(...)
117   {
118     SG_LOG
119     (
120       SG_GENERAL,
121       SG_ALERT,
122       "command '" << name << "' failed with unknown exception."
123     );
124   }
125
126   return false;
127 }
128
129 bool SGCommandMgr::removeCommand(const std::string& name)
130 {
131     command_map::iterator it = _commands.find(name);
132     if (it == _commands.end())
133         return false;
134     
135     delete it->second;
136     _commands.erase(it);
137     return true;
138 }
139
140 // end of commands.cxx