libzed 1.9.9
A general-purpose library for quick and simple data manipulation.
 
Loading...
Searching...
No Matches
z::util::generic Class Reference

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< genericlist
 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< doublecomplex () const noexcept
 Conversion to a complex value.
 
zstring toString (bool printArrays=false) const noexcept
 Safely convert to a string.
 
zstringstring ()
 Get a reference to this string.
 
const zstringstring () const
 Get a const reference to this string.
 
listarray ()
 Get a reference to this array.
 
const listarray () const
 Get a const reference to this array.
 
const listarrayOr (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.
 
genericoperator+= (const generic &other)
 Add a value to this numeric value.
 
generic operator+ (const generic &other) const
 Add a value to this numeric value.
 
genericoperator-= (const generic &other)
 Subtract a value from this numeric value.
 
generic operator- (const generic &other) const
 Subtract a value from this numeric value.
 
genericoperator*= (const generic &other)
 Multiply a value with this numeric value.
 
generic operator* (const generic &other) const
 Multiply a value with this numeric value.
 
genericoperator/= (const generic &other)
 Divide this value by another numeric value.
 
generic operator/ (const generic &other) const
 Divide this value by another numeric value.
 
genericoperator%= (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.
 

Detailed Description

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.

Examples
generic.cpp.

Constructor & Destructor Documentation

◆ generic() [1/8]

template<typename INT , typename = typename std::enable_if<std::is_integral<INT>::value, INT>::type>
z::util::generic::generic ( INT  initVal)
inlinenoexcept

Construct as integer type.

Parameters
initValThe value to initialize with.

◆ generic() [2/8]

z::util::generic::generic ( double  initVal)
inlinenoexcept

Construct as floating point type.

Parameters
initValThe value to initialize with.

◆ generic() [3/8]

z::util::generic::generic ( const std::complex< double > &  initVal)
inlinenoexcept

Construct as complex type.

Parameters
initValThe value to initialize with.

◆ generic() [4/8]

z::util::generic::generic ( const char initVal)
inlinenoexcept

Construct string type from string literal.

Parameters
initValThe value to initialize with.

◆ generic() [5/8]

z::util::generic::generic ( const wchar_t initVal)
inlinenoexcept

Construct string type from wide string literal.

Parameters
initValThe value to initialize with.

◆ generic() [6/8]

z::util::generic::generic ( const zstring initVal)
inlinenoexcept

Construct as string type.

Parameters
initValThe value to initialize with.

◆ generic() [7/8]

z::util::generic::generic ( const list initVal)
inlinenoexcept

Construct as array type.

Parameters
initValThe value to initialize with.

◆ generic() [8/8]

z::util::generic::generic ( const std::initializer_list< generic > &  initVal)
inlinenoexcept

Construct as array type from initializer list.

This allows generic objects to be constructed with less syntax fluff.

Parameters
initValThe value to initialize with.

Member Function Documentation

◆ array() [1/2]

list & z::util::generic::array ( )

Get a reference to this array.

This method allows fast access to and manipulation of array contents.

Exceptions
std::exceptionif the current type is not an array. It's a good idea to check before calling this method.
Returns
A reference to the current array value.

◆ array() [2/2]

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.

Exceptions
std::exceptionif the current type is not an array. It's a good idea to check before calling this method.
Returns
A reference to the current array value.

◆ arrayOr()

const list & z::util::generic::arrayOr ( const list def) const
noexcept

Get a reference to this array, or a default value if this is not an array.

Parameters
defThe default value to return if this is not an array.
Returns
A reference to the current array value, or the default value if this is not an array.

◆ complex()

std::complex< double > z::util::generic::complex ( ) const
noexcept

Conversion to a complex value.

Returns
The contained value, cast to std::complex<double>.

◆ equals()

bool z::util::generic::equals ( const generic other) const
inline

Check strict equality.

Parameters
otherThe object to compare against.
Returns
True if both objects have the same type and identical data.
See also
operator==(), equivalent()
Note
When comparing generics of array types, all sub-elements may be compared!

◆ equivalent()

bool z::util::generic::equivalent ( const generic other) const

Check loose equality.

Parameters
otherThe object to compare against.
Returns
True if objects have comparable types and data that can be cast to the same value without losing information.
See also
operator==(), equals()
Note
When comparing generics of array types, all sub-elements may be compared!

◆ floating()

double z::util::generic::floating ( ) const
noexcept

Conversion to floating point.

Returns
The contained value, cast to a double.

◆ integer()

long z::util::generic::integer ( ) const
noexcept

Conversion to integer.

Returns
The contained value, cast to an integer.

◆ is()

bool z::util::generic::is ( std::size_t  type) const
noexcept

Check if this value is a specific type.

Parameters
typeThe type to check for.
Returns
true if the types are the same, false otherwise.

◆ load()

template<class archive >
void z::util::generic::load ( archive ar)
inline

Serialization input.

Parameters
arThe input archive.

◆ numeric()

bool z::util::generic::numeric ( ) const
noexcept

Whether this value is a numeric type.

Returns
true for integer, floating point and complex types, false otherwise.

◆ operator double()

z::util::generic::operator double ( ) const
inlineexplicitnoexcept

Explicit conversion to double.

Returns
The contained value, cast to a double.

◆ operator float()

z::util::generic::operator float ( ) const
inlineexplicitnoexcept

Explicit conversion to float.

Returns
The contained value, cast to a float.

◆ operator INT()

template<typename INT , typename = typename std::enable_if<std::is_integral<INT>::value, INT>::type>
z::util::generic::operator INT ( ) const
inlineexplicit

Explicit conversion to integer types.

Returns
The contained value, cast to an integer.

◆ operator!=()

bool z::util::generic::operator!= ( const generic other) const
inline

Check strict inequality.

Parameters
otherThe object to compare against.
Returns
True if objects have different types or data.

◆ operator%()

generic z::util::generic::operator% ( const generic other) const
inline

Get the remainder of division by another value.

Exceptions
nonnumericif the operation is attempted with either value being non-numeric.
Note
This casts both values to integers, truncating any other data.
Parameters
otherThe value to get the remainder of division by.
Returns
The remainder of division.

◆ operator%=()

Get the remainder of division by another value.

Exceptions
nonnumericif the operation is attempted with either value being non-numeric.
Note
This casts both values to integers, truncating any other data.
Parameters
otherThe value to get the remainder of division by.
Returns
A reference to this after division.

◆ operator*()

generic z::util::generic::operator* ( const generic other) const
inline

Multiply a value with this numeric value.

Exceptions
nonnumericif the operation is attempted with either value being non-numeric.
Parameters
otherThe value to multiply.
Returns
The multiplication result.

◆ operator*=()

Multiply a value with this numeric value.

Exceptions
nonnumericif the operation is attempted with either value being non-numeric.
Parameters
otherThe value to multiply.
Returns
A reference to this after multiplication.

◆ operator+()

generic z::util::generic::operator+ ( const generic other) const
inline

Add a value to this numeric value.

Exceptions
nonnumericif the operation is attempted with either value being non-numeric (and both are not strings).
Parameters
otherThe value to add.
Returns
The addition result.

◆ operator+=()

Add a value to this numeric value.

Exceptions
nonnumericif the operation is attempted with either value being non-numeric (and both are not strings).
Parameters
otherThe value to add.
Returns
A reference to this after addition.

◆ operator-() [1/2]

Get the negation of this value.

Exceptions
nonnumericif the operation is attempted on a non-numeric value.
Returns
A reference to this after negation.

◆ operator-() [2/2]

generic z::util::generic::operator- ( const generic other) const
inline

Subtract a value from this numeric value.

Exceptions
nonnumericif the operation is attempted with either value being non-numeric.
Parameters
otherThe value to subtract.
Returns
The subtraction result.

◆ operator-=()

Subtract a value from this numeric value.

Exceptions
nonnumericif the operation is attempted with either value being non-numeric.
Parameters
otherThe value to subtract.
Returns
A reference to this after subtraction.

◆ operator/()

generic z::util::generic::operator/ ( const generic other) const
inline

Divide this value by another numeric value.

Exceptions
nonnumericif the operation is attempted with either value being non-numeric.
Parameters
otherThe value to divide by.
Returns
The division result.

◆ operator/=()

Divide this value by another numeric value.

Exceptions
nonnumericif the operation is attempted with either value being non-numeric.
Parameters
otherThe value to divide by.
Returns
A reference to this after division.

◆ operator==()

Check strict equality.

Parameters
otherThe object to compare against.
Returns
True if both objects have the same type and identical data.
See also
equals(), equivalent()
Note
When comparing generics of array types, all sub-elements may be compared!

◆ promote()

bool z::util::generic::promote ( generic other)
noexcept

Upcast two values as much as is needed for them to be the same type.

Parameters
otherThe value to upcast along with this.
Returns
true if upcast successfully made the types the same, false otherwise.

◆ reduce()

bool z::util::generic::reduce ( bool  castStrings = false)
noexcept

Downcast a value as low as possible without losing precision.

Parameters
castStringsWhether to downcast strings to numeric types.
Returns
true if downcast changed the type, false otherwise.

◆ reducesTo()

int z::util::generic::reducesTo ( bool  castStrings = false) const
noexcept

Get the lowest type this value can be cast to without losing precision.

Parameters
castStringswhether to attempt to downcast strings to numeric types.
Returns
The lowest possible type this can be cast to without losing precision.

◆ save()

template<typename archive >
void z::util::generic::save ( archive ar) const
inline

Serialization output.

Parameters
arThe output archive.

◆ string() [1/2]

zstring & z::util::generic::string ( )

Get a reference to this string.

This method is faster than toString() and allows manipulation of string contents.

Exceptions
std::exceptionif the current type is not a string. Use toString() if you do not know for sure what type this is.
Returns
A reference to the current string value.

◆ string() [2/2]

const zstring & z::util::generic::string ( ) const

Get a const reference to this string.

This method is faster than toString() and allows access to string contents.

Exceptions
std::exceptionif the current type is not a string. Use toString() if you do not know for sure what type this is.
Returns
A reference to the current string value.

◆ toString()

zstring z::util::generic::toString ( bool  printArrays = false) const
noexcept

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.

Parameters
printArraysIf true, generates a full string representation of array contents. Otherwise arrays always result in a null string.
Returns
A string containing a representation of this value.
Examples
generic.cpp.

◆ type()

int z::util::generic::type ( ) const
noexcept

Get the current type of this object.

Returns
The current datatype.

◆ typeString()

zstring z::util::generic::typeString ( ) const
noexcept

Get a character string representing the current type.

Returns
A string representing the type.
Examples
generic.cpp.

The documentation for this class was generated from the following file: