]> git.mxchange.org Git - simgear.git/blob - simgear/misc/commands.cxx
MSVC++ bug work-around from Frederic Bouvier.
[simgear.git] / simgear / misc / 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 "commands.hxx"
8 #include "props_io.hxx"
9
10
11 \f
12 ////////////////////////////////////////////////////////////////////////
13 // Implementation of SGCommandState class.
14 ////////////////////////////////////////////////////////////////////////
15
16 SGCommandState::SGCommandState ()
17   : _args(0)
18 {
19 }
20
21 SGCommandState::SGCommandState (const SGPropertyNode * args)
22   : _args(0)
23 {
24   setArgs(args);
25 }
26
27 SGCommandState::~SGCommandState ()
28 {
29   delete _args;
30 }
31
32 void
33 SGCommandState::setArgs (const SGPropertyNode * args)
34 {
35   delete _args;
36   _args = new SGPropertyNode();
37   copyProperties(args, _args);
38 }
39
40
41 \f
42 ////////////////////////////////////////////////////////////////////////
43 // Implementation of SGCommandMgr class.
44 ////////////////////////////////////////////////////////////////////////
45
46
47 SGCommandMgr::SGCommandMgr ()
48 {
49   // no-op
50 }
51
52 SGCommandMgr::~SGCommandMgr ()
53 {
54   // no-op
55 }
56
57 void
58 SGCommandMgr::addCommand (const string &name, command_t command)
59 {
60   _commands[name] = command;
61 }
62
63 SGCommandMgr::command_t
64 SGCommandMgr::getCommand (const string &name) const
65 {
66   const command_map::const_iterator it = _commands.find(name);
67   return (it != _commands.end() ? it->second : 0);
68 }
69
70 vector<string>
71 SGCommandMgr::getCommandNames () const
72 {
73   vector<string> names;
74   command_map::const_iterator it = _commands.begin();
75   command_map::const_iterator last = _commands.end();
76   while (it != last) {
77     names.push_back(it->first);
78     it++;
79   }
80   return names;
81 }
82
83 bool
84 SGCommandMgr::execute (const string &name, const SGPropertyNode * arg,
85                        SGCommandState ** state) const
86 {
87   command_t command = getCommand(name);
88   if (command == 0)
89     return false;
90   else
91     return (*command)(arg, state);
92 }
93
94 // end of commands.cxx