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/threads/SGThread.hxx>
20 #include <simgear/math/sg_types.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
39 * Command functor object
44 virtual ~Command() { }
45 virtual bool operator()(const SGPropertyNode * arg) = 0;
49 typedef bool (*command_t) (const SGPropertyNode * arg);
52 class FunctionCommand : public Command
55 FunctionCommand( command_t fun )
58 virtual bool operator()(const SGPropertyNode * arg) { return (*f_)(arg); }
63 template< class ObjPtr, typename MemFn >
64 class MethodCommand : public Command
67 MethodCommand( const ObjPtr& pObj, MemFn pMemFn ) :
68 pObj_(pObj), pMemFn_(pMemFn) {}
70 virtual bool operator()(const SGPropertyNode * arg)
72 return ((*pObj_).*pMemFn_)(arg);
80 * Helper template functions.
83 template< class ObjPtr, typename MemFn >
84 Command* make_functor( const ObjPtr& pObj, MemFn pMemFn )
86 return new MethodCommand<ObjPtr,MemFn>(pObj, pMemFn );
95 virtual ~SGCommandMgr ();
98 * Implement the classical singleton.
100 static SGCommandMgr* instance();
103 * Register a new command with the manager.
105 * @param name The command name. Any existing command with
106 * the same name will silently be overwritten.
107 * @param command A pointer to a one-arg function returning
108 * a bool result. The argument is always a const pointer to
109 * an SGPropertyNode (which may contain multiple values).
111 void addCommand(const std::string& name, command_t f)
112 { addCommandObject(name, new FunctionCommand(f)); }
114 void addCommandObject (const std::string &name, Command* command);
116 template<class OBJ, typename METHOD>
117 void addCommand(const std::string& name, const OBJ& o, METHOD m)
119 addCommandObject(name, make_functor(o,m));
123 * Look up an existing command.
125 * @param name The command name.
126 * @return A pointer to the command, or 0 if there is no registered
127 * command with the name specified.
129 virtual Command* getCommand (const std::string &name) const;
133 * Get a list of all existing command names.
135 * @return A (possibly empty) vector of the names of all registered
138 virtual string_list getCommandNames () const;
144 * @param name The name of the command.
145 * @param arg A const pointer to an SGPropertyNode. The node
146 * may have a value and/or children, etc., so that it is possible
147 * to pass an arbitrarily complex data structure to a command.
148 * @return true if the command is present and executes successfully,
151 virtual bool execute (const std::string &name, const SGPropertyNode * arg) const;
154 * Remove a command registration
156 bool removeCommand(const std::string& name);
159 * Default constructor.
166 typedef std::map<std::string,Command*> command_map;
167 command_map _commands;
169 static SGMutex _instanceMutex;
173 #endif // __COMMANDS_HXX
175 // end of commands.hxx