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