aGrUM  0.14.2
continuousVariable_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 #ifndef DOXYGEN_SHOULD_SKIP_THIS
27 
28 # include <cctype>
29 # include <cstdlib>
30 
32 
33 namespace gum {
34 
35 
37  template <>
39  operator[](const std::string& str) const {
40  float value;
41  std::size_t pos;
42  try {
43  value = std::stof(str, &pos);
44  } catch (std::invalid_argument&) {
45  GUM_ERROR(TypeError, "the value is not a number");
46  } catch (std::out_of_range&) {
47  GUM_ERROR(OutOfBounds, "the value is too huge");
48  }
49 
50  // check whether there remains non-space unprocessed characters
51  for (auto iter = str.begin() + pos, end = str.end(); iter != end; ++iter) {
52  if (!std::isspace(static_cast< unsigned char >(*iter)))
53  GUM_ERROR(TypeError, "the value is not a number");
54  }
55 
56  if (belongs(value))
57  return value;
58  else
59  GUM_ERROR(OutOfBounds,
60  "the value does not belong to the domain of the variable");
61  }
62 
63 
65  template <>
66  INLINE double ContinuousVariable< double >::
67  operator[](const std::string& str) const {
68  double value;
69  std::size_t pos;
70  try {
71  value = std::stod(str, &pos);
72  } catch (std::invalid_argument&) {
73  GUM_ERROR(TypeError, "the value is not a number");
74  } catch (std::out_of_range&) {
75  GUM_ERROR(OutOfBounds, "the value is too huge");
76  }
77 
78  // check whether there remains non-space unprocessed characters
79  for (auto iter = str.begin() + pos, end = str.end(); iter != end; ++iter) {
80  if (!std::isspace(static_cast< unsigned char >(*iter)))
81  GUM_ERROR(TypeError, "the value is not a number");
82  }
83 
84  if (belongs(value))
85  return value;
86  else
87  GUM_ERROR(OutOfBounds,
88  "the value does not belong to the domain of the variable");
89  }
90 
91 } /* namespace gum */
92 
93 #endif // DOXYGEN_SHOULD_SKIP_THIS
GUM_SCALAR operator[](const std::string &str) const
returns the T_VAL corresponding to a string
Header of ContinuousVariable.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52