3#include "../core/array.hpp"
4#include "../core/string.hpp"
8#if __has_include(<cereal/cereal.hpp>)
9#include <cereal/types/complex.hpp>
15typedef std::complex<double> cplx;
31 std::variant<bool, long, double, std::complex<double>, zstring,
list> value;
35 enum { VOID, INT, FLOAT, COMPLEX, STRING, ARRAY };
44 template <typename INT, typename = typename std::enable_if<std::is_integral<INT>::value, INT>
::type>
45 generic(INT initVal)
noexcept : value(long(initVal)) {}
51 generic(
double initVal) noexcept : value(initVal) {}
57 generic(
const std::complex<double> &initVal)
noexcept : value(initVal) {}
63 generic(
const char *initVal) noexcept : value(zstring(initVal)) {}
69 generic(
const wchar_t *initVal)
noexcept : value(zstring(initVal)) {}
75 generic(
const zstring &initVal) noexcept : value(initVal) {}
81 generic(
const list &initVal)
noexcept : value(initVal) {}
90 generic(
const std::initializer_list<generic> &initVal) noexcept : value(
list(initVal)) {}
96 template <typename INT, typename = typename std::enable_if<std::is_integral<INT>::value, INT>
::type>
97 explicit inline operator INT()
const {
105 explicit inline operator float() const noexcept {
113 explicit inline operator double() const noexcept {
146 zstring
toString(
bool printArrays = false) const noexcept;
170 const zstring &
string() const;
214 bool is(std::
size_t type) const noexcept;
240 bool reduce(
bool castStrings = false) noexcept;
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();
405 ar(CEREAL_NVP(
type));
408 ar(cereal::make_nvp(
"value",
array()));
409 }
else if (
type == STRING) {
410 ar(cereal::make_nvp(
"value",
string()));
411 }
else if (
type == 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()));
425 template <
class archive>
426 void load(archive &ar) {
427 short int type = VOID;
428 ar(CEREAL_NVP(
type));
432 ar(cereal::make_nvp(
"value", val));
434 }
else if (
type == STRING) {
436 ar(cereal::make_nvp(
"value", val));
438 }
else if (
type == COMPLEX) {
439 std::complex<double> val;
440 ar(cereal::make_nvp(
"value", val));
442 }
else if (
type == FLOAT) {
444 ar(cereal::make_nvp(
"value", val));
446 }
else if (
type == INT) {
448 ar(cereal::make_nvp(
"value", val));
A wrapper for std::vector.
Definition array.hpp:38
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.
generic() noexcept
Construct as void type.
Definition generic.hpp:38
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.
Section containing utility classes.