]> git.mxchange.org Git - simgear.git/blob - simgear/structure/commands.cxx
Merge branch 'topic/modelopt' into next
[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 #include <memory>
8 #include <simgear/props/props_io.hxx>
9
10 #include <OpenThreads/Mutex>
11 #include <OpenThreads/ScopedLock>
12
13 #include "commands.hxx"
14
15
16 \f
17 ////////////////////////////////////////////////////////////////////////
18 // Implementation of SGCommandMgr class.
19 ////////////////////////////////////////////////////////////////////////
20
21
22 SGCommandMgr::SGCommandMgr ()
23 {
24   // no-op
25 }
26
27 SGCommandMgr::~SGCommandMgr ()
28 {
29   // no-op
30 }
31
32 OpenThreads::Mutex SGCommandMgr::_instanceMutex;
33
34 SGCommandMgr*
35 SGCommandMgr::instance()
36 {
37   static std::auto_ptr<SGCommandMgr> mgr;
38   if (mgr.get())
39     return mgr.get();
40
41   OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_instanceMutex);
42   if (mgr.get())
43     return mgr.get();
44
45   mgr = std::auto_ptr<SGCommandMgr>(new SGCommandMgr);
46   return mgr.get();
47 }
48
49 void
50 SGCommandMgr::addCommand (const string &name, command_t command)
51 {
52   _commands[name] = command;
53 }
54
55 SGCommandMgr::command_t
56 SGCommandMgr::getCommand (const string &name) const
57 {
58   const command_map::const_iterator it = _commands.find(name);
59   return (it != _commands.end() ? it->second : 0);
60 }
61
62 vector<string>
63 SGCommandMgr::getCommandNames () const
64 {
65   vector<string> names;
66   command_map::const_iterator it = _commands.begin();
67   command_map::const_iterator last = _commands.end();
68   while (it != last) {
69     names.push_back(it->first);
70     it++;
71   }
72   return names;
73 }
74
75 bool
76 SGCommandMgr::execute (const string &name, const SGPropertyNode * arg) const
77 {
78   command_t command = getCommand(name);
79   if (command == 0)
80     return false;
81   else
82     return (*command)(arg);
83 }
84
85 // end of commands.cxx