Inspirel banner

Programming Distributed Systems with YAMI4

3.8.2 C++

In order to find whether some named entry exists in the parameters object, two additional variables have to be used - the parameter_entry acting as a ``proxy'' to the found entry and a bool flag for testing whether the search was successful:

parameter_entry e;
bool found = params.find("first_name", e);

If the params object is filled according to previous examples, then there is an entry named ``first_name'' and the parameter_entry variable refers to it:

if (found)
{   
    std::cout << e.get_string();
}

If the content is being read in a truly exploratory way and the code has to be resistant not only to missing entries, but also to entries that have unexpected type, more elaborate check can be performed instead:

if (found && e.type() == string)
{
    std::cout << e.get_string();
}

In the above example the type function returns the type name of the given entry. All possible type names are defined as enumeration directly in the yami package:

namespace yami
{

enum parameter_type
{
    unused,
    boolean,
    integer,
    long_long,
    double_float,
    string,
    binary,
    boolean_array,
    integer_array,
    long_long_array,
    double_float_array,
    string_array,
    binary_array,
    nested_parameters,
    nested_parameters_array
};

} // namespace yami

Some applications might have a need for custom serialization or completely general data handling schemes - then a switch statement with separate branches for all possible type names is a natural approach. In fact, a conceptually similar code is part of the YAMI4 library itself.

The type of the entry can be explored directly as well, which might be handy if it is already known that such entry exists:

parameter_type age_type = params.type("age");

The parameter_entry type offers all reading operations and they are used in exactly the same way as the reading operations of the parameters object itself, with the exception that there is no need for the entry name parameter - the given entry is already being referred to, so there is no need to look for it by name.

The entry name can be also extracted from the parameter_entry object:

std::string name = e.name();