• Main Page
  • Related Pages
  • Classes
  • Files
  • File List

src/Singleton.hh

00001 /*
00002  * Copyright Anders Boberg 2006.
00003  * Copyright Staffan Gimåker 2008.
00004  *
00005  * Distributed under the Boost Software License, Version 1.0.
00006  * (See accompanying file LICENSE_1_0.txt or copy at
00007  * http://www.boost.org/LICENSE_1_0.txt)
00008  */
00009 
00010 #ifndef __PEEKABOT_SINGLETON_HH
00011 #define __PEEKABOT_SINGLETON_HH
00012 
00013 namespace peekabot
00014 {
00015     namespace singleton
00016     {
00017         template<class T>
00018         class StaticSingleton
00019         {
00020         protected:
00021             virtual ~StaticSingleton() {}
00022 
00023             inline static T *_instance()
00024             {
00025                 static T inst;
00026                 return &inst;
00027             }
00028         };
00029 
00030         template<class T>
00031         class LeakySingleton
00032         {
00033         protected:
00034             virtual ~LeakySingleton() {}
00035 
00036             inline static T *_instance()
00037             {
00038                 static T *inst = new T;
00039                 return inst;
00040             }
00041         };
00042     }
00043 
00044     template<
00045         class T, 
00046         template<class> class SingletonType = singleton::LeakySingleton >
00047     class Singleton : public SingletonType<T>
00048     {
00049     public:
00050         typedef T InstanceType;
00051 
00052         inline static InstanceType &instance()
00053         {
00054             return *SingletonType<T>::_instance();
00055         }
00056 
00057     private:
00058         Singleton() {}
00059         ~Singleton() {}
00060         Singleton(Singleton const&) {}
00061         Singleton& operator=(Singleton const&) {}
00062     };
00063 
00064 }
00065 #endif //__PEEKABOT_SINGLETON_HH

Generated on Sun Jan 30 2011 for peekabot by  doxygen 1.7.1