| [a80f419] | 1 | /* | 
|---|
|  | 2 | * Singleton_impl.hpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Mar 10, 2010 | 
|---|
|  | 5 | *      Author: crueger | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #ifndef SINGLETON_IMPL_HPP_ | 
|---|
|  | 9 | #define SINGLETON_IMPL_HPP_ | 
|---|
|  | 10 |  | 
|---|
| [70672e3] | 11 | // include config.h | 
|---|
|  | 12 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 13 | #include <config.h> | 
|---|
|  | 14 | #endif | 
|---|
|  | 15 |  | 
|---|
| [9b8fa4] | 16 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 17 | #include "CodePatterns/Singleton.hpp" | 
|---|
| [a80f419] | 18 |  | 
|---|
|  | 19 | /****** Static instance Variables of the template *******/ | 
|---|
|  | 20 |  | 
|---|
|  | 21 | template <class T,bool _may_create> | 
|---|
|  | 22 | typename Singleton<T,_may_create>::ptr_t Singleton<T,_may_create>::theInstance(0); | 
|---|
|  | 23 |  | 
|---|
|  | 24 | template <class T,bool _may_create> | 
|---|
|  | 25 | boost::recursive_mutex Singleton<T,_may_create>::instanceLock; | 
|---|
|  | 26 |  | 
|---|
|  | 27 | /****** templates singleton creation and destruction functions ******/ | 
|---|
|  | 28 |  | 
|---|
|  | 29 | template <class T,bool _may_create> | 
|---|
|  | 30 | T& Singleton<T,_may_create>::getInstance(){ | 
|---|
|  | 31 | // boost supports RAII-Style locking, so we don't need to unlock | 
|---|
|  | 32 | boost::recursive_mutex::scoped_lock guard(instanceLock); | 
|---|
|  | 33 | if(!theInstance.get()) { | 
|---|
|  | 34 | theInstance.reset(creator::make()); | 
|---|
|  | 35 | } | 
|---|
|  | 36 | return *theInstance; | 
|---|
|  | 37 | } | 
|---|
|  | 38 |  | 
|---|
|  | 39 | template <class T,bool _may_create> | 
|---|
|  | 40 | T* Singleton<T,_may_create>::getPointer(){ | 
|---|
|  | 41 | // boost supports RAII-Style locking, so we don't need to unlock | 
|---|
|  | 42 | boost::recursive_mutex::scoped_lock guard(instanceLock); | 
|---|
|  | 43 | if(!theInstance.get()) { | 
|---|
|  | 44 | theInstance.reset(creator::make()); | 
|---|
|  | 45 | } | 
|---|
|  | 46 | return theInstance.get(); | 
|---|
|  | 47 |  | 
|---|
|  | 48 | } | 
|---|
|  | 49 |  | 
|---|
|  | 50 | template <class T,bool _may_create> | 
|---|
|  | 51 | void Singleton<T,_may_create>::purgeInstance(){ | 
|---|
|  | 52 | // boost supports RAII-Style locking, so we don't need to unlock | 
|---|
|  | 53 | boost::recursive_mutex::scoped_lock guard(instanceLock); | 
|---|
|  | 54 | theInstance.reset(); | 
|---|
|  | 55 | } | 
|---|
|  | 56 |  | 
|---|
|  | 57 | template <class T,bool _may_create> | 
|---|
|  | 58 | T& Singleton<T,_may_create>::resetInstance(){ | 
|---|
|  | 59 | ptr_t oldInstance; | 
|---|
|  | 60 | { | 
|---|
|  | 61 | // boost supports RAII-Style locking, so we don't need to unlock | 
|---|
|  | 62 | boost::recursive_mutex::scoped_lock guard(instanceLock); | 
|---|
|  | 63 |  | 
|---|
|  | 64 | oldInstance = theInstance; | 
|---|
|  | 65 | theInstance.reset(creator::make()); | 
|---|
|  | 66 | // oldworld does not need protection any more, | 
|---|
|  | 67 | // since we should have the only reference | 
|---|
|  | 68 |  | 
|---|
|  | 69 | // worldLock handles access to the pointer, | 
|---|
|  | 70 | // not to the object | 
|---|
|  | 71 | } // scope-end releases the lock | 
|---|
|  | 72 |  | 
|---|
|  | 73 | // oldInstance goes out of scope at the End of this function. The carried object will then be destroyed by the auto_ptr | 
|---|
|  | 74 | return *theInstance; | 
|---|
|  | 75 | } | 
|---|
|  | 76 |  | 
|---|
|  | 77 |  | 
|---|
|  | 78 | template <class T,bool _may_create> | 
|---|
|  | 79 | void Singleton<T,_may_create>::setInstance(T* newInstance){ | 
|---|
|  | 80 | ASSERT(!theInstance.get(), "Trying to set the instance of an already created singleton"); | 
|---|
|  | 81 | boost::recursive_mutex::scoped_lock guard(instanceLock); | 
|---|
|  | 82 | theInstance.reset(newInstance); | 
|---|
|  | 83 | } | 
|---|
|  | 84 |  | 
|---|
|  | 85 | template<class T, bool _may_create> | 
|---|
|  | 86 | Singleton<T,_may_create>::Singleton(){/* empty */} | 
|---|
|  | 87 |  | 
|---|
|  | 88 | // private copy constructor to avoid unintended copying | 
|---|
|  | 89 | template <class T, bool _may_create> | 
|---|
|  | 90 | Singleton<T,_may_create>::Singleton(const Singleton<T,_may_create>&){ | 
|---|
|  | 91 | ASSERT(0, "Copy constructor of singleton template called"); | 
|---|
|  | 92 | } | 
|---|
|  | 93 |  | 
|---|
|  | 94 | /** | 
|---|
|  | 95 | * This define allows simple instantiation of the necessary singleton functions | 
|---|
|  | 96 | * at a chosen place. | 
|---|
|  | 97 | */ | 
|---|
|  | 98 | #define CONSTRUCT_SINGLETON(name) \ | 
|---|
|  | 99 | template name& Singleton< name , name::may_create >::getInstance(); \ | 
|---|
|  | 100 | template name* Singleton< name , name::may_create >::getPointer();  \ | 
|---|
|  | 101 | template void  Singleton< name , name::may_create >::purgeInstance(); \ | 
|---|
|  | 102 | template name& Singleton< name , name::may_create >::resetInstance(); \ | 
|---|
|  | 103 | template void  Singleton< name , name::may_create >::setInstance( name* ); | 
|---|
|  | 104 |  | 
|---|
|  | 105 | /************ Internal Pointer Wrapper to allow automatic purging *************/ | 
|---|
|  | 106 |  | 
|---|
|  | 107 | template <class T,bool _may_create> | 
|---|
|  | 108 | Singleton<T,_may_create>::ptr_t::ptr_t() : | 
|---|
|  | 109 | content(0){}; | 
|---|
|  | 110 |  | 
|---|
|  | 111 | template <class T,bool _may_create> | 
|---|
|  | 112 | Singleton<T,_may_create>::ptr_t::ptr_t(T* _content) : | 
|---|
|  | 113 | content(_content){}; | 
|---|
|  | 114 |  | 
|---|
|  | 115 | template <class T,bool _may_create> | 
|---|
|  | 116 | Singleton<T,_may_create>::ptr_t:: ~ptr_t() | 
|---|
|  | 117 | {delete content;}; | 
|---|
|  | 118 |  | 
|---|
|  | 119 | template <class T,bool _may_create> | 
|---|
|  | 120 | T& Singleton<T,_may_create>::ptr_t::operator*() | 
|---|
|  | 121 | {return *content;}; | 
|---|
|  | 122 |  | 
|---|
|  | 123 | template <class T,bool _may_create> | 
|---|
|  | 124 | T* Singleton<T,_may_create>::ptr_t::get() | 
|---|
|  | 125 | {return content;}; | 
|---|
|  | 126 |  | 
|---|
|  | 127 | template <class T,bool _may_create> | 
|---|
|  | 128 | void Singleton<T,_may_create>::ptr_t::reset(T* _content){ | 
|---|
|  | 129 | delete content; | 
|---|
|  | 130 | content = _content; | 
|---|
|  | 131 | } | 
|---|
|  | 132 |  | 
|---|
|  | 133 | template <class T,bool _may_create> | 
|---|
|  | 134 | void Singleton<T,_may_create>::ptr_t::reset() | 
|---|
|  | 135 | {reset(0);} | 
|---|
|  | 136 |  | 
|---|
|  | 137 | template <class T,bool _may_create> | 
|---|
|  | 138 | typename Singleton<T,_may_create>::ptr_t& Singleton<T,_may_create>::ptr_t::operator=(const typename Singleton<T,_may_create>::ptr_t& rhs){ | 
|---|
|  | 139 | if(&rhs!=this){ | 
|---|
|  | 140 | delete content; | 
|---|
|  | 141 | content = rhs.content; | 
|---|
|  | 142 | rhs.content = 0; | 
|---|
|  | 143 | } | 
|---|
|  | 144 | return *this; | 
|---|
|  | 145 | } | 
|---|
|  | 146 |  | 
|---|
|  | 147 |  | 
|---|
|  | 148 | #endif /* SINGLETON_IMPL_HPP_ */ | 
|---|