[omniORB] multiple interface inheritance

Sai-Lai Lo S.Lo@uk.research.att.com
12 Aug 1999 11:27:27 +0100


>>>>> Duncan Grisby writes:

> On Thursday 12 August, andrew.brown@bt.com wrote:
>> When I use a class to implement two interfaces e.g.
>> 
>> class implementation :
>> public virtual _sk_interface1,
>> public virtual _sk_interface2
>> 
>> 
>> I get the compile time errors:

> [...]

>> What is the best way to achieve multiple interface inheritance with
>> omniORB2?

> You cannot declare a C++ class which implements more than one IDL
> interface. You should declare a new IDL interface which inherits from
> the two base interfaces, then provide an implementation of that.

> Alternatively, you can create a servant which supports more than one
> interface using the DSI, but then you have to do a lot more work
> yourself, and lose a lot of compile-time type checking.


Actually, you can define a C++ implementation class that implements more
than one IDL interfaces through inheritance on the _sk classes. However,
to do so you have to define final override virtual functions for the base
classes that both _sk classes inherit from. This is unnecessarily complex and
make your code highly non-portable. In your example, the compiler requires
you to provide the final override for an omniORB internal virtual function.

Depends on your design, IDL interface inheritance may be what you need
especially when you want to express the inheritance relationship in the
interface.

However, there are other situations that your want to have one
implementation to serve 2 disjoint IDL interfaces, in that case 
I personally prefer the tie approach to deal with this.

Here is an example (in addition to the tie echo example in src/examples/echo).

Sai-Lai

--------------------------------------------------------------------------
// IDL: multiEcho.idl  compile with omniidl2 -t.
//      
//
//   interface Echo1 {
//        string echoString(in string mesg);
//   };
//
//   interface Echo2 {
//     long echoLong(in long mesg);
//   };
//
// Implement multiple interfaces using tie template

#include <iostream.h>
#include <multiEcho.hh>

// implementation using tie template
class multiEcho_i {
public:

  multiEcho_i() {}

  // implement interface Echo1
  char * echoString(const char *mesg) {
    char *p = CORBA::string_alloc(strlen((const char*)mesg));
    strcpy((char *)p,(const char *)mesg);
    return p;
  }

  // implement interface Echo2
  CORBA::Long echoLong(CORBA::Long mesg) {
    return mesg;
  }
};


int
main(int argc, char **argv) {

  CORBA::ORB_ptr orb = CORBA::ORB_init(argc,argv,"omniORB2");
  CORBA::BOA_ptr boa = orb->BOA_init(argc,argv,"omniORB2_BOA");

  multiEcho_i *myobj = new multiEcho_i();

  _tie_Echo1<multiEcho_i,1> *myEcho1 = new _tie_Echo1<multiEcho_i,1>(myobj);
  //                     ^
  //                     |
  //            Set this boolean flag to 1 so that when the tie 
  //            implementation is deleted by _dispose(), the real
  //            implementation (multiEcho_i) is also deleted.

  _tie_Echo2<multiEcho_i,0> *myEcho2 = new _tie_Echo2<mutliEcho_i,0>(myobj);
  //                     ^
  //                     |
  //             Set this boolean flag to 0 so that when the tie
  //             implementation is deleted by _dispose(), the real
  //             implementation (multiEcho_i) is *NOT* deleted.


  //  When multiple tie implementations are sharing one real implementation
  //  Make sure that only one tie template instantiation has the release flag
  //  set to 1.

  myEcho1->_obj_is_ready(boa);
  myEcho2->_obj_is_ready(boa);

  {
    Echo1_var myEcho1Ref = myEcho1->_this();
    CORBA::String_var p = orb->object_to_string(myEcho1Ref);
    cerr << "Echo1 IOR is " << (char*)p << endl;

    Echo2_var myEcho2Ref = myEcho2->_this();
      p = orb->object_to_string(myEcho2Ref);
      cerr << "Echo2 IOR is " << (char*)p << endl;
  }

  boa->impl_is_ready();
  return 0;
}
--------------------------------------------------------------------------




-- 
Sai-Lai Lo                                   S.Lo@uk.research.att.com
AT&T Laboratories Cambridge           WWW:   http://www.uk.research.att.com 
24a Trumpington Street                Tel:   +44 1223 343000
Cambridge CB2 1QA                     Fax:   +44 1223 313542
ENGLAND