Another feature of MICO's Any
implementation is its subtyping
support. The extraction operators of type Any
implement the
subtyping rules for recursive types as prescribed by the
Reference Model for Open Distributed Processing (RM-ODP), see
[1, 2, 3, 4] for details. The idea behind
subtyping is the following: Imagine you want to call a CORBA method
void bar (in long x);
but want to pass a short
as an argument instead of the required
long
. This should work in theory since each possible
short
value is also a long
value which means
short
is a subtype of long
. More generally speaking a
type is a subtype of type if you could pass as an
input parameter where a is expected. This means for basic types
such as long
: a basic type is a subtype of a basic type
iff the set of possible values of is a subset of the set
of possible values of . Figure 5.1 shows the
subtype relations between CORBA's basic data types. In C++ the
compiler can automatically convert types along a chain of arrows, but
in a distributed CORBA application this can't be done by the compiler
alone because binding between client and server is performed at
runtime using a trader or a naming service. That is the subtype
checking must be done at runtime as well.
Figure 5.1: Subtype relations between basic CORBA types.
In MICO the Any
type performs subtype checking at runtime. For
example:
// C++ CORBA::Any a; a <<= (CORBA::Short) 42; ... CORBA::Double d; a >>= d;
will work because short
is a subtype of double
according to
figure 5.1 but:
// C++ CORBA::Any a; a <<= (CORBA::Long) 42; ... CORBA::ULong d; a >>= d;
will fail because long is not a subtype of unsigned long. There is a special subtyping rule for structured types: A struct type is a subtype of a struct type iff the elements of are supertypes of the first elements of . struct S1 is for example a subtype of struct S2:
struct S1 { short s; long l; }; struct S2 { long s; };
That is you can put a struct S1
into an Any
and unpack it
as a struct S2
later:
// C++ CORBA::Any a; S1 s1 = { 10, 20 }; a <<= s1; ... S2 s2; a >>= s2;
There are similar rules for the other constructed types.