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>
19 #include <simgear/math/sg_types.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
38 * Command functor object
43 virtual ~Command() { }
44 virtual bool operator()(const SGPropertyNode * arg) = 0;
48 typedef bool (*command_t) (const SGPropertyNode * arg);
51 class FunctionCommand : public Command
54 FunctionCommand( command_t fun )
57 virtual bool operator()(const SGPropertyNode * arg) { return (*f_)(arg); }
62 template< class ObjPtr, typename MemFn >
63 class MethodCommand : public Command
66 MethodCommand( const ObjPtr& pObj, MemFn pMemFn ) :
67 pObj_(pObj), pMemFn_(pMemFn) {}
69 virtual bool operator()(const SGPropertyNode * arg)
71 return ((*pObj_).*pMemFn_)(arg);
79 * Helper template functions.
82 template< class ObjPtr, typename MemFn >
83 Command* make_functor( const ObjPtr& pObj, MemFn pMemFn )
85 return new MethodCommand<ObjPtr,MemFn>(pObj, pMemFn );
90 * Default constructor (sets instance to created item)
95 * Destructor. (sets instance to NULL)
97 virtual ~SGCommandMgr ();
99 static SGCommandMgr* instance();
102 * Register a new command with the manager.
104 * @param name The command name. Any existing command with the same name
105 * will silently be overwritten.
106 * @param f A pointer to a one-arg function returning a bool result. The
107 * argument is always a const pointer to an SGPropertyNode
108 * (which may contain multiple values).
110 void addCommand(const std::string& name, command_t f)
111 { addCommandObject(name, new FunctionCommand(f)); }
113 void addCommandObject (const std::string &name, Command* command);
115 template<class OBJ, typename METHOD>
116 void addCommand(const std::string& name, const OBJ& o, METHOD m)
118 addCommandObject(name, make_functor(o,m));
122 * Look up an existing command.
124 * @param name The command name.
125 * @return A pointer to the command, or 0 if there is no registered
126 * command with the name specified.
128 virtual Command* getCommand (const std::string &name) const;
132 * Get a list of all existing command names.
134 * @return A (possibly empty) vector of the names of all registered
137 virtual string_list getCommandNames () const;
143 * @param name The name of the command.
144 * @param arg A const pointer to an SGPropertyNode. The node
145 * may have a value and/or children, etc., so that it is possible
146 * to pass an arbitrarily complex data structure to a command.
147 * @return true if the command is present and executes successfully,
150 virtual bool execute (const std::string &name, const SGPropertyNode * arg) const;
153 * Remove a command registration
155 bool removeCommand(const std::string& name);
162 typedef std::map<std::string,Command*> command_map;
163 command_map _commands;
167 #endif // __COMMANDS_HXX
169 // end of commands.hxx