[omniORB] class constructors

Stefan Seefeld stefan@berlin-consortium.org
Mon, 18 Dec 2000 12:18:47 -0500


Marco Verlato wrote:
> 
> Hello Stefan,
> thanks for your e-mail,
> i need a little more help due to the lack of examples in OMG CORBA/IIOP
> 2.3.1 specification document chapter 3. I found the keyword "factory" under
> "Initializers" paragraph, but don`t understand how to use it. Perhaps you
> can help me:
> let`s say i take the Echo example:
> interface Echo{
>     string echoString(in string mesg);
> };
> and i put in the servant implementation class Echo_i the private member
> variable "int myVar", and i want to have an additional constructor of
> Echo_i(int Var) { myVar = Var}, is there a way to make this initialization
> from the client when it makes Echo_var echoref = Echo::_narrow(obj), and how
> should i do?
> I think that i could achieve this by defining an attribute myVar
> in echo interface:
> interface Echo{
>     attribute short myVar;
>     string echoString(in string mesg);
> };
> since it create the accessor and mutator methods and i could then make in
> the client:
> Echo_var echoref = Echo::_narrow(obj);
> echoref->myVar(Var);
> 
> but i would prefer to make this mandatory at the instantiation time.
> Do you think it is possible?

Sure, with a factory. The problem with the Echo example is that the Echo servant
exists before the client calls the server. What you want is a factory object instead
of the echo, which then creates the Echo servant on demand:

interface EchoFactory
{
  Echo create_echo(in short parameter);
};

which you implement like this:

Echo_ptr EchoFactory::create_echo(short parameter)
{
   Echo_i *echo = new Echo_i(parameter);  // 'parameter' can even be a const member !
   // activate echo here
   return echo->_this();
};

and from your client you call:

CORBA::Object_var obj = getObjectReference(orb);

EchoFactory_var factory = EchoFactory::_narrow(obj);
Echo_var echo = factory->create_echo(42);
hello(echo);

Regards,	Stefan

PS: of course, to make it right, you also need to care about ref counting or some other
    means to manage the various object/servant references. I suggest you read some good
    books about CORBA/C++ such as the bible by Henning & Vinoski.

_______________________________________________________              
              
Stefan Seefeld
Departement de Physique
Universite de Montreal
email: seefelds@magellan.umontreal.ca

_______________________________________________________

      ...ich hab' noch einen Koffer in Berlin...