From 0c55a4d7bbdd71d162312efc56d57f9273b1c83c Mon Sep 17 00:00:00 2001 From: James Turner Date: Mon, 9 Aug 2010 09:12:26 +0100 Subject: [PATCH] Catch exceptions raised executing a command. --- simgear/structure/SGBinding.hxx | 2 +- simgear/structure/commands.cxx | 22 +++++++++++++++------- simgear/structure/commands.hxx | 16 ++++++---------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/simgear/structure/SGBinding.hxx b/simgear/structure/SGBinding.hxx index b15f7061..25e1601e 100644 --- a/simgear/structure/SGBinding.hxx +++ b/simgear/structure/SGBinding.hxx @@ -58,7 +58,7 @@ public: * * @return The string name of the command for this binding. */ - const string &getCommandName () const { return _command_name; } + const std::string &getCommandName () const { return _command_name; } /** diff --git a/simgear/structure/commands.cxx b/simgear/structure/commands.cxx index b2391e3b..6f6db1c6 100644 --- a/simgear/structure/commands.cxx +++ b/simgear/structure/commands.cxx @@ -17,7 +17,8 @@ #include "commands.hxx" #include - +#include +#include //////////////////////////////////////////////////////////////////////// @@ -53,22 +54,22 @@ SGCommandMgr::instance() } void -SGCommandMgr::addCommand (const string &name, command_t command) +SGCommandMgr::addCommand (const std::string &name, command_t command) { _commands[name] = command; } SGCommandMgr::command_t -SGCommandMgr::getCommand (const string &name) const +SGCommandMgr::getCommand (const std::string &name) const { const command_map::const_iterator it = _commands.find(name); return (it != _commands.end() ? it->second : 0); } -vector +string_list SGCommandMgr::getCommandNames () const { - vector names; + string_list names; command_map::const_iterator it = _commands.begin(); command_map::const_iterator last = _commands.end(); while (it != last) { @@ -79,13 +80,20 @@ SGCommandMgr::getCommandNames () const } bool -SGCommandMgr::execute (const string &name, const SGPropertyNode * arg) const +SGCommandMgr::execute (const std::string &name, const SGPropertyNode * arg) const { command_t command = getCommand(name); if (command == 0) return false; - else + + + try { return (*command)(arg); + } catch (sg_exception& e) { + SG_LOG(SG_GENERAL, SG_ALERT, "command '" << name << "' failed with exception\n" + << "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")"); + return false; + } } // end of commands.cxx diff --git a/simgear/structure/commands.hxx b/simgear/structure/commands.hxx index 0825ffd9..3f8a248f 100644 --- a/simgear/structure/commands.hxx +++ b/simgear/structure/commands.hxx @@ -19,13 +19,9 @@ #include +#include #include -using std::string; -using std::map; -using std::vector; - - /** * Manage commands. * @@ -65,7 +61,7 @@ public: * 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); + virtual void addCommand (const std::string &name, command_t command); /** @@ -75,7 +71,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_t getCommand (const std::string &name) const; /** @@ -84,7 +80,7 @@ public: * @return A (possibly empty) vector of the names of all registered * commands. */ - virtual vector getCommandNames () const; + virtual string_list getCommandNames () const; /** @@ -97,7 +93,7 @@ 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: /** @@ -108,7 +104,7 @@ protected: private: - typedef map command_map; + typedef std::map command_map; command_map _commands; static OpenThreads::Mutex _instanceMutex; -- 2.39.5