Value
#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).
enum Type
The type of a value.
| VALUE | DESCRIPTION |
|---|---|
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.
| Value | ( | ); |
constructor Value
Create a boolean value.
| Value | ( | bool value | ); |
| PARAMETER | DESCRIPTION |
|---|---|
value |
The boolean value. |
constructor Value
Create a floating point value.
| Value | ( | double value | ); |
| PARAMETER | DESCRIPTION |
|---|---|
value |
The floating point value. |
constructor Value
Create a string value.
| Value | ( | Symbol value | ); |
| PARAMETER | DESCRIPTION |
|---|---|
value |
The string value. |
constructor Value
Create a binary or a string value.
| Value | ( | std::string_view value, | |
| bool binary | ); |
| PARAMETER | DESCRIPTION |
|---|---|
value |
The binary data or string value. |
binary |
If |
constructor Value
Create an unsigned integer value.
| Value | ( | uint64_t value, | |
| uint64_t default_size = 0 | ); |
| PARAMETER | DESCRIPTION |
|---|---|
value |
The unsigned integer value. |
default_size |
Explicitly set the default size of the value. If |
constructor Value
Create a signed integer value.
| Value | ( | int64_t value, | |
| uint64_t default_size = 0 | ); |
| PARAMETER | DESCRIPTION |
|---|---|
value |
The signed integer value. |
default_size |
Explicitly set the default size of the value. If |
method binary_valueconst
Get the binary data of the value.
| std::string binary_value | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
std::string |
The value as binary data. |
| THROWS | DESCRIPTION |
|---|---|
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.
| bool boolean_value | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
bool |
The value as a boolean. |
| THROWS | DESCRIPTION |
|---|---|
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.
| std::optional< uint64_t > default_size | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
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.
| double float_value | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
double |
The value as a floating point number. |
| THROWS | DESCRIPTION |
|---|---|
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.
| size_t hash_value | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
size_t |
The hash value of the value. |
method is_binaryconst
Check if value is binary data.
| bool is_binary | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
method is_booleanconst
Check if it is a boolean value.
| bool is_boolean | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
method is_floatconst
Check if it is a floating point number.
| bool is_float | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
method is_integerconst
Check if it is an integer (signed or unsigned).
| bool is_integer | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
method is_numberconst
Check if it is a number (integer or floating point).
| bool is_number | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
method is_signedconst
Check if it is a signed integer.
| bool is_signed | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
method is_stringconst
Check if it is a string.
| bool is_string | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
method is_subtypestatic
Check if a type is a subtype of another type.
| static bool is_subtype | ( | Type type, | |
| Type supertype | ); |
| PARAMETER | DESCRIPTION |
|---|---|
type |
The type to check. |
supertype |
The supertype to check against. |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
method is_unsignedconst
Check if it is an unsigned integer.
| bool is_unsigned | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
method is_voidconst
Check if it is void (has no value).
| bool is_void | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
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.
| operator bool | ( | ) const; |
| THROWS | DESCRIPTION |
|---|---|
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.
| bool operator!= | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to compare with. |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
method operator%const
The remainder of this value divided by another value. The remainder can only be computed from unsigned integers.
| Value operator% | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to divide by. |
| RETURNS | DESCRIPTION |
|---|---|
Value |
The remainder of the division. |
| THROWS | DESCRIPTION |
|---|---|
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.
| Value operator& | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to AND with. |
| RETURNS | DESCRIPTION |
|---|---|
Value |
The result of the bitwise AND operation. |
| THROWS | DESCRIPTION |
|---|---|
Exception |
if either value is not an unsigned integer. |
method operator&const
Bitwise AND of this value and an unsigned integer.
| Value operator& | ( | uint64_t other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The unsigned integer to AND with. |
| RETURNS | DESCRIPTION |
|---|---|
Value |
The result of the bitwise AND operation. |
| THROWS | DESCRIPTION |
|---|---|
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.
| Value operator&& | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to AND with. |
| RETURNS | DESCRIPTION |
|---|---|
Value |
True if both values are true. |
| THROWS | DESCRIPTION |
|---|---|
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.
| Value operator* | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to multiply by. |
| RETURNS | DESCRIPTION |
|---|---|
Value |
The product of the two values. |
| THROWS | DESCRIPTION |
|---|---|
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.
| Value & operator*= | ( | const Value & other | ); |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to multiply by. |
| RETURNS | DESCRIPTION |
|---|---|
Value & |
This value after multiplication. |
| THROWS | DESCRIPTION |
|---|---|
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.
| Value operator+ | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to add. |
| RETURNS | DESCRIPTION |
|---|---|
Value |
The sum of the two values. |
| THROWS | DESCRIPTION |
|---|---|
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.
| Value & operator+= | ( | const Value & other | ); |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to add. |
| RETURNS | DESCRIPTION |
|---|---|
Value & |
This value after addition. |
| THROWS | DESCRIPTION |
|---|---|
Exception |
if either value is not a number or if the addition would overflow. |
method operator-const
Negate value. Only numbers can be negated.
| Value operator- | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
Value |
The negated value. |
| THROWS | DESCRIPTION |
|---|---|
Exception |
if the value is not a number. |
method operator-const
Subtract another value from this value. Only numbers can be subtracted.
| Value operator- | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to subtract. |
| RETURNS | DESCRIPTION |
|---|---|
Value |
The difference of the two values. |
| THROWS | DESCRIPTION |
|---|---|
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.
| Value & operator-= | ( | const Value & other | ); |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to subtract. |
| RETURNS | DESCRIPTION |
|---|---|
Value & |
This value after subtraction. |
| THROWS | DESCRIPTION |
|---|---|
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.
| Value operator/ | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to divide by. |
| RETURNS | DESCRIPTION |
|---|---|
Value |
The quotient of the two values. |
| THROWS | DESCRIPTION |
|---|---|
Exception |
if either value is not a number or if dividing by zero. |
method operator/=
Divide this value by another value, modifying this value.
| Value & operator/= | ( | const Value & other | ); |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to divide by. |
| RETURNS | DESCRIPTION |
|---|---|
Value & |
This value after division. |
| THROWS | DESCRIPTION |
|---|---|
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.
| bool operator< | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to compare with. |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
method operator<<const
Left shift this value by another value. Only integers can be left shifted.
| Value operator<< | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to shift by. |
| RETURNS | DESCRIPTION |
|---|---|
Value |
The result of the left shift operation. |
| THROWS | DESCRIPTION |
|---|---|
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.
| bool operator<= | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to compare with. |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
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.
| bool operator== | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to compare with. |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
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.
| bool operator> | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to compare with. |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
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.
| bool operator>= | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to compare with. |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
method operator>>const
Right shift this value by another value. Only integers can be right shifted.
| Value operator>> | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to shift by. |
| RETURNS | DESCRIPTION |
|---|---|
Value |
The result of the right shift operation. |
| THROWS | DESCRIPTION |
|---|---|
Exception |
if either value is not an integer or an overflow occurs. |
method operator>>const
Left shift this value by an unsigned integer.
| Value operator>> | ( | uint64_t other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The unsigned integer to shift by. |
| RETURNS | DESCRIPTION |
|---|---|
Value |
The result of the left shift operation. |
| THROWS | DESCRIPTION |
|---|---|
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.
| Value operator^ | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to exclusive OR with. |
| RETURNS | DESCRIPTION |
|---|---|
Value |
The result of the bitwise exclusive OR operation. |
| THROWS | DESCRIPTION |
|---|---|
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.
| Value operator| | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to OR with. |
| RETURNS | DESCRIPTION |
|---|---|
Value |
The result of the bitwise OR operation. |
| THROWS | DESCRIPTION |
|---|---|
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.
| Value operator|| | ( | const Value & other | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
other |
The value to OR with. |
| RETURNS | DESCRIPTION |
|---|---|
Value |
True if either value is true. |
| THROWS | DESCRIPTION |
|---|---|
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.
| Value operator~ | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
Value |
The bitwise NOT of the value. |
| THROWS | DESCRIPTION |
|---|---|
Exception |
if the value is not an integer. |
method serializeconst
Serialize the value to a stream.
| void serialize | ( | std::ostream & stream | ) const; |
| PARAMETER | DESCRIPTION |
|---|---|
stream |
The stream to serialize to. |
method signed_valueconst
Get the signed integer value.
| int64_t signed_value | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
int64_t |
The value as a signed integer. |
| THROWS | DESCRIPTION |
|---|---|
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.
| std::string string_value | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
std::string |
The value as a string. |
| THROWS | DESCRIPTION |
|---|---|
Exception |
if the value can't be interpreted as a string. |
method symbol_valueconst
Get the symbol for string value.
| Symbol symbol_value | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
Symbol |
The value as a symbol. |
| THROWS | DESCRIPTION |
|---|---|
Exception |
if the value is not a string. |
method typeconst
Get the type of the value.
| Type type | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
Type |
The type of the value. |
method type_nameconst
Get the name of the value's type.
| std::string type_name | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
std::string |
The name of the value's type. |
method type_namestatic
Get the name of a value type.
| static std::string type_name | ( | Type type | ); |
| PARAMETER | DESCRIPTION |
|---|---|
type |
The type to get the name of. |
| RETURNS | DESCRIPTION |
|---|---|
std::string |
The name of the type. |
method unsigned_valueconst
Get the unsigned integer value.
| uint64_t unsigned_value | ( | ) const; |
| RETURNS | DESCRIPTION |
|---|---|
uint64_t |
The value as an unsigned integer. |
| THROWS | DESCRIPTION |
|---|---|
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.
| std::optional< Value > operator&& | ( | const std::optional< Value > & a, | |
| const std::optional< Value > & b | ); |
| PARAMETER | DESCRIPTION |
|---|---|
a |
The first value. |
b |
The second value. |
| RETURNS | DESCRIPTION |
|---|---|
std::optional< Value > |
The result of the logical AND operation, or not set if either value is not set. |
| THROWS | DESCRIPTION |
|---|---|
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.
| std::optional< Value > operator* | ( | const std::optional< Value > & a, | |
| const std::optional< Value > & b | ); |
| PARAMETER | DESCRIPTION |
|---|---|
a |
The first value. |
b |
The second value. |
| RETURNS | DESCRIPTION |
|---|---|
std::optional< Value > |
The product of the two values, or not set if either value is not set. |
| THROWS | DESCRIPTION |
|---|---|
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.
| std::optional< Value > operator+ | ( | const std::optional< Value > & a, | |
| const std::optional< Value > & b | ); |
| PARAMETER | DESCRIPTION |
|---|---|
a |
The first value. |
b |
The second value. |
| RETURNS | DESCRIPTION |
|---|---|
std::optional< Value > |
The sum of the two values, or not set if either value is not set. |
| THROWS | DESCRIPTION |
|---|---|
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.
| std::optional< Value > operator- | ( | const std::optional< Value > & a, | |
| const std::optional< Value > & b | ); |
| PARAMETER | DESCRIPTION |
|---|---|
a |
The first value. |
b |
The second value. |
| RETURNS | DESCRIPTION |
|---|---|
std::optional< Value > |
The difference of the two values, or not set if either value is not set. |
| THROWS | DESCRIPTION |
|---|---|
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.
| std::optional< Value > operator/ | ( | const std::optional< Value > & a, | |
| const std::optional< Value > & b | ); |
| PARAMETER | DESCRIPTION |
|---|---|
a |
The first value. |
b |
The second value. |
| RETURNS | DESCRIPTION |
|---|---|
std::optional< Value > |
The quotient of the two values, or not set if either value is not set. |
| THROWS | DESCRIPTION |
|---|---|
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.
| bool operator< | ( | const std::optional< Value > & a, | |
| const std::optional< Value > & b | ); |
| PARAMETER | DESCRIPTION |
|---|---|
a |
The first value. |
b |
The second value. |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
| THROWS | DESCRIPTION |
|---|---|
Exception |
if both values are set and they are not comparable. |
function operator<<
Output a value to a stream.
| std::ostream & operator<< | ( | std::ostream & stream, | |
| const Value & value | ); |
| PARAMETER | DESCRIPTION |
|---|---|
stream |
The stream to output to. |
value |
The value to output. |
| RETURNS | DESCRIPTION |
|---|---|
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.
| bool operator<= | ( | const std::optional< Value > & a, | |
| const std::optional< Value > & b | ); |
| PARAMETER | DESCRIPTION |
|---|---|
a |
The first value. |
b |
The second value. |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
| THROWS | DESCRIPTION |
|---|---|
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.
| bool operator> | ( | const std::optional< Value > & a, | |
| const std::optional< Value > & b | ); |
| PARAMETER | DESCRIPTION |
|---|---|
a |
The first value. |
b |
The second value. |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
| THROWS | DESCRIPTION |
|---|---|
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.
| bool operator>= | ( | const std::optional< Value > & a, | |
| const std::optional< Value > & b | ); |
| PARAMETER | DESCRIPTION |
|---|---|
a |
The first value. |
b |
The second value. |
| RETURNS | DESCRIPTION |
|---|---|
bool |
|
| THROWS | DESCRIPTION |
|---|---|
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.
| std::optional< Value > operator|| | ( | const std::optional< Value > & a, | |
| const std::optional< Value > & b | ); |
| PARAMETER | DESCRIPTION |
|---|---|
a |
The first value. |
b |
The second value. |
| RETURNS | DESCRIPTION |
|---|---|
std::optional< Value > |
The result of the logical OR operation, or not set if either value is not set. |
| THROWS | DESCRIPTION |
|---|---|
Exception |
if both values are set and either value can't be interpreted as a boolean |