source: src/Parameters/Value_impl.hpp@ f10b0c

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 Candidate_v1.7.0 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 f10b0c was f10b0c, checked in by Frederik Heber <heber@…>, 14 years ago

disastrously big and ugly commit

  • using new Parameter<T> classes:
    • Actions
    • Queries (all UIs)
  • TODO:
    • actions crash cause Value is unset
    • no query<BoxVector>
  • Property mode set to 100644
File size: 6.0 KB
Line 
1/*
2 * Value_impl.hpp
3 *
4 * Created on: Apr 13, 2012
5 * Author: ankele
6 */
7
8#ifndef VALUE_IMPL_HPP_
9#define VALUE_IMPL_HPP_
10
11
12// include config.h
13#ifdef HAVE_CONFIG_H
14#include <config.h>
15#endif
16
17
18#include <boost/any.hpp>
19
20#include "CodePatterns/Assert.hpp"
21
22#include "CodePatterns/Log.hpp"
23
24#include "Validators/DummyValidator.hpp"
25#include "Validators/DiscreteValidator.hpp"
26#include "Validators/RangeValidator.hpp"
27
28
29
30// static member
31template <class T> ConvertTo<T> Value<T>::Converter;
32
33/** Constructor of class Value.
34 */
35template <class T>
36Value<T>::Value() :
37 ValueSet(false),
38 validator(new DummyValidator<T>)
39{}
40
41/** Constructor of class Value with a validator.
42 *
43 * @param _validator general validator to use
44 */
45template <class T>
46Value<T>::Value(const Validator<T> &_validator) :
47 ValueSet(false),
48 validator(_validator.clone())
49{}
50
51/** Constructor of class Value with a discrete validator.
52 *
53 * @param _ValidValues vector with all valid values
54 */
55template <class T>
56Value<T>::Value(const std::vector<T> &_ValidValues) :
57 ValueSet(false),
58 validator(NULL)
59{
60 validator = new DiscreteValidator<T>(_ValidValues);
61}
62
63/** Constructor of class Value with a range validator.
64 *
65 * @param _ValidRange range of valid values
66 */
67template <class T>
68Value<T>::Value(const range<T> &_ValidRange) :
69 ValueSet(false),
70 validator(NULL)
71{
72 validator = new RangeValidator<T>(_ValidRange);
73}
74
75/** Destructor of class Value.
76 */
77template <class T>
78Value<T>::~Value()
79{
80 ASSERT(validator,
81 "Value<T>::~Value() - validator missing.");
82 delete(validator);
83}
84
85/** Checks whether \a _value is a valid value.
86 * \param _value value to check for validity.
87 * \return true - \a _value is valid, false - is not
88 */
89template <class T>
90bool Value<T>::isValid(const T & _value) const
91{
92 ASSERT(validator,
93 "Value<T>::isValid() - validator missing.");
94 return (*validator)(_value);
95}
96
97/** Compares this discrete value against another \a _instance.
98 *
99 * @param _instance other value to compare to
100 * @return true - if value and valid ranges are the same, false - else
101 */
102template <class T>
103bool Value<T>::operator==(const Value<T> &_instance) const
104{
105 ASSERT(validator,
106 "Value<T>::operator==() - validator missing.");
107 ASSERT(_instance.validator,
108 "Value<T>::operator==() - instance.validator missing.");
109 bool status = true;
110 status = status && (*validator == *_instance.validator);
111 status = status && (ValueSet == _instance.ValueSet);
112 if (ValueSet && _instance.ValueSet)
113 status = status && (value == _instance.value);
114 return status;
115}
116
117
118/** Getter of value
119 *
120 * @return value
121 */
122template <class T>
123const T & Value<T>::get() const
124{
125 ASSERT(ValueSet,
126 "Value<T>::get() - value has never been set.");
127 return value;
128}
129
130/** Setter of value
131 *
132 * @param _value new value
133 */
134template <class T>
135void Value<T>::set(const T & _value)
136{
137 ASSERT(isValid(_value),
138 "Value<T>::setValue() - trying to set invalid value "+toString(_value)+".");
139 if (!ValueSet)
140 ValueSet = true;
141 value = _value;
142}
143
144
145
146/** Checks whether \a _value is a valid value.
147 * \param _value value to check for validity.
148 * \return true - \a _value is valid, false - is not
149 */
150template <class T>
151bool Value<T>::isValidAsString(const std::string _value) const
152{
153 const T castvalue = Converter(_value);
154// LOG(0, "Converted value reads " << castvalue <<".");
155 return isValid(castvalue);
156}
157
158template <>
159inline bool Value<std::string>::isValidAsString(const std::string _value) const
160{
161 return isValid(_value);
162}
163
164/** Getter of value, returning string.
165 *
166 * @return string value
167 */
168template <class T>
169const std::string Value<T>::getAsString() const
170{
171 ASSERT(ValueSet,
172 "Value<T>::getAsString() - requesting unset value.");
173 return toString(value);
174}
175
176/** Setter of value for string
177 *
178 * @param _value string containing new value
179 */
180template <class T>
181void Value<T>::setAsString(const std::string _value)
182{
183 const T castvalue = Converter(_value);
184// LOG(0, "Converted value reads " << castvalue <<".");
185 set(castvalue);
186// LOG(0, "STATUS: Value is now set to " << value << ".");
187}
188
189template <>
190inline void Value<std::string>::setAsString(const std::string _value)
191{
192 set(_value);
193// LOG(0, "STATUS: Value is now set to " << value << ".");
194}
195
196/** Returns the validator as a const reference.
197 *
198 * @return the validator
199 */
200template <class T>
201const Validator<T> &Value<T>::getValidator() const
202{
203 ASSERT(validator,
204 "Value<T>::getValidator() const - validator missing.");
205 return *validator;
206}
207
208/** Returns the validator.
209 *
210 * @return the validator
211 */
212template <class T>
213Validator<T> &Value<T>::getValidator()
214{
215 ASSERT(validator,
216 "Value<T>::getValidator() - validator missing.");
217 return *validator;
218}
219
220
221
222template <class T>
223const range<T> & Value<T>::getValidRange() const
224{
225 dynamic_cast<RangeValidator<T>&>(getValidator()).getValidRange();
226}
227
228/** Setter for the valid range.
229 *
230 * If value is invalid in new range, we throw AssertFailure and set ValueSet to false.
231 *
232 * @param _range range (pair of values)
233 */
234template <class T>
235void Value<T>::setValidRange(const range<T> &_range)
236{
237 dynamic_cast<RangeValidator<T>&>(getValidator()).setValidRange(_range);
238 if (ValueSet) {
239 //std::cout << "Checking whether " << value << " is in range " << _range << "." << std::endl;
240 if (!isValid(value)){
241 //std::cout << "ValueSet to false." << std::endl;
242 ValueSet = false;
243 // have full check again in assert such that it appears in output, too
244 ASSERT(isValid(value),
245 "Value<T>::setValidRange() - new range "
246 +toString(_range)+" invalidates current value "+toString(value)+".");
247 }
248 }
249 // LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
250}
251
252template <class T>
253void Value<T>::appendValidValue(const T &_value)
254{
255 dynamic_cast<DiscreteValidator<T>&>(getValidator()).appendValidValue(_value);
256}
257
258template <class T>
259const std::vector<T> &Value<T>::getValidValues() const
260{
261 dynamic_cast<DiscreteValidator<T>&>(getValidator()).getValidValues();
262}
263
264
265
266#endif /* VALUE_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.