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 <simgear/threads/SGThread.hxx>
21 #include <simgear/math/sg_types.hxx>
22 #include <simgear/props/props.hxx>
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>
32 * @author David Megginson, david@megginson.com
39 * Type for a command function.
41 typedef bool (*command_t) (const SGPropertyNode * arg);
47 virtual ~SGCommandMgr ();
50 * Implement the classical singleton.
52 static SGCommandMgr* instance();
55 * Register a new command with the manager.
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).
63 virtual void addCommand (const std::string &name, command_t command);
67 * Look up an existing command.
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.
73 virtual command_t getCommand (const std::string &name) const;
77 * Get a list of all existing command names.
79 * @return A (possibly empty) vector of the names of all registered
82 virtual string_list getCommandNames () const;
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,
95 virtual bool execute (const std::string &name, const SGPropertyNode * arg) const;
99 * Default constructor.
106 typedef std::map<std::string,command_t> command_map;
107 command_map _commands;
109 static SGMutex _instanceMutex;
113 #endif // __COMMANDS_HXX
115 // end of commands.hxx