Sequences

Sai-Lai Lo S.Lo@orl.co.uk
Wed, 5 Nov 1997 21:03:23 GMT


I *strongly* encourage you to study the CORBA C++ mapping chapters
(ch15-18) in the CORBA 2.0 specification. The specfication is available for
download at www.omg.org.

To use sequence (and other data types) correctly, you have to understand
the memory mangement responsibilities. The mapping chapters explain this
clearly.

In your example:

interface Reader {
    int ReadLog(out Log contents);
};

The OUT argument <contents> is a sequence, the mapping specifies that it
is the callee's responsibility to allocate the storage for the sequence
and returns this to the caller. Once the callee returns the sequence to the
caller, the latter is responsible for releasing the sequence when it is no
longer needed. In terms of client and server, the caller is the client and
the callee is the server.

Your object implementation:
>
> unsigned short Reader_impl::ReadLog(Log *& contents)
> {
>   contents-> length(2);
>   return 0;
> }

is wrong because you have not allocate the storage for the sequence.
It should look something like this:

unsigned short Reader_impl::ReadLog(Log *& contents)
{
   contents = new Log;  // Allocate a sequence
   contents->length(2);
   .... // write something to the sequence
	// e.g. (*contents)[0] = 0;
	//      (*contents)[1] = 1;
   return 0;
}

On the client side, you do something like this:

   Log_var x;
   obj->ReadLog(x);
   cerr << "length is " << x->length() << endl;
   cerr << "value is " << x[0] << " " << x[1] << endl;

You can also use Log instead of Log_var. The difference is that Log_var
auto-manage the storage of the sequence. Please read the mapping chapers
for the use of the T_var types. 


Sai-Lai

-- 
E-mail:         S.Lo@orl.co.uk          |       Olivetti & Oracle Research Lab
                                        |       24a Trumpington Street
Tel:            +44 223 343000          |       Cambridge CB2 1QA
Fax:            +44 223 313542          |       ENGLAND