C++ bindings for CORBA Array types

An Interface Definition Language (IDL) array type is mapped to the corresponding C++ array definition. There also is a corresponding _var type. For example, given the following IDL definition:

typedef long LongArray [4][5];

The C++ bindings are similar to the following definitions:

typedef CORBA::Long LongArray[4][5];
typedef CORBA::Long LongArray_slice[5];
typedef LongArray_slice* LongArray_slice_vPtr;
typedef const LongArray_slice* LongArray_slice_cvPtr;
class LongArray_var 
{
   public:
   LongArray_var();
   LongArray_var(LongArray_slice*);
   LongArray_var(const LongArray_var&);
   LongArray_var & operator= (LongArray_slice*);
   LongArray_var & operator= (const LongArray_var &);
   ~LongArray_var();
   const LongArray_slice& operator[] (int) const;
   const LongArray_slice& operator[] (CORBA::ULong) const;
   LongArray_slice & operator[] (int);
   LongArray_slice & operator[] (CORBA::ULong);
   operator LongArray_slice_cvPtr () const;
   operator LongArray_slice_vPtr& ();
   const LongArray_slice* in() const;
   LongArray_slice* inout();
   LongArray_slice* out();
   LongArray_slice* _retn();
};
LongArray_slice * LongArray_alloc();
void LongArray_free (LongArray_slice*);
LongArray_slice * LongArray_dup (const LongArray_slice*);
LongArray_copy (LongArray_slice* to, const LongArray_slice* from);

As shown previously, array mappings provide the following funcations:

alloc function
This is used for allocating storage. The alloc function dynamically allocates an array, which can be later freed using the free function.
dup function
This is used for duplicating arrays. The dup function dynamically allocates an array and copies the elements of an existing array into it.
copy function
This is used for copying array elements. The copy function copies elements from a previously allocated array to another previously allocated array.
free function
This is used for freeing array storage. The free function frees an array allocated using the alloc or dup function and properly releases the elements of the array. A NULL pointer can be passed to the free function.
None of these functions throws exceptions.

The type of the pointer returned from LongArray_alloc is LongArray_slice*. The C++ bindings define "slice" types for all arrays declared in IDL to indicate the type of the array. In this case, the slice type is an array of Long. The slice type has one less array dimension than the array. Thus, the bindings for LongArray include the following typedef:

typedef Long LongArray_slice[5];

Hence, LongArray_slice* is the correct type for do describe an array of Long arrays.

As with structs and sequences, arrays use special auxiliary classes for automatic storage management of string and object reference elements. The auxiliary classes for strings and object references manage storage the same way the associated _var classes do.

When the array is allocated, the default constructor for each element is automatically invoked to construct the element. If the array's elements are object references, the elements are set to nil when the array is allocated. If the array's elements are strings or wstrings, the elements are set to the empty string.

When assigning a value to an array element that is an object reference, the assignment operator automatically releases the previous value, if any. When assigning an object reference pointer to an array element, the array assumes ownership of the pointer (no _duplicate is done) and the application must no longer access the pointer directly. If this is not the desired behavior, then the caller can explicitly _duplicate the object reference before assigning it. However, when assigning to an object reference array element from a _var object or from another struct, union, array, or sequence member (rather than from an object reference pointer), a _duplicate is done automatically.

The following is an example that involves multidimensional arrays and array_vars from the IDL snippet at the end of this article:

typedef string s2_3[2][3];
typedef string s3_2[3][2];

The code at the end of this article uses the C++ arrays that correspond to the previous IDL snippet. In the following example, there is no need to explicitly use slice types when working with the array _var types. This is possible because the bindings declare the pointer held by an array _var type using the appropriate slice type. At the end, the program explicitly frees the storage pointed to by s2_3p (using an array delete operator), but does not do this for s3_2v. Instead, its pointer is deleted when the destructor for s3_2v is implemented. This is the purpose of the _var types.

#include arr_C.cpp
#include stdio.h
main()
{
   int i,j;
   char id[40];
   // create arrays 
   s2_3_slice* s2_3p = s2_3_alloc();
   s3_2_var s3_2v = s3_2_alloc();
   // load the arrays
   for(i=0; i<2; i++) 
   {
      for (j=0; j<3; j++) 
      {
         sprintf(id, "s2_3 element [%d][%d]",i,j);
         // Use string_dup when assigning a char*
         // if you do not want the array to own the original:
         s2_3p[i][j] = CORBA::string_dup(id);
      }
   }
   for(i=0; i<3; i++) 
   {
      for (j=0; j<2; j++) 
      {
         sprintf(id, "s3_2_var element [%d][%d]",i,j);
         // Use string_dup when assigning a char*
         // if you do not want the array to own the original:
         s3_2v[i][j] = CORBA::string_dup(id);
      }
   }   
   // print the array contents
   for(i=0; i<2; i++) 
   {
      for (j=0; j<3; j++) 
      {
         printf("%s\n", s2_3p[i][j]);
      }
   }
   for(i=0; i<3; i++) 
   {
      for (j=0; j<2; j++) 
      {
         printf("%s\n", s3_2v[i][j]);
      }
   } 
   delete [] s2_3; // needed to prevent a storage leak.
   // Nothing is needed for s3_2v, because
   // it is a _var type.
}

Output from the above program is:

s2_3 element [0][0]
s2_3 element [0][1]
s2_3 element [0][2]
s2_3 element [1][0]
s2_3 element [1][1]
s2_3 element [1][2]
s3_2_var element [0][0]
s3_2_var element [0][1]
s3_2_var element [1][0]
s3_2_var element [1][1]
s3_2_var element [2][0]
s3_2_var element [2][1]

Related reference
CORBA C++ bindings for data types



Searchable topic ID:   rcor_copart
Last updated: Jun 21, 2007 8:07:48 PM CDT    WebSphere Business Integration Server Foundation, Version 5.0.2
http://publib.boulder.ibm.com/infocenter/wasinfo/index.jsp?topic=/com.ibm.wasee.doc/info/ee/corba/ref/rcor_copart.html

Library | Support | Terms of Use | Feedback