source: src/CodePatterns/Singleton_impl.hpp@ 084729c

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 084729c was 084729c, checked in by Frederik Heber <heber@…>, 8 years ago

Squashed 'ThirdParty/CodePatterns/' content from commit c1e1041

git-subtree-dir: ThirdParty/CodePatterns
git-subtree-split: c1e10418c454f98be2f43d93167642b0008428fc

  • Property mode set to 100644
File size: 5.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 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>
40const 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
49template <class T,bool _may_create>
50AtomicInstance<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
60template <class T,bool _may_create>
61T* 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
71template <class T,bool _may_create>
72const 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
82template <class T,bool _may_create>
83void 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
89template <class T,bool _may_create>
90T& 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
110template <class T,bool _may_create>
111void 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
117template<class T, bool _may_create>
118Singleton<T,_may_create>::Singleton(){/* empty */}
119
120// private copy constructor to avoid unintended copying
121template <class T, bool _may_create>
122Singleton<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
142template <class T,bool _may_create>
143Singleton<T,_may_create>::ptr_t::ptr_t() :
144content(0){};
145
146template <class T,bool _may_create>
147Singleton<T,_may_create>::ptr_t::ptr_t(T* _content) :
148content(_content){};
149
150template <class T,bool _may_create>
151Singleton<T,_may_create>::ptr_t:: ~ptr_t()
152{delete content;};
153
154template <class T,bool _may_create>
155T& Singleton<T,_may_create>::ptr_t::operator*()
156{return *content;};
157
158template <class T,bool _may_create>
159T* Singleton<T,_may_create>::ptr_t::get()
160{return content;};
161
162template <class T,bool _may_create>
163void Singleton<T,_may_create>::ptr_t::reset(T* _content){
164 delete content;
165 content = _content;
166}
167
168template <class T,bool _may_create>
169void Singleton<T,_may_create>::ptr_t::reset()
170{reset(0);}
171
172template <class T,bool _may_create>
173typename 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_ */
Note: See TracBrowser for help on using the repository browser.