13.3 Using var Classes
Automatic variables (vars) are provided to simplify memory management. Vars are provided through a var class that assumes ownership for the memory required for the type and frees the memory when the instance of the var object is destroyed or when a new value is assigned to the var object.
The Oracle Tuxedo provides var classes for the following types:
- String (
CORBA::String_var) - Object references (
CORBA::Object_var) - User-defined OMG IDL types (
struct,union,sequence,array, andinterface)
The var classes have common member functions, but may support additional operators depending upon the OMG IDL type. For an OMG IDL type TYPE, the TYPE_var class contains constructors, destructors, assignment operators, and operators to access the underlying TYPE type. An example var class is as follows:
class TYPE_var
{
public:
// constructors
TYPE_var();
TYPE_var(TYPE *);
TYPE_var(const TYPE_var &);
// destructor
~TYPE_var();
// assignment operators
TYPE_var &operator=(TYPE *);
TYPE_var &operator=(const TYPE_var &);
// accessor operators
TYPE *operator->();
TYPE *operator->() const;
TYPE_var_ptr in() const;
TYPE_var_ptr& inout();
TYPE_var_ptr& out();
TYPE_var_ptr _retn();
operator const TYPE_ptr&() const;
operator TYPE_ptr&();
operator TYPE_ptr;
};The details of the member functions are as follows:
-
TYPE_var() - This is the default constructor for the
TYPE_varclass. The constructor initializes to 0 (zero) theTYPE *owned by the var class. You may not invoke theoperator->on aTYPE_varclass unless a validTYPE *has been assigned to it. -
TYPE_var(TYPE * Value); - This constructor assumes ownership of the specified
TYPE *parameter. When theTYPE_varis destroyed, theTYPEis released. TheValueargument is a pointer to theTYPEto be owned by this var class. This pointer must not be 0 (zero). -
TYPE_var(const TYPE_var &From); - This copy constructor allocates a new
TYPEand makes a deep copy of the data contained in theTYPEowned by theFromparameter. When theTYPE_varis destroyed, the copy of theTYPEis released or deleted. TheFromparameter specifies the var class that points to theTYPEto be copied. -
~TYPE_var(); - This destructor uses the appropriate mechanism to release the
TYPEowned by the var class. For strings, this is theCORBA::string_freeroutine. For object references, this is theCORBA::releaseroutine. For other types, this may bedeleteor a generated static routine used to free allocated memory. -
TYPE_var &operator=(TYPE * NewValue); - This assignment operator assumes ownership of the
TYPEpointed to by theNewValueparameter. If theTYPE_varcurrently owns aTYPE, it is released before assuming ownership of theNewValueparameter. TheNewValueargument is a pointer to theTYPEto be owned by this var class. This pointer must not be 0 (zero). -
TYPE_var &operator=(const TYPE_var &From); - This assignment operator allocates a new
TYPEand makes a deep copy of the data contained in theTYPEowned by theFromTYPE_varparameter. IfTYPE_varcurrently owns aTYPE, it is released. When theTYPE_varis destroyed, the copy of theTYPEis released. TheFromparameter specifies the var class that points to the data to be copied. -
TYPE *operator->(); TYPE *operator->() const; - These operators return a pointer to the
TYPEowned by the var class. The var class continues to own theTYPEand it is the responsibility of the var class to releaseTYPE. You cannot use theoperator->until the var owns a validTYPE. Do not try to release this return value or access this return value after theTYPE_varhas been destroyed. -
TYPE_var_ptr in() const; TYPE_var_ptr&inout(); TYPE_var_ptr&out(); TYPE_var_ptr _retn(); - Because implicit conversions can sometimes cause a problem with
some C++ compilers and with code readability, the
TYPE_vartypes also support member functions that allow them to be explicitly converted for purposes of parameter passing. To pass aTYPE_varand aninparameter, call thein()member function; forinoutparameters, theinout()member function; foroutparameters, theout()member function. To obtain a return value from theTYPE_var, call the_return()function. For eachTYPE_vartype, the return types of each of these functions will match the type shown in the following table for thein,inout,out, and return modes for the underlying typeTYPE,respectively.
Some differences occur in the operators supported for the user-defined data types. The following table describes the various operators supported by each OMG IDL data type, in the generated C++ code. Because the assignment operators are supported for all of the data types described in the following table, they are not included in the comparison.
Table 13-3 Comparison of Operators Supported for User-defined Data Type var Classes
| OMG IDL Data Type | operator -> | operator [] |
|---|---|---|
struct
|
Yes | No |
union
|
Yes | No |
sequence
|
Yes | Yes, non-const only |
array
|
No | Yes |
The signatures are as shown in the following table.
Table 13-4 Operator Signatures for _var Classes
| OMG IDL Data Type | Operator Member Functions |
|---|---|
struct
|
TYPE * operator-> ()
|
union
|
TYPE * operator-> ()
|
sequence
|
TYPE * operator-> ()
|
array
|
TYPE_slice & operator[](CORBA::Long index) TYPE_slice & operator[](CORBA::Long index) const |