A class for simple storage and manipulation of data regardless of type. More...
#include <generic.hpp>
Public Types | |
enum | { VOID , INT , FLOAT , COMPLEX , STRING , ARRAY } |
List of possible data types. | |
typedef z::core::array< generic > | list |
typedef for an array of generic objects | |
Public Member Functions | |
generic () noexcept | |
Construct as void type. | |
template<typename INT , typename = typename std::enable_if<std::is_integral<INT>::value, INT>::type> | |
generic (INT initVal) noexcept | |
Construct as integer type. | |
generic (double initVal) noexcept | |
Construct as floating point type. | |
generic (const std::complex< double > &initVal) noexcept | |
Construct as complex type. | |
generic (const char *initVal) noexcept | |
Construct string type from string literal. | |
generic (const wchar_t *initVal) noexcept | |
Construct string type from wide string literal. | |
generic (const zstring &initVal) noexcept | |
Construct as string type. | |
generic (const list &initVal) noexcept | |
Construct as array type. | |
generic (const std::initializer_list< generic > &initVal) noexcept | |
Construct as array type from initializer list. | |
template<typename INT , typename = typename std::enable_if<std::is_integral<INT>::value, INT>::type> | |
operator INT () const | |
Explicit conversion to integer types. | |
operator float () const noexcept | |
Explicit conversion to float. | |
operator double () const noexcept | |
Explicit conversion to double. | |
long | integer () const noexcept |
Conversion to integer. | |
double | floating () const noexcept |
Conversion to floating point. | |
std::complex< double > | complex () const noexcept |
Conversion to a complex value. | |
zstring | toString (bool printArrays=false) const noexcept |
Safely convert to a string. | |
zstring & | string () |
Get a reference to this string. | |
const zstring & | string () const |
Get a const reference to this string. | |
list & | array () |
Get a reference to this array. | |
const list & | array () const |
Get a const reference to this array. | |
const list & | arrayOr (const list &def) const noexcept |
Get a reference to this array, or a default value if this is not an array. | |
bool | numeric () const noexcept |
Whether this value is a numeric type. | |
bool | is (std::size_t type) const noexcept |
Check if this value is a specific type. | |
int | type () const noexcept |
Get the current type of this object. | |
zstring | typeString () const noexcept |
Get a character string representing the current type. | |
int | reducesTo (bool castStrings=false) const noexcept |
Get the lowest type this value can be cast to without losing precision. | |
bool | reduce (bool castStrings=false) noexcept |
Downcast a value as low as possible without losing precision. | |
bool | promote (generic *other) noexcept |
Upcast two values as much as is needed for them to be the same type. | |
generic & | operator+= (const generic &other) |
Add a value to this numeric value. | |
generic | operator+ (const generic &other) const |
Add a value to this numeric value. | |
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. | |
generic & | operator*= (const generic &other) |
Multiply a value with this numeric value. | |
generic | operator* (const generic &other) const |
Multiply a value with this numeric value. | |
generic & | operator/= (const generic &other) |
Divide this value by another numeric value. | |
generic | operator/ (const generic &other) const |
Divide this value by another numeric value. | |
generic & | operator%= (const generic &other) |
Get the remainder of division by another value. | |
generic | operator% (const generic &other) const |
Get the remainder of division by another value. | |
generic | operator- () const |
Get the negation of this value. | |
bool | operator== (const generic &other) const |
Check strict equality. | |
bool | equals (const generic &other) const |
Check strict equality. | |
bool | operator!= (const generic &other) const |
Check strict inequality. | |
bool | equivalent (const generic &other) const |
Check loose equality. | |
template<typename archive > | |
void | save (archive &ar) const |
Serialization output. | |
template<class archive > | |
void | load (archive &ar) |
Serialization input. | |
A class for simple storage and manipulation of data regardless of type.
The idea of this class is to allow somewhat loose typing in c++ projects. Note that due to type checking, using this class will not be quite as fast as using the raw types. An example use case is when the data types may be unknown until runtime.
|
inlinenoexcept |
Construct as integer type.
initVal | The value to initialize with. |
|
inlinenoexcept |
Construct as floating point type.
initVal | The value to initialize with. |
Construct as complex type.
initVal | The value to initialize with. |
Construct string type from string literal.
initVal | The value to initialize with. |
Construct string type from wide string literal.
initVal | The value to initialize with. |
Construct as string type.
initVal | The value to initialize with. |
Construct as array type.
initVal | The value to initialize with. |
Construct as array type from initializer list.
This allows generic objects to be constructed with less syntax fluff.
initVal | The value to initialize with. |
list & z::util::generic::array | ( | ) |
Get a reference to this array.
This method allows fast access to and manipulation of array contents.
std::exception | if the current type is not an array. It's a good idea to check before calling this method. |
const list & z::util::generic::array | ( | ) | const |
Get a const reference to this array.
This method allows fast access to (but not manipulation of) array contents.
std::exception | if the current type is not an array. It's a good idea to check before calling this method. |
Get a reference to this array, or a default value if this is not an array.
def | The default value to return if this is not an array. |
|
noexcept |
Conversion to a complex value.
Check strict equality.
other | The object to compare against. |
Check loose equality.
other | The object to compare against. |
|
noexcept |
Conversion to floating point.
|
noexcept |
Conversion to integer.
|
noexcept |
Check if this value is a specific type.
type | The type to check for. |
Serialization input.
ar | The input archive. |
|
noexcept |
Whether this value is a numeric type.
|
inlineexplicitnoexcept |
Explicit conversion to double.
|
inlineexplicitnoexcept |
Explicit conversion to float.
|
inlineexplicit |
Explicit conversion to integer types.
|
inline |
Check strict inequality.
other | The object to compare against. |
|
inline |
Get the remainder of division by another value.
nonnumeric | if the operation is attempted with either value being non-numeric. |
other | The value to get the remainder of division by. |
generic & z::util::generic::operator%= | ( | const generic & | other | ) |
Get the remainder of division by another value.
nonnumeric | if the operation is attempted with either value being non-numeric. |
other | The value to get the remainder of division by. |
|
inline |
Multiply a value with this numeric value.
nonnumeric | if the operation is attempted with either value being non-numeric. |
other | The value to multiply. |
generic & z::util::generic::operator*= | ( | const generic & | other | ) |
Multiply a value with this numeric value.
nonnumeric | if the operation is attempted with either value being non-numeric. |
other | The value to multiply. |
|
inline |
Add a value to this numeric value.
nonnumeric | if the operation is attempted with either value being non-numeric (and both are not strings). |
other | The value to add. |
generic & z::util::generic::operator+= | ( | const generic & | other | ) |
Add a value to this numeric value.
nonnumeric | if the operation is attempted with either value being non-numeric (and both are not strings). |
other | The value to add. |
generic z::util::generic::operator- | ( | ) | const |
Get the negation of this value.
nonnumeric | if the operation is attempted on a non-numeric value. |
|
inline |
Subtract a value from this numeric value.
nonnumeric | if the operation is attempted with either value being non-numeric. |
other | The value to subtract. |
generic & z::util::generic::operator-= | ( | const generic & | other | ) |
Subtract a value from this numeric value.
nonnumeric | if the operation is attempted with either value being non-numeric. |
other | The value to subtract. |
|
inline |
Divide this value by another numeric value.
nonnumeric | if the operation is attempted with either value being non-numeric. |
other | The value to divide by. |
generic & z::util::generic::operator/= | ( | const generic & | other | ) |
Divide this value by another numeric value.
nonnumeric | if the operation is attempted with either value being non-numeric. |
other | The value to divide by. |
bool z::util::generic::operator== | ( | const generic & | other | ) | const |
Check strict equality.
other | The object to compare against. |
Upcast two values as much as is needed for them to be the same type.
other | The value to upcast along with this. |
Downcast a value as low as possible without losing precision.
castStrings | Whether to downcast strings to numeric types. |
Get the lowest type this value can be cast to without losing precision.
castStrings | whether to attempt to downcast strings to numeric types. |
Serialization output.
ar | The output archive. |
zstring & z::util::generic::string | ( | ) |
Get a reference to this string.
This method is faster than toString() and allows manipulation of string contents.
std::exception | if the current type is not a string. Use toString() if you do not know for sure what type this is. |
Get a const reference to this string.
This method is faster than toString() and allows access to string contents.
std::exception | if the current type is not a string. Use toString() if you do not know for sure what type this is. |
Safely convert to a string.
Generates a string representation of the contained value. Note that for performance reasons, arrays come back as null strings by default.
printArrays | If true, generates a full string representation of array contents. Otherwise arrays always result in a null string. |
|
noexcept |
Get the current type of this object.
|
noexcept |
Get a character string representing the current type.