Storage management and _var types

The C++ bindings attempt to make the programmer's storage management responsibility as easy as possible. One aspect of this is the "_var" types. For each user-defined structured Interface Definition List (IDL) type T (struct, union, sequences, and arrays) and for interfaces, the bindings generate both a class T and a class T_var. The classes CORBA::String and CORBA::Any also have corresponding CORBA::String_var and CORBA::Any_var classes.

The essential purpose of a _var object is to hold a pointer to dynamically allocated memory. A _var object can be used as if it were a pointer to the IDL type for which it is named. Special constructors, assignment operators, and conversion operators make this work in a way that is invisible to programmers. The memory pointed to by a _var object always is considered to be owned (managed) by the _var object. When the _var object is deleted, goes out of scope, or is assigned a new value, it deletes (or, in the case of an object reference, releases) the managed memory.

A typical _var object is declared by a programmer as an automatic (stack) variable within a code block and is then used to receive an operation result or is passed to an operation as an out parameter. Later, when the code block is exited, the _var object destructor runs and its managed memory is deleted (or, for object references, released).

When a pointer (rather than another _var object, struct, union, array, or sequence element) is assigned to (or used to construct) a _var object, this pointer must point to dynamically allocated memory. It must point to dynamically allocated memory because the _var object does not make a copy. Instead, it assumes ownership of the pointer and later deletes it (or, for object references, releases it). The single exception is that pointers to const data can be assigned to a _var object. When this occurs, the _var object dynamically allocates new memory and copies the const data into the new memory. A pointer assigned to a _var object must not be "owned" by some other data structure and the pointer must not be subsequently used by the application except by the _var object.

The default constructor for a _var type loads the contained pointer with NULL. You must assign a value to a _var object created by a default constructor before invoking methods on it. This is similar to when you assign a value to a pointer variable before invoking methods on it.

The copy constructor and _var assignment operator of a _var type perform a deep copy of the source data. The copy is later deallocated (or released, in the case of object references) when the _var is destroyed or when it is assigned a new value.

Methods are provided to fully support the passing of _var types as parameters. In most cases, these methods are not necessary, but they might be used solely to self-document the code. The in() method returns the appropriate type for in parameters. The inout() method returns the appropriate type for inout parameters. The out() method returns the appropriate type for out parameters and ensures that any pre-existing value is properly released.

Note: Out signatures use the _out class, which also takes care of releasing pre-existing values.

The _retn() method is useful for obtaining a value from a _var type that contains a returned value. The _retn() method obtains the value and releases/clears the value in the _var.

The following is the typical form for a T_var class that is emitted for an IDL-constructed data type named T:

class T_var 
{
   public:
   T_var ();
   T_var (T*);
   T_var (const T_var&);
   ~T_var ();
   T_var  &operator= (T*);
   T_var  &operator= (const T_var&);
   T * operator-> ();
   const T * operator-> () const;
   in_paramater_type in() const;
   inout_paramater_type inout();
   out_paramater_type out();
   return_paramater_type _retn();
   
   ...
};

For more information on storage management and argument passing, see "Argument passing considerations for C++ bindings".


Related reference
CORBA programming reference
Argument passing considerations for C++ bindings



Searchable topic ID:   rcor_copsmv
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_copsmv.html

Library | Support | Terms of Use | Feedback