source: src/Parameters/Value_impl.hpp@ e5d7970

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 e5d7970 was 95f965, checked in by Frederik Heber <heber@…>, 13 years ago

QtQueries only set default values if not already set

  • added Value.isSet() function
  • Property mode set to 100644
File size: 6.1 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/** Tests, if a value has been set
146 *
147 * @return true, if a value has been set
148 */
149template <class T>
150bool Value<T>::isSet() const
151{
152 return ValueSet;
153}
154
155
156
157/** Checks whether \a _value is a valid value.
158 * \param _value value to check for validity.
159 * \return true - \a _value is valid, false - is not
160 */
161template <class T>
162bool Value<T>::isValidAsString(const std::string _value) const
163{
164 const T castvalue = Converter(_value);
165// LOG(0, "Converted value reads " << castvalue <<".");
166 return isValid(castvalue);
167}
168
169template <>
170inline bool Value<std::string>::isValidAsString(const std::string _value) const
171{
172 return isValid(_value);
173}
174
175/** Getter of value, returning string.
176 *
177 * @return string value
178 */
179template <class T>
180const std::string Value<T>::getAsString() const
181{
182 ASSERT(ValueSet,
183 "Value<T>::getAsString() - requesting unset value.");
184 return toString(value);
185}
186
187/** Setter of value for string
188 *
189 * @param _value string containing new value
190 */
191template <class T>
192void Value<T>::setAsString(const std::string _value)
193{
194 const T castvalue = Converter(_value);
195// LOG(0, "Converted value reads " << castvalue <<".");
196 set(castvalue);
197// LOG(0, "STATUS: Value is now set to " << value << ".");
198}
199
200template <>
201inline void Value<std::string>::setAsString(const std::string _value)
202{
203 set(_value);
204// LOG(0, "STATUS: Value is now set to " << value << ".");
205}
206
207/** Returns the validator as a const reference.
208 *
209 * @return the validator
210 */
211template <class T>
212const Validator<T> &Value<T>::getValidator() const
213{
214 ASSERT(validator,
215 "Value<T>::getValidator() const - validator missing.");
216 return *validator;
217}
218
219/** Returns the validator.
220 *
221 * @return the validator
222 */
223template <class T>
224Validator<T> &Value<T>::getValidator()
225{
226 ASSERT(validator,
227 "Value<T>::getValidator() - validator missing.");
228 return *validator;
229}
230
231
232
233template <class T>
234const range<T> & Value<T>::getValidRange() const
235{
236 dynamic_cast<RangeValidator<T>&>(getValidator()).getValidRange();
237}
238
239/** Setter for the valid range.
240 *
241 * If value is invalid in new range, we throw AssertFailure and set ValueSet to false.
242 *
243 * @param _range range (pair of values)
244 */
245template <class T>
246void Value<T>::setValidRange(const range<T> &_range)
247{
248 dynamic_cast<RangeValidator<T>&>(getValidator()).setValidRange(_range);
249 if (ValueSet) {
250 //std::cout << "Checking whether " << value << " is in range " << _range << "." << std::endl;
251 if (!isValid(value)){
252 //std::cout << "ValueSet to false." << std::endl;
253 ValueSet = false;
254 // have full check again in assert such that it appears in output, too
255 ASSERT(isValid(value),
256 "Value<T>::setValidRange() - new range "
257 +toString(_range)+" invalidates current value "+toString(value)+".");
258 }
259 }
260 // LOG(0, "STATUS: Valid range is now " << ValidRange << ".");
261}
262
263template <class T>
264void Value<T>::appendValidValue(const T &_value)
265{
266 dynamic_cast<DiscreteValidator<T>&>(getValidator()).appendValidValue(_value);
267}
268
269template <class T>
270const std::vector<T> &Value<T>::getValidValues() const
271{
272 dynamic_cast<DiscreteValidator<T>&>(getValidator()).getValidValues();
273}
274
275
276
277#endif /* VALUE_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.