Wednesday, April 19, 2006

Array Declaration in IDL and its C++ mapping

CORBA IDL Series Continued (Part 7)

IDL Declaration

<type_dcl> ::= “typedef” <type_spec> <array_declarator>

<array_declarator> ::= <identifier> <fixed_array_size>+

<fixed_array_size> ::= “[” <positive_int_const> “]”

Here the type could be any type.

Arrays could be multidimensional and their sizes are fixed at compile time. One difference between an array and a sequence is that during transmission, all the elements are sent across on the wire.

C++ Mapping

All IDL arrays are mapped to C++ arrays of the C++ mapping of the IDL types, but for string, wstring, all interface types and valuetypes. For these types, the array is mapped to array of their var type.

An array slice is an array with all the dimensions of the original specified, but for the first one. An array slice is also mapped as name of the array suffixed by _slice. For example,

// IDL
typedef long LongArray[4][5];
// C++
typedef Long LongArray[4][5];
typedef Long LongArray_slice[5];

Other functions

Also for an array type Foo, the mapping provides functions
// C++
Foo_slice *Foo_alloc();
Foo_slice *Foo_dup(const Foo_slice*);
void Foo_copy(Foo_slice* to, const Foo_slice* from);
void Foo_free(Foo_slice *);

Var Variables

The var variable has a default constructor and another taking the slice pointer, apart from the copy constructor.

Also, instead of overloading -> operator, the subscript operator is overloaded in the var variable.

Parameter Passing

For an array of type T, the parameter passing rules are as follows -

// IDL
typedef Foo FooArray[xx]; // array of fixed type
typedef Bar BarArray[yy]; // array of variable type
interface I {
FooArray op1(in FooArray, inout FooArray, inout FooArray);
BarArray op1(in BarArray, inout BarArray, inout BarArray);
};

// C++
class I ... {
..
virtual FooArray_slice* op1(const FooArray, FooArray, FooArray);
virtual BarArray_slice* op2(const BarArray, BarArray, BarArray_slice*&);
};

class I ... {
..
virtual FooArray_var op1(const FooArray_var&, FooArray_var&, FooArray_var&);
virtual BarArray_var op2(const BarArray_var&, BarArray_var&, BarArray_var&);
};