source: src/CodePatterns/Registry.hpp@ 084729c

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 084729c was 084729c, checked in by Frederik Heber <heber@…>, 8 years ago

Squashed 'ThirdParty/CodePatterns/' content from commit c1e1041

git-subtree-dir: ThirdParty/CodePatterns
git-subtree-split: c1e10418c454f98be2f43d93167642b0008428fc

  • Property mode set to 100644
File size: 3.3 KB
Line 
1/*
2 * Registry.hpp
3 *
4 * Based on initial ActionRegistry code by Till Crueger.
5 *
6 * The registry pattern is basically just a singleton map, wherein instantiations
7 * of a class can be registered, unregistered and retrieved.
8 *
9 * Created on: Jul 28, 2010
10 * Author: heber
11 */
12
13#ifndef REGISTRY_HPP_
14#define REGISTRY_HPP_
15
16// include config.h
17#ifdef HAVE_CONFIG_H
18#include <config.h>
19#endif
20
21#include <map>
22#include <string>
23
24/**
25 * This template produces a generic registry pattern.
26 *
27 * <h1> Registry Howto </h1>
28 *
29 * The Registry is a class where instances of other classes are stored and be retrieved
30 * by a string token when desired. For this purpose a Registry should always be a singleton
31 * (i.e. use both this Registry and the Singleton pattern to declare a Registry class). It
32 * basically is simply a singleton container of a map, where the pointers to the class
33 * instances are stored by a string key and can be retrieved thereby.
34 *
35 * The available functions are as follows if your class instances to be stored in Registry
36 * are of type 'foo':
37 *
38 * - <code>foo* Registry<foo>::getByName()</code> : returns the instance of a specific
39 * class foo instance as a pointer associated with the given name
40 * - <code>bool Registry<foo>::isPresentByName()</code> : returns whether an instance
41 * of class foo is present under the given name.
42 * - <code>map<string,foo*>::iterator Registry<foo>::getBeginIter()</code> : returns an
43 * iterator to the beginning of the storage map (STL).
44 * - <code>map<string,foo*>::const_iterator Registry<foo>::getBeginIter()</code> : returns a
45 * constant iterator to the beginning of the storage map (STL).
46 * - <code>map<string,foo*>::const_iterator Registry<foo>::getEndIter()</code> : returns an
47 * iterator to the one step past the last element of the storage map (STL).
48 * - <code>map<string,foo*>::const_iterator Registry<foo>::getEndIter()</code> : returns a
49 * constant iterator to the one step past the last element of the storage map (STL).
50 *
51 * In order to use this pattern, additionally to the requirements of the Singleton pattern,
52 * do this:
53 * -# in the declaration derive your class from Registry<foo>, where foo is the class to be
54 * stored
55 * -# in the definition add CONSTRUCT_REGISTRY(foo) to the code such that the templated
56 * functions get instantiated there (otherwise you'll get undefined reference errors).
57 * -# The type stored in the registry needs to have a std::string getName() function that
58 * returns the name under which the instance is stored.
59 *
60 */
61
62template <class T>
63class Registry
64{
65public:
66 Registry();
67 ~Registry();
68
69 typedef typename std::map<const std::string,T*> instance_map;
70 typedef typename std::map<const std::string,T*>::iterator iterator;
71 typedef typename std::map<const std::string,T*>::const_iterator const_iterator;
72
73 T* getByName(const std::string name) const;
74 bool isPresentByName(const std::string name) const;
75 void registerInstance(T*);
76 void unregisterInstance(T*);
77 void cleanup();
78
79 iterator getBeginIter();
80 const_iterator getBeginIter() const;
81 iterator getEndIter();
82 const_iterator getEndIter() const;
83
84private:
85 instance_map InstanceMap;
86};
87
88template <class T> std::ostream& operator<<(std::ostream& ost, const Registry<T>& m);
89
90#endif /* REGISTRY_HPP_ */
Note: See TracBrowser for help on using the repository browser.