Skip to content

Value

#include <tpau-cpp-kernal/Value.h>

INCLUDE FILE
#include <tpau-cpp-kernal/Value.h>

class Value

This class represents a value of various types.

All operations check for compatible types and integer overflow. Invalid operations throw an exception.

Supported types are boolean, signed and unsigned integers, floating point numbers, strings, binary data and void (has no value).

DEFINITION
class Value;

enum Type

The type of a value.

DEFINITION
enum Type;
VALUEDESCRIPTION
BINARY

Binary data.

BOOLEAN

Boolean value.

FLOAT

Floating point number.

INTEGER

Integer (signed or unsigned).

NUMBER

Number (integer or floating point).

SIGNED

Signed integer.

STRING

String value.

UNSIGNED

Unsigned integer.

VOID

No value.

constructor Value

Create a void value.

DEFINITION
Value ( );

constructor Value

Create a boolean value.

DEFINITION
Value ( bool  value );
PARAMETERDESCRIPTION
value

The boolean value.

constructor Value

Create a floating point value.

DEFINITION
Value ( double  value );
PARAMETERDESCRIPTION
value

The floating point value.

constructor Value

Create a string value.

DEFINITION
Value ( Symbol  value );
PARAMETERDESCRIPTION
value

The string value.

constructor Value

Create a binary or a string value.

DEFINITION
Value ( std::string_view  value,
bool  binary );
PARAMETERDESCRIPTION
value

The binary data or string value.

binary

If true, a binary value is created, otherwise a string value is created.

constructor Value

Create an unsigned integer value.

DEFINITION
Value ( uint64_t  value,
uint64_t  default_size = 0 );
PARAMETERDESCRIPTION
value

The unsigned integer value.

default_size

Explicitly set the default size of the value. If 0, the smallest size that can hold the value is used.

constructor Value

Create a signed integer value.

DEFINITION
Value ( int64_t  value,
uint64_t  default_size = 0 );
PARAMETERDESCRIPTION
value

The signed integer value.

default_size

Explicitly set the default size of the value. If 0, the smallest size that can hold the value is used.

method binary_valueconst

Get the binary data of the value.

DEFINITION
std::string  binary_value ( ) const;
RETURNSDESCRIPTION
std::string

The value as binary data.

THROWSDESCRIPTION
Exception

if the value is not binary.

method boolean_valueconst

Interpret value as a boolean.

Numbers are true if they are nonzero, strings are true if they are nonempty. Binary data and void values can't be interpreted as booleans.

DEFINITION
bool  boolean_value ( ) const;
RETURNSDESCRIPTION
bool

The value as a boolean.

THROWSDESCRIPTION
Exception

Value can't be interpreted as a boolean.

method default_sizeconst

Get the default size of the value.

For integer values, this is the smallest size that can hold the value, or the explicitly set default size if it is larger. For binary data, this is its size in bytes.

DEFINITION
std::optional< uint64_t >  default_size ( ) const;
RETURNSDESCRIPTION
std::optional< uint64_t >

The default size of the value, if it has a default size.

method float_valueconst

Get the floating point value. Integer values are converted to floating point numbers.

DEFINITION
double  float_value ( ) const;
RETURNSDESCRIPTION
double

The value as a floating point number.

THROWSDESCRIPTION
Exception

if the value is not a number.

method hash_valueconst

Compute the hash value of the value. This is used to implement std::hash for Value.

DEFINITION
size_t  hash_value ( ) const;
RETURNSDESCRIPTION
size_t

The hash value of the value.

method is_binaryconst

Check if value is binary data.

DEFINITION
bool  is_binary ( ) const;
RETURNSDESCRIPTION
bool

true if it is binary data, false otherwise.

method is_booleanconst

Check if it is a boolean value.

DEFINITION
bool  is_boolean ( ) const;
RETURNSDESCRIPTION
bool

true if it is a boolean value, false otherwise.

method is_floatconst

Check if it is a floating point number.

DEFINITION
bool  is_float ( ) const;
RETURNSDESCRIPTION
bool

true if it is a floating point number, false otherwise.

method is_integerconst

Check if it is an integer (signed or unsigned).

DEFINITION
bool  is_integer ( ) const;
RETURNSDESCRIPTION
bool

true if it is an integer, false otherwise.

method is_numberconst

Check if it is a number (integer or floating point).

DEFINITION
bool  is_number ( ) const;
RETURNSDESCRIPTION
bool

true if it is a number, false otherwise.

method is_signedconst

Check if it is a signed integer.

DEFINITION
bool  is_signed ( ) const;
RETURNSDESCRIPTION
bool

true if it is a signed integer, false otherwise.

method is_stringconst

Check if it is a string.

DEFINITION
bool  is_string ( ) const;
RETURNSDESCRIPTION
bool

true if it is a string, false otherwise.

method is_subtypestatic

Check if a type is a subtype of another type.

DEFINITION
static bool  is_subtype ( Type  type,
Type  supertype );
PARAMETERDESCRIPTION
type

The type to check.

supertype

The supertype to check against.

RETURNSDESCRIPTION
bool

true if type is a subtype of supertype, false otherwise.

method is_unsignedconst

Check if it is an unsigned integer.

DEFINITION
bool  is_unsigned ( ) const;
RETURNSDESCRIPTION
bool

true if it is an unsigned integer, false otherwise.

method is_voidconst

Check if it is void (has no value).

DEFINITION
bool  is_void ( ) const;
RETURNSDESCRIPTION
bool

true if it is void, false otherwise.

method operator boolconst

Interpret the value as a boolean.

Numbers are true if they are nonzero, strings are true if they are nonempty. Binary data and void values can't be interpreted as booleans.

DEFINITION
operator bool ( ) const;
THROWSDESCRIPTION
Exception

Value can't be interpreted as a boolean.

method operator!=const

Check if two values are different. Numbers are compared by their numeric value, regardless of their type. Values of other types are not equal if their types are different.

DEFINITION
bool  operator!= ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to compare with.

RETURNSDESCRIPTION
bool

true if the values are different, false otherwise.

method operator%const

The remainder of this value divided by another value. The remainder can only be computed from unsigned integers.

DEFINITION
Value  operator% ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to divide by.

RETURNSDESCRIPTION
Value

The remainder of the division.

THROWSDESCRIPTION
Exception

if either value is not an unsigned integer or if dividing by zero.

method operator&const

Bitwise AND of this value and another value. Only unsigned integers can be bitwise ANDed.

DEFINITION
Value  operator& ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to AND with.

RETURNSDESCRIPTION
Value

The result of the bitwise AND operation.

THROWSDESCRIPTION
Exception

if either value is not an unsigned integer.

method operator&const

Bitwise AND of this value and an unsigned integer.

DEFINITION
Value  operator& ( uint64_t  other ) const;
PARAMETERDESCRIPTION
other

The unsigned integer to AND with.

RETURNSDESCRIPTION
Value

The result of the bitwise AND operation.

THROWSDESCRIPTION
Exception

if this value is not an unsigned integer.

method operator&&const

Logical AND of this value and another value. Only values that can be interpreted as booleans can be logically ANDed.

DEFINITION
Value  operator&& ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to AND with.

RETURNSDESCRIPTION
Value

True if both values are true.

THROWSDESCRIPTION
Exception

if either value can't be interpreted as a boolean.

method operator*const

Multiply this value by another value. Only numbers can be multiplied.

DEFINITION
Value  operator* ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to multiply by.

RETURNSDESCRIPTION
Value

The product of the two values.

THROWSDESCRIPTION
Exception

if either value is not a number or if the multiplication would overflow.

method operator*=

Multiply this value by another value, modifying this value.

DEFINITION
Value & operator*= ( const Value & other );
PARAMETERDESCRIPTION
other

The value to multiply by.

RETURNSDESCRIPTION
Value &

This value after multiplication.

THROWSDESCRIPTION
Exception

if either value is not a number or if the multiplication would overflow.

method operator+const

Add another value to this value. Only numbers can be added.

DEFINITION
Value  operator+ ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to add.

RETURNSDESCRIPTION
Value

The sum of the two values.

THROWSDESCRIPTION
Exception

if either value is not a number or if the addition would overflow.

method operator+=

Add another value to this value, modifying this value.

DEFINITION
Value & operator+= ( const Value & other );
PARAMETERDESCRIPTION
other

The value to add.

RETURNSDESCRIPTION
Value &

This value after addition.

THROWSDESCRIPTION
Exception

if either value is not a number or if the addition would overflow.

method operator-const

Negate value. Only numbers can be negated.

DEFINITION
Value  operator- ( ) const;
RETURNSDESCRIPTION
Value

The negated value.

THROWSDESCRIPTION
Exception

if the value is not a number.

method operator-const

Subtract another value from this value. Only numbers can be subtracted.

DEFINITION
Value  operator- ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to subtract.

RETURNSDESCRIPTION
Value

The difference of the two values.

THROWSDESCRIPTION
Exception

if either value is not a number or if the subtraction would overflow.

method operator-=

Subtract another value from this value, modifying this value.

DEFINITION
Value & operator-= ( const Value & other );
PARAMETERDESCRIPTION
other

The value to subtract.

RETURNSDESCRIPTION
Value &

This value after subtraction.

THROWSDESCRIPTION
Exception

if either value is not a number or if the subtraction would overflow.

method operator/const

Divide this value by another value. Only numbers can be divided.

DEFINITION
Value  operator/ ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to divide by.

RETURNSDESCRIPTION
Value

The quotient of the two values.

THROWSDESCRIPTION
Exception

if either value is not a number or if dividing by zero.

method operator/=

Divide this value by another value, modifying this value.

DEFINITION
Value & operator/= ( const Value & other );
PARAMETERDESCRIPTION
other

The value to divide by.

RETURNSDESCRIPTION
Value &

This value after division.

THROWSDESCRIPTION
Exception

if either value is not a number or if dividing by zero.

method operator<const

Check if this value is less than another value. Numbers and strings are comparable to other numbers and strings, respectively. No other types are comparable.

DEFINITION
bool  operator< ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to compare with.

RETURNSDESCRIPTION
bool

true if the values are comparable and this value is less than the other value, false otherwise.

method operator<<const

Left shift this value by another value. Only integers can be left shifted.

DEFINITION
Value  operator<< ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to shift by.

RETURNSDESCRIPTION
Value

The result of the left shift operation.

THROWSDESCRIPTION
Exception

if either value is not an integer or an overflow occurs.

method operator<=const

Check if this value is less than or equal to another value. Numbers and strings are comparable to other numbers and strings, respectively. No other types are comparable.

DEFINITION
bool  operator<= ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to compare with.

RETURNSDESCRIPTION
bool

true if the values are comparable and this value is less than or equal to the other value, false otherwise.

method operator==const

Check if two values are equal. Numbers are compared by their numeric value, regardless of their type. Values of other types are not equal if their types are different.

DEFINITION
bool  operator== ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to compare with.

RETURNSDESCRIPTION
bool

true if the values are equal, false otherwise.

method operator>const

Check if this value is greater than another value. Numbers and strings are comparable to other numbers and strings, respectively. No other types are comparable.

DEFINITION
bool  operator> ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to compare with.

RETURNSDESCRIPTION
bool

true if the values are comparable and this value is greater than the other value, false otherwise.

method operator>=const

Check if this value is greater than or equal to another value. Numbers and strings are comparable to other numbers and strings, respectively. No other types are comparable.

DEFINITION
bool  operator>= ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to compare with.

RETURNSDESCRIPTION
bool

true if the values are comparable and this value is greater than or equal to the other value, false otherwise.

method operator>>const

Right shift this value by another value. Only integers can be right shifted.

DEFINITION
Value  operator>> ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to shift by.

RETURNSDESCRIPTION
Value

The result of the right shift operation.

THROWSDESCRIPTION
Exception

if either value is not an integer or an overflow occurs.

method operator>>const

Left shift this value by an unsigned integer.

DEFINITION
Value  operator>> ( uint64_t  other ) const;
PARAMETERDESCRIPTION
other

The unsigned integer to shift by.

RETURNSDESCRIPTION
Value

The result of the left shift operation.

THROWSDESCRIPTION
Exception

if this value is not an integer or an overflow occurs.

method operator^const

Bitwise exclusive OR of this value and another value. Only unsigned integers can be bitwise exclusive ORed.

DEFINITION
Value  operator^ ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to exclusive OR with.

RETURNSDESCRIPTION
Value

The result of the bitwise exclusive OR operation.

THROWSDESCRIPTION
Exception

if either value is not an unsigned integer.

method operator|const

Bitwise OR of this value and another value. Only unsigned integers can be bitwise ORed.

DEFINITION
Value  operator| ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to OR with.

RETURNSDESCRIPTION
Value

The result of the bitwise OR operation.

THROWSDESCRIPTION
Exception

if either value is not an unsigned integer.

method operator||const

Logical OR of this value and another value. Only values that can be interpreted as booleans can be logically ORed.

DEFINITION
Value  operator|| ( const Value & other ) const;
PARAMETERDESCRIPTION
other

The value to OR with.

RETURNSDESCRIPTION
Value

True if either value is true.

THROWSDESCRIPTION
Exception

if either value can't be interpreted as a boolean.

method operator~const

Bitwise NOT of the value. Only integers can be bitwise negated.

DEFINITION
Value  operator~ ( ) const;
RETURNSDESCRIPTION
Value

The bitwise NOT of the value.

THROWSDESCRIPTION
Exception

if the value is not an integer.

method serializeconst

Serialize the value to a stream.

DEFINITION
void  serialize ( std::ostream & stream ) const;
PARAMETERDESCRIPTION
stream

The stream to serialize to.

method signed_valueconst

Get the signed integer value.

DEFINITION
int64_t  signed_value ( ) const;
RETURNSDESCRIPTION
int64_t

The value as a signed integer.

THROWSDESCRIPTION
Exception

if the value is not an integer or doesn't fit in a signed integer.

method string_valueconst

Get string representation of the value.

Numbers are converted to their string representation, strings are returned as they are. Boolean, binary data and void values can't be interpreted as strings.

DEFINITION
std::string  string_value ( ) const;
RETURNSDESCRIPTION
std::string

The value as a string.

THROWSDESCRIPTION
Exception

if the value can't be interpreted as a string.

method symbol_valueconst

Get the symbol for string value.

DEFINITION
Symbol  symbol_value ( ) const;
RETURNSDESCRIPTION
Symbol

The value as a symbol.

THROWSDESCRIPTION
Exception

if the value is not a string.

method typeconst

Get the type of the value.

DEFINITION
Type  type ( ) const;
RETURNSDESCRIPTION
Type

The type of the value.

method type_nameconst

Get the name of the value's type.

DEFINITION
std::string  type_name ( ) const;
RETURNSDESCRIPTION
std::string

The name of the value's type.

method type_namestatic

Get the name of a value type.

DEFINITION
static std::string  type_name ( Type  type );
PARAMETERDESCRIPTION
type

The type to get the name of.

RETURNSDESCRIPTION
std::string

The name of the type.

method unsigned_valueconst

Get the unsigned integer value.

DEFINITION
uint64_t  unsigned_value ( ) const;
RETURNSDESCRIPTION
uint64_t

The value as an unsigned integer.

THROWSDESCRIPTION
Exception

if the value is not an integer or doesn't fit in an unsigned integer.

function operator&&

Logical AND of two optional values. If either value is not set, the result is not set.

DEFINITION
std::optional< Value >  operator&& ( const std::optional< Value > & a,
const std::optional< Value > & b );
PARAMETERDESCRIPTION
a

The first value.

b

The second value.

RETURNSDESCRIPTION
std::optional< Value >

The result of the logical AND operation, or not set if either value is not set.

THROWSDESCRIPTION
Exception

if both values are set and either value can't be interpreted as a boolean

function operator*

Multiply two optional values. If either value is not set, the result is not set.

DEFINITION
std::optional< Value >  operator* ( const std::optional< Value > & a,
const std::optional< Value > & b );
PARAMETERDESCRIPTION
a

The first value.

b

The second value.

RETURNSDESCRIPTION
std::optional< Value >

The product of the two values, or not set if either value is not set.

THROWSDESCRIPTION
Exception

if either value is not a number or if the multiplication would overflow.

function operator+

Add two optional values. If either value is not set, the result is not set.

DEFINITION
std::optional< Value >  operator+ ( const std::optional< Value > & a,
const std::optional< Value > & b );
PARAMETERDESCRIPTION
a

The first value.

b

The second value.

RETURNSDESCRIPTION
std::optional< Value >

The sum of the two values, or not set if either value is not set.

THROWSDESCRIPTION
Exception

if either value is not a number or if the addition would overflow.

function operator-

Subtract two optional values. If either value is not set, the result is not set.

DEFINITION
std::optional< Value >  operator- ( const std::optional< Value > & a,
const std::optional< Value > & b );
PARAMETERDESCRIPTION
a

The first value.

b

The second value.

RETURNSDESCRIPTION
std::optional< Value >

The difference of the two values, or not set if either value is not set.

THROWSDESCRIPTION
Exception

if either value is not a number or if the subtraction would overflow.

function operator/

Divide two optional values. If either value is not set, the result is not set.

DEFINITION
std::optional< Value >  operator/ ( const std::optional< Value > & a,
const std::optional< Value > & b );
PARAMETERDESCRIPTION
a

The first value.

b

The second value.

RETURNSDESCRIPTION
std::optional< Value >

The quotient of the two values, or not set if either value is not set.

THROWSDESCRIPTION
Exception

if both values are set and either value is not a number, an overflow occurs, or if dividing by zero.

function operator<

Check if a value is less than another value. If either value is not set, the result is false.

DEFINITION
bool  operator< ( const std::optional< Value > & a,
const std::optional< Value > & b );
PARAMETERDESCRIPTION
a

The first value.

b

The second value.

RETURNSDESCRIPTION
bool

true if both values are set and the first value is less than the second value, false otherwise.

THROWSDESCRIPTION
Exception

if both values are set and they are not comparable.

function operator<<

Output a value to a stream.

DEFINITION
std::ostream & operator<< ( std::ostream & stream,
const Value & value );
PARAMETERDESCRIPTION
stream

The stream to output to.

value

The value to output.

RETURNSDESCRIPTION
std::ostream &

The stream after outputting the value.

function operator<=

Check if a value is less than or equal to another value. If either value is not set, the result is false.

DEFINITION
bool  operator<= ( const std::optional< Value > & a,
const std::optional< Value > & b );
PARAMETERDESCRIPTION
a

The first value.

b

The second value.

RETURNSDESCRIPTION
bool

true if both values are set and the first value is less than or equal to the second value, false otherwise.

THROWSDESCRIPTION
Exception

if both values are set and they are not comparable.

function operator>

Check if a value is greater than another value. If either value is not set, the result is false.

DEFINITION
bool  operator> ( const std::optional< Value > & a,
const std::optional< Value > & b );
PARAMETERDESCRIPTION
a

The first value.

b

The second value.

RETURNSDESCRIPTION
bool

true if both values are set and the first value is greater than the second value, false otherwise.

THROWSDESCRIPTION
Exception

if both values are set and they are not comparable.

function operator>=

Check if a value is greater than or equal to another value. If either value is not set, the result is false.

DEFINITION
bool  operator>= ( const std::optional< Value > & a,
const std::optional< Value > & b );
PARAMETERDESCRIPTION
a

The first value.

b

The second value.

RETURNSDESCRIPTION
bool

true if both values are set and the first value is greater than or equal to the second value, false otherwise.

THROWSDESCRIPTION
Exception

if both values are set and they are not comparable.

function operator||

Logical OR of two optional values. If either value is not set, the result is not set.

DEFINITION
std::optional< Value >  operator|| ( const std::optional< Value > & a,
const std::optional< Value > & b );
PARAMETERDESCRIPTION
a

The first value.

b

The second value.

RETURNSDESCRIPTION
std::optional< Value >

The result of the logical OR operation, or not set if either value is not set.

THROWSDESCRIPTION
Exception

if both values are set and either value can't be interpreted as a boolean