moving from interface class to implementation class

Sai-Lai Lo S.Lo@orl.co.uk
Thu, 16 Oct 1997 11:21:10 +0100


>>>>> Bernd Daum writes:

> Example:

> IDL:

> interface A {.....};

> C++:

> class A_i: virtual public _sk_A {};

> If i have an object reference ('ref') for an A and
> want to get an pointer ('p') for the implementation
> object, i usually use the C++ dynamic cast:

> A_ptr ref;
> A_i* p = dynamic_cast<A_i*>(ref);

In this example code, you have made the assumption that the object
reference A_ptr is a base class of the implementation skeleton _sk_A.  This
is not a behaviour mandated by the CORBA spec. In fact, the Portable Object
Adaptor (POA) spec. says:

18.2.1

"In several existing ORB implementations, each skeleton class derives from
 the corresponding interface class. ... These systems therefore allow an
 object reference for a servant to be implicitly obtained via normal C++
 derived-to-base conversion rules:
      MyImplOfA my_a;
      A_ptr     a = &my_a;
 Such code can be supported by a conforming ORB implementation, but it is
 not required, and is thus not portable. The equivalent portable code invokes
                   ^^^^^^^^^^^^^^^^^^^^
 _this() on the implementation object.."

Also, I'm not sure there is a portable way to get back to the implemntation
from the object reference.

With omniORB, the recommended way to obtain an object reference from an
implementation is using _this() which is a member function of the
implementation skeleton. The function returns an object reference which you
must call CORBA::release() to deallocate. 

Coming back to your question, it is better to avoid making this assumption
and not to use dynamic_cast or any other means to get back to the
implementation class from an object reference. 

I should also say that in future, the internal structure of the stub may
change such that it is not possible to use C++ derived-to-base conversion
rule to obtain an object reference from an implementation. To ensure smooth
transition, it is important to avoid making this assumption and 
*******always use _this()******* to obtain the object reference. 

Regards,

Sai-Lai