MICO's Any
implementation offers an extended interface for
typesafe insertion and extraction of constructed types that were not
known at compile time. This interface is also used by the <<=
and
>>=
operators generated by the IDL compiler for constructed
types. Lets look at the generated operators for a simple structure:
1: // IDL 2: struct foo { 3: long l; 4: string s; 5: }; 6: 7: // C++ 8: CORBA::Boolean operator<<= ( CORBA::Any &a, const foo &s ) 9: { 10: a.type( _tc_foo ); 11: return a.struct_put_begin() && 12: (a <<= s.l) && 13: (a <<= s.s) && 14: a.struct_put_end(); 15: } 16: 17: CORBA::Boolean operator>>=( const CORBA::Any &a, foo &s ) 18: { 19: return a.struct_get_begin() && 20: (a >>= s.l) && 21: (a >>= s.s) && 22: a.struct_get_end(); 23: }
The <<=
operator tells the Any
the TypeCode
(_tc_foo
) of the to be inserted structure in line 10. Those
_tc_*
constants are generated by the IDL compiler as well. If you
want to insert a constructed type that was not known at compile time you
have to get the TypeCode
from somewhere else (e.g., from the
interface repository) or you have to create one using the
create_*_tc()
ORB methods.
After telling the Any
the TypeCode
the <<=
operator
opens a structure in line 11, shifts in the elements of the struct in lines
12-13 and closes the struct in line 14. While doing so the Any
checks the correctness of the inserted items using the TypeCode
. If
it detects an error (e.g., the TypeCode
says the first element of the
struct is a short and you insert a float) the corresponding method or
<<=
operator will return FALSE. If the structure contained another
constructed type you had to make nested calls to struct_put_begin()
and struct_put_end()
or the corresponding methods for unions,
exceptions, arrays, or sequences.
The >>=
operator in lines 17-23 has the same structure as
the <<=
operator but uses >>=
operators to extract the struct
elements and struct_get_begin()
and struct_get_end()
to open
and close the structure. There is no need to specify a TypeCode
before
extraction because the Any
knows it already.