source: src/CodePatterns/Registry_impl.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: 5.1 KB
Line 
1/*
2 * Registry_impl.hpp
3 *
4 * Created on: Jul 28, 2010
5 * Author: heber
6 */
7
8#ifndef REGISTRY_IMPL_HPP_
9#define REGISTRY_IMPL_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include "CodePatterns/Registry.hpp"
17
18#include "CodePatterns/Assert.hpp"
19#include <iostream>
20
21/** Constructor for class Registry.
22 */
23template <class T> Registry<T>::Registry()
24{}
25
26/** Destructor for class Registry.
27 */
28template <class T> Registry<T>::~Registry()
29{}
30
31/** Returns pointer to an instance named by \a name.
32 * \param name name of instance
33 * \return pointer to instance
34 */
35template <class T> T* Registry<T>::getByName(const std::string name) const
36{
37 typename std::map<const std::string,T*>::const_iterator iter;
38 iter = InstanceMap.find(name);
39 ASSERT(iter!=InstanceMap.end(),"Query for an instance "+name+" not stored in registry");
40 return (iter!=InstanceMap.end()) ? iter->second : NULL;
41}
42
43/** States whether instance is present or not.
44 * \note This is needed as Registry<T>::getByName() ASSERT()s that instance is in std::map.
45 * \param name name of instance
46 * \return true - present, false - instance absent
47 */
48template <class T>bool Registry<T>::isPresentByName(const std::string name) const
49{
50 typename std::map<const std::string,T*>::const_iterator iter;
51 iter = InstanceMap.find(name);
52 return iter!=InstanceMap.end();
53}
54
55/** Registers an instance with the Registry.
56 * \param *instance pointer to T.
57 */
58template <class T>void Registry<T>::registerInstance(T* instance){
59 std::pair<typename std::map<const std::string,T*>::iterator,bool> ret;
60 //std::cout << "Trying to register instance of type " << typeid(T).name() << " with name " << instance->getName() << "." << std::endl;
61 ret = InstanceMap.insert(typename std::pair<const std::string,T*>(instance->getName(),instance));
62 ASSERT(ret.second,"Two instances with the same name "+instance->getName()+" added to registry");
63}
64
65/** Unregisters an instance.
66 * \param *instance pointer to instance.
67 */
68template <class T>void Registry<T>::unregisterInstance(T* instance){
69 //std::cout << "Unregistering instance of type " << typeid(T).name() << " with name " << instance->getName() << "." << std::endl;
70 InstanceMap.erase(instance->getName());
71}
72
73/** Removes every instance from the registry.
74 */
75template <class T>void Registry<T>::cleanup()
76{
77 typename std::map<const std::string,T*>::iterator iter = InstanceMap.begin();
78 for(;!InstanceMap.empty();iter=InstanceMap.begin()) {
79 //std::cerr << "Removing instance "+iter->first+" from registry" << std::endl;
80 T* instance = iter->second;
81 InstanceMap.erase(iter);
82 delete instance;
83 }
84 InstanceMap.clear();
85}
86
87
88/** Returns an iterator pointing to the start of the std::map of instance's.
89 * \return begin iterator
90 */
91template <class T>
92typename std::map<const std::string,T*>::iterator Registry<T>::getBeginIter()
93{
94 return InstanceMap.begin();
95}
96
97/** Returns an iterator pointing to the end of the std::map of instance's.
98 * \return end iterator
99 */
100template <class T>
101typename std::map<const std::string,T*>::iterator Registry<T>::getEndIter()
102{
103 return InstanceMap.end();
104}
105
106/** Returns a const iterator pointing to the start of the std::map of instance's.
107 * \return constant begin iterator
108 */
109template <class T>
110typename std::map<const std::string,T*>::const_iterator Registry<T>::getBeginIter() const
111{
112 return InstanceMap.begin();
113}
114
115/** Returns a const iterator pointing to the end of the std::map of instance's.
116 * \return constant end iterator
117 */
118template <class T>
119typename std::map<const std::string,T*>::const_iterator Registry<T>::getEndIter() const
120{
121 return InstanceMap.end();
122}
123
124/** Prints the contents of the Registry \a &m to \a &ost.
125 * \param &ost output stream
126 * \param &m reference to Registry
127 * \return reference to the above out stream for concatenation
128 */
129template <class T>
130std::ostream& operator<<(std::ostream& ost, const Registry<T>& m)
131{
132 ost << "Registry contains:" << std::endl;
133 for (typename std::map<const std::string,T*>::const_iterator iter = m.getBeginIter(); iter != m.getEndIter(); ++iter) {
134 ost << "\t" << iter->first << " with pointer " << iter->second << std::endl;
135 }
136 return ost;
137};
138
139/**
140 * This define allows simple instantiation of the necessary registryfunctions
141 * at a chosen place.
142 */
143#define CONSTRUCT_REGISTRY(InstanceType) \
144 template InstanceType* Registry<InstanceType>::getByName(const std::string) const; \
145 template bool Registry<InstanceType>::isPresentByName(const std::string) const; \
146 template void Registry<InstanceType>::registerInstance(InstanceType*); \
147 template void Registry<InstanceType>::unregisterInstance(InstanceType*); \
148 template void Registry<InstanceType>::cleanup(); \
149 template std::map<const std::string,InstanceType*>::iterator Registry<InstanceType>::getBeginIter(); \
150 template std::map<const std::string,InstanceType*>::const_iterator Registry<InstanceType>::getBeginIter() const; \
151 template std::map<const std::string,InstanceType*>::iterator Registry<InstanceType>::getEndIter(); \
152 template std::map<const std::string,InstanceType*>::const_iterator Registry<InstanceType>::getEndIter() const;
153
154
155#endif /* REGISTRY_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.