]> git.mxchange.org Git - simgear.git/blobdiff - simgear/structure/commands.cxx
Some Linux platforms need <cstdio> for snprintf.
[simgear.git] / simgear / structure / commands.cxx
index fd67ae4fc3edebac035ca0f5cf02ea86a48853fd..752f880c2ad13c60eb36f63e987516a8e77d4854 100644 (file)
@@ -9,15 +9,15 @@
 #endif
 
 #include <memory>
-#include <simgear/props/props_io.hxx>
+#include <cassert>
 
-#include <OpenThreads/Mutex>
-#include <OpenThreads/ScopedLock>
+#include <simgear/props/props_io.hxx>
 
 #include "commands.hxx"
 
-#include <simgear/math/SGMath.hxx>
 #include <simgear/structure/exception.hxx>
+#include <simgear/threads/SGThread.hxx>
+#include <simgear/threads/SGGuard.hxx>
 #include <simgear/debug/logstream.hxx>
 
 \f
 // 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;
 }
 
-OpenThreads::Mutex SGCommandMgr::_instanceMutex;
-
 SGCommandMgr*
 SGCommandMgr::instance()
 {
-  static std::auto_ptr<SGCommandMgr> mgr;
-  if (mgr.get())
-    return mgr.get();
-
-  OpenThreads::ScopedLock<OpenThreads::Mutex> 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
-SGCommandMgr::addCommand (const std::string &name, command_t command)
+SGCommandMgr::addCommandObject (const std::string &name, Command* command)
 {
+    if (_commands.find(name) != _commands.end())
+        throw sg_exception("duplicate command name:" + name);
+    
   _commands[name] = command;
 }
 
-SGCommandMgr::command_t
+SGCommandMgr::Command*
 SGCommandMgr::getCommand (const std::string &name) const
 {
   const command_map::const_iterator it = _commands.find(name);
@@ -82,18 +77,58 @@ SGCommandMgr::getCommandNames () const
 bool
 SGCommandMgr::execute (const std::string &name, const SGPropertyNode * arg) const
 {
-  command_t command = getCommand(name);
+  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