Re: CORBA_any usage



Hi Wim,

On Wed, 24 Jan 2001, Wim Oudshoorn wrote:
> I know this is not ORBit specific question but a C-language binding
> one but I do not know of a more apropriate place to ask.

        Fine.
 
> But suppose that I have a corba method
> 
> any getMeSomething();
   
        The best thing to do (IMHO) is to use the DynamicAny interface,
people shouldn't really be fiddling around creating their own Any's I     
think. So; to do what you want to do do this:
   
        CORBA_DynamicAny dyn_any = CORBA_ORB_create_basic_dynany (
                orb, TC_long, ev);
        CORBA_Any *any;

        DynamicAny_DynAny_insert_long (dyn_any, 12345, ev);
        any = DynamicAny_DynAny_to_any (dyn_any, ev);
        CORBA_Object_release (dyn_any, ev);

        use any ...

        This should be speedy and efficient, the overhead is not large.

> CORBA_any* getMeSomething (....., .....)
> {
>       long x;
>       CORBA_any *returnValue;
>
>       x = aFunctionReturningAnInteger ();
>               
>       // Initializing the CORBA_any value.
>
>       returnValue = CORBA_any__alloc ();
>       returnValue->_type = TC_CORBA_long;
>
>       // What now?????
>
>       XXXXXXX;
  
        For an example of how to do it without using DynamicAny see
bonobo/bonobo-arg.c - this is not a good way to do it though, this code
predates DynamicAny.
 
> The XXXXXX if I read the spec correctly should be:
>
>       returnValue->_value = &x;
> But this obviously is not going to work because x is allocated on the
> stack. But allocating memory for just an integer seems

        If you want to return it you need to allocate it; if you are
passing an Any to another method as an in parameter you can just construct
it on the stack yourself.

> b) When is that memory freed?

        CORBA memory allocation / deallocation is 'clever' stuff is  
tracked in a header block defined before the Any; in this case it is freed
using CORBA__any_free (see CORBA_any_alloc ) that will deal with this
problem for you - read ORBit/src/orb/orbit.c / allocators.c
 
>               returnValue->_value = CORBA_char (sizeof (CORBA_long) -
1);
>               * ((CORBA_long*) returnValue->_value) = x;
>               CORBA_any_set_release (returnValue, CORBA_TRUE);
   
        This should work.

        Regards,
   
                Michael.

-- 
 mmeeks gnu org  <><, Pseudo Engineer, itinerant idiot





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]