TypeCode and how this is represented in OrbixWeb. TypeCode is used to describe arbitrary complex IDL types at runtime.
TypeCode is used for many purposes in the system:
Any (defined in package IE.Iona.Orbix2.CORBA). This is covered in detail later in this chapter.
TypeCode interface is available in an IDL file only if it includes the directive:
#include <orb.idl>Note that the file
orb.idl is not generally availableit is known internally to OrbixWeb.In an IDL specification, a
TypeCode can be used as an attribute type, or as the type of a parameter or return value to an operation. The IDL TypeCode interface is implemented by the Java class TypeCode (defined in package IE.Iona.Orbix2.CORBA).The class
TypeCode is particularly useful when querying the type of an instance of Any. The class Any has a public member variable, type, of type TypeCode. This variable can be queried at runtime to determine the type of the Any.
TypeCode, as well as the Java class TypeCode.Each
TypeCode consists of a kind and a sequence of parameters. The kind specifies the overall classification of the TypeCode: for example, whether it is a basic type, a struct, a sequence, and so on. The parameters give the details of the type definition. For example, the IDL type sequence<long,20> has the kind tk_sequence and has two parametersthe first parameter is a TypeCode for long, the second parameter is 20.The IDL interface for
TypeCode is defined as follows:
// IDL
// in module CORBA
enum TCKind {
tk_null, tk_void,
tk_short, tk_long, tk_ushort, tk_ulong,
tk_float, tk_double, tk_boolean, tk_char,
tk_octet, tk_any, tk_TypeCode, tk_Principal,
tk_objref, tk_struct, tk_union, tk_enum,
tk_string, tk_sequence, tk_array
};
exception Bounds {};
pseudo interface TypeCode {
TCKind kind();
long param_count();
any parameter(in long index) raises(Bounds);
boolean equal(in TypeCode tc);
};
This interface includes an operation kind() to query the kind of a TypeCode and an operation parameter() to access individual parameters of a TypeCode. The generated signatures of these IDL operations are as follows:
// Java // in package IE.Iona.Orbix2.CORBA, // in class TypeCode public int kind(); public Any parameter(int index) throws Bounds;An IDL enumerated type maps to a Java
int, with an associated class which defines a constant value for each member of the enumeration. Class TypeCode's kind is therefore a value of type int, which may take any of the values defined in class TCKind. The parameter() operation raises the exception Bounds if an attempt is made to access a non-existent parameter.The number of parameters which a
TypeCode has varies with the kind of the TypeCode. This number is returned by the param_count() operation of the TypeCode interface. The generated signature of this operation is as follows:
// Java // in package IE.Iona.Orbix2.CORBA, // in class TypeCode public int param_count();The parameters of each kind of
TypeCode are listed in detail in the description of class TypeCode in Part
II, "Class and Interface Reference" of the OrbixWeb Reference Guide. Some examples follow:
TypeCode of kind tk_struct has one parameter giving the struct name, and then two parameters for each member of the struct: the first giving the member's name and the second giving its TypeCode. A struct with N members has 2N+1 parameters.
TypeCode of kind tk_string has one parameteran integer giving the maximum length of the string. A 0 length indicates an unbounded string.
TypeCode is implemented by the Java class TypeCode. An IDL operation with a parameter of type TypeCode is translated into a Java method which takes a parameter of Java class TypeCode.The IDL compiler can generate constant declarations for
TypeCode pseudo object references from named type definitions that appear in an IDL filethat is from the following types:
typedef struct union enumThus, for each user-defined type which appears in an IDL file, a
TypeCode constant may be generated. This is of the form _tc_<type>, scoped within the generated Java class for <type>, where <type> is the user-defined type. For example, consider the following IDL specification:
struct Useful {
long l;
};
The following TypeCode constant will be generated:
Useful._tc_UsefulThis definition will only be generated if the
-A switch to the IDL compiler is used.In addition, a number of predefined
TypeCode pseudo object reference constants are always available to allow the user to access TypeCodes for standard types. These are exhaustively listed in Part II, "Class and Interface Reference" of the OrbixWeb Reference Guide, under the entry for TypeCode. _CORBA._tc_float is an object reference for a float TypeCode.
_CORBA._tc_string is an object reference for a string TypeCode.
_CORBA._tc_TypeCode is an object reference for a TypeCode TypeCode.
TypeCode defines the following public members:
public TypeCode(); public TypeCode(TypeCode tc); public TypeCode(String tc_info);
copy() method.
equals() which allows comparisons of objects of type TypeCode.
_tc_string member variable which holds the object reference string for the TypeCode, and a toString() method which returns the value of this variable.
kind() which returns an int value from the constants defined in type TCKind.
param_count() which returns the number of parameters of the TypeCode.
parameter() which returns an individual parameter. This takes the parameter index as an argument.
TypeCode:
// Java // in package IE.Iona.Orbix2.CORBA, // in class TypeCode public static TypeCode IT_create(); public static TypeCode IT_create(TypeCode tc); public static TypeCode IT_create(String tc_info); public static TypeCode _nil();These methods allow the programmer to initialise a
TypeCode object reference.
TypeCode are shown in the following subsections.
// IDL
struct Example {
long l;
};
Use of the -A switch will cause the TypeCode for type Example to be generated. Now, assume that a client program invokes the IDL operation op():
// IDL
interface Test {
void op(in any a);
};
// Java
// in file Client.java
import IE.Iona.Orbix2.CORBA.SystemException;
import IE.Iona.Orbix2.CORBA.Any;
public class Client {
public static void main (String args[]) {
_TestRef tRef;
Any a;
try {
tRef = Test._bind ();
// We shall see how to initialise 'a' in
// a later section.
tRef.op (a);
}
catch (SystemException se){
...
}
}
}
On the server side, or at a later point in the client code, we may wish to query the actual type of the Any. For example:
// Java
// in file Client.java,
// in class Client
IE.Iona.Orbix2.CORBA.TypeCode t (a.type());
if(t.equals(Test._tc_Example)) {
...
}
This is one of the most common uses of TypeCodesnamely, the runtime querying of type information from a Any.Use of
TypeCode in type Any is explained fully in Chapter 18, "Type any".
17.3.2 Use of TypeCode when Interrogating the Interface Repository
The kind() and parameter() methods of class TypeCode are frequently useful when using the Interface Repository functions. For example, when finding information about an operation of an interface, the number of its arguments can be found, and then the TypeCode of each argument can be determined. The functions kind() and parameter() can be used on each TypeCode to determine the details of the type of each argument. We will take a detailed look, with examples, at the Interface Repository in Chapter 20, "Interface Repository".