Inspirel banner

Programming Distributed Systems with YAMI4

3.8.1 Ada

In order to find whether some named entry exists in the parameters object, two additional variables have to be used - the Parameter_Entry (this type is defined in the same package as Parameters_Collection) acting as a ``pointer'' to the found entry and a Boolean flag for testing whether the search was successful:

declare
   E : Parameter_Entry;
   Found : Boolean;
begin
   Params.Find ("first_name", E, Found);

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 then
      Put_Line (Get_String (E));
   end if;
end;

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 and then Entry_Type (E) = String_Type then
      Put_Line (Get_String (E));
   end if;
end;

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

type Parameter_Type is (Boolean_Type,
                        Integer_Type,
                        Long_Long_Integer_Type,
                        Long_Float_Type,
                        String_Type,
                        Binary_Type,
                        Boolean_Array_Type,
                        Integer_Array_Type,
                        Long_Long_Integer_Array_Type,
                        Long_Float_Array_Type,
                        String_Array_Type,
                        Binary_Array_Type,
                        Nested_Parameters_Type,
                        Nested_Parameters_Array_Type);

Some applications might have a need for custom serialization or completely general data handling schemes - then a case 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:

Age_Type : Parameter_Type := Params.Entry_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 two exceptions:

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

Name : String := Entry_Name (E);