Container
The Container class.
Overview
The container class represents an object which should be (de)serializable through GPDS.
The following figure shows the container class design and related classes:
Value
bool
int
double
std::string
container*
(for nesting)
Add a value
Adding a value can be achieved by using the container::add_value() function:
gpds::container c; double scale = 4.2; c.add_value( "scale", scale );
Get a value
Getting a value from a deserialized container can be achieved
through the container::get_value() function. The value's
key
is passed as a parameter:
double scale = c.get_value<double>( "scale" );
Get a value with a default value
The container::get_value() function also allows supplying an
optional default value which will be returned if no value exists for the specified
key
:
double scale = c.get_value<double>( "scale", 1.0 );
Get multiple values with the same key
It's possible to store multiple values with the same key
in a
container. Multiple values can be retrieved from a deserialized container by using
the container::get_values() function:
for (const gpds::container* car_container : c.get_values<gpds::container*>( "car" ) ) {
class car car;
car.from_container(*car_container);
cars.push_front(car);
}
Attribute
An attribute is a key-value pair where both the key and the value or of type std::string
.
Keys are unique. Calling add_argument() with a
key that has previously alredy been added will have no effect.
The previously passed value
will remain being associated with the
specified key
.
Add an argument
gpds::container c; c.add_attribute("id", "72042135");
Get an argument
gpds::container c; std::cout << "id=" << c.get_attribute("id") << std::endl;