This section describes the DynAny feature of VisiBroker, which allows you to construct and interpret data types at runtime.
The DynAny interface provides a way to dynamically create basic and constructed data types at runtime. It also allows information to be interpreted and extracted from an
Any object, even if the type it contains was not known to the server at compile-time. Using the
DynAny interface, you can build powerful client and server applications that create and interpret data types at run time.
Example client and server applications that illustrate the use of DynAny are included as part of the VisiBroker distribution. The examples are located in the following directory:
A DynAny object has an associated value that may either be a basic data type (such as
boolean,
int, or
float) or a constructed data type. The
DynAny interface, its methods and classes are also documented in the VisiBroker API References.
“Programmer tools for Java” provides methods for determining the type of the contained data as well as for setting and extracting the value of primitive data types.
Constructed data types are represented by the following interfaces, which are all derived from DynAny. Each of these interfaces provides its own set of methods that are appropriate for setting and extracting the values it contains.
A DynAny object may only be used locally by the process which created it. Any attempt to use a
DynAny object as a parameter on an operation request for a bound object or to externalize it using the
ORB.object_to_string method will cause a
MARSHAL exception to be raised.
Furthermore, any attempt to use a DynAny object as a parameter on DII request will cause a
NO_IMPLEMENT exception to be raised.
A DynAny object is created by invoking an operation on a
DynAnyFactory object. First obtain a reference to the
DynAnyFactory object, and then use that object to create the new
DynAny object.
The DynAny.insert_<type> methods allow you to initialize a
DynAny object with a variety of basic data types, where
<type> is
boolean,
octet,
char, and so on. Any attempt to insert a type that does not match the
TypeCode defined for the
DynAny will cause an
TypeMismatch exception to be raised.
The DynAny.get_<type> methods allow you to access the value contained in a
DynAny object, where
<type> is
boolean,
octet,
char, and so on. Any attempt to access a value from a
DynAny component which does not match the
TypeCode defined for the
DynAny will cause a
TypeMismatch exception to be raised.
The DynAny interface also provides methods for copying, assigning, and converting to or from an
Any object. The sample programs, described in
“DynAny example client application” and
“DynAny example server application”, provide examples of how to use some of these methods.
The following types are derived from the DynAny interface and are used to represent constructed data types.
Several of the interfaces that are derived from DynAny actually contain multiple components. The
DynAny interface provides methods that allow you to iterate through these components. The
DynAny-derived objects that contain multiple components maintain a pointer to the current component.
The DynEnum interface represents a single enumeration constant. Methods are provided for setting and obtaining the value as a string or as an integral value.
The DynStruct interface represents a dynamically constructed
struct type. The members of the structure can be retrieved or set using a sequence of
NameValuePair objects. Each
NameValuePair object contains the member's name and an
Any containing the member's Type and value.
You may use the rewind,
next,
current_component, and
seek methods to traverse the members in the structure. Methods are provided for setting and obtaining the structure's members.
The DynUnion interface represents a
union and contains two components. The first component represents the discriminator and the second represents the member value.
You may use the rewind,
next,
current_component, and
seek methods to traverse the components. Methods are provided for setting and obtaining the union's discriminator and member value.
A DynSequence or
DynArray represents a sequence of basic or constructed data types without the need of generating a separate
DynAny object for each component in the sequence or array. The number of components in a
DynSequence may be changed, while the number of components in a
DynArray is fixed.
You can use the rewind,
next,
current_component, and
seek methods to traverse the members in a
DynArray or
DynSequence.
The following code sample shows the IDL used in the example client and server applications. The StructType structure contains two basic data types and an enumeration value. The
PrinterManager interface is used to display the contents of an
Any without any static information about the data type it contains.
The client application uses the DynStruct interface to dynamically create a
StructType structure.
The DynStruct interface uses a sequence of
NameValuePair objects to represent the structure members and their corresponding values. Each name-value pair consists of a string containing the structure member's name and an
Any object containing the structure member's value.
You must use the DynAny.to_any method to convert a
DynAny object, or one of its derived types, to an
Any before passing it as a parameter on an operation request.
The following code sample shows how the PrinterManager implementation follows these steps in using a
DynAny to process the
Any object, without any compile-time knowledge of the type the
Any contains.