|
Variants provide a mechanism to store several types of data in one construct and send them via Ice. The Variant class offers a unified interface for storing and accessing values of several data types. It is one of the essential data types of ArmarX and can be used in Ice communication. Variants can be extended and several Variant-based implementations of simple or complex data types like 6D poses are available in ArmarX.
Variants can store instances of all classes that implement the VariantDataClass Ice interface. For the basic data types int, bool, float, double and std::string there are three main ways for creating a Variant.
In the following code three int-Variant instances are created using the different ways of instantiation:
This works analogously for the other basic data types. The setting of complex datatypes work similarly:
TestComplexFloat
is from the HowTo How to Create Custom Variant Types.The stored values can be retrieved using the respective getter methods:
Or for complex types:
Note that after assigning a value to a variant and therefore after initially determining the Variant's type, changing the type is not possible anymore. Any assignment of data of a different than the set type will result in a LocalException.
There exist several implementations of Variant-types apart from the elementary ones:
List of all Variants: Variant Group
There are further classes that realize flexible containers of Variant-values, which are Variants themselves:
These containers are needed for the same reason as the Variant themselves: We need to be able to send different data via the same Ice interface. Since Ice interfaces always have a specific type, we need these classes with a common base class (VariantDataClass and VariantContainerBase).
Take a look at How to Create Custom Variant Types if you want to implement your own specialized Variant classes.
It is easy to serialize a Variant into a JSON format. Every Variant type has this feature built in. You only need the armarx::JSONObject for the serialization and deserialization:
The JSON then looks as follows:
{ "value" : { "typeName" : "::armarx::FloatVariantData", "value" : 3.0 } }