Wednesday, April 19, 2006

Structure Declaration in IDL and its C++ mapping

CORBA IDL Series Continued (Part 4)

Declaration in IDL - Duplicated here for readability.

<struct_type> ::= “struct” <identifier> “{” <member_list> “}”

<member_list> ::= <member>+

<member> ::= <type_spec> <declarators> “;”

Recursion

IDL allows recursion of structures by having members which are sequence types of its own incomplete type. Note that a structure (or for that matter union) is termed incomplete until its definition is completed with a closing "{". Recursion is allowed by using the following techniques
  1. Anonymous sequence member (Deprecated usage)
  2. Sequence using forward declaraed struct type.

The examples below illustrate the two -

struct Foo {
long value;
sequence<Foo> chain; // anonymous sequence member
};

struct Bar; // Forward declaration
typedef sequence<Bar> BarSequence; // sequence of incomplete type
struct Bar {
long value;
BarSequence chain; // recursion
};

Some rules regarding the above -
  • As said earlier, when structures are forward declared, they are termed incomplete as their definition is still not seen. It is a rule that the structure should be completed in the same IDL file. So, forward declaration of structures defined in other IDLs is not permitted.
  • When sequences are thus used for recursion, the sequence should be member of the structure definition defining the incomplete struct contained in the recursion.
  • Incomplete type definitions can only appear as element type of a sequence definition and such as sequence is called Incomplete Sequence Type.
  • Incomplete Sequence types can appear only as element types of another sequence or as a member of structure or union.
Mutual recursion is not possible because of the above rule. For example, the snippet below is illegal -

struct Foo;
typedef sequence<Foo> FooSeq;
struct Bar {
FooSeq chain; // illegal as Foo is incomplete
};
struct Foo {
long l;
};

The same rules apply to unions as well.

Please note that incomplete structures and unions are said to be complete after it has been defined. Hence any such sequence can also be used after the structure is defined.

Structure C++ Mapping

A structure is mapped to to C++ struct or a class with default and copy constructor, assignment operator and a destructor. All the members are publically accessible and the memory management of the internal members is internal to the structure and users of the structure dont have to worry about that. What this means is that the default constructor initializes all the data members appropriately, the copy constructor does a deep copy of the members (such as duplicating object references, copy string data etc), assignment operator first releases the current member memory and then does deep copy and the destructor deletes all member memory.

Fixed and Variable length structures

If a structure contains any variable lenght members such as object reference or string, then the structure is called Variable Length Structure. This has implications when passing the structure though an operation.

For example consider the following IDL

struct Foo {
long l; // Fixed len
};
struct Bar {
string s; // Variable len
};
interface I {
Bar op1(in Bar, inout Bar, out Bar);
Foo op2(in Foo, inout Foo, out Foo);
}

When sending in and reading out fixed length structures, since the memory needed is fixed, the caller is responsible for allocating the memory. However for variable length structures, the caller needs to allocate only for the in and inout parameters and the callee will allocate for inout and out parameters. So, the mapping for the two operation becomes -

class I ... {
...
virtual Bar op1(const Bar&, Bar&, Bar&); // For Fixed length
virtual Foo* op2(const Foo&, Foo&, Foo*&); // For Variable length
};

Var types

To hide the above complication, the mapping provides a helper class call var class. For every structure T, a helper class T_var is also generated.

Some of the salient features of this var types is


  • pointer constructor owns the pointer
  • copy constructor deep copies the structure
  • assignment operator releases old and then deep copies
  • destructor deletes the contained structure.
  • Also hides the parameter passing complications.

With the _var type, the above operations can be consistent as follows -

virtual Bar_var op1(const Bar_var&, Bar_var&, Bar_var&); // For Fixed
virtual Foo_var op2(const Foo_var&, Foo_var&, Foo_var&); // For Variable

The T_var type also has in(), inout(), out() and _retn() functions. To avoid any C++ compiler bug which does not allow correct casting, we could use these functions when making calls. For example,

Foo_var f = ...;
f = obj->op2(f.in(), f.inout(), f.out());
return f._retn();