#endif
#include <memory>
+#include <cassert>
+
#include <simgear/props/props_io.hxx>
#include "commands.hxx"
// Implementation of SGCommandMgr class.
////////////////////////////////////////////////////////////////////////
+static SGCommandMgr* static_instance = NULL;
SGCommandMgr::SGCommandMgr ()
{
- // no-op
+ assert(static_instance == NULL);
+ static_instance = this;
}
SGCommandMgr::~SGCommandMgr ()
{
- // no-op
+ assert(static_instance == this);
+ static_instance = NULL;
}
-SGMutex SGCommandMgr::_instanceMutex;
-
SGCommandMgr*
SGCommandMgr::instance()
{
- static std::auto_ptr<SGCommandMgr> mgr;
- if (mgr.get())
- return mgr.get();
-
- SGGuard<SGMutex> lock(_instanceMutex);
- if (mgr.get())
- return mgr.get();
-
- mgr = std::auto_ptr<SGCommandMgr>(new SGCommandMgr);
- return mgr.get();
+ return static_instance ? static_instance : new SGCommandMgr;
}
void
{
Command* command = getCommand(name);
if (command == 0)
+ {
+ SG_LOG(SG_GENERAL, SG_WARN, "command not found: '" << name << "'");
return false;
+ }
-
- try {
+ 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;
}
+ catch(sg_exception& e)
+ {
+ SG_LOG
+ (
+ SG_GENERAL,
+ SG_ALERT,
+ "command '" << name << "' failed with exception\n"
+ "\tmessage:" << e.getMessage() << " (from " << e.getOrigin() << ")"
+ );
+ }
+ catch(std::exception& ex)
+ {
+ SG_LOG
+ (
+ SG_GENERAL,
+ SG_ALERT,
+ "command '" << name << "' failed with exception: " << ex.what()
+ );
+ }
+ catch(...)
+ {
+ SG_LOG
+ (
+ SG_GENERAL,
+ SG_ALERT,
+ "command '" << name << "' failed with unknown exception."
+ );
+ }
+
+ return false;
+}
+
+bool SGCommandMgr::removeCommand(const std::string& name)
+{
+ command_map::iterator it = _commands.find(name);
+ if (it == _commands.end())
+ return false;
+
+ delete it->second;
+ _commands.erase(it);
+ return true;
}
// end of commands.cxx