changed Singleton to public-constructor variety

This commit is contained in:
James Turk 2005-03-15 18:51:00 +00:00
parent ca927a0672
commit 0ef66e5017

View File

@ -5,12 +5,11 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: Singleton.hpp,v 1.6 2005/03/04 13:06:49 cozman Exp $ // $Id: Singleton.hpp,v 1.7 2005/03/15 18:51:00 cozman Exp $
#ifndef PHOTON_UTIL_SINGLETON_HPP #ifndef PHOTON_UTIL_SINGLETON_HPP
#define PHOTON_UTIL_SINGLETON_HPP #define PHOTON_UTIL_SINGLETON_HPP
#include <memory>
#include <boost/utility.hpp> #include <boost/utility.hpp>
#include "exceptions.hpp" #include "exceptions.hpp"
@ -28,33 +27,23 @@ namespace util
// (code) // (code)
// YourClass : public Singleton<Class> // YourClass : public Singleton<Class>
// { // {
// // class specific data // // class definition
//
// // Singleton-required code
// private:
// // Singleton-required code
// YourClass();
// ~YourClass();
//
// friend class util::Singleton<YourClass>;
// friend class std::auto_ptr<YourClass>;
// }; // };
// (end) // (end)
// //
// Using The Singleton: // Using The Singleton:
// (code) // (code)
// YourClass::initialize(); //optional // new YourClass;
// YourClass& yc(YourClass::getInstance()); // YourClass& yc(YourClass::getInstance());
// //
// // use yc // // use yc
// //
// YourClass::destroy(); //optional // YourClass::destroy();
// (end) // (end)
template<class T> template<class T>
class Singleton : public boost::noncopyable class Singleton : public boost::noncopyable
{ {
public: public:
// Function: initialize // Function: initialize
// Initialize the instance of the singleton, can be done explicitly if // Initialize the instance of the singleton, can be done explicitly if
// order of construction matters. Will be done on first call to // order of construction matters. Will be done on first call to
@ -71,55 +60,60 @@ public:
static T& getInstance(); static T& getInstance();
protected: protected:
virtual ~Singleton()=0; // allow inheritance Singleton();
virtual ~Singleton(); // allow inheritance
private: private:
static std::auto_ptr<T> instance_; static T* instance_;
}; };
// template implementation // template implementation
template<class T>
Singleton<T>::Singleton()
{
if(instance_ != 0)
{
throw PreconditionException("Attempt to double-initialize singleton.");
}
instance_ = static_cast<T*>(this); // cast self to type of T*
}
template<class T> template<class T>
Singleton<T>::~Singleton() Singleton<T>::~Singleton()
{ {
} }
template<class T>
void Singleton<T>::initialize()
{
if(instance_.get() != 0)
{
throw PreconditionException("Attempt to double-initialize singleton.");
}
instance_ = std::auto_ptr<T>(new T);
}
template<class T> template<class T>
void Singleton<T>::destroy() void Singleton<T>::destroy()
{ {
if(instance_.get() == 0) if(instance_ == 0)
{ {
throw PreconditionException("Attempt to destroy null singleton."); throw PreconditionException("Attempt to destroy null singleton.");
} }
instance_.reset(); if(instance_)
{
delete instance_;
instance_ = 0;
}
} }
template<class T> template<class T>
T& Singleton<T>::getInstance() T& Singleton<T>::getInstance()
{ {
if(instance_.get() == 0) if(instance_ == 0)
{ {
initialize(); //initialize if nonexistant throw PreconditionException("Attempt to access null singleton.");
} }
return *instance_; //return dereferenced version return *instance_; //return dereferenced instance
} }
template<class T> template<class T>
std::auto_ptr<T> Singleton<T>::instance_(0); T* Singleton<T>::instance_(0);
} }
} }