source: src/Patterns/Singleton_impl.hpp@ 456e78

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 456e78 was c0bccb, checked in by Frederik Heber <heber@…>, 14 years ago

Moved SingletonTest to Patterns/unittests/SingletonUnitTest.

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