]> git.mxchange.org Git - simgear.git/blob - simgear/structure/commands.cxx
Fix use count for deleted, reference counted objects.
[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 <cassert>
13
14 #include <simgear/props/props_io.hxx>
15
16 #include "commands.hxx"
17
18 #include <simgear/structure/exception.hxx>
19 #include <simgear/threads/SGThread.hxx>
20 #include <simgear/threads/SGGuard.hxx>
21 #include <simgear/debug/logstream.hxx>
22
23 \f
24 ////////////////////////////////////////////////////////////////////////
25 // Implementation of SGCommandMgr class.
26 ////////////////////////////////////////////////////////////////////////
27
28 static SGCommandMgr* static_instance = NULL;
29
30 SGCommandMgr::SGCommandMgr ()
31 {
32     assert(static_instance == NULL);
33     static_instance = this;
34 }
35
36 SGCommandMgr::~SGCommandMgr ()
37 {
38     assert(static_instance == this);
39     static_instance = NULL;
40 }
41
42 SGCommandMgr*
43 SGCommandMgr::instance()
44 {
45     return static_instance ? static_instance : new SGCommandMgr;
46 }
47
48 void
49 SGCommandMgr::addCommandObject (const std::string &name, Command* command)
50 {
51     if (_commands.find(name) != _commands.end())
52         throw sg_exception("duplicate command name:" + name);
53     
54   _commands[name] = command;
55 }
56
57 SGCommandMgr::Command*
58 SGCommandMgr::getCommand (const std::string &name) const
59 {
60   const command_map::const_iterator it = _commands.find(name);
61   return (it != _commands.end() ? it->second : 0);
62 }
63
64 string_list
65 SGCommandMgr::getCommandNames () const
66 {
67   string_list names;
68   command_map::const_iterator it = _commands.begin();
69   command_map::const_iterator last = _commands.end();
70   while (it != last) {
71     names.push_back(it->first);
72     ++it;
73   }
74   return names;
75 }
76
77 bool
78 SGCommandMgr::execute (const std::string &name, const SGPropertyNode * arg) const
79 {
80   Command* command = getCommand(name);
81   if (command == 0)
82   {
83     SG_LOG(SG_GENERAL, SG_WARN, "command not found: '" << name << "'");
84     return false;
85   }
86
87   try
88   {
89     return (*command)(arg);
90   }
91   catch(sg_exception& e)
92   {
93     SG_LOG
94     (
95       SG_GENERAL,
96       SG_ALERT,
97       "command '" << name << "' failed with exception\n"
98       "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")"
99     );
100   }
101   catch(std::exception& ex)
102   {
103     SG_LOG
104     (
105       SG_GENERAL,
106       SG_ALERT,
107       "command '" << name << "' failed with exception: " << ex.what()
108     );
109   }
110   catch(...)
111   {
112     SG_LOG
113     (
114       SG_GENERAL,
115       SG_ALERT,
116       "command '" << name << "' failed with unknown exception."
117     );
118   }
119
120   return false;
121 }
122
123 bool SGCommandMgr::removeCommand(const std::string& name)
124 {
125     command_map::iterator it = _commands.find(name);
126     if (it == _commands.end())
127         return false;
128     
129     delete it->second;
130     _commands.erase(it);
131     return true;
132 }
133
134 // end of commands.cxx