]> git.mxchange.org Git - simgear.git/blob - simgear/structure/commands.hxx
Working 'noshadow' animation
[simgear.git] / simgear / structure / commands.hxx
1 /**
2  * \file commands.hxx
3  * Interface definition for encapsulated commands.
4  * Started Spring 2001 by David Megginson, david@megginson.com
5  * This code is released into the Public Domain.
6  *
7  * $Id$
8  */
9
10 #ifndef __COMMANDS_HXX
11 #define __COMMANDS_HXX
12
13
14 #include <simgear/compiler.h>
15
16 #include <string>
17 #include <map>
18 #include <vector>
19
20 #include <simgear/threads/SGThread.hxx>
21 #include <simgear/math/sg_types.hxx>
22 #include <simgear/props/props.hxx>
23
24 /**
25  * Manage commands.
26  *
27  * <p>This class allows the application to register and unregister
28  * commands, and provides shortcuts for executing them.  Commands are
29  * simple functions that take a const pointer to an SGPropertyNode:
30  * the function may use the nodes children as parameters.</p>
31  *
32  * @author David Megginson, david@megginson.com
33  */
34 class SGCommandMgr
35 {
36 public:
37
38   /**
39    * Type for a command function.
40    */
41   typedef bool (*command_t) (const SGPropertyNode * arg);
42
43
44   /**
45    * Destructor.
46    */
47   virtual ~SGCommandMgr ();
48
49   /**
50    * Implement the classical singleton.
51    */
52   static SGCommandMgr* instance();
53
54   /**
55    * Register a new command with the manager.
56    *
57    * @param name The command name.  Any existing command with
58    * the same name will silently be overwritten.
59    * @param command A pointer to a one-arg function returning
60    * a bool result.  The argument is always a const pointer to
61    * an SGPropertyNode (which may contain multiple values).
62    */
63   virtual void addCommand (const std::string &name, command_t command);
64
65
66   /**
67    * Look up an existing command.
68    *
69    * @param name The command name.
70    * @return A pointer to the command, or 0 if there is no registered
71    * command with the name specified.
72    */
73   virtual command_t getCommand (const std::string &name) const;
74
75
76   /**
77    * Get a list of all existing command names.
78    *
79    * @return A (possibly empty) vector of the names of all registered
80    * commands.
81    */
82   virtual string_list getCommandNames () const;
83
84
85   /**
86    * Execute a command.
87    *
88    * @param name The name of the command.
89    * @param arg A const pointer to an SGPropertyNode.  The node
90    * may have a value and/or children, etc., so that it is possible
91    * to pass an arbitrarily complex data structure to a command.
92    * @return true if the command is present and executes successfully,
93    * false otherwise.
94    */
95   virtual bool execute (const std::string &name, const SGPropertyNode * arg) const;
96
97 protected:
98   /**
99    * Default constructor.
100    */
101   SGCommandMgr ();
102
103
104 private:
105
106   typedef std::map<std::string,command_t> command_map;
107   command_map _commands;
108
109   static SGMutex _instanceMutex;
110
111 };
112
113 #endif // __COMMANDS_HXX
114
115 // end of commands.hxx