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