]> git.mxchange.org Git - simgear.git/blob - simgear/structure/commands.cxx
two warning fixes
[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 <OpenThreads/Mutex>
15 #include <OpenThreads/ScopedLock>
16
17 #include "commands.hxx"
18
19 #include <simgear/math/SGMath.hxx>
20 #include <simgear/structure/exception.hxx>
21 #include <simgear/debug/logstream.hxx>
22
23 \f
24 ////////////////////////////////////////////////////////////////////////
25 // Implementation of SGCommandMgr class.
26 ////////////////////////////////////////////////////////////////////////
27
28
29 SGCommandMgr::SGCommandMgr ()
30 {
31   // no-op
32 }
33
34 SGCommandMgr::~SGCommandMgr ()
35 {
36   // no-op
37 }
38
39 OpenThreads::Mutex SGCommandMgr::_instanceMutex;
40
41 SGCommandMgr*
42 SGCommandMgr::instance()
43 {
44   static std::auto_ptr<SGCommandMgr> mgr;
45   if (mgr.get())
46     return mgr.get();
47
48   OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_instanceMutex);
49   if (mgr.get())
50     return mgr.get();
51
52   mgr = std::auto_ptr<SGCommandMgr>(new SGCommandMgr);
53   return mgr.get();
54 }
55
56 void
57 SGCommandMgr::addCommand (const std::string &name, command_t command)
58 {
59   _commands[name] = command;
60 }
61
62 SGCommandMgr::command_t
63 SGCommandMgr::getCommand (const std::string &name) const
64 {
65   const command_map::const_iterator it = _commands.find(name);
66   return (it != _commands.end() ? it->second : 0);
67 }
68
69 string_list
70 SGCommandMgr::getCommandNames () const
71 {
72   string_list names;
73   command_map::const_iterator it = _commands.begin();
74   command_map::const_iterator last = _commands.end();
75   while (it != last) {
76     names.push_back(it->first);
77     it++;
78   }
79   return names;
80 }
81
82 bool
83 SGCommandMgr::execute (const std::string &name, const SGPropertyNode * arg) const
84 {
85   command_t command = getCommand(name);
86   if (command == 0)
87     return false;
88   
89   
90   try {
91     return (*command)(arg);
92   } catch (sg_exception& e) {
93     SG_LOG(SG_GENERAL, SG_ALERT, "command '" << name << "' failed with exception\n"
94       << "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")");
95     return false;
96   }
97 }
98
99 // end of commands.cxx