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