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_var
class. The constructor initializes to 0 (zero) theTYPE *
owned by the var class. You may not invoke theoperator->
on aTYPE_var
class unless a validTYPE *
has been assigned to it. -
TYPE_var(TYPE * Value);
- This constructor assumes ownership of the specified
TYPE *
parameter. When theTYPE_
var
is destroyed, theTYPE
is released. TheValue
argument is a pointer to theTYPE
to 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
TYPE
and makes a deep copy of the data contained in theTYPE
owned by theFrom
parameter. When theTYPE_
var
is destroyed, the copy of theTYPE
is released or deleted. TheFrom
parameter specifies the var class that points to theTYPE
to be copied. -
~TYPE_var();
- This destructor uses the appropriate mechanism to release the
TYPE
owned by the var class. For strings, this is theCORBA::string_free
routine. For object references, this is theCORBA::release
routine. For other types, this may bedelete
or a generated static routine used to free allocated memory. -
TYPE_var &operator=(TYPE * NewValue);
- This assignment operator assumes ownership of the
TYPE
pointed to by theNewValue
parameter. If theTYPE_
var
currently owns aTYPE
, it is released before assuming ownership of theNewValue
parameter. TheNewValue
argument is a pointer to theTYPE
to 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
TYPE
and makes a deep copy of the data contained in theTYPE
owned by theFrom
TYPE_
var
parameter. IfTYPE_
var
currently owns aTYPE
, it is released. When theTYPE_
var
is destroyed, the copy of theTYPE
is released. TheFrom
parameter specifies the var class that points to the data to be copied. -
TYPE *operator->(); TYPE *operator->() const;
- These operators return a pointer to the
TYPE
owned by the var class. The var class continues to own theTYPE
and 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_var
has 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
_var
types also support member functions that allow them to be explicitly converted for purposes of parameter passing. To pass aTYPE
_var
and anin
parameter, call thein()
member function; forinout
parameters, theinout()
member function; forout
parameters, theout()
member function. To obtain a return value from theTYPE
_var
, call the_return()
function. For eachTYPE
_var
type, 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 |