libzed 1.11.4
A general-purpose library for quick and simple data manipulation.
 
Loading...
Searching...
No Matches
generic.hpp
1#pragma once
2
3#include "../core/array.hpp"
4#include "../core/string.hpp"
5#include <variant>
6
7#ifdef __has_include
8#if __has_include(<cereal/cereal.hpp>)
9#include <cereal/types/complex.hpp>
10#endif
11#endif
12
13namespace z {
14// Default complex type is double.
15typedef std::complex<double> cplx;
16
17namespace util {
25class generic {
26public:
29
30private:
31 std::variant<bool, long, double, std::complex<double>, zstring, list> value;
32
33public:
35 enum { VOID, INT, FLOAT, COMPLEX, STRING, ARRAY };
36
38 generic() noexcept {}
39
45 generic(INT initVal) noexcept : value(long(initVal)) {}
46
51 generic(double initVal) noexcept : value(initVal) {}
52
57 generic(const std::complex<double> &initVal) noexcept : value(initVal) {}
58
63 generic(const char *initVal) noexcept : value(zstring(initVal)) {}
64
69 generic(const wchar_t *initVal) noexcept : value(zstring(initVal)) {}
70
75 generic(const zstring &initVal) noexcept : value(initVal) {}
76
81 generic(const list &initVal) noexcept : value(initVal) {}
82
90 generic(const std::initializer_list<generic> &initVal) noexcept : value(list(initVal)) {}
91
97 explicit inline operator INT() const {
98 return integer();
99 }
100
105 explicit inline operator float() const noexcept {
106 return floating();
107 }
108
113 explicit inline operator double() const noexcept {
114 return floating();
115 }
116
122
128
134
146 zstring toString(bool printArrays = false) const noexcept; // safely cast to string; slow, copies to new string
147
158 zstring &string();
159
170 const zstring &string() const;
171
183
195
202
208
214 bool is(std::size_t type) const noexcept;
215
221
227
233 int reducesTo(bool castStrings = false) const noexcept; // the lowest we can downcast without losing precision
234
241
247 bool promote(generic *other) noexcept; // promote two numeric values to the same type
248
256
264 auto tmp = *this;
265 return tmp += other;
266 }
267
274 generic &operator-=(const generic &other);
275
282 generic operator-(const generic &other) const {
283 auto tmp = *this;
284 return tmp -= other;
285 }
286
293 generic &operator*=(const generic &other);
294
301 generic operator*(const generic &other) const {
302 auto tmp = *this;
303 return tmp *= other;
304 }
305
312 generic &operator/=(const generic &other);
313
320 generic operator/(const generic &other) const {
321 auto tmp = *this;
322 return tmp /= other;
323 }
324
332 generic &operator%=(const generic &other);
333
341 generic operator%(const generic &other) const {
342 auto tmp = *this;
343 return tmp %= other;
344 }
345
351 generic operator-() const;
352
361 bool operator==(const generic &other) const;
362
371 bool equals(const generic &other) const {
372 return operator==(other);
373 }
374
380 inline bool operator!=(const generic &other) const {
381 return !operator==(other);
382 }
383
394 bool equivalent(const generic &other) const;
395
396#ifdef __has_include
397#if __has_include(<cereal/cereal.hpp>)
402 template <typename archive>
403 void save(archive &ar) const {
404 const short int type = this->value.index();
406
407 if (type == ARRAY) {
408 ar(cereal::make_nvp("value", array()));
409 } else if (type == STRING) {
410 ar(cereal::make_nvp("value", string()));
411 } else if (type == COMPLEX) {
412 auto val = complex();
413 ar(cereal::make_nvp("value", val));
414 } else if (type == FLOAT) {
415 ar(cereal::make_nvp("value", floating()));
416 } else if (type == INT) {
417 ar(cereal::make_nvp("value", integer()));
418 }
419 }
420
425 template <class archive>
426 void load(archive &ar) {
427 short int type = VOID;
429
430 if (type == ARRAY) {
431 list val;
432 ar(cereal::make_nvp("value", val));
433 value = val;
434 } else if (type == STRING) {
435 zstring val;
436 ar(cereal::make_nvp("value", val));
437 value = val;
438 } else if (type == COMPLEX) {
439 std::complex<double> val;
440 ar(cereal::make_nvp("value", val));
441 value = val;
442 } else if (type == FLOAT) {
443 double val;
444 ar(cereal::make_nvp("value", val));
445 value = val;
446 } else if (type == INT) {
447 long val;
448 ar(cereal::make_nvp("value", val));
449 value = val;
450 }
451 }
452#endif
453#endif
454};
455
456} // namespace util
457} // namespace z
A wrapper for std::vector.
Definition array.hpp:38
A template class for character strings.
Definition string.hpp:62
A class for simple storage and manipulation of data regardless of type.
Definition generic.hpp:25
bool operator==(const generic &other) const
Check strict equality.
z::core::array< generic > list
typedef for an array of generic objects
Definition generic.hpp:28
generic & operator*=(const generic &other)
Multiply a value with this numeric value.
bool equals(const generic &other) const
Check strict equality.
Definition generic.hpp:371
bool reduce(bool castStrings=false) noexcept
Downcast a value as low as possible without losing precision.
generic & operator-=(const generic &other)
Subtract a value from this numeric value.
generic operator-(const generic &other) const
Subtract a value from this numeric value.
Definition generic.hpp:282
generic & operator%=(const generic &other)
Get the remainder of division by another value.
zstring toString(bool printArrays=false) const noexcept
Safely convert to a string.
bool promote(generic *other) noexcept
Upcast two values as much as is needed for them to be the same type.
const list & arrayOr(const list &def) const noexcept
Get a reference to this array, or a default value if this is not an array.
generic & operator/=(const generic &other)
Divide this value by another numeric value.
bool is(std::size_t type) const noexcept
Check if this value is a specific type.
std::complex< double > complex() const noexcept
Conversion to a complex value.
double floating() const noexcept
Conversion to floating point.
bool equivalent(const generic &other) const
Check loose equality.
bool operator!=(const generic &other) const
Check strict inequality.
Definition generic.hpp:380
int type() const noexcept
Get the current type of this object.
int reducesTo(bool castStrings=false) const noexcept
Get the lowest type this value can be cast to without losing precision.
list & array()
Get a reference to this array.
generic operator/(const generic &other) const
Divide this value by another numeric value.
Definition generic.hpp:320
long integer() const noexcept
Conversion to integer.
generic operator*(const generic &other) const
Multiply a value with this numeric value.
Definition generic.hpp:301
generic operator-() const
Get the negation of this value.
generic operator%(const generic &other) const
Get the remainder of division by another value.
Definition generic.hpp:341
zstring typeString() const noexcept
Get a character string representing the current type.
bool numeric() const noexcept
Whether this value is a numeric type.