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 config.h
|
---|
12 | #ifdef HAVE_CONFIG_H
|
---|
13 | #include <config.h>
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #include "CodePatterns/Assert.hpp"
|
---|
17 | #include "CodePatterns/Singleton.hpp"
|
---|
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 | const T& Singleton<T,_may_create>::getConstInstance(){
|
---|
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;
|
---|
47 | }
|
---|
48 |
|
---|
49 | template <class T,bool _may_create>
|
---|
50 | AtomicInstance<T> Singleton<T,_may_create>::getLockedInstance(){
|
---|
51 | // boost supports RAII-Style locking, so we don't need to unlock
|
---|
52 | boost::recursive_mutex::scoped_lock guard(instanceLock);
|
---|
53 | if(!theInstance.get()) {
|
---|
54 | theInstance.reset(creator::make());
|
---|
55 | }
|
---|
56 | T *ptr = theInstance.get();
|
---|
57 | return AtomicInstance<T>(ptr);
|
---|
58 | }
|
---|
59 |
|
---|
60 | template <class T,bool _may_create>
|
---|
61 | T* Singleton<T,_may_create>::getPointer(){
|
---|
62 | // boost supports RAII-Style locking, so we don't need to unlock
|
---|
63 | boost::recursive_mutex::scoped_lock guard(instanceLock);
|
---|
64 | if(!theInstance.get()) {
|
---|
65 | theInstance.reset(creator::make());
|
---|
66 | }
|
---|
67 | return theInstance.get();
|
---|
68 |
|
---|
69 | }
|
---|
70 |
|
---|
71 | template <class T,bool _may_create>
|
---|
72 | const T* Singleton<T,_may_create>::getConstPointer(){
|
---|
73 | // boost supports RAII-Style locking, so we don't need to unlock
|
---|
74 | boost::recursive_mutex::scoped_lock guard(instanceLock);
|
---|
75 | if(!theInstance.get()) {
|
---|
76 | theInstance.reset(creator::make());
|
---|
77 | }
|
---|
78 | return theInstance.get();
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | template <class T,bool _may_create>
|
---|
83 | void Singleton<T,_may_create>::purgeInstance(){
|
---|
84 | // boost supports RAII-Style locking, so we don't need to unlock
|
---|
85 | boost::recursive_mutex::scoped_lock guard(instanceLock);
|
---|
86 | theInstance.reset();
|
---|
87 | }
|
---|
88 |
|
---|
89 | template <class T,bool _may_create>
|
---|
90 | T& Singleton<T,_may_create>::resetInstance(){
|
---|
91 | ptr_t oldInstance;
|
---|
92 | {
|
---|
93 | // boost supports RAII-Style locking, so we don't need to unlock
|
---|
94 | boost::recursive_mutex::scoped_lock guard(instanceLock);
|
---|
95 |
|
---|
96 | oldInstance = theInstance;
|
---|
97 | theInstance.reset(creator::make());
|
---|
98 | // oldworld does not need protection any more,
|
---|
99 | // since we should have the only reference
|
---|
100 |
|
---|
101 | // worldLock handles access to the pointer,
|
---|
102 | // not to the object
|
---|
103 | } // scope-end releases the lock
|
---|
104 |
|
---|
105 | // oldInstance goes out of scope at the End of this function. The carried object will then be destroyed by the auto_ptr
|
---|
106 | return *theInstance;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | template <class T,bool _may_create>
|
---|
111 | void Singleton<T,_may_create>::setInstance(T* newInstance){
|
---|
112 | ASSERT(!theInstance.get(), "Trying to set the instance of an already created singleton");
|
---|
113 | boost::recursive_mutex::scoped_lock guard(instanceLock);
|
---|
114 | theInstance.reset(newInstance);
|
---|
115 | }
|
---|
116 |
|
---|
117 | template<class T, bool _may_create>
|
---|
118 | Singleton<T,_may_create>::Singleton(){/* empty */}
|
---|
119 |
|
---|
120 | // private copy constructor to avoid unintended copying
|
---|
121 | template <class T, bool _may_create>
|
---|
122 | Singleton<T,_may_create>::Singleton(const Singleton<T,_may_create>&){
|
---|
123 | ASSERT(0, "Copy constructor of singleton template called");
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * This define allows simple instantiation of the necessary singleton functions
|
---|
128 | * at a chosen place.
|
---|
129 | */
|
---|
130 | #define CONSTRUCT_SINGLETON(name) \
|
---|
131 | template name& Singleton< name , name::may_create >::getInstance(); \
|
---|
132 | template const name& Singleton< name , name::may_create >::getConstInstance(); \
|
---|
133 | template AtomicInstance<name> Singleton< name , name::may_create >::getLockedInstance(); \
|
---|
134 | template name* Singleton< name , name::may_create >::getPointer(); \
|
---|
135 | template const name* Singleton< name , name::may_create >::getConstPointer(); \
|
---|
136 | template void Singleton< name , name::may_create >::purgeInstance(); \
|
---|
137 | template name& Singleton< name , name::may_create >::resetInstance(); \
|
---|
138 | template void Singleton< name , name::may_create >::setInstance( name* );
|
---|
139 |
|
---|
140 | /************ Internal Pointer Wrapper to allow automatic purging *************/
|
---|
141 |
|
---|
142 | template <class T,bool _may_create>
|
---|
143 | Singleton<T,_may_create>::ptr_t::ptr_t() :
|
---|
144 | content(0){};
|
---|
145 |
|
---|
146 | template <class T,bool _may_create>
|
---|
147 | Singleton<T,_may_create>::ptr_t::ptr_t(T* _content) :
|
---|
148 | content(_content){};
|
---|
149 |
|
---|
150 | template <class T,bool _may_create>
|
---|
151 | Singleton<T,_may_create>::ptr_t:: ~ptr_t()
|
---|
152 | {delete content;};
|
---|
153 |
|
---|
154 | template <class T,bool _may_create>
|
---|
155 | T& Singleton<T,_may_create>::ptr_t::operator*()
|
---|
156 | {return *content;};
|
---|
157 |
|
---|
158 | template <class T,bool _may_create>
|
---|
159 | T* Singleton<T,_may_create>::ptr_t::get()
|
---|
160 | {return content;};
|
---|
161 |
|
---|
162 | template <class T,bool _may_create>
|
---|
163 | void Singleton<T,_may_create>::ptr_t::reset(T* _content){
|
---|
164 | delete content;
|
---|
165 | content = _content;
|
---|
166 | }
|
---|
167 |
|
---|
168 | template <class T,bool _may_create>
|
---|
169 | void Singleton<T,_may_create>::ptr_t::reset()
|
---|
170 | {reset(0);}
|
---|
171 |
|
---|
172 | template <class T,bool _may_create>
|
---|
173 | typename Singleton<T,_may_create>::ptr_t& Singleton<T,_may_create>::ptr_t::operator=(const typename Singleton<T,_may_create>::ptr_t& rhs){
|
---|
174 | if(&rhs!=this){
|
---|
175 | delete content;
|
---|
176 | content = rhs.content;
|
---|
177 | rhs.content = 0;
|
---|
178 | }
|
---|
179 | return *this;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | #endif /* SINGLETON_IMPL_HPP_ */
|
---|