X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fstructure%2Fcommands.hxx;h=7f4185ec421fad63aa33b4b5ae3f336dfc09baee;hb=5c9f5361bda56dddad8068d8e45dc08584440d70;hp=0825ffd9fc58c3c97b966d3d498dd11892acd1a6;hpb=7e7ce2f38e87d6244e05730fa4382da088bb25f1;p=simgear.git diff --git a/simgear/structure/commands.hxx b/simgear/structure/commands.hxx index 0825ffd9..7f4185ec 100644 --- a/simgear/structure/commands.hxx +++ b/simgear/structure/commands.hxx @@ -15,17 +15,12 @@ #include #include -#include - -#include - -#include - -using std::string; -using std::map; -using std::vector; +#include +// forward decls +class SGPropertyNode; + /** * Manage commands. * @@ -39,35 +34,90 @@ using std::vector; 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); - +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(pObj, pMemFn ); + } + +public: + /** + * Default constructor (sets instance to created item) + */ + SGCommandMgr (); /** - * Destructor. + * Destructor. (sets instance to NULL) */ virtual ~SGCommandMgr (); - /** - * Implement the classical singleton. - */ static SGCommandMgr* instance(); /** * Register a new command with the manager. * - * @param name The command name. Any existing command with - * the same name will silently be overwritten. - * @param command A pointer to a one-arg function returning - * a bool result. The argument is always a const pointer to - * an SGPropertyNode (which may contain multiple values). + * @param name The command name. Any existing command with the same name + * will silently be overwritten. + * @param f A pointer to a one-arg function returning 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 + void addCommand(const std::string& name, const OBJ& o, METHOD m) + { + addCommandObject(name, make_functor(o,m)); + } + /** * Look up an existing command. * @@ -75,7 +125,7 @@ public: * @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; /** @@ -84,7 +134,7 @@ public: * @return A (possibly empty) vector of the names of all registered * commands. */ - virtual vector getCommandNames () const; + virtual string_list getCommandNames () const; /** @@ -97,22 +147,21 @@ public: * @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; -protected: /** - * Default constructor. + * Remove a command registration */ - SGCommandMgr (); + bool removeCommand(const std::string& name); +protected: + private: - typedef map command_map; + typedef std::map command_map; command_map _commands; - static OpenThreads::Mutex _instanceMutex; - }; #endif // __COMMANDS_HXX