Programming CORBA with Orbacus on Linux
Important methods in generated classes
There are some important methods in the classes generated by the IDL compiler.
Narrowing references to a special interface
All CORBA objects are derived from CORBA::Object, and in many cases references
are initially returned as objects of this general type. You have to downcast them before
using with a special static method:
X X::_narrow(CORBA::Object_ptr)
This method gets a reference to a CORBA objects (this can already be a derived one) and
returns a reference to an object of the desired type or nil if the object does not
support the desired interface. Before use you should therefore test the return value with
is_nil(). The method calls _duplicate so you have to release the returned
reference by yourself! A typical usage is the following:
CORBA::Object_ptr obj = <get reference from somewhere>
X_ptr xp = X::_narrow(obj);
CORBA::release(obj); // no longer needed
if (CORBA::is_nil(xp)){
// interface not supported by object
} else {
<use object>
CORBA::release(xp);
}
Note that the initial reference obj is released immediately after the narrowing
because we do not longer need this initial reference. This must not be forgotten!
After checking the narrowed reference for non-nil, we use it and release it after use.
Of course, the usage of _var variables can make things easier:
CORBA::Object_var obj = <get reference from somewhere>
X_var xv = X::_narrow(obj);
if (CORBA::is_nil(xv)){
// interface not supported by object
} else {
<use object>
}
If these references goes out of scope, they are automatically be released.
Checking for interfaces
The method
_is_a(<typeid>)
checks wether the reference on which it is invoked is of specified type. It may throw an
exception if the type specification is incorrect. It throws a system exception like
TRANSIENT because for type checking, the server object may be contacted, resulting in
CORBA communication failures.
Checking for existence
The method
_non_existent()
allows you to check if the reference on which it is invoked points to an existing object
or not. The result is definitive true, if the reference dangles. A result of false indicate
either that the server object does not exist or cannot be contacted to check the existence!
For that, the method may throw system exceptions. A typical usage is the following:
X_ptr xp = <get reference from somewhere>
try {
if (xp->non_existent()){
// definitive answer, release resources
CORBA::release(xp);
} else {
// try to use reference
xp->f();
}
} catch (const CORBA::TRANSIENT&) {
} catch (const CORBA::SystemException&) {
}
back to CORBA page
|