source: src/Patterns/Singleton.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: 1.9 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 <memory>
12
13#include "defs.hpp"
14
15/**
16 * This template produces the generic singleton pattern using the CRTP idiom.
17 */
18template <class T, bool _may_create=true>
19class Singleton
20{
21private:
22 // simple auto_ptr that allows destruction of the object
23 // std::auto_ptr cannot do this because the destructor of T is ussually private
24 class ptr_t {
25 public:
26 ptr_t();
27 ptr_t(T* _content);
28 ~ptr_t();
29 T& operator*();
30 T* get();
31 void reset(T* _content);
32 void reset();
33 ptr_t& operator=(ptr_t& rhs);
34 private:
35 T* content;
36 };
37
38 /**
39 * this creator checks what it may or may not do
40 */
41 template<class creator_T, bool creator_may_create>
42 struct creator_t {
43 static creator_T* make();
44 static void set(creator_T*&,creator_T*);
45 };
46
47 // specialization to allow fast creations
48
49 template<class creator_T>
50 struct creator_t<creator_T,true>{
51 static creator_T* make(){
52 return new creator_T();
53 }
54
55 static void set(creator_T*&,creator_T*){
56 assert(0 && "Cannot set the Instance for a singleton of this type");
57 }
58 };
59
60 template<class creator_T>
61 struct creator_t<creator_T,false>{
62 static creator_T* make(){
63 assert(0 && "Cannot create a singleton of this type directly");
64 }
65 static void set(ptr_t& dest,creator_T* src){
66 dest.reset(src);
67 }
68 };
69
70public:
71
72 // make the state of this singleton accessible
73 static const bool may_create=_may_create;
74
75 // this is used for creation
76 typedef creator_t<T,_may_create> creator;
77
78 static T& getInstance();
79 static T* getPointer();
80
81 static void purgeInstance();
82 static T& resetInstance();
83
84 static void setInstance(T*);
85protected:
86
87private:
88 static boost::mutex instanceLock;
89 static ptr_t theInstance;
90};
91
92#endif /* SINGLETON_HPP_ */
Note: See TracBrowser for help on using the repository browser.