13.1.25.4 Distinguishing Boolean, Octet, Char, and Bounded Strings
Since the Boolean, octet, and char OMG IDL types are not
required to map to distinct C++ types, another means of
distinguishing them from each other is necessary so that they can
be used with the type-safe Any interface. Similarly, since both
bounded and unbounded strings map to char*
, another
means of distinguishing them must be provided. This is done by
introducing several new helper types nested in the Any class
interface. For example, this is accomplished as shown below:
// C++
class Any
{
public:
// special helper types needed for boolean, octet,
// char, and bounded string insertion
struct from_boolean {
from_boolean(Boolean b) : val(b) {}
Boolean val;
};
struct from_octet {
from_octet(Octet o) : val(o) {}
Octet val;
};
struct from_char {
from_char(Char c) : val(c) {}
Char val;
};
struct from_string {
from_string(char* s, ULong b,
Boolean nocopy = FALSE) :
val(s), bound(b) {}
char *val;
ULong bound;
};
void operator<<=(from_boolean);
void operator<<=(from_char);
void operator<<=(from_octet);
void operator<<=(from_string);
// special helper types needed for boolean, octet,
// char, and bounded string extraction
struct to_boolean {
to_boolean(Boolean &b) : ref(b) {}
Boolean &ref;
};
struct to_char {
to_char(Char &c) : ref(c) {}
Char &ref;
};
struct to_octet {
to_octet(Octet &o) : ref(o) {}
Octet &ref;
};
struct to_string {
to_string(char *&s, ULong b) : val(s), bound(b) {}
char *&val;
ULong bound;
};
Boolean operator>>=(to_boolean) const;
Boolean operator>>=(to_char) const;
Boolean operator>>=(to_octet) const;
Boolean operator>>=(to_string) const;
// other public Any details omitted
private:
// these functions are private and not implemented
// hiding these causes compile-time errors for
// unsigned char
void operator<<=(unsigned char);
Boolean operator>>=(unsigned char &) const;
};
The ORB provides the overloaded operator<<=
and operator>>=
functions for these special
helper types. These helper types are used as shown here:
// C++
Boolean b = TRUE;
Any any;
any <<= Any::from_boolean(b);
// ...
if (any >>= Any::to_boolean(b)) {
// ...any contained a Boolean...
}
char* p = "bounded";
any <<= Any::from_string(p, 8);
// ...
if (any >>= Any::to_string(p, 8)) {
// ...any contained a string<8>...
}
A bound value of 0 (zero) indicates an unbounded string.
For noncopying insertion of a bounded or unbounded string into
an Any, the nocopy
flag on the
from_string
constructor must be set to
TRUE
:
// C++
char* p = string_alloc(8);
// ...initialize string p...
any <<= Any::from_string(p, 8, 1); // any consumes p
Assuming that boolean, char, and octet all map the C++ type unsigned char
, the private and unimplemented operator<<=
and operator>>=
functions for unsigned char
cause a compile-time error if straight insertion or extraction of any of the boolean, char, or octet types is attempted:
// C++
Octet oct = 040;
Any any;
any <<= oct; // this line will not compile
any <<= Any::from_octet(oct); // but this one will
Parent topic: Any Type