3#if __cplusplus < 201703L
4#pragma message("\nNote: std::variant is not available so z::util::generic is disabled.\nCompile with STD=c++17 or greater to enable this feature.")
6#define Z_GENERIC_EXISTS
8#include "../core/array.hpp"
9#include "../core/string.hpp"
13#if __has_include(<cereal/cereal.hpp>)
14#include <cereal/types/complex.hpp>
20typedef std::complex<double> cplx;
36 std::variant<bool, long, double, std::complex<double>,
zstring,
list> value;
40 enum { VOID, INT, FLOAT, COMPLEX, STRING, ARRAY };
102 explicit inline operator INT()
const {
402#if __has_include(<cereal/cereal.hpp>)
407 template <
typename archive>
409 const short int type = this->value.index();
413 ar(cereal::make_nvp(
"value",
array()));
414 }
else if (
type == STRING) {
415 ar(cereal::make_nvp(
"value",
string()));
416 }
else if (
type == COMPLEX) {
418 ar(cereal::make_nvp(
"value",
val));
419 }
else if (
type == FLOAT) {
421 }
else if (
type == INT) {
422 ar(cereal::make_nvp(
"value",
integer()));
430 template <
class archive>
432 short int type = VOID;
437 ar(cereal::make_nvp(
"value",
val));
439 }
else if (
type == STRING) {
441 ar(cereal::make_nvp(
"value",
val));
443 }
else if (
type == COMPLEX) {
444 std::complex<double>
val;
445 ar(cereal::make_nvp(
"value",
val));
447 }
else if (
type == FLOAT) {
449 ar(cereal::make_nvp(
"value",
val));
451 }
else if (
type == INT) {
453 ar(cereal::make_nvp(
"value",
val));
A wrapper for std::vector.
Definition array.hpp:72
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:30
bool operator==(const generic &other) const
Check strict equality.
z::core::array< generic > list
typedef for an array of generic objects
Definition generic.hpp:33
generic & operator*=(const generic &other)
Multiply a value with this numeric value.
bool equals(const generic &other) const
Check strict equality.
Definition generic.hpp:376
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:287
generic & operator%=(const generic &other)
Get the remainder of division by another value.
void save(archive &ar) const
Serialization output.
Definition generic.hpp:408
zstring toString(bool printArrays=false) const noexcept
Safely convert to a string.
void load(archive &ar)
Serialization input.
Definition generic.hpp:431
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:385
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:325
long integer() const noexcept
Conversion to integer.
generic operator*(const generic &other) const
Multiply a value with this numeric value.
Definition generic.hpp:306
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:346
zstring typeString() const noexcept
Get a character string representing the current type.
bool numeric() const noexcept
Whether this value is a numeric type.