| 1 | /*
|
|---|
| 2 | * Singleton.hpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: Mar 10, 2010
|
|---|
| 5 | * Author: crueger
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #ifndef SINGLETON_HPP_
|
|---|
| 9 | #define SINGLETON_HPP_
|
|---|
| 10 |
|
|---|
| 11 | #include <cassert>
|
|---|
| 12 | #include <boost/thread.hpp>
|
|---|
| 13 |
|
|---|
| 14 | #include "defs.hpp"
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * This template produces the generic singleton pattern using the CRTP idiom.
|
|---|
| 18 | */
|
|---|
| 19 | template <class T, bool _may_create=true>
|
|---|
| 20 | class Singleton
|
|---|
| 21 | {
|
|---|
| 22 | private:
|
|---|
| 23 | // simple auto_ptr that allows destruction of the object
|
|---|
| 24 | // std::auto_ptr cannot do this because the destructor of T is ussually private
|
|---|
| 25 | class ptr_t {
|
|---|
| 26 | public:
|
|---|
| 27 | ptr_t();
|
|---|
| 28 | ptr_t(T* _content);
|
|---|
| 29 | ~ptr_t();
|
|---|
| 30 | T& operator*();
|
|---|
| 31 | T* get();
|
|---|
| 32 | void reset(T* _content);
|
|---|
| 33 | void reset();
|
|---|
| 34 | ptr_t& operator=(const ptr_t& rhs);
|
|---|
| 35 | private:
|
|---|
| 36 | mutable T* content;
|
|---|
| 37 | };
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * this creator checks what it may or may not do
|
|---|
| 41 | */
|
|---|
| 42 | template<class creator_T, bool creator_may_create>
|
|---|
| 43 | struct creator_t {
|
|---|
| 44 | static creator_T* make();
|
|---|
| 45 | static void set(creator_T*&,creator_T*);
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | // specialization to allow fast creations
|
|---|
| 49 |
|
|---|
| 50 | template<class creator_T>
|
|---|
| 51 | struct creator_t<creator_T,true>{
|
|---|
| 52 | static creator_T* make(){
|
|---|
| 53 | return new creator_T();
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | static void set(creator_T*&,creator_T*){
|
|---|
| 57 | assert(0 && "Cannot set the Instance for a singleton of this type");
|
|---|
| 58 | }
|
|---|
| 59 | };
|
|---|
| 60 |
|
|---|
| 61 | template<class creator_T>
|
|---|
| 62 | struct creator_t<creator_T,false>{
|
|---|
| 63 | static creator_T* make(){
|
|---|
| 64 | assert(0 && "Cannot create a singleton of this type directly");
|
|---|
| 65 | }
|
|---|
| 66 | static void set(ptr_t& dest,creator_T* src){
|
|---|
| 67 | dest.reset(src);
|
|---|
| 68 | }
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | public:
|
|---|
| 72 |
|
|---|
| 73 | // make the state of this singleton accessible
|
|---|
| 74 | static const bool may_create=_may_create;
|
|---|
| 75 |
|
|---|
| 76 | // this is used for creation
|
|---|
| 77 | typedef creator_t<T,_may_create> creator;
|
|---|
| 78 |
|
|---|
| 79 | static T& getInstance();
|
|---|
| 80 | static T* getPointer();
|
|---|
| 81 |
|
|---|
| 82 | static void purgeInstance();
|
|---|
| 83 | static T& resetInstance();
|
|---|
| 84 |
|
|---|
| 85 | static void setInstance(T*);
|
|---|
| 86 | protected:
|
|---|
| 87 | // constructor accessible by subclasses
|
|---|
| 88 | Singleton();
|
|---|
| 89 |
|
|---|
| 90 | private:
|
|---|
| 91 | // private copy constructor to avoid unintended copying
|
|---|
| 92 | Singleton(const Singleton&);
|
|---|
| 93 |
|
|---|
| 94 | static boost::recursive_mutex instanceLock;
|
|---|
| 95 | static ptr_t theInstance;
|
|---|
| 96 | };
|
|---|
| 97 |
|
|---|
| 98 | #endif /* SINGLETON_HPP_ */
|
|---|