From 3980f08cc556865eadc94f854b08753aacb8f15d Mon Sep 17 00:00:00 2001 From: curt Date: Fri, 1 Jun 2001 15:28:49 +0000 Subject: [PATCH] Initial revsion of new command manager (contributed by David Megginson) Fix to configure.in. --- configure.in | 1 + simgear/misc/Makefile.am | 2 + simgear/misc/commands.cxx | 132 +++++++++++++++++++++++ simgear/misc/commands.hxx | 215 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 350 insertions(+) create mode 100644 simgear/misc/commands.cxx create mode 100644 simgear/misc/commands.hxx diff --git a/configure.in b/configure.in index f53cdb8f..900d8ee8 100644 --- a/configure.in +++ b/configure.in @@ -34,6 +34,7 @@ AC_PROG_RANLIB AC_PROG_INSTALL AC_PROG_LN_S +AR="ar" OS=`uname -s` if test "$OS" = "IRIX" -o "$OS" = "IRIX64"; then if test "$CXX" = "CC"; then diff --git a/simgear/misc/Makefile.am b/simgear/misc/Makefile.am index a4a37a18..4cd43742 100644 --- a/simgear/misc/Makefile.am +++ b/simgear/misc/Makefile.am @@ -9,6 +9,7 @@ endif lib_LIBRARIES = libsgmisc.a include_HEADERS = \ + commands.hxx \ props.hxx \ sg_path.hxx \ sgstream.hxx \ @@ -18,6 +19,7 @@ include_HEADERS = \ zfstream.hxx libsgmisc_a_SOURCES = \ + commands.cxx \ props.cxx \ props_io.cxx \ sg_path.cxx \ diff --git a/simgear/misc/commands.cxx b/simgear/misc/commands.cxx new file mode 100644 index 00000000..73964428 --- /dev/null +++ b/simgear/misc/commands.cxx @@ -0,0 +1,132 @@ +// commands.cxx - encapsulated commands. +// Started Spring 2001 by David Megginson, david@megginson.com +// This code is released into the Public Domain. +// +// $Id$ + +#include "commands.hxx" + + + +//////////////////////////////////////////////////////////////////////// +// Implementation of SGCommandMgr class. +//////////////////////////////////////////////////////////////////////// + + +SGCommandMgr::SGCommandMgr () +{ + // no-op +} + +SGCommandMgr::~SGCommandMgr () +{ + // no-op +} + +void +SGCommandMgr::addCommand (const string &name, command_t command) +{ + _commands[name] = command; +} + +SGCommandMgr::command_t +SGCommandMgr::getCommand (const string &name) const +{ + const command_map::const_iterator it = _commands.find(name); + return (it != _commands.end() ? it->second : 0); +} + +vector +SGCommandMgr::getCommandNames () const +{ + vector names; + command_map::const_iterator it = _commands.begin(); + command_map::const_iterator last = _commands.end(); + while (it != last) { + names.push_back(it->first); + it++; + } + return names; +} + +bool +SGCommandMgr::execute (const string &name, const SGPropertyNode * arg) const +{ + command_t command = getCommand(name); + if (command == 0) + return false; + else + return (*command)(arg); +} + + +bool +SGCommandMgr::execute (const string &name) const +{ + // FIXME + SGPropertyNode node; + execute(name, &node); +} + + +bool +SGCommandMgr::execute (const string &name, bool value) const +{ + // FIXME + SGPropertyNode node; + node.setBoolValue(value); + execute(name, &node); +} + + +bool +SGCommandMgr::execute (const string &name, int value) const +{ + // FIXME + SGPropertyNode node; + node.setIntValue(value); + execute(name, &node); +} + + +bool +SGCommandMgr::execute (const string &name, long value) const +{ + // FIXME + SGPropertyNode node; + node.setLongValue(value); + execute(name, &node); +} + + +bool +SGCommandMgr::execute (const string &name, float value) const +{ + // FIXME + SGPropertyNode node; + node.setFloatValue(value); + execute(name, &node); +} + + +bool +SGCommandMgr::execute (const string &name, double value) const +{ + // FIXME + SGPropertyNode node; + node.setDoubleValue(value); + execute(name, &node); +} + + +bool +SGCommandMgr::execute (const string &name, string value) const +{ + // FIXME + SGPropertyNode node; + node.setStringValue(value); + execute(name, &node); +} + + +// end of commands.cxx diff --git a/simgear/misc/commands.hxx b/simgear/misc/commands.hxx new file mode 100644 index 00000000..6cfc5381 --- /dev/null +++ b/simgear/misc/commands.hxx @@ -0,0 +1,215 @@ +/** + * \file commands.hxx + * Interface definition for encapsulated commands. + * Started Spring 2001 by David Megginson, david@megginson.com + * This code is released into the Public Domain. + * + * $Id$ + */ + +#ifndef __COMMANDS_HXX +#define __COMMANDS_HXX + + +#include +#include +#include + +#include "props.hxx" + +using std::string; +using std::map; +using std::vector; + + +/** + * Manage commands. + * + *

This class allows the application to register and unregister + * commands, and provides shortcuts for executing them. Commands + * are simple functions that take a const pointer to an SGPropertyNode + * as an argument and return a bool value indicating success or failure. + * The property node may be ignored, or it may contain values that + * the command uses as parameters.

+ * + *

There are convenience methods for invoking a command function + * with no arguments or with a single, primitive argument.

+ * + * @author David Megginson, david@megginson.com + */ +class SGCommandMgr +{ +public: + + /** + * Type for a command function. + */ + typedef bool (*command_t) (const SGPropertyNode * arg); + + + /** + * Default constructor. + */ + SGCommandMgr (); + + + /** + * Destructor. + */ + virtual ~SGCommandMgr (); + + + /** + * 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). + */ + virtual void addCommand (const string &name, command_t command); + + + /** + * Look up an existing command. + * + * @param name The command name. + * @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; + + + /** + * Get a list of all existing command names. + * + * @return A (possibly empty) vector of the names of all registered + * commands. + */ + virtual vector getCommandNames () const; + + + /** + * Execute a command. + * + * This is the primary method for invoking a command; the others + * are convenience methods that invoke this one indirectly. + * + * @param name The name of the command. + * @param arg A const pointer to an SGPropertyNode. The node + * may have a value and/or children, etc., so that it is possible + * to pass an arbitrarily complex data structure to a command. + * @return true if the command is present and executes successfully, + * false otherwise. + */ + virtual bool execute (const string &name, const SGPropertyNode * arg) const; + + + /** + * Execute a command with no argument. + * + * The command function will receive a pointer to a property node + * with no value and no children. + * + * @param name The name of the command. + * @return true if the command is present and executes successfully, + * false otherwise. + */ + virtual bool execute (const string &name) const; + + + /** + * Execute a command with a single bool argument. + * + * The command function will receive a pointer to a property node + * with a bool value and no children. + * + * @param name The name of the command. + * @param arg The bool argument to the command. + * @return true if the command is present and executes successfully, + * false otherwise. + */ + virtual bool execute (const string &name, bool arg) const; + + + /** + * Execute a command with a single int argument. + * + * The command function will receive a pointer to a property node + * with a int value and no children. + * + * @param name The name of the command. + * @param arg The int argument to the command. + * @return true if the command is present and executes successfully, + * false otherwise. + */ + virtual bool execute (const string &name, int arg) const; + + + /** + * Execute a command with a single long argument. + * + * The command function will receive a pointer to a property node + * with a long value and no children. + * + * @param name The name of the command. + * @param arg The long argument to the command. + * @return true if the command is present and executes successfully, + * false otherwise. + */ + virtual bool execute (const string &name, long arg) const; + + + /** + * Execute a command with a single float argument. + * + * The command function will receive a pointer to a property node + * with a float value and no children. + * + * @param name The name of the command. + * @param arg The float argument to the command. + * @return true if the command is present and executes successfully, + * false otherwise. + */ + virtual bool execute (const string &name, float arg) const; + + + /** + * Execute a command with a single double argument. + * + * The command function will receive a pointer to a property node + * with a double value and no children. + * + * @param name The name of the command. + * @param arg The double argument to the command. + * @return true if the command is present and executes successfully, + * false otherwise. + */ + virtual bool execute (const string &name, double arg) const; + + + /** + * Execute a command with a single string argument. + * + * The command function will receive a pointer to a property node + * with a string value and no children. + * + * @param name The name of the command. + * @param arg The string argument to the command. + * @return true if the command is present and executes successfully, + * false otherwise. + */ + virtual bool execute (const string &name, string arg) const; + + +private: + + typedef map command_map; + command_map _commands; + +}; + +#endif __COMMANDS_HXX + +// end of commands.hxx -- 2.39.5