source: src/Patterns/Singleton_impl.hpp@ 23b547

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 23b547 was 23b547, checked in by Tillmann Crueger <crueger@…>, 15 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.