Evoplex  0.2.1
Public Types | Public Member Functions | Friends | List of all members
evoplex::Value Class Reference

A class for variant data types (tagged union). More...

#include <value.h>

Public Types

enum  Type {
  BOOL, CHAR, DOUBLE, INT,
  STRING, INVALID
}
 This enum defines the types of variable that a Value can contain.
 

Public Member Functions

 Value ()
 Constructs an invalid Value. More...
 
 Value (const Value &value)
 Constructs a copy of the value passed as the argument to this constructor.
 
 Value (bool value)
 Constructs a new Value with a boolean value.
 
 Value (char value)
 Constructs a new Value with a char value.
 
 Value (double value)
 Constructs a new Value with a double value.
 
 Value (int value)
 Constructs a new Value with an integer value.
 
 Value (const char *value)
 Constructs a new Value with a string value.
 
 Value (const QString &value)
 Constructs a new Value with a string value.
 
 Value (std::vector< bool >::reference value)
 Constructs a new Value with a boolean value. More...
 
 ~Value ()
 Destroys this Value.
 
Type type () const
 Returns the storage type of the data stored in the Value.
 
bool isValid () const
 Returns true if the storage type of this Value is not Type::INVALID.
 
bool isBool () const
 Returns true if the storage type of this Value is equal to Type::BOOL.
 
bool isChar () const
 Returns true if the storage type of this Value is equal to Type::CHAR.
 
bool isDouble () const
 Returns true if the storage type of this Value is equal to Type::DOUBLE.
 
bool isInt () const
 Returns true if the storage type of this Value is equal to Type::INT.
 
bool isString () const
 Returns true if the storage type of this Value is equal to Type::STRING.
 
bool toBool () const
 Returns the Value as a boolean. More...
 
char toChar () const
 Returns the Value as a char. More...
 
double toDouble () const
 Returns the Value as a double. More...
 
int toInt () const
 Returns the Value as an int. More...
 
quint32 toUInt () const
 Returns the Value as an unsigned integer. More...
 
const char * toString () const
 Returns the Value as a string. More...
 
QString toQString (char format='g', int precision=8) const
 Returns the Value as a QString. More...
 
Valueoperator= (const Value &v)
 Assigns the value v to this Value.
 
bool operator== (const Value &v) const
 Checks if this Value is equal to v.
 
bool operator!= (const Value &v) const
 Checks if this Value is different from v.
 
bool operator< (const Value &v) const
 Checks if this Value is less than v.
 
bool operator> (const Value &v) const
 Checks if this Value is greater than v.
 
bool operator<= (const Value &v) const
 Checks if this Value is less or equal to v.
 
bool operator>= (const Value &v) const
 Checks if this Value is greater or equal to v.
 

Friends

struct std::hash< Value >
 

Detailed Description

A class for variant data types (tagged union).

This is a very important class in Evoplex, it defines a data type that can behave like an integer, double, char, bool or string in a very efficient way.

A Value object holds a single value of a single type() at a time. You can find out what type (T) the Value holds and get its value using one of the toT() functions (e.g., toInt()).

The functions named toT() (e.g., toInt(), toString()) are const. If you ask for the stored type, they return a copy of the stored object.

Here is some example code to demonstrate the use of Value:

Value v(123); // The Value now contains an int
int x = v.toInt(); // x = 123
v = Value("hello"); // The Value now contains a string
int y = v.toInt(); // throws an exception as v cannot be converted to an int
QString s = v.toQString(); // s = "hello"
...
v = 1.1; // v.type() is equal to DOUBLE
v = v.toDouble() + 1.2; // The Value now holds 2.3

Constructor & Destructor Documentation

◆ Value() [1/2]

evoplex::Value::Value ( )

Constructs an invalid Value.

See also
isValid()

◆ Value() [2/2]

evoplex::Value::Value ( std::vector< bool >::reference  value)

Constructs a new Value with a boolean value.

This constructor is available just to accept the specialized vector<bool>. http://www.cplusplus.com/reference/vector/vector-bool

Member Function Documentation

◆ toBool()

bool evoplex::Value::toBool ( ) const
inline

Returns the Value as a boolean.

It returns a boolean if the data type() is equal to Type::BOOL. Otherwise, it throws an expection.

Exceptions
std::logic_error.

◆ toChar()

char evoplex::Value::toChar ( ) const
inline

Returns the Value as a char.

It returns a char if the data type() is equal to Type::CHAR. Otherwise, it throws an expection.

Exceptions
std::logic_error.

◆ toDouble()

double evoplex::Value::toDouble ( ) const
inline

Returns the Value as a double.

It returns a double if the data type() is equal to Type::DOUBLE. Otherwise, it throws an expection.

Exceptions
std::logic_error.

◆ toInt()

int evoplex::Value::toInt ( ) const
inline

Returns the Value as an int.

It returns an int if the data type() is equal to Type::INT. Otherwise, it throws an expection.

Exceptions
std::logic_error.

◆ toQString()

QString evoplex::Value::toQString ( char  format = 'g',
int  precision = 8 
) const

Returns the Value as a QString.

It returns a QString for any data type(). For example:

Value v = 123; // v is now an integer equal to 123
QString s = v.toQString(); // s = "123"
v = true; // v is now a boolean
s = v.toQString(); // s = "1"
v = "hello"; // v is now a string
s = v.toQString(); // s = "hello"

◆ toString()

const char * evoplex::Value::toString ( ) const
inline

Returns the Value as a string.

It returns a string if the data type() is equal to Type::STRING. Otherwise, it throws an expection.

Exceptions
std::logic_error.

◆ toUInt()

quint32 evoplex::Value::toUInt ( ) const
inline

Returns the Value as an unsigned integer.

It returns an unsigned integer if the data type() is equal to Type::INT and the value is >= 0. Otherwise, it throws an expection.

Exceptions
std::logic_error.

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