source: src/Parser/DiscreteValue_impl.hpp@ a7d753

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

Extended DiscreteValue to ContinuousValue and added ValueInterface for both.

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * DiscreteValues_impl.hpp
3 *
4 * Created on: Sep 28, 2011
5 * Author: heber
6 */
7
8#ifndef DISCRETEVALUE_IMPL_HPP_
9#define DISCRETEVALUE_IMPL_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include <algorithm>
17#include <vector>
18
19#include <boost/any.hpp>
20
21#include "CodePatterns/Assert.hpp"
22
23#include "CodePatterns/Log.hpp"
24
25// static member
26template <class T> ConvertTo<T> DiscreteValue<T>::Converter;
27
28/** Constructor of class DiscreteValue.
29 */
30template <class T>
31DiscreteValue<T>::DiscreteValue() :
32 ValueSet(false)
33{}
34
35/** Constructor of class DiscreteValue with set of valid values.
36 *
37 * @param _ValidValues vector with all valid values
38 */
39template <class T>
40DiscreteValue<T>::DiscreteValue(const std::vector<T> &_ValidValues) :
41 ValueSet(false),
42 ValidValues(_ValidValues)
43{}
44
45/** Destructor of class DiscreteValue.
46 */
47template <class T>
48DiscreteValue<T>::~DiscreteValue()
49{}
50
51/** Checks whether \a _value is a valid value.
52 * \param _value value to check for validity.
53 * \return true - \a _value is valid, false - is not
54 */
55template <class T>
56bool DiscreteValue<T>::isValid(const std::string _value) const
57{
58 const T castvalue = Converter(_value);
59 return isValidValue(castvalue);
60}
61
62/** Getter of value, returning string.
63 *
64 * @return string value
65 */
66template <class T>
67const std::string DiscreteValue<T>::get() const
68{
69 ASSERT(ValueSet,
70 "DiscreteValue<T>::get() - requesting unset value.");
71 return toString(getValue());
72}
73
74/** Setter of value for string
75 *
76 * @param _value string containing new value
77 */
78template <class T>
79void DiscreteValue<T>::set(const std::string _value)
80{
81 const T castvalue = Converter(_value);
82 setValue(castvalue);
83}
84
85
86/** Internal function for finding the index of a desired value.
87 *
88 * \note As this is internal, we do not ASSERT value's presence, but return -1
89 * such that other functions may ASSERT on that.
90 *
91 * \param _value value to get the index of
92 * \return index such that ValidValues[index] == _value
93 */
94template <class T>
95const size_t DiscreteValue<T>::findIndexOfValue(const T &_value) const
96{
97 size_t index = 0;
98 const size_t max = ValidValues.size();
99 for (; index < max; ++index) {
100 if (ValidValues[index] == _value)
101 break;
102 }
103 if (index == max)
104 return (size_t)-1;
105 else
106 return index;
107}
108
109/** Adds another value to the valid ones.
110 *
111 * We check whether its already present, otherwise we throw an Assert::AssertionFailure.
112 *
113 * @param _value value to add
114 */
115template <class T>
116void DiscreteValue<T>::appendValidValue(const T &_value)
117{
118 ASSERT(!isValidValue(_value),
119 "DiscreteValue<>::appendValidValue() - value "+toString(_value)+" is already among the valid");
120 ValidValues.push_back(_value);
121}
122
123/** Returns all possible valid values.
124 *
125 * @return vector with all allowed values
126 */
127template <class T>
128const std::vector<T> &DiscreteValue<T>::getValidValues() const
129{
130 return ValidValues;
131}
132
133/** Sets the value.
134 *
135 * We check for its validity, otherwise we throw an Assert::AssertionFailure.
136 *
137 * @param _value const reference of value to set
138 */
139template <class T>
140void DiscreteValue<T>::setValue(const T &_value)
141{
142 const size_t index = findIndexOfValue(_value);
143 ASSERT(index != (size_t)-1,
144 "DiscreteValue<>::set() - value "+toString(_value)+" is not valid.");
145 if (!ValueSet)
146 ValueSet = true;
147 value = index;
148}
149
150/** Getter for the set value.
151 *
152 * We check whether it has been set, otherwise we throw an Assert::AssertionFailure.
153 *
154 * @return set value
155 */
156template <class T>
157const T & DiscreteValue<T>::getValue() const
158{
159 ASSERT(ValueSet,
160 "DiscreteValue<>::get() - value has never been set.");
161 return ValidValues[value];
162}
163
164/** Checks whether \a _value is a valid value.
165 * \param _value value to check for validity.
166 * \return true - \a _value is valid, false - is not
167 */
168template <class T>
169bool DiscreteValue<T>::isValidValue(const T &_value) const
170{
171 typename ValidRange::const_iterator iter = std::find(ValidValues.begin(), ValidValues.end(), _value);
172 if (iter != ValidValues.end()) {
173 //std::cout << "Found " << _value << ":" << *iter << std::endl;
174 return true;
175 } else {
176 //std::cout << "Did not find " << _value << "." << std::endl;
177 return false;
178 }
179}
180
181#endif /* DISCRETEVALUE_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.