source: src/Patterns/Singleton_impl.hpp@ 8a34392

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 8a34392 was 0f6f3a, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Small improvements to singleton pattern

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