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