source: molecuilder/src/Patterns/Singleton_impl.hpp@ 4c60ef

Last change on this file since 4c60ef was 4c60ef, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Added generic singleton Pattern that can be inherited to any class making that class a singleton.

  • Property mode set to 100644
File size: 3.5 KB
Line 
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 <cassert>
12
13template <class T,bool _may_create>
14Singleton<T,_may_create>::ptr_t::ptr_t() :
15content(0){};
16
17template <class T,bool _may_create>
18Singleton<T,_may_create>::ptr_t::ptr_t(T* _content) :
19content(_content){};
20
21template <class T,bool _may_create>
22Singleton<T,_may_create>::ptr_t:: ~ptr_t()
23{delete content;};
24
25template <class T,bool _may_create>
26T& Singleton<T,_may_create>::ptr_t::operator*()
27{return *content;};
28
29template <class T,bool _may_create>
30T* Singleton<T,_may_create>::ptr_t::get()
31{return content;};
32
33template <class T,bool _may_create>
34void Singleton<T,_may_create>::ptr_t::reset(T* _content){
35 delete content;
36 content = _content;
37}
38
39template <class T,bool _may_create>
40void Singleton<T,_may_create>::ptr_t::reset()
41{reset(0);}
42
43template <class T,bool _may_create>
44typename 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
53template <class T,bool _may_create>
54typename Singleton<T,_may_create>::ptr_t Singleton<T,_may_create>::theInstance(0);
55
56template <class T,bool _may_create>
57boost::mutex Singleton<T,_may_create>::instanceLock;
58
59template <class T,bool _may_create>
60T& 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
69template <class T,bool _may_create>
70T* 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
80template <class T,bool _may_create>
81void 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
87template <class T,bool _may_create>
88T& 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
108template <class T,bool _may_create>
109void 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_ */
Note: See TracBrowser for help on using the repository browser.