The C++ Any
class implements the basic IDL Any
type. The Any
type permits the specification of values that can express any OMG IDL type. An Any
consists of a TypeCode and a pointer to the data value. This type is for specifying values in a type-safe way, or for handling values whose type is not known when created by the C++ compiler. A process receiving a value of the Any
type must determine its type before it can specify its value.
The Any
type is often used with interfaces from the Interface Repository or the Dynamic Invocation Interface.
To reduce the chance of creating an Any
with a mismatched typecode and value, C++ function overloading is used. The functions use overloaded operators to avoid name space problems. The Any
interface requires C++ types that are generated to be distinct except for the following:
Any
class interface._out
and _forAny
classes must be used directly.Any( );
The default constructor creates an Any
with a TypeCode
of type CORBA::tk_null
and a zero value. The easiest and safest way to construct an Any
is using the default constructor as follows:
//C++ CORBA::Short s = 10; CORBA::Any a; a <<= s;
CORBA::Any::operator<<=( ), the other Any
constructors:
CORBA::Any::Any( ) Copy Constructor
CORBA::Any::Any( ) Complex Constructor
Any(const Any& a);
This is the Any
copy constructor that duplicates the TypeCode_ptr
of a
and deep copies the value.
Other Any
constructors:
CORBA::Any::Any( ) Default Constructor
CORBA::Any::Any( ) Complex Constructor
Any (typeCode_ptr <type>, void* <val>, Boolean release = 0);
This constructor creates an Any
with a TypeCode
and value. It is needed when you cannot use the default constructor with the operator <<=( )
. You might use this constructor, for example, to create an Any
with a specific TypeCode
for a bounded string.
This constructor is not type-safe. You must ensure consistency between the TypeCode
and the type of the argument <val>
.
<type>
CORBA::TypeCode
. The constructor duplicates this object reference.
<val>
release = 1
. release
<val>
. If it is set to 1, the Any
object assumes ownership of the storage. A compliant program should not use <val>
after the constructor call. If the release parameter is set to 0 (the default), the calling program is responsible for managing the memory pointed to by <val>
. Other Any
constructors:
CORBA::Any::Any( ) Default Constructor
CORBA::Any::Any( ) Copy Constructor
~Any( );
This is the destructor for an Any
. The destructor frees the value contained in the Any
based on the TypeCode
of the Any
. The TypeCode
is also released.
CORBA::Any::Any( ) Complex Constructor
Any& operator=(const Any& a);
This is the assignment operator. It releases TypeCode
and frees the value as necessary. It duplicates the TypeCode
of a
and performs a deep copy of the value of the parameter.
void operator<<=(Short); void operator<<=(Long); void operator<<=(UShort); void operator<<=(ULong); void operator<<=(Float); void operator<<=(Double); void operator<<=(const char*); //Unbounded string void operator<<=(Any&); void operator<<=(Object_ptr); void operator<<=(from_boolean); void operator<<=(from_octet); void operator<<=(from_char); void operator<<=(from_string);
This inserts a value of the specified type into an Any
. Previous values held by the Any
are deallocated. Each operator<<=( )
copies the inserted value. _duplicate( )
is used in the case of an object reference. The Any
is then responsible for memory management of the copy.
The insertion function operator<<=(const char* <s>)
considers the value passed in <s>
to be an unbounded string. The function CORBA::Any::replace( )
must be used to insert a bounded string into an existing Any
or from_string
struct.
For function overloading, the C++ mapping for IDL types boolean, octet, and char cannot be distinguished. You must use the CORBA::Any::from_boolean
, CORBA::Any::from_octet
, and CORBA::Any::from_char
helper classes to distinguish these types.
For example:
//C++ CORBA::Octet o = 050; CORBA::Any a; a <<= CORBA::Any::from_octet(o);
The following is invalid:
a<<= o;
Any attempt to insert an unsigned char value into an Any
causes a compiler error.
const Boolean operator>>=(Short&) const; const Boolean operator>>=(Long&) const; const Boolean operator>>=(UShort&) const; const Boolean operator>>=(ULong&) const; const Boolean operator>>=(Float&) const; const Boolean operator>>=(Double&) const; const Boolean operator>>=(char*&) const; const Boolean operator>>=(Any&) const; const Boolean operator>>=(Object_ptr&) const; const Boolean operator>>=(to_boolean) const; const Boolean operator>>=(to_octet) const; const Boolean operator>>=(to_char) const; const Boolean operator>>=(to_string) const; const Boolean operator>>=(to_object) const;
This extracts a value of the specified type from an Any
. You can determine the type of an Any
by calling the member function CORBA::Any::type( )
and then extracting the value using operator>>=( )
.
For function overloading, the C++ mapping for IDL types boolean, octet, and char cannot be distinguished. You must use the CORBA::Any::to_boolean
, CORBA::Any::to_octet
, and CORBA::Any::to_char
helper classes to distinguish these types.
For example:
//C++ CORBA::Octet o; CORBA::Any a = ...; if (a >>= CORBA::Any::to_octet(o))...
The following is invalid:
if (a>>= o) ...
Any attempt to extract an unsigned char value from an Any
causes a compiler error.
The operators return 1 if the Any
contains a value of the appropriate type. Otherwise, they return 0, set their parameter to 0, and return a 0 value if the Any
contains no value.
The to_object
helper type is used to extract an object reference for an Any
as the base object type. If the Any
contains a value of an object reference type as indicated by its TypeCode
, the extraction function operator>>=(to_object)
widens the contained object reference to Object
and returns true; otherwise, it returns false. This is the only object reference extraction function that can widen the extracted object reference. The to_object
operator performs no duplication of the object reference.
void replace(TypeCode_ptr <type>, void* <val>, Boolean release = 0);
This member function is required when you cannot use operator<<=( )
to insert a value into an Any
. For example, you can use this function to create an Any
with a specific TypeCode
for a bounded string. Note that you must ensure consistency between the TypeCode
and the type of the <val>
argument.
<type>
CORBA::TypeCode
. The function duplicates this object reference.
<val>
release
<val>
points to: 1 means the Any
object gets ownership. A compliant program should not use <val>
after the call. 0 means the calling program is responsible for managing the memory pointed to by <val>
. The value pointed to by <val>
is deep copied in the any
.TypeCode_ptr type( ) const;
This returns a reference to the TypeCode
associated with the Any
.
The called should not release the reference nor assign it to a CORBA::TypeCode var
variable.
CORBA::Any::operator<<=( )
CORBA::Any::operator>>=( )
void* value( ) const;
This returns a pointer to the value stored in the Any
. The returned value depends on the type of the value as listed in Table 1:
IDL Type | Value( ) |
---|---|
void |
0 |
boolean |
CORBA::Boolean* |
char |
CORBA::Char* |
octet |
CORBA::Octet* |
short |
CORBA::Short* |
unsigned short |
CORBA::UShort* |
long |
CORBA::Long* |
unsigned long |
CORBA::ULong* |
float |
CORBA::Float* |
double |
CORBA::Double* |
any |
CORBA::Any* |
Object |
CORBA::Object_ptr |
TypeCode |
CORBA::TypeCode_ptr |
NamedValue |
CORBA::NamedValue_ptr |
InterfaceDescription |
CORBA::InterfaceDescription_ptr |
OperationDescription |
CORBA::OperationDescription_ptr |
AttributeDescription |
CORBA::AttributeDescription_ptr |
ParameterDescription |
CORBA::ParameterDescription_ptr |
ModuleDescription |
CORBA::ModuleDescription_ptr |
ConstantDescription |
CORBA::ConstantDescription_ptr |
ExceptionDescription |
CORBA::ExceptionDescription_ptr |
TypeDescription |
CORBA::TypeDescription_ptr |
FullInterfaceDescription |
CORBA::InterfaceDef::FullInterfaceDescription_ptr |
Sequences of above types | Pointer to sequence |
struct |
Pointer to struct |
string |
char* |
array |
Pointer to array slice |
The calling program should not free the memory returned by the value( )
call.