The typedef
keyword associates identifiers with a type. Typedefs
create aliases for one or more types.
For example, the following IDL code
// IDL typedef long x; interface A1; typedef A1 A2; typedef sequence<long> F1; typedef F1 F2;
maps to the following C++ code:
typedef Long x; //A1 definitions... typedef A1, A2; typedef A1_ptr A2_ptr; typedef A1_var A2_var; //F1 definitions... typedef FA1,F2; typedef F1_var F2_var;
The mapping for a struct, union, or sequence is a C++ struct or class with a default constructor. The constructor initializes object reference members to NULL. The copy constructor performs a deep copy from the existing structure to create a new structure. The assignment operator releases all object reference members and frees all string members from the existing structure before performing the deep copy. The destructor releases all object reference members and frees all string members.
The mappping for structured types varies depending on whether the type is of fixed or variable length. Fixed-length data types are those whose size is known at compile time. The size of variable-length data types can only be determied at runtime.
The following data types are variable length:
typedef
to a variable-length typeany
Fixed- and variable-length data types are handled differently to handle out parameter allocation and permit client-side stubs for operations that return a sequence of strings. You can use _var
types to hide these differences from the application.
IDL struct data types are mapped to C++ structs, and each member of an IDL struct is mapped to a public member of the C++ struct. For example:
struct FixedSizedStruct { long i; char c; float f; };
maps to the following C++:
struct FixedSizedstruct { ORBA::Long i; CORBA::Char c; CORBA::Float f; };
The C++ version of FixedSizedStruct
does not contain any explicitly defined constructors or operator=( )
, it is therefore legal to initialize an instance of it as follows:
fixedSizedStruct s1 = {125466545,'x',3.14};
The IDL compiler generates a C++ type FixedSizedStruct_var
that is used to manage the dynamically allocated structures. Therefore, structures can be dynamically allocated using either of the following:
FixedSizedStruct* p1 = new FixedSizedStruct; FixedSizedStruct_var p2 = new FixedSizedStruct;
The structure pointed to by p2
is deallocated automatically when p2
goes out of scope, whereas the structure pointed to by p1
must be deallocated explicitly using delete p1
.
Structs with all fixed sized members, called fixed sized structs in CORBA, have their storage requirements determined at compile time. Other structs that use string or object reference members are called variable sized, because they contain pointers that can reference varying sizes of storage.
Use of the default constructor for any struct initializes any object reference members to nil. Assigning or copying any struct does deep copying including duplication of any object reference members.
When a struct
is passed as a parameter or return type to an operation, the corresponding C++ formal parameter types are as shown below.
IDL type | in | out | inout | return |
---|---|---|---|---|
struct W of only fixed-sized members
|
const W& |
W& |
W& |
W |
struct W with one or more variable-sized members |
const W& |
W*& |
W& |
W* |
Remember pointers are not a valid IDL type, so a zero pointer cannot be returned when a valid pointer to a struct is expected.
The parameter passing rules are easy to remember once they are understood. In any case, they will be checked by the C++ compiler. In addition, a _var
variable can be passed as an out
parameter without concern for the different rules for fixed- and variable-sized structs, the _var
variable need only be passed, and conversion operators will handle the differences.