aGrUM  0.16.0
continuousVariable_inl.h
Go to the documentation of this file.
1 
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 
31 # include <cctype>
32 # include <cstdlib>
33 
35 
36 namespace gum {
37 
38 
40  template <>
42  operator[](const std::string& str) const {
43  float value;
44  std::size_t pos;
45  try {
46  value = std::stof(str, &pos);
47  } catch (std::invalid_argument&) {
48  GUM_ERROR(TypeError, "the value is not a number");
49  } catch (std::out_of_range&) {
50  GUM_ERROR(OutOfBounds, "the value is too huge");
51  }
52 
53  // check whether there remains non-space unprocessed characters
54  for (auto iter = str.begin() + pos, end = str.end(); iter != end; ++iter) {
55  if (!std::isspace(static_cast< unsigned char >(*iter)))
56  GUM_ERROR(TypeError, "the value is not a number");
57  }
58 
59  if (belongs(value))
60  return value;
61  else
62  GUM_ERROR(OutOfBounds,
63  "the value does not belong to the domain of the variable");
64  }
65 
66 
68  template <>
69  INLINE double ContinuousVariable< double >::
70  operator[](const std::string& str) const {
71  double value;
72  std::size_t pos;
73  try {
74  value = std::stod(str, &pos);
75  } catch (std::invalid_argument&) {
76  GUM_ERROR(TypeError, "the value is not a number");
77  } catch (std::out_of_range&) {
78  GUM_ERROR(OutOfBounds, "the value is too huge");
79  }
80 
81  // check whether there remains non-space unprocessed characters
82  for (auto iter = str.begin() + pos, end = str.end(); iter != end; ++iter) {
83  if (!std::isspace(static_cast< unsigned char >(*iter)))
84  GUM_ERROR(TypeError, "the value is not a number");
85  }
86 
87  if (belongs(value))
88  return value;
89  else
90  GUM_ERROR(OutOfBounds,
91  "the value does not belong to the domain of the variable");
92  }
93 
94 } /* namespace gum */
95 
96 #endif // DOXYGEN_SHOULD_SKIP_THIS
GUM_SCALAR operator[](const std::string &str) const
returns the T_VAL corresponding to a string
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55