source: src/Parameters/Value_impl.hpp@ 50eff3

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 50eff3 was 50eff3, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: Value::getValidRange() and ::getValidValues() were broken.

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