#include <simgear/compiler.h>
-#include STL_STRING
+#include <string>
#include <map>
-#include <vector>
-
-#include <simgear/props/props.hxx>
-
-SG_USING_STD(string);
-SG_USING_STD(map);
-SG_USING_STD(vector);
+#include <simgear/math/sg_types.hxx>
+// forward decls
+class SGPropertyNode;
+
/**
* Manage commands.
*
class SGCommandMgr
{
public:
+ /**
+ * Command functor object
+ */
+ class Command
+ {
+ public:
+ virtual ~Command() { }
+ virtual bool operator()(const SGPropertyNode * arg) = 0;
+ };
+
+
+ typedef bool (*command_t) (const SGPropertyNode * arg);
- /**
- * Type for a command function.
- */
- typedef bool (*command_t) (const SGPropertyNode * arg);
-
-
- /**
- * Default constructor.
- */
- SGCommandMgr ();
-
+private:
+ class FunctionCommand : public Command
+ {
+ public:
+ FunctionCommand( command_t fun )
+ : f_(fun) {}
+
+ virtual bool operator()(const SGPropertyNode * arg) { return (*f_)(arg); }
+ private:
+ command_t f_;
+ };
+
+ template< class ObjPtr, typename MemFn >
+ class MethodCommand : public Command
+ {
+ public:
+ MethodCommand( const ObjPtr& pObj, MemFn pMemFn ) :
+ pObj_(pObj), pMemFn_(pMemFn) {}
+
+ virtual bool operator()(const SGPropertyNode * arg)
+ {
+ return ((*pObj_).*pMemFn_)(arg);
+ }
+ private:
+ ObjPtr pObj_;
+ MemFn pMemFn_;
+ };
+
+ /**
+ * Helper template functions.
+ */
+
+ template< class ObjPtr, typename MemFn >
+ Command* make_functor( const ObjPtr& pObj, MemFn pMemFn )
+ {
+ return new MethodCommand<ObjPtr,MemFn>(pObj, pMemFn );
+ }
+
+public:
+ /**
+ * Default constructor (sets instance to created item)
+ */
+ SGCommandMgr ();
/**
- * Destructor.
+ * Destructor. (sets instance to NULL)
*/
virtual ~SGCommandMgr ();
+ static SGCommandMgr* instance();
/**
* Register a new command with the manager.
* a bool result. The argument is always a const pointer to
* an SGPropertyNode (which may contain multiple values).
*/
- virtual void addCommand (const string &name, command_t command);
-
-
+ void addCommand(const std::string& name, command_t f)
+ { addCommandObject(name, new FunctionCommand(f)); }
+
+ void addCommandObject (const std::string &name, Command* command);
+
+ template<class OBJ, typename METHOD>
+ void addCommand(const std::string& name, const OBJ& o, METHOD m)
+ {
+ addCommandObject(name, make_functor(o,m));
+ }
+
/**
* Look up an existing command.
*
* @return A pointer to the command, or 0 if there is no registered
* command with the name specified.
*/
- virtual command_t getCommand (const string &name) const;
+ virtual Command* getCommand (const std::string &name) const;
/**
* @return A (possibly empty) vector of the names of all registered
* commands.
*/
- virtual vector<string> getCommandNames () const;
+ virtual string_list getCommandNames () const;
/**
* @return true if the command is present and executes successfully,
* false otherwise.
*/
- virtual bool execute (const string &name, const SGPropertyNode * arg) const;
+ virtual bool execute (const std::string &name, const SGPropertyNode * arg) const;
+
+ /**
+ * Remove a command registration
+ */
+ bool removeCommand(const std::string& name);
+protected:
+
+
private:
- typedef map<string,command_t> command_map;
+ typedef std::map<std::string,Command*> command_map;
command_map _commands;
};