source: src/Actions/ValueStorage.hpp@ 9df680

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 9df680 was 56f73b, checked in by Frederik Heber <heber@…>, 14 years ago

Added config.h also to all header files, code check test ascertain this in the future.

  • as we want to use config.h to pass stuff such as MEMDEBUG, NDEBUG, LOG_OBSERVER, we have to make sure that it is present in each and every file.
  • split up CodeChecks/testsuite.at: each test has its own .at file.
  • Property mode set to 100644
File size: 5.4 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;
40
41namespace po = boost::program_options;
42
43using boost::lexical_cast;
44
45#include "CodePatterns/Singleton.hpp"
46
47/** ValueStorage serves as a mediator to MapOfActions.
48 * This is needed to relax inter-dependencies between the Queries and the Actions.
49 * I.e. this is the interface implemented in MapOfActions which both can safely rely on
50 * to store&retrieve/exchange values.
51 */
52class ValueStorage : public Singleton<ValueStorage> {
53 friend class Singleton<ValueStorage>;
54
55public:
56
57 bool isCurrentValuePresent(const char *name) const;
58 void queryCurrentValue(const char * name, const atom * &_T);
59 void queryCurrentValue(const char * name, const element * &_T);
60 void queryCurrentValue(const char * name, const molecule * &_T);
61 void queryCurrentValue(const char * name, class Box &_T);
62 void queryCurrentValue(const char * name, class Vector &_T);
63 void queryCurrentValue(const char * name, class BoxVector &_T);
64 void queryCurrentValue(const char * name, std::vector<const atom *>&_T);
65 void queryCurrentValue(const char * name, std::vector<const element *>&_T);
66 void queryCurrentValue(const char * name, std::vector<const molecule *>&_T);
67 void queryCurrentValue(const char * name, boost::filesystem::path&_T);
68
69 /** Gets a value from the storage
70 * If the value is not present, an ASSERT is thrown unless optional is set to true.
71 * \param _T key of value
72 * \param optional whether this value is optional, i.e. may actually not be in the storage (i.e. may return false in this case).
73 * \return true - value present, false - value not present (only given when optional set to true)
74 */
75 template<typename T> void queryCurrentValue(const char * name, T &_T)
76 {
77 if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
78 if (CurrentValueMap.find(name) == CurrentValueMap.end())
79 throw MissingValueException(__FILE__, __LINE__);
80 _T = lexical_cast<T>(CurrentValueMap[name].c_str());
81 CurrentValueMap.erase(name);
82 } else
83 throw IllegalTypeException(__FILE__,__LINE__);
84 }
85 template<typename T> void queryCurrentValue(const char * name, std::vector<T> &_T)
86 {
87 T temp;
88 if (typeid( std::vector<T> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
89 if (CurrentValueMap.find(name) == CurrentValueMap.end())
90 throw MissingValueException(__FILE__, __LINE__);
91 std::istringstream stream(CurrentValueMap[name]);
92 CurrentValueMap.erase(name);
93 while (!stream.fail()) {
94 stream >> temp >> std::ws;
95 if (!stream.fail()) {
96 _T.push_back(temp);
97 }
98 }
99 } else
100 throw IllegalTypeException(__FILE__,__LINE__);
101 }
102
103 void setCurrentValue(const char * name, const atom * &_T);
104 void setCurrentValue(const char * name, const element * &_T);
105 void setCurrentValue(const char * name, const molecule * &_T);
106 void setCurrentValue(const char * name, class Box &_T);
107 void setCurrentValue(const char * name, class Vector &_T);
108 void setCurrentValue(const char * name, std::vector<const atom *>&_T);
109 void setCurrentValue(const char * name, std::vector<const element *>&_T);
110 void setCurrentValue(const char * name, std::vector<const molecule *>&_T);
111 void setCurrentValue(const char * name, boost::filesystem::path&_T);
112
113 /** Sets a value in the storage.
114 * \param name key of value
115 * \param _T value
116 */
117 template<class T> void setCurrentValue(const char * name, T &_T)
118 {
119 std::ostringstream stream;
120 if (typeid( T ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
121 stream << _T;
122 CurrentValueMap[name] = stream.str();
123 } else
124 throw IllegalTypeException(__FILE__,__LINE__);
125 }
126 /** Sets a value in the storage.
127 * \param name key of value
128 * \param _T value
129 */
130 template<class T> void setCurrentValue(const char * name, std::vector<T> &_T)
131 {
132 std::ostringstream stream;
133 if (typeid( std::vector<T> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) { // constructor of type_info is private, hence can only store by ref or ptr
134 std::ostringstream stream;
135 for (typename std::vector<T>::const_iterator iter = _T.begin(); iter != _T.end(); ++iter) {
136 stream << (*iter) << " ";
137 }
138 CurrentValueMap[name] = stream.str();
139 } else
140 throw IllegalTypeException(__FILE__,__LINE__);
141 }
142
143 const std::string getCurrentValue(std::string actionname);
144
145protected:
146 ValueStorage();
147 ~ValueStorage();
148
149 std::map<std::string, std::string> CurrentValueMap;
150
151 OptionRegistry &OptionRegistry_instance;
152};
153
154#endif /* VALUESTORAGE_HPP_ */
Note: See TracBrowser for help on using the repository browser.