source: src/Parameters/DiscreteValue_impl.hpp@ 9b5eb0

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 9b5eb0 was 047cad, checked in by Michael Ankele <ankele@…>, 13 years ago

added string functions to ValueInterface

  • Property mode set to 100644
File size: 5.1 KB
RevLine 
[c68409]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/** Constructor of class DiscreteValue.
26 */
27template <class T>
28DiscreteValue<T>::DiscreteValue() :
29 ValueSet(false)
30{}
31
32/** Constructor of class DiscreteValue with set of valid values.
33 *
34 * @param _ValidValues vector with all valid values
35 */
36template <class T>
37DiscreteValue<T>::DiscreteValue(const std::vector<T> &_ValidValues) :
38 ValueSet(false),
39 ValidValues(_ValidValues)
40{}
41
42/** Destructor of class DiscreteValue.
43 */
44template <class T>
45DiscreteValue<T>::~DiscreteValue()
46{}
47
48/** Checks whether \a _value is a valid value.
49 * \param _value value to check for validity.
50 * \return true - \a _value is valid, false - is not
51 */
52template <class T>
53bool DiscreteValue<T>::isValid(const T & _value) const
54{
[047cad]55 typename ValidRange::const_iterator iter = std::find(ValidValues.begin(), ValidValues.end(), _value);
56 if (iter != ValidValues.end()) {
57 //std::cout << "Found " << _value << ":" << *iter << std::endl;
58 return true;
59 } else {
60 //std::cout << "Did not find " << _value << "." << std::endl;
61 return false;
62 }
[c68409]63}
64
65/** Compares this discrete value against another \a _instance.
66 *
67 * @param _instance other value to compare to
68 * @return true - if value and valid ranges are the same, false - else
69 */
70template <class T>
71bool DiscreteValue<T>::operator==(const DiscreteValue<T> &_instance) const
72{
73 bool status = true;
74 status = status && (ValidValues == _instance.ValidValues);
75 status = status && (ValueSet == _instance.ValueSet);
76 if (ValueSet && _instance.ValueSet)
77 status = status && (value == _instance.value);
78 return status;
79}
80
81
82/** Getter of value, returning string.
83 *
84 * @return string value
85 */
86template <class T>
87const T & DiscreteValue<T>::get() const
88{
89 ASSERT(ValueSet,
[047cad]90 "DiscreteValue<>::get() - value has never been set.");
91 return ValidValues[value];
[c68409]92}
93
94/** Setter of value for string
95 *
96 * @param _value string containing new value
97 */
98template <class T>
99void DiscreteValue<T>::set(const T & _value)
100{
[047cad]101 const size_t index = findIndexOfValue(_value);
102 ASSERT(index != (size_t)-1,
103 "DiscreteValue<>::set() - value "+toString(_value)+" is not valid.");
104 if (!ValueSet)
105 ValueSet = true;
106 value = index;
[c68409]107}
108
109
110/** Internal function for finding the index of a desired value.
111 *
112 * \note As this is internal, we do not ASSERT value's presence, but return -1
113 * such that other functions may ASSERT on that.
114 *
115 * \param _value value to get the index of
116 * \return index such that ValidValues[index] == _value
117 */
118template <class T>
119const size_t DiscreteValue<T>::findIndexOfValue(const T &_value) const
120{
121 size_t index = 0;
122 const size_t max = ValidValues.size();
123 for (; index < max; ++index) {
124 if (ValidValues[index] == _value)
125 break;
126 }
127 if (index == max)
128 return (size_t)-1;
129 else
130 return index;
131}
132
133/** Adds another value to the valid ones.
134 *
135 * We check whether its already present, otherwise we throw an Assert::AssertionFailure.
136 *
137 * @param _value value to add
138 */
139template <class T>
140void DiscreteValue<T>::appendValidValue(const T &_value)
141{
[047cad]142 ASSERT(!isValid(_value),
[c68409]143 "DiscreteValue<>::appendValidValue() - value "+toString(_value)+" is already among the valid");
144 ValidValues.push_back(_value);
145}
146
147/** Returns all possible valid values.
148 *
149 * @return vector with all allowed values
150 */
151template <class T>
152const std::vector<T> &DiscreteValue<T>::getValidValues() const
153{
154 return ValidValues;
155}
156
157/** Sets the value.
158 *
159 * We check for its validity, otherwise we throw an Assert::AssertionFailure.
160 *
161 * @param _value const reference of value to set
162 */
163template <class T>
[047cad]164void DiscreteValue<T>::setAsString(const std::string _value)
[c68409]165{
[047cad]166 /*const size_t index = findIndexOfValue(_value);
[c68409]167 ASSERT(index != (size_t)-1,
168 "DiscreteValue<>::set() - value "+toString(_value)+" is not valid.");
169 if (!ValueSet)
170 ValueSet = true;
[047cad]171 value = index;*/
[c68409]172}
173
174/** Getter for the set value.
175 *
176 * We check whether it has been set, otherwise we throw an Assert::AssertionFailure.
177 *
178 * @return set value
179 */
180template <class T>
[047cad]181const std::string DiscreteValue<T>::getAsString() const
[c68409]182{
183 ASSERT(ValueSet,
184 "DiscreteValue<>::get() - value has never been set.");
[047cad]185 return toString(ValidValues[value]);
[c68409]186}
187
188/** Checks whether \a _value is a valid value.
189 * \param _value value to check for validity.
190 * \return true - \a _value is valid, false - is not
191 */
192template <class T>
[047cad]193bool DiscreteValue<T>::isValidAsString(const std::string _value) const
[c68409]194{
[047cad]195 /*typename ValidRange::const_iterator iter = std::find(ValidValues.begin(), ValidValues.end(), _value);
[c68409]196 if (iter != ValidValues.end()) {
197 //std::cout << "Found " << _value << ":" << *iter << std::endl;
198 return true;
199 } else {
200 //std::cout << "Did not find " << _value << "." << std::endl;
201 return false;
[047cad]202 }*/
203 return true;
[c68409]204}
205
206#endif /* DISCRETEVALUE_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.