source: src/CodePatterns/Singleton_impl.hpp@ 73ab25

Last change on this file since 73ab25 was 9b8fa4, checked in by Frederik Heber <heber@…>, 14 years ago

Huge update of file structure to place installation header files into right folder.

  • The problem ist that we desire use as include "CodePatterns/...". For this to work, especially with the new Observer subfolder structure, it has been necessary to place all header files away from their source files into a distinct folder called CodePatterns. This emulates the later, after make install present structure.
  • essentially all source and header files had to be changed to adapt the include.
  • all Makefile.am's had to be changed.
  • nobase_ ... was removed such that header files are installed flat and not creating their subfolder along the process.
  • We placed Observer into its own convenience library and own folder Observer away from Patterns.

Some other changes:

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