Home | Libraries | People | FAQ | More |
dynamic::variable
supports a pre-defined list
of fundamental data types, strings, and containers. These are generally
referred to as the supported types. Each type also
belongs to a type category that is represented by a tag type.
Table 1.7. Supported types and tags
Stored type |
Tag type |
Description |
---|---|---|
|
No value. |
|
|
Boolean value. |
|
|
Very short signed integer. |
|
|
|
Short signed integer. |
|
|
Signed integer. |
|
|
Long signed integer. |
|
|
Very long signed integer. |
|
|
Very short unsigned integer. |
|
|
Short unsigned integer. |
|
|
Unsigned integer. |
|
|
Long unsigned integer. |
|
|
Very long unsigned integer. |
|
Short floating-point number. |
|
|
|
Floating-point number. |
|
|
Long floating-point number. |
Narrow-character string. Same as |
||
Wide-character string. Same as |
||
UTF-16 character string. Same as |
||
UTF-32 character string. Same as |
||
Sequence of zero or more |
||
Ordered sequence of zero or more key-value pairs sorted by the
key. Both key and value are |
The current type of a variable can either be queried by type or tag. It is also possible to obtain an enumerator that identifies the current type.
Query-by-type is done with the same<T>()
function, which returns true if the current value is stored as type T
.
dynamic::variable data = 3.0; assert(data.same<double>()); // Value is stored as a double. assert(!data.same<float>()); // Valus is not stored as a float.
Query-by-tag is done with the is<T>()
function, which returns true if the current value is stored as a type belonging
to the category T
. T
can be a tag or a type. In the latter
case the associated tag is looked up, and the variable is queried using
this tag.
dynamic::variable data = 3.0; assert(data.is<dynamic::real>()); // Value is stored as a floating-point number. assert(data.is<double>()); // Query using the dynamic::real tag. assert(data.is<float>()); // Query using the dynamic::real tag.
Notice that any supported floating-point type can be used to query for the tag.
Query-by-enumeration is done with the code()
or symbol()
functions. These returns an enumerator that indicates the type. The enumerator
is suitable for switch
statements.
switch (data.symbol()) { case dynamic::symbol::integer: break; // Do integer stuff case dynamic::symbol::real: break; // Do floating-point number stuff default: break; // Do other stuff }
Table 1.8. Codes and symbols
Stored type |
Code |
Symbol |
---|---|---|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
A dynamic variable can be compared against another dynamic variable, or against a supported type. Supported types are grouped into comparison categories as shown below. Comparison against unsupported types results in a compile-time error.
Comparison category |
Tag type |
Rank |
---|---|---|
Nullable |
0 |
|
Arithmetic |
|
1 |
Narrow string |
2 |
|
Wide string |
3 |
|
UTF-16 string |
4 |
|
UTF-32 string |
5 |
|
Sequenced array |
6 |
|
Associative array |
7 |
Equality operations first check the argument types. If the argument types belong to different comparison categories, then they are unequal. Otherwise their values are compared according to the normal C++ rules, with the addition that null compares equal to null.
dynamic::variable first; dynamic::variable second = 2; assert(first.is<dynamic::nullable>()); assert(second.is<dynamic::integer>()); assert(first != second); // Incompatible types are unequal
Relative operations first check the argument types. If the argument types belong to different comparison categories, then their ranks are compared. The ranks are shown in the table above. For example, a nullable type is always less than other types, while an associative array is always greater than other types. Otherwise their values are compared according to the normal C++ rules.
dynamic::variable first; dynamic::variable second = 2; assert(first.is<dynamic::nullable>()); assert(second.is<dynamic::integer>()); assert(first < second); // Null is less than integers
Sequenced arrays perform a pair-wise comparison of the elements.
dynamic::variable first = { 1, 20, 300 }; dynamic::variable second = { 1, 20, 300 }; assert(first == second); assert(first <= second); assert(!(first < second)); assert(first >= second); assert(!(first > second));
Associative arrays perform a pair-wise comparison of the key-value elements.
dynamic::variable first = { { "alpha", true } }; dynamic::variable second = { { "alpha", true } }; assert(first == second); assert(first <= second); assert(!(first < second)); assert(first >= second); assert(!(first > second));