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