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.
10 #ifndef __COMMANDS_HXX
11 #define __COMMANDS_HXX
14 #include <simgear/compiler.h>
20 #include <OpenThreads/Mutex>
22 #include <simgear/math/sg_types.hxx>
23 #include <simgear/props/props.hxx>
28 * <p>This class allows the application to register and unregister
29 * commands, and provides shortcuts for executing them. Commands are
30 * simple functions that take a const pointer to an SGPropertyNode:
31 * the function may use the nodes children as parameters.</p>
33 * @author David Megginson, david@megginson.com
40 * Type for a command function.
42 typedef bool (*command_t) (const SGPropertyNode * arg);
48 virtual ~SGCommandMgr ();
51 * Implement the classical singleton.
53 static SGCommandMgr* instance();
56 * Register a new command with the manager.
58 * @param name The command name. Any existing command with
59 * the same name will silently be overwritten.
60 * @param command A pointer to a one-arg function returning
61 * a bool result. The argument is always a const pointer to
62 * an SGPropertyNode (which may contain multiple values).
64 virtual void addCommand (const std::string &name, command_t command);
68 * Look up an existing command.
70 * @param name The command name.
71 * @return A pointer to the command, or 0 if there is no registered
72 * command with the name specified.
74 virtual command_t getCommand (const std::string &name) const;
78 * Get a list of all existing command names.
80 * @return A (possibly empty) vector of the names of all registered
83 virtual string_list getCommandNames () const;
89 * @param name The name of the command.
90 * @param arg A const pointer to an SGPropertyNode. The node
91 * may have a value and/or children, etc., so that it is possible
92 * to pass an arbitrarily complex data structure to a command.
93 * @return true if the command is present and executes successfully,
96 virtual bool execute (const std::string &name, const SGPropertyNode * arg) const;
100 * Default constructor.
107 typedef std::map<std::string,command_t> command_map;
108 command_map _commands;
110 static OpenThreads::Mutex _instanceMutex;
114 #endif // __COMMANDS_HXX
116 // end of commands.hxx