source: src/Parameters/Value_impl.hpp@ b9c69d

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 b9c69d was 6c05d8, checked in by Michael Ankele <ankele@…>, 13 years ago

FIX: Value<string>, empty validators

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