13.1.25.3 Extraction from Any
To allow type-safe retrieval of a value from an
any
, the ORB provides the following operators for each
OMG IDL type T:
// C++
Boolean operator>>=(const Any&, T&);
This function signature suffices for primitive types that are usually passed by value. For values of type T that are too large to be passed by value efficiently, the ORB provides a different signature, as follows:
// C++
Boolean operator>>=(const Any&, T*&);
The first form of this function is used only for the following types:
Boolean, Char, Octet, Short, UShort, Long, ULong, Float, Double
- Enumerations
- Unbounded strings (
char*
passed by reference, i.e.,char*&
) - Object references (
T_ptr
)
For all other types, the second form of the function is used.
This “right-shift-assign” operator is used to
extract a typed value from an any,
as follows:
// C++
Long value;
Any a;
a <<= Long(42);
if (a >>= value) {
// ... use the value ...
}
In this case, the version of operator>>=
for
type Long
determines whether the Any truly does
contain a value of type Long
and, if so, copies its
value into the reference variable provided by the caller and
returns TRUE
. If the Any does not contain a value of
type Long
, the value of the caller’s reference
variable is not changed, and operator>>=
returns
FALSE
.
For nonprimitive types, extraction is done by pointer. For example, consider the following OMG IDL struct:
// IDL
struct MyStruct {
long lmem;
short smem;
};
Such a struct could be extracted from an Any as follows:
// C++
Any a;
// ... a is somehow given a value of type MyStruct ...
MyStruct *struct_ptr;
if (a >>= struct_ptr) {
// ... use the value ...
}
If the extraction is successful, the caller’s pointer
points to storage managed by the Any, and
operator>>=
returns TRUE
. The
caller must not try to delete
or otherwise release
this storage. The caller also must not use the storage after the
contents of the Any variable are replaced via assignment,
insertion, or the replace
function, or after the Any
variable is destroyed. Care must be taken to avoid using
T_var
types with these extraction operators, since
they will try to assume responsibility for deleting the storage
owned by the Any.
If the extraction is not successful, the value of the
caller’s pointer is set equal to the NULL pointer, and
operator>>=
returns FALSE
.
Correct extraction of array types relies on the Array_forany
types described in Arrays.
An example of the OMG IDL is as follows:
// IDL
typedef long A[20];
typedef A B[30][40][50];
// C++
typedef Long A[20];
typedef Long A_slice;
class A_forany { ... };
typedef A B[30][40][50];
typedef A B_slice[40][50];
class B_forany { ... };
Boolean operator>>=(const Any&, A_forany&); // for
type A
Boolean operator>>=(const Any&, B_forany&); // for type
B
The Array_forany
types are always passed to
operator>>=
by reference.
For strings and arrays, applications are responsible for checking the TypeCode of the Any to be sure that they do not overstep the bounds of the array or string object when using the extracted value.
The operator>>=
is also supported on the
Any_var
type.
Parent topic: Any Type