From 7471a2b4e67b7e425f02c2ca38b76be3c699627d Mon Sep 17 00:00:00 2001 From: James Turk Date: Sun, 27 Feb 2005 05:50:54 +0000 Subject: [PATCH] initial import --- include/util/Singleton.hpp | 84 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 include/util/Singleton.hpp diff --git a/include/util/Singleton.hpp b/include/util/Singleton.hpp new file mode 100644 index 0000000..cb49c7f --- /dev/null +++ b/include/util/Singleton.hpp @@ -0,0 +1,84 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: Singleton.hpp,v 1.1 2005/02/27 05:50:54 cozman Exp $ + +#ifndef PHOTON_UTIL_SINGLETON_HPP +#define PHOTON_UTIL_SINGLETON_HPP + +#include +#include + +namespace photon +{ +namespace util +{ + +template +class Singleton : public boost::noncopyable +{ +public: + static void initSingleton(); + + static void destroySingleton(); + + static T& getSingleton(); + +protected: + virtual ~Singleton()=0; + +private: + static std::auto_ptr instance_; +}; + + +// template implementation + +template +Singleton::~Singleton() +{ +} + +template +void Singleton::initSingleton() +{ + assert(instance_.get() == 0); + + instance_ = std::auto_ptr(new T); +} + +template +void Singleton::destroySingleton() +{ + assert(instance_.get() != 0); + + instance_.reset(); +} + +template +T& Singleton::getSingleton() +{ + assert(instance_.get() != 0); + + return *instance_; +} + +template +std::auto_ptr Singleton::getSingletonPtr() +{ + assert(instance_.get() != 0); + + return instance_; +} + +template +std::auto_ptr Singleton::instance_(0); + +} +} + +#endif //PHOTON_UTIL_SINGLETON_HPP