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 template< typename Fun >
50 class FunctionCommand : public Command
53 FunctionCommand( const Fun* fun )
56 virtual bool operator()(const SGPropertyNode * arg) { return (*f_)(arg); }
61 template< class ObjPtr, typename MemFn >
62 class MethodCommand : public Command
65 MethodCommand( const ObjPtr& pObj, MemFn pMemFn ) :
66 pObj_(pObj), pMemFn_(pMemFn) {}
68 virtual bool operator()(const SGPropertyNode * arg)
70 return ((*pObj_).*pMemFn_)(arg);
78 * Helper template functions.
81 template< typename Fun >
82 Command* make_functor( const Fun* fun )
84 return new FunctionCommand<Fun>(fun);
87 template< class ObjPtr, typename MemFn >
88 Command* make_functor( const ObjPtr& pObj, MemFn pMemFn )
90 return new MethodCommand<ObjPtr,MemFn>(pObj, pMemFn );
95 typedef bool (*command_t) (const SGPropertyNode * arg);
100 virtual ~SGCommandMgr ();
103 * Implement the classical singleton.
105 static SGCommandMgr* instance();
108 * Register a new command with the manager.
110 * @param name The command name. Any existing command with
111 * the same name will silently be overwritten.
112 * @param command A pointer to a one-arg function returning
113 * a bool result. The argument is always a const pointer to
114 * an SGPropertyNode (which may contain multiple values).
116 template<typename FUNC>
117 void addCommand(const std::string& name, const FUNC* f)
118 { addCommand(name, make_functor(f)); }
120 void addCommand (const std::string &name, Command* command);
122 template<class OBJ, typename METHOD>
123 void addCommand(const std::string& name, const OBJ& o, METHOD m)
125 addCommand(name, make_functor(o,m));
129 * Look up an existing command.
131 * @param name The command name.
132 * @return A pointer to the command, or 0 if there is no registered
133 * command with the name specified.
135 virtual Command* getCommand (const std::string &name) const;
139 * Get a list of all existing command names.
141 * @return A (possibly empty) vector of the names of all registered
144 virtual string_list getCommandNames () const;
150 * @param name The name of the command.
151 * @param arg A const pointer to an SGPropertyNode. The node
152 * may have a value and/or children, etc., so that it is possible
153 * to pass an arbitrarily complex data structure to a command.
154 * @return true if the command is present and executes successfully,
157 virtual bool execute (const std::string &name, const SGPropertyNode * arg) const;
161 * Default constructor.
168 typedef std::map<std::string,Command*> command_map;
169 command_map _commands;
171 static SGMutex _instanceMutex;
175 #endif // __COMMANDS_HXX
177 // end of commands.hxx