[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 |
|
---|
| 13 | template <class T,bool _may_create>
|
---|
| 14 | Singleton<T,_may_create>::ptr_t::ptr_t() :
|
---|
| 15 | content(0){};
|
---|
| 16 |
|
---|
| 17 | template <class T,bool _may_create>
|
---|
| 18 | Singleton<T,_may_create>::ptr_t::ptr_t(T* _content) :
|
---|
| 19 | content(_content){};
|
---|
| 20 |
|
---|
| 21 | template <class T,bool _may_create>
|
---|
| 22 | Singleton<T,_may_create>::ptr_t:: ~ptr_t()
|
---|
| 23 | {delete content;};
|
---|
| 24 |
|
---|
| 25 | template <class T,bool _may_create>
|
---|
| 26 | T& Singleton<T,_may_create>::ptr_t::operator*()
|
---|
| 27 | {return *content;};
|
---|
| 28 |
|
---|
| 29 | template <class T,bool _may_create>
|
---|
| 30 | T* Singleton<T,_may_create>::ptr_t::get()
|
---|
| 31 | {return content;};
|
---|
| 32 |
|
---|
| 33 | template <class T,bool _may_create>
|
---|
| 34 | void Singleton<T,_may_create>::ptr_t::reset(T* _content){
|
---|
| 35 | delete content;
|
---|
| 36 | content = _content;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | template <class T,bool _may_create>
|
---|
| 40 | void Singleton<T,_may_create>::ptr_t::reset()
|
---|
| 41 | {reset(0);}
|
---|
| 42 |
|
---|
| 43 | template <class T,bool _may_create>
|
---|
| 44 | typename Singleton<T,_may_create>::ptr_t& Singleton<T,_may_create>::ptr_t::operator=(typename Singleton<T,_may_create>::ptr_t& rhs){
|
---|
| 45 | if(&rhs!=this){
|
---|
| 46 | delete content;
|
---|
| 47 | content = rhs.content;
|
---|
| 48 | rhs.content = 0;
|
---|
| 49 | }
|
---|
| 50 | return *this;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | template <class T,bool _may_create>
|
---|
| 54 | typename Singleton<T,_may_create>::ptr_t Singleton<T,_may_create>::theInstance(0);
|
---|
| 55 |
|
---|
| 56 | template <class T,bool _may_create>
|
---|
| 57 | boost::mutex Singleton<T,_may_create>::instanceLock;
|
---|
| 58 |
|
---|
| 59 | template <class T,bool _may_create>
|
---|
| 60 | T& Singleton<T,_may_create>::getInstance(){
|
---|
| 61 | // boost supports RAII-Style locking, so we don't need to unlock
|
---|
| 62 | boost::mutex::scoped_lock guard(instanceLock);
|
---|
| 63 | if(!theInstance.get()) {
|
---|
| 64 | theInstance.reset(creator::make());
|
---|
| 65 | }
|
---|
| 66 | return *theInstance;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | template <class T,bool _may_create>
|
---|
| 70 | T* Singleton<T,_may_create>::getPointer(){
|
---|
| 71 | // boost supports RAII-Style locking, so we don't need to unlock
|
---|
| 72 | boost::mutex::scoped_lock guard(instanceLock);
|
---|
| 73 | if(!theInstance.get()) {
|
---|
| 74 | theInstance.reset(creator::make());
|
---|
| 75 | }
|
---|
| 76 | return theInstance.get();
|
---|
| 77 |
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | template <class T,bool _may_create>
|
---|
| 81 | void Singleton<T,_may_create>::purgeInstance(){
|
---|
| 82 | // boost supports RAII-Style locking, so we don't need to unlock
|
---|
| 83 | boost::mutex::scoped_lock guard(instanceLock);
|
---|
| 84 | theInstance.reset();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | template <class T,bool _may_create>
|
---|
| 88 | T& Singleton<T,_may_create>::resetInstance(){
|
---|
| 89 | ptr_t oldInstance;
|
---|
| 90 | {
|
---|
| 91 | // boost supports RAII-Style locking, so we don't need to unlock
|
---|
| 92 | boost::mutex::scoped_lock guard(instanceLock);
|
---|
| 93 |
|
---|
| 94 | oldInstance = theInstance;
|
---|
| 95 | theInstance.reset(creator::make());
|
---|
| 96 | // oldworld does not need protection any more,
|
---|
| 97 | // since we should have the only reference
|
---|
| 98 |
|
---|
| 99 | // worldLock handles access to the pointer,
|
---|
| 100 | // not to the object
|
---|
| 101 | } // scope-end releases the lock
|
---|
| 102 |
|
---|
| 103 | // oldInstance goes out of scope at the End of this function. The carried object will then be destroyed by the auto_ptr
|
---|
| 104 | return *theInstance;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | template <class T,bool _may_create>
|
---|
| 109 | void Singleton<T,_may_create>::setInstance(T* newInstance){
|
---|
| 110 | assert(!theInstance.get() && "Trying to set the instance of an already created singleton");
|
---|
| 111 | boost::mutex::scoped_lock guard(instanceLock);
|
---|
| 112 | theInstance.reset(newInstance);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | #define CONSTRUCT_SINGLETON(name) \
|
---|
| 116 | template name& Singleton< name , name::may_create >::getInstance(); \
|
---|
| 117 | template name* Singleton< name , name::may_create >::getPointer(); \
|
---|
| 118 | template void Singleton< name , name::may_create >::purgeInstance(); \
|
---|
| 119 | template name& Singleton< name , name::may_create >::resetInstance(); \
|
---|
| 120 | template void Singleton< name , name::may_create >::setInstance( name* );
|
---|
| 121 |
|
---|
| 122 | #endif /* SINGLETON_IMPL_HPP_ */
|
---|