source: src/Actions/ValueStorage.hpp@ eb33c4

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 eb33c4 was 0275ad, checked in by Frederik Heber <heber@…>, 14 years ago

Final step in Random numbers is done ...

  • Property mode set to 100644
File size: 6.1 KB
Line 
1/*
2 * ValueStorage.hpp
3 *
4 * Created on: Jul 22, 2010
5 * Author: heber
6 */
7
8#ifndef VALUESTORAGE_HPP_
9#define VALUESTORAGE_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16
17#include <boost/filesystem.hpp>
18#include <boost/lexical_cast.hpp>
19#include <boost/program_options.hpp>
20
21#include <map>
22#include <set>
23#include <vector>
24#include <typeinfo>
25
26#include "Actions/OptionTrait.hpp"
27#include "Actions/OptionRegistry.hpp"
28#include "Exceptions/IllegalTypeException.hpp"
29#include "Exceptions/MissingValueException.hpp"
30#include "CodePatterns/Assert.hpp"
31#include "CodePatterns/Singleton.hpp"
32
33class MapOfActionsTest;
34
35class Box;
36class atom;
37class element;
38class molecule;
39class Vector;
40class RandomNumberDistribution_Parameters;
41
42namespace po = boost::program_options;
43
44using boost::lexical_cast;
45
46#include "CodePatterns/Singleton.hpp"
47
48/** ValueStorage serves as a mediator to MapOfActions.
49 * This is needed to relax inter-dependencies between the Queries and the Actions.
50 * I.e. this is the interface implemented in MapOfActions which both can safely rely on
51 * to store&retrieve/exchange values.
52 *
53 * \section <ValueStorage> (ValueStorage howto)
54 *
55 * If you ever need to add a particular class to the ValueStorage, do as follows:
56 * -# add a specialized queryCurrentValue and setCurrentValue to the definition
57 * of ValueStorage.
58 * -# implement both in the declaration of ValueStorage.
59 * -# in the implementation either directly implement the serializing of the
60 * class' members to a stringstream or use an already implemented operator<<
61 * or operator<<, respectively.
62 *
63 */
64class ValueStorage : public Singleton<ValueStorage> {
65 friend class Singleton<ValueStorage>;
66
67public:
68
69 bool isCurrentValuePresent(const char *name) const;
70 void queryCurrentValue(const char * name, const atom * &_T);
71 void queryCurrentValue(const char * name, const element * &_T);
72 void queryCurrentValue(const char * name, const molecule * &_T);
73 void queryCurrentValue(const char * name, class Box &_T);
74 void queryCurrentValue(const char * name, class Vector &_T);
75 void queryCurrentValue(const char * name, class BoxVector &_T);
76 void queryCurrentValue(const char * name, class RandomNumberDistribution_Parameters &_T);
77 void queryCurrentValue(const char * name, std::vector<const atom *>&_T);
78 void queryCurrentValue(const char * name, std::vector<const element *>&_T);
79 void queryCurrentValue(const char * name, std::vector<const molecule *>&_T);
80 void queryCurrentValue(const char * name, boost::filesystem::path&_T);
81
82 /** Gets a value from the storage
83 * If the value is not present, an ASSERT is thrown unless optional is set to true.
84 * \param _T key of value
85 * \param optional whether this value is optional, i.e. may actually not be in the storage (i.e. may return false in this case).
86 * \return true - value present, false - value not present (only given when optional set to true)
87 */
88 template<typename T> void queryCurrentValue(const char * name, T &_T)
89 {
90 if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
91 if (CurrentValueMap.find(name) == CurrentValueMap.end())
92 throw MissingValueException(__FILE__, __LINE__);
93 _T = lexical_cast<T>(CurrentValueMap[name].c_str());
94 CurrentValueMap.erase(name);
95 } else
96 throw IllegalTypeException(__FILE__,__LINE__);
97 }
98 template<typename T> void queryCurrentValue(const char * name, std::vector<T> &_T)
99 {
100 T temp;
101 if (typeid( std::vector<T> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
102 if (CurrentValueMap.find(name) == CurrentValueMap.end())
103 throw MissingValueException(__FILE__, __LINE__);
104 std::istringstream stream(CurrentValueMap[name]);
105 CurrentValueMap.erase(name);
106 while (!stream.fail()) {
107 stream >> temp >> std::ws;
108 if (!stream.fail()) {
109 _T.push_back(temp);
110 }
111 }
112 } else
113 throw IllegalTypeException(__FILE__,__LINE__);
114 }
115
116 void setCurrentValue(const char * name, const atom * &_T);
117 void setCurrentValue(const char * name, const element * &_T);
118 void setCurrentValue(const char * name, const molecule * &_T);
119 void setCurrentValue(const char * name, class Box &_T);
120 void setCurrentValue(const char * name, class Vector &_T);
121 void setCurrentValue(const char * name, class RandomNumberDistribution_Parameters &_T);
122 void setCurrentValue(const char * name, std::vector<const atom *>&_T);
123 void setCurrentValue(const char * name, std::vector<const element *>&_T);
124 void setCurrentValue(const char * name, std::vector<const molecule *>&_T);
125 void setCurrentValue(const char * name, boost::filesystem::path&_T);
126
127 /** Sets a value in the storage.
128 * \param name key of value
129 * \param _T value
130 */
131 template<class T> void setCurrentValue(const char * name, T &_T)
132 {
133 std::ostringstream stream;
134 if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
135 stream << _T;
136 CurrentValueMap[name] = stream.str();
137 } else
138 throw IllegalTypeException(__FILE__,__LINE__);
139 }
140 /** Sets a value in the storage.
141 * \param name key of value
142 * \param _T value
143 */
144 template<class T> void setCurrentValue(const char * name, std::vector<T> &_T)
145 {
146 std::ostringstream stream;
147 if (typeid( std::vector<T> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
148 std::ostringstream stream;
149 for (typename std::vector<T>::const_iterator iter = _T.begin(); iter != _T.end(); ++iter) {
150 stream << (*iter) << " ";
151 }
152 CurrentValueMap[name] = stream.str();
153 } else
154 throw IllegalTypeException(__FILE__,__LINE__);
155 }
156
157 const std::string getCurrentValue(std::string actionname);
158
159protected:
160 ValueStorage();
161 ~ValueStorage();
162
163 std::map<std::string, std::string> CurrentValueMap;
164
165 OptionRegistry &OptionRegistry_instance;
166};
167
168#endif /* VALUESTORAGE_HPP_ */
Note: See TracBrowser for help on using the repository browser.