Some Interface Repository methods and TypeCode functions return information typed as the IDL basic type any. Usually this is done when a wide variety of different types of data may need to be returned through a common interface. The type any actually consists of a structure with two fields: a _type field and a _value field. The _value field is a pointer to the actual datum that was returned, while the _type field holds a TypeCode that describes the datum.
In many cases, the context in which an operation occurs makes the type of the datum apparent. If so, there is no need to examine the TypeCode unless it is simply as a consistency check. For example, when accessing the first parameter of a tk_struct TypeCode, the type of the result will always be the name of the structure (a string). Because this is known ahead of time, there is no need to examine the returned TypeCode in the any_type field to verify that it is a tk_string TypeCode. You can just rely on the fact that it is a string; or, you can check the TypeCode in the _type field to verify it, if you so choose.
An IDL any type can be used in an interface as a way of bypassing the strong type checking that occurs in languages like ANSI C and C++. Your compiler can only check that the interface returns the any structure; it has no way of knowing what type of data will be carried by the any during execution of the program. Consequently, in order to write C or C++ code that accesses the contents of the any correctly, you must always cast the _value field to reflect the actual type of the datum at the time of the access.
Here is an example of a code fragment written in C that illustrates how the casting must be done to extract various values from an any:
#include <som.h> /* For "any" & "Environment" typedefs */ #include <somtc.h> /* For TypeCode_kind prototype */ any result; Environment *ev; printf ("result._value = "); switch (TypeCode_kind (result._type, ev)) { case tk_string: printf ("%s\n", *((string *) result._value)); break; case tk_long: printf ("%ld\n", *((long *) result._value)); break; case tk_boolean: printf ("%d\n", *((boolean *) result._value)); break; case tk_float: printf ("%f\n", *((float *) result._value)); break; case tk_double: printf ("%f\n", *((double *) result._value)); break; default: printf ("something else!\n"); }
Note: Of course, an any has no restriction, per se, on the type of datum that it can carry. Frequently, however, methods that return an any or that accept an any as an argument do place semantic restrictions on the actual type of data they can accept or return. Always consult the reference page for a method that uses an any to determine whether it limits the range of types that may be acceptable.