knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Numeric.h
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #ifndef KNOWROB_NUMERIC_TERM_H
7 #define KNOWROB_NUMERIC_TERM_H
8 
9 #include "XSDAtomic.h"
10 #include "sstream"
11 #include "optional"
12 #include "string"
13 
14 namespace knowrob {
18  class Numeric : public XSDAtomic {
19  public:
21 
22  ~Numeric() override = default;
23 
28  virtual bool isSameNumeric(const Numeric &other) const = 0;
29 
33  virtual double asDouble() const = 0;
34 
38  virtual float asFloat() const = 0;
39 
43  virtual int asInteger() const = 0;
44 
48  virtual long asLong() const = 0;
49 
53  virtual short asShort() const = 0;
54 
58  virtual unsigned int asUnsignedInteger() const = 0;
59 
63  virtual unsigned long asUnsignedLong() const = 0;
64 
68  virtual unsigned short asUnsignedShort() const = 0;
69 
73  virtual bool asBoolean() const = 0;
74 
78  static std::shared_ptr<Numeric> trueAtom();
79 
83  static std::shared_ptr<Numeric> falseAtom();
84 
88  bool isFloatingNumber() const;
89  };
90 
97  template<typename T1, XSDType T2>
98  class NumericTemplate : public Numeric {
99  public:
103  explicit NumericTemplate(T1 numericForm) : Numeric(T2), numericForm_(numericForm) {}
104 
108  explicit NumericTemplate(std::string_view stringForm) : Numeric(T2), stringForm_(stringForm) {}
109 
110  ~NumericTemplate() override = default;
111 
112  T1 operator()() const { return numericForm(); }
113 
117  T1 numericForm() const {
118  if (!numericForm_) {
119  std::istringstream iss(*stringForm_);
120  auto &value = numericForm_.emplace();
121  iss >> std::fixed >> value;
122  return value;
123  } else {
124  return numericForm_.value();
125  }
126  }
127 
128  // override Numeric
129  double asDouble() const override { return static_cast<double>(numericForm()); }
130 
131  // override Numeric
132  float asFloat() const override { return static_cast<float>(numericForm()); }
133 
134  // override Numeric
135  int asInteger() const override { return static_cast<int>(numericForm()); }
136 
137  // override Numeric
138  long asLong() const override { return static_cast<long>(numericForm()); }
139 
140  // override Numeric
141  short asShort() const override { return static_cast<short>(numericForm()); }
142 
143  // override Numeric
144  unsigned int asUnsignedInteger() const override { return static_cast<unsigned int>(numericForm()); }
145 
146  // override Numeric
147  unsigned long asUnsignedLong() const override { return static_cast<unsigned long>(numericForm()); }
148 
149  // override Numeric
150  unsigned short asUnsignedShort() const override { return static_cast<unsigned short>(numericForm()); }
151 
152  // override Numeric
153  bool asBoolean() const override { return static_cast<bool>(numericForm()); }
154 
155  // override Atomic
156  std::string_view stringForm() const override {
157  if (!stringForm_) {
158  std::ostringstream oss;
159  oss << std::fixed << std::boolalpha << *numericForm_;
160  stringForm_ = oss.str();
161  }
162  return *stringForm_;
163  }
164 
165  // override Numeric
166  bool isSameNumeric(const Numeric &other) const override {
167  if (xsdType() != other.xsdType()) {
168  return false;
169  }
170  const auto otherNumeric = static_cast<const NumericTemplate<T1, T2> *>(&other);
171  return numericForm() == otherNumeric->numericForm();
172  }
173 
174  // override Printable
175  void write(std::ostream &os) const override { os << std::defaultfloat << std::boolalpha << numericForm(); }
176 
177  private:
178  // Note: both are mutable because they are initialized in a lazy fashion
179  mutable std::optional<std::string> stringForm_;
180  mutable std::optional<T1> numericForm_;
181  };
182 
186  using Integer = NumericTemplate<int, XSDType::INTEGER>;
187 
191  using Long = NumericTemplate<long, XSDType::LONG>;
192 
196  using Short = NumericTemplate<short, XSDType::SHORT>;
197 
201  using UnsignedLong = NumericTemplate<unsigned long, XSDType::UNSIGNED_LONG>;
202 
206  using UnsignedInt = NumericTemplate<unsigned int, XSDType::UNSIGNED_INT>;
207 
211  using UnsignedShort = NumericTemplate<unsigned short, XSDType::UNSIGNED_SHORT>;
212 
216  using Float = NumericTemplate<float, XSDType::FLOAT>;
217 
221  using Double = NumericTemplate<double, XSDType::DOUBLE>;
222 
226  using Boolean = NumericTemplate<bool, XSDType::BOOLEAN>;
227 
228 } // knowrob
229 
230 #endif //KNOWROB_NUMERIC_TERM_H
bool isFloatingNumber() const
virtual float asFloat() const =0
Numeric(XSDType xsdType)
Definition: Numeric.h:20
virtual short asShort() const =0
virtual long asLong() const =0
virtual bool isSameNumeric(const Numeric &other) const =0
virtual int asInteger() const =0
static std::shared_ptr< Numeric > falseAtom()
virtual unsigned long asUnsignedLong() const =0
virtual unsigned short asUnsignedShort() const =0
virtual double asDouble() const =0
virtual bool asBoolean() const =0
virtual unsigned int asUnsignedInteger() const =0
static std::shared_ptr< Numeric > trueAtom()
~Numeric() override=default
void write(std::ostream &os) const override
Definition: Numeric.h:175
std::string_view stringForm() const override
Definition: Numeric.h:156
~NumericTemplate() override=default
bool asBoolean() const override
Definition: Numeric.h:153
T1 operator()() const
Definition: Numeric.h:112
bool isSameNumeric(const Numeric &other) const override
Definition: Numeric.h:166
double asDouble() const override
Definition: Numeric.h:129
short asShort() const override
Definition: Numeric.h:141
int asInteger() const override
Definition: Numeric.h:135
float asFloat() const override
Definition: Numeric.h:132
T1 numericForm() const
Definition: Numeric.h:117
unsigned short asUnsignedShort() const override
Definition: Numeric.h:150
NumericTemplate(std::string_view stringForm)
Definition: Numeric.h:108
long asLong() const override
Definition: Numeric.h:138
unsigned long asUnsignedLong() const override
Definition: Numeric.h:147
unsigned int asUnsignedInteger() const override
Definition: Numeric.h:144
NumericTemplate(T1 numericForm)
Definition: Numeric.h:103
XSDType xsdType() const
Definition: XSDAtomic.h:43
NumericTemplate< unsigned int, XSDType::UNSIGNED_INT > UnsignedInt
Definition: Numeric.h:206
NumericTemplate< bool, XSDType::BOOLEAN > Boolean
Definition: Numeric.h:226
NumericTemplate< unsigned short, XSDType::UNSIGNED_SHORT > UnsignedShort
Definition: Numeric.h:211
NumericTemplate< int, XSDType::INTEGER > Integer
Definition: Numeric.h:186
NumericTemplate< long, XSDType::LONG > Long
Definition: Numeric.h:191
NumericTemplate< unsigned long, XSDType::UNSIGNED_LONG > UnsignedLong
Definition: Numeric.h:201
NumericTemplate< double, XSDType::DOUBLE > Double
Definition: Numeric.h:221
NumericTemplate< float, XSDType::FLOAT > Float
Definition: Numeric.h:216
XSDType
The XSDType enum Enumeration of the XSD types.
Definition: XSDType.h:16
NumericTemplate< short, XSDType::SHORT > Short
Definition: Numeric.h:196