]> git.mxchange.org Git - simgear.git/blob - simgear/structure/commands.cxx
Working 'noshadow' animation
[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::addCommand (const std::string &name, command_t command)
56 {
57   _commands[name] = command;
58 }
59
60 SGCommandMgr::command_t
61 SGCommandMgr::getCommand (const std::string &name) const
62 {
63   const command_map::const_iterator it = _commands.find(name);
64   return (it != _commands.end() ? it->second : 0);
65 }
66
67 string_list
68 SGCommandMgr::getCommandNames () const
69 {
70   string_list names;
71   command_map::const_iterator it = _commands.begin();
72   command_map::const_iterator last = _commands.end();
73   while (it != last) {
74     names.push_back(it->first);
75     ++it;
76   }
77   return names;
78 }
79
80 bool
81 SGCommandMgr::execute (const std::string &name, const SGPropertyNode * arg) const
82 {
83   command_t command = getCommand(name);
84   if (command == 0)
85     return false;
86
87
88   try {
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() << ")");
93     return false;
94   }
95 }
96
97 // end of commands.cxx