Allow Command-manager singleton to be deleted.

(Shutdown can delete commands in an orderly way)
This commit is contained in:
James Turner 2013-11-23 20:00:23 +00:00
parent dd367daac9
commit 10375086ed
2 changed files with 14 additions and 26 deletions

View File

@ -9,6 +9,8 @@
#endif #endif
#include <memory> #include <memory>
#include <cassert>
#include <simgear/props/props_io.hxx> #include <simgear/props/props_io.hxx>
#include "commands.hxx" #include "commands.hxx"
@ -23,32 +25,24 @@
// Implementation of SGCommandMgr class. // Implementation of SGCommandMgr class.
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
static SGCommandMgr* static_instance = NULL;
SGCommandMgr::SGCommandMgr () SGCommandMgr::SGCommandMgr ()
{ {
// no-op assert(static_instance == NULL);
static_instance = this;
} }
SGCommandMgr::~SGCommandMgr () SGCommandMgr::~SGCommandMgr ()
{ {
// no-op assert(static_instance == this);
static_instance = NULL;
} }
SGMutex SGCommandMgr::_instanceMutex;
SGCommandMgr* SGCommandMgr*
SGCommandMgr::instance() SGCommandMgr::instance()
{ {
static std::auto_ptr<SGCommandMgr> mgr; return static_instance;
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();
} }
void void

View File

@ -16,7 +16,6 @@
#include <string> #include <string>
#include <map> #include <map>
#include <simgear/threads/SGThread.hxx>
#include <simgear/math/sg_types.hxx> #include <simgear/math/sg_types.hxx>
// forward decls // forward decls
@ -87,16 +86,16 @@ private:
} }
public: public:
/**
* Default constructor (sets instance to created item)
*/
SGCommandMgr ();
/** /**
* Destructor. * Destructor. (sets instance to NULL)
*/ */
virtual ~SGCommandMgr (); virtual ~SGCommandMgr ();
/**
* Implement the classical singleton.
*/
static SGCommandMgr* instance(); static SGCommandMgr* instance();
/** /**
@ -155,10 +154,7 @@ public:
*/ */
bool removeCommand(const std::string& name); bool removeCommand(const std::string& name);
protected: protected:
/**
* Default constructor.
*/
SGCommandMgr ();
private: private:
@ -166,8 +162,6 @@ private:
typedef std::map<std::string,Command*> command_map; typedef std::map<std::string,Command*> command_map;
command_map _commands; command_map _commands;
static SGMutex _instanceMutex;
}; };
#endif // __COMMANDS_HXX #endif // __COMMANDS_HXX