[Enterprise Extensions only]

C++ bindings for CORBA Struct types

An IDL struct type is mapped to a corresponding C++ struct whose field names correspond to those in the IDL declaration, and whose field types support access and storage of the C++ types corresponding to the IDL struct field types. Dynamically allocated storage used to hold such a C++ struct must be allocated and freed using the C++ new and delete operators.

When a new struct is created, the default constructor for each of its fields implements. Object reference fields are initialized to nil references, and String fields are initialized to NULL. When the struct is deleted (or goes out of scope), the destructor for each of its fields implements. The (default) copy constructor performs a deep copy, including duplicating object references; the (default) assignment operator acts as the destructor followed by the copy constructor.

The actual types of the fields in the C++ struct to which an IDL struct is mapped may be auxiliary classes for the purpose of storage management. In particular, String and object reference field types are auxiliary classes that manage Strings and object references in the same way that the associated _var classes do. Although client code should not depend on the names of these auxiliary classes, the client code does need to know that struct fields containing Strings and object references are managed by these auxiliary classes.

When assigning a value to a struct field that is an object reference, the assignment operator for the struct field will automatically release the previous value (if any). When assigning an object reference pointer to a struct member, the struct member assumes ownership of the pointer (no _duplicate is done), and the application should 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 to the struct member.) However, when assigning to an object reference struct member 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.

When assigning a value to a struct field that is a String, or when the struct is deleted or goes out of scope, any previously held (non-null) String is automatically freed. As when assigning to String_vars, assigning a char* to a String field does not make a copy, but assigning a const char *, a String_var, or another struct/union/array/sequence String member does automatically make a copy. One should never assign a string literal (for example, "abc") to a String struct member without an explicit cast to "const char*". When assigning a char* that occupies static storage (rather than one that was dynamically allocated), the caller can use CORBA::string_dup to duplicate the string before assigning it.

As with all constructed types, a _var type is provided for managing an instance of the C++ struct that corresponds to an IDL struct. When assigning one struct's _var to another, the receiving _var deletes its current pointer (thus running all contained destructors), and creates a new struct to hold the assignment result, which is initialized using copy constructors for each of the contained fields. Thus, for example, if the source struct has an object reference field, the struct _var assignment will automatically duplicate this reference.

The IDL that follows is used in the succeeding example, which shows both correct and incorrect ways to to create and manipulate the corresponding C++ struct and the corresponding _var type :

Interface A 
{
   struct S 
   {
      string f1;
      A      f2;
   };
};

The following code illustrates both correct and incorrect ways to create and manipulate the corresponding C++ struct and the corresponding _var type.

{
   A::S_var sv1 = new A::S; 
   A::S_var sv2 = new A::S;
   // sv1->f1 = "abc"; -- Wrong! f1 cannot free this pointer later
   sv1->f1 = CORBA::string_alloc(20); 
   A_ptr a1 =  // get an A somehow 
   A_ptr a2 =  // get an A somehow
   sv1->f2 = a1; // a1 still has ref cnt = 1
   sv2->f1 = CORBA::string_alloc(20);
   sv2->f2 = a2; // a2 still has ref cnt = 1
   sv1 = sv2; // This runs copy ctors, and increments a2's ref cnt.
   // Also, a1's ref count is decremented.
   sv1->f1 = sv2->f1; 
}