]> git.mxchange.org Git - simgear.git/blob - simgear/misc/commands.cxx
Simplified the command-manager interface.
[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 SGCommandMgr class.
14 ////////////////////////////////////////////////////////////////////////
15
16
17 SGCommandMgr::SGCommandMgr ()
18 {
19   // no-op
20 }
21
22 SGCommandMgr::~SGCommandMgr ()
23 {
24   // no-op
25 }
26
27 void
28 SGCommandMgr::addCommand (const string &name, command_t command)
29 {
30   _commands[name] = command;
31 }
32
33 SGCommandMgr::command_t
34 SGCommandMgr::getCommand (const string &name) const
35 {
36   const command_map::const_iterator it = _commands.find(name);
37   return (it != _commands.end() ? it->second : 0);
38 }
39
40 vector<string>
41 SGCommandMgr::getCommandNames () const
42 {
43   vector<string> names;
44   command_map::const_iterator it = _commands.begin();
45   command_map::const_iterator last = _commands.end();
46   while (it != last) {
47     names.push_back(it->first);
48     it++;
49   }
50   return names;
51 }
52
53 bool
54 SGCommandMgr::execute (const string &name, const SGPropertyNode * arg) const
55 {
56   command_t command = getCommand(name);
57   if (command == 0)
58     return false;
59   else
60     return (*command)(arg);
61 }
62
63 // end of commands.cxx