NonStop Software

IDL to C++ Mapping for Union Types

Previous Topic | Next Topic | Contents | Index
Getting Started Guide | Administration Guide | Programmer's Guide

Subtopics

IDL Unions
Generated C++ Classes for IDL Unions
C++ Parameter Passing Table for IDL Unions

IDL Unions

An IDL union is mapped to a C++ class that allows the value of the union to be set and read, and the current type to be determined. For example, the following IDL:

union U switch (long){
cass 1: long x;
case 2: float y;
default: string z;
};

maps to a C++ class that allows an instance of u to be created and one of the members x, y, or z to be set and read:

U Un1;
Un1.y(2.45);	Set the y member.
cout <<   Un1.y( )	<< endl; // output the value.

Given an instance of U, a programmer can determine which of its members is currently set using the _d( ) member function:

switch (Unl. Do( )) {
case 1: cout << Un1->x( ); break;
case 2: cout << Un1->y( ); break;
default: cout << Un1->z( );
};

A call to _do( ) returns the discriminant value, which is of the IDL union's discriminant type (IDL long in this case). The result of calling one of the selector functions is undefined if the discriminant value is set inappropriately.

As is the case with the other IDL data types, the IDL compiler also produces a _var type that can be used to manage dynamically allocated storage. A union can therefore be allocated dynamically using either of the following statements:

U* p1, = new U;
U_var p2 = new U;

The difference is that the instance pointed to by p1 must be deallocated explicitly using delete p1, whereas the instance pointed to by p2 is deallocated automatically when p2 goes out of scope. When a union itself is deallocated, any space that the current member of the union points to is deallocated. In the case of an object reference, the target object's reference count is decremented.

A member of a union that's assigned a new value will have its old value deallocated. Therefore, no memory leak occurs in the following code:

Short l;
i1.z = (const char*) "hello"; 
i1.x(4);	// string is deallocated.
l = i1._d( ); //l gets set to 1.

Generated C++ Classes for IDL Unions

The IDL definition:

typedef char Arr[10];
union	Un2 (switch unsigned long)
    case 1: short a;
    case 2: string b;
    case 3: StockPortfolio c;
    case 4: Arr d;

would generate the following C++ class:

class Un2
public:
    Un2( );
    Un2 (const Un2&)
    ~Un2( );
    Un2& operator=(const Un2&);
    void _d( )(CORBA::ULong); 
    CORBA::ULong _do( );
    void a(CORBA::Short); 
    CORBA::Short a( ) const;
    void b(char*);	// Free old; no copy of new.
    void b(const char*); // Free old; copy new.
    void b(const CORBA::String_var&); // Free old; copy new.
    const char* b( ) const; // No copying.
    void c(StockPortfolio_ptr); // Release old; duplicate new. 
    StockPortfolio_ptr c( ) const; // No duplication.
void d(Arr); 
    Arr_slice* do const;
    };

The member function b(char*) adopts ownership of the string passed to it, whereas the member function b(const char*) copies the parameter passed to it and adopts ownership of the copy.

The accessor function b does not copy the string before returning a (const) pointer to it. The member function c (StockPortfolio_ptr) deallocates/releases any old storage and calls duplicate on the object reference passed to it.

The c( ) function does not duplicate the object reference that the union holds before returning it. All member functions that modify the union call the appropriate deallocation function on the old value; for example, they call CORBA::release( ) if the union previously contains an object reference.

The fact that IDL unions map to C++ classes with modifier and selector functions, rather than to C++ unions, makes it significantly easier to do this deallocation. Each modifier function can determine the nature of the currently held member and deallocate its storage if appropriate. Note that the discriminant of a union can be changed using the _d( ) member function.

C++ Parameter Passing Table for IDL Unions

When a union is passed as a parameter to an operation or appears as the return type, then the corresponding C++ formal parameter types are as shown in the following table.

Table 1. Parameter Passing Modes for Unions
IDL type in out inout return
Union U of all fixed-sized members const  U& U& U& U
Union U with one or more variable-sized members const U& U*& U& U*

You should not return or pass the zero pointer for the other data types that are passed or returned by pointer.


Previous Topic | Next Topic | Contents | Top
Getting Started Guide | Administration Guide | Programmer's Guide
Bibliography | Index | Glossary
© Tandem, a division of Compaq. All rights reserved. Legal notices.