cpp_photon/include/util/Singleton.hpp

122 lines
2.2 KiB
C++
Raw Normal View History

2005-02-27 05:50:54 +00:00
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
2005-11-13 07:59:48 +00:00
// $Id: Singleton.hpp,v 1.9 2005/11/13 07:59:48 cozman Exp $
2005-02-27 05:50:54 +00:00
#ifndef PHOTON_UTIL_SINGLETON_HPP
#define PHOTON_UTIL_SINGLETON_HPP
#include <boost/utility.hpp>
2005-03-03 09:25:19 +00:00
#include "exceptions.hpp"
2005-02-27 05:50:54 +00:00
namespace photon
{
namespace util
{
2005-03-01 07:51:23 +00:00
// Class: Singleton
2005-07-18 06:18:50 +00:00
// Template class for singleton pattern. Is non-copyable to enforce correct
// behavior.
2005-03-01 07:51:23 +00:00
//
// Defining a Singleton:
// (code)
2005-07-18 06:18:50 +00:00
// class YourClass : public Singleton<Class>
// {
// // class definition
2005-03-01 07:51:23 +00:00
// };
// (end)
//
// Using The Singleton:
// (code)
// new YourClass;
2005-03-01 07:51:23 +00:00
// YourClass& yc(YourClass::getInstance());
//
// // use yc
//
// YourClass::destroy();
2005-03-01 07:51:23 +00:00
// (end)
2005-02-27 05:50:54 +00:00
template<class T>
class Singleton : public boost::noncopyable
{
public:
2005-03-01 07:51:23 +00:00
// Function: destroy
2005-11-13 07:59:48 +00:00
// Destroy the instance of the singleton, must be done for every singleton
// created.
//
// Throws:
// <PreconditionException> if called for uninitialized singleton
2005-02-27 07:43:37 +00:00
static void destroy();
2005-02-27 05:50:54 +00:00
2005-03-01 07:51:23 +00:00
// Function: getInstance
// Get a reference to the instance of the derived class.
2005-11-13 07:59:48 +00:00
//
// Throws:
// <PreconditionException> if called for uninitialized singleton
2005-02-27 07:43:37 +00:00
static T& getInstance();
2005-02-27 05:50:54 +00:00
2005-03-01 07:51:23 +00:00
protected:
Singleton();
virtual ~Singleton(); // allow inheritance
2005-02-27 05:50:54 +00:00
private:
static T* instance_;
2005-02-27 05:50:54 +00:00
};
// template implementation
template<class T>
2005-02-27 07:43:37 +00:00
void Singleton<T>::destroy()
2005-02-27 05:50:54 +00:00
{
if(instance_ == 0)
2005-03-03 09:25:19 +00:00
{
throw PreconditionException("Attempt to destroy null singleton.");
}
2005-02-27 07:43:37 +00:00
if(instance_)
{
delete instance_;
instance_ = 0;
}
2005-02-27 05:50:54 +00:00
}
template<class T>
2005-02-27 07:43:37 +00:00
T& Singleton<T>::getInstance()
2005-02-27 05:50:54 +00:00
{
if(instance_ == 0)
2005-03-03 09:25:19 +00:00
{
throw PreconditionException("Attempt to access null singleton.");
2005-03-03 09:25:19 +00:00
}
2005-02-27 07:43:37 +00:00
return *instance_; //return dereferenced instance
2005-02-27 05:50:54 +00:00
}
2005-11-13 07:59:48 +00:00
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>
Singleton<T>::~Singleton()
{
}
2005-02-27 05:50:54 +00:00
template<class T>
T* Singleton<T>::instance_(0);
2005-02-27 05:50:54 +00:00
}
}
#endif //PHOTON_UTIL_SINGLETON_HPP