source: src/Patterns/Singleton.hpp@ 0f6f3a

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 0f6f3a was 0f6f3a, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Small improvements to singleton pattern

  • Property mode set to 100644
File size: 2.1 KB
Line 
1/*
2 * Singleton.hpp
3 *
4 * Created on: Mar 10, 2010
5 * Author: crueger
6 */
7
8#ifndef SINGLETON_HPP_
9#define SINGLETON_HPP_
10
11#include <cassert>
12#include <boost/thread.hpp>
13
14#include "defs.hpp"
15
16/**
17 * This template produces the generic singleton pattern using the CRTP idiom.
18 */
19template <class T, bool _may_create=true>
20class Singleton
21{
22private:
23 // simple auto_ptr that allows destruction of the object
24 // std::auto_ptr cannot do this because the destructor of T is ussually private
25 class ptr_t {
26 public:
27 ptr_t();
28 ptr_t(T* _content);
29 ~ptr_t();
30 T& operator*();
31 T* get();
32 void reset(T* _content);
33 void reset();
34 ptr_t& operator=(const ptr_t& rhs);
35 private:
36 mutable T* content;
37 };
38
39 /**
40 * this creator checks what it may or may not do
41 */
42 template<class creator_T, bool creator_may_create>
43 struct creator_t {
44 static creator_T* make();
45 static void set(creator_T*&,creator_T*);
46 };
47
48 // specialization to allow fast creations
49
50 template<class creator_T>
51 struct creator_t<creator_T,true>{
52 static creator_T* make(){
53 return new creator_T();
54 }
55
56 static void set(creator_T*&,creator_T*){
57 assert(0 && "Cannot set the Instance for a singleton of this type");
58 }
59 };
60
61 template<class creator_T>
62 struct creator_t<creator_T,false>{
63 static creator_T* make(){
64 assert(0 && "Cannot create a singleton of this type directly");
65 }
66 static void set(ptr_t& dest,creator_T* src){
67 dest.reset(src);
68 }
69 };
70
71public:
72
73 // make the state of this singleton accessible
74 static const bool may_create=_may_create;
75
76 // this is used for creation
77 typedef creator_t<T,_may_create> creator;
78
79 static T& getInstance();
80 static T* getPointer();
81
82 static void purgeInstance();
83 static T& resetInstance();
84
85 static void setInstance(T*);
86protected:
87 // constructor accessible by subclasses
88 Singleton();
89
90private:
91 // private copy constructor to avoid unintended copying
92 Singleton(const Singleton&);
93
94 static boost::recursive_mutex instanceLock;
95 static ptr_t theInstance;
96};
97
98#endif /* SINGLETON_HPP_ */
Note: See TracBrowser for help on using the repository browser.