Evoplex  0.2.1
value.h
1 /* Evoplex <https://evoplex.org>
2  * Copyright (C) 2016-present - Marcos Cardinot <marcos@cardinot.net>
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef VALUE_H
18 #define VALUE_H
19 
20 #include <functional>
21 #include <vector>
22 #include <QString>
23 
24 namespace evoplex {
25 
26 class Value;
27 using Values = std::vector<Value>;
28 
56 class Value
57 {
58  friend struct std::hash<Value>;
59 
60 public:
64  enum Type { BOOL, CHAR, DOUBLE, INT, STRING, INVALID };
65 
70  Value();
71 
76  Value(const Value& value);
77 
81  Value(bool value);
82 
86  Value(char value);
87 
91  Value(double value);
92 
96  Value(int value);
97 
101  Value(const char* value);
102 
106  Value(const QString& value);
107 
113  Value(std::vector<bool>::reference value);
114 
118  ~Value();
119 
123  inline Type type() const;
124 
129  inline bool isValid() const;
130 
135  inline bool isBool() const;
136 
141  inline bool isChar() const;
142 
147  inline bool isDouble() const;
148 
153  inline bool isInt() const;
154 
159  inline bool isString() const;
160 
167  inline bool toBool() const;
168 
175  inline char toChar() const;
176 
183  inline double toDouble() const;
184 
191  inline int toInt() const;
192 
199  inline quint32 toUInt() const;
200 
207  inline const char* toString() const;
208 
222  QString toQString(char format = 'g', int precision = 8) const;
223 
227  Value& operator=(const Value& v);
228 
232  bool operator==(const Value& v) const;
233 
237  bool operator!=(const Value& v) const;
238 
242  bool operator<(const Value& v) const;
243 
247  bool operator>(const Value& v) const;
248 
252  bool operator<=(const Value& v) const;
253 
257  bool operator>=(const Value& v) const;
258 
259 private:
260  union { bool b; char c; double d; int i; const char* s; } m_data;
261  Type m_type;
262 
263  std::logic_error throwError() const;
264 };
265 
266 /************************************************************************
267  Value: Inline member functions
268  ************************************************************************/
269 
270 inline Value::Type Value::type() const
271 { return m_type; }
272 
273 inline bool Value::isValid() const
274 { return m_type != INVALID; }
275 
276 inline bool Value::isBool() const
277 { return m_type == BOOL; }
278 
279 inline bool Value::isChar() const
280 { return m_type == CHAR; }
281 
282 inline bool Value::isDouble() const
283 { return m_type == DOUBLE; }
284 
285 inline bool Value::isInt() const
286 { return m_type == INT; }
287 
288 inline bool Value::isString() const
289 { return m_type == STRING; }
290 
291 inline bool Value::toBool() const
292 { if (m_type == BOOL) { return m_data.b; } throw throwError(); }
293 
294 inline char Value::toChar() const
295 { if (m_type == CHAR) { return m_data.c; } throw throwError(); }
296 
297 inline double Value::toDouble() const
298 { if (m_type == DOUBLE) { return m_data.d; } throw throwError(); }
299 
300 inline int Value::toInt() const
301 { if (m_type == INT) { return m_data.i; } throw throwError(); }
302 
303 inline const char* Value::toString() const
304 { if (m_type == STRING) { return m_data.s; } throw throwError(); }
305 
306 inline quint32 Value::toUInt() const {
307  if (m_type == INT && m_data.i >= 0) { return static_cast<quint32>(m_data.i); }
308  throw throwError();
309 }
310 
311 } // evoplex
312 
313 
314 namespace std
315 {
320 template <>
321 struct hash<char*>
322 {
323  size_t operator()(const char *s) const {
324  size_t h = 5381;
325  int c;
326  while ((c = *s++)) {
327  h = ((h << 5) + h) + c;
328  }
329  return h;
330  }
331 };
332 
336 template <>
337 struct hash<evoplex::Value>
338 {
339  size_t operator()(const evoplex::Value& v) const {
340  switch (v.type()) {
341  case evoplex::Value::INT: return std::hash<int>()(v.m_data.i);
342  case evoplex::Value::DOUBLE: return std::hash<double>()(v.m_data.d);
343  case evoplex::Value::BOOL: return std::hash<bool>()(v.m_data.b);
344  case evoplex::Value::CHAR: return std::hash<char>()(v.m_data.c);
345  case evoplex::Value::STRING: return std::hash<char*>()(v.m_data.s);
346  default: throw std::invalid_argument("invalid type of Value");
347  }
348  }
349 };
350 } // std
351 #endif // VALUE_H
A class for variant data types (tagged union).
Definition: value.h:56
bool isString() const
Returns true if the storage type of this Value is equal to Type::STRING.
Definition: value.h:288
bool isChar() const
Returns true if the storage type of this Value is equal to Type::CHAR.
Definition: value.h:279
Type
This enum defines the types of variable that a Value can contain.
Definition: value.h:64
Definition: value.h:314
bool operator!=(const Value &v) const
Checks if this Value is different from v.
bool operator==(const Value &v) const
Checks if this Value is equal to v.
bool operator<(const Value &v) const
Checks if this Value is less than v.
bool operator>(const Value &v) const
Checks if this Value is greater than v.
QString toQString(char format='g', int precision=8) const
Returns the Value as a QString.
const char * toString() const
Returns the Value as a string.
Definition: value.h:303
Type type() const
Returns the storage type of the data stored in the Value.
Definition: value.h:270
bool isInt() const
Returns true if the storage type of this Value is equal to Type::INT.
Definition: value.h:285
bool isDouble() const
Returns true if the storage type of this Value is equal to Type::DOUBLE.
Definition: value.h:282
bool toBool() const
Returns the Value as a boolean.
Definition: value.h:291
Value & operator=(const Value &v)
Assigns the value v to this Value.
bool isBool() const
Returns true if the storage type of this Value is equal to Type::BOOL.
Definition: value.h:276
bool isValid() const
Returns true if the storage type of this Value is not Type::INVALID.
Definition: value.h:273
~Value()
Destroys this Value.
Value()
Constructs an invalid Value.
bool operator<=(const Value &v) const
Checks if this Value is less or equal to v.
double toDouble() const
Returns the Value as a double.
Definition: value.h:297
Definition: abstractgraph.h:29
int toInt() const
Returns the Value as an int.
Definition: value.h:300
bool operator>=(const Value &v) const
Checks if this Value is greater or equal to v.
char toChar() const
Returns the Value as a char.
Definition: value.h:294
quint32 toUInt() const
Returns the Value as an unsigned integer.
Definition: value.h:306
A hash function for char*.
Definition: value.h:321