Keywords: doc

Next: Unknown Constructed Types Up: C++ mapping Previous: Using strings

# Untyped values

The handling of untyped values is one of CORBAs strengths. The pre-defined C++ class `Any` in the namespace `CORBA` provides this support. An instance of class `Any` represents a value of an arbitrary IDL-type. For each type, the class `Any` defines the overloaded operators `>>=` and `<<=`. These two operators are responsible for the insertion and extraction of the data values. The following code fragment demonstrates the usage of these operators:

      // C++
      CORBA::Any a;

      // Insertion into any
      a <<= (CORBA::ULong) 10;

      // Extraction from any
      CORBA::ULong l;
      a >>= l;

At the end of this example the variable `l` should have the value 10. The library of MICO provides overloaded definitions of these operators for all basic data types. Some of these data types are ambiguous in the sense that they collide with other basic data types. This is true for the IDL-types `boolean`, `octet`, `char` and `string`. For each of these IDL-types, CORBA prescribes a pair of supporting functions which help to disambiguate the type clashes. For the type `boolean` for example the usage of these supporting function is:

      CORBA::Any a;

      // Insertion into any
      a <<= CORBA::Any::from_boolean( TRUE );

      // Extraction from any
      CORBA::Boolean b;
      a >>= CORBA::Any::to_boolean( b );

The usage of the other supporting functions for `octet`, `char` and `string` is equivalent. For bounded strings the supporting functions `from_string` and `to_string` accept an additional `long`-parameter which reflects the bound.

For each type defined in an IDL specification, the IDL-compiler generates an overloaded version of the operators `>>=` and `<<=`. For example given the following IDL specification:

      // IDL
      struct S1 {
        long x;
        char c;
      };

      struct S2 {
        string str;
      };

The MICO IDL-compiler will automatically generate appropriate definitions of `>>=` and `<<=` for the IDL types `S1` and `S2`. The following code fragment demonstrates the usage of these operators:

     1:  void show_any( const CORBA::Any& a )
     2:  {
     3:    S1 s1;
     4:    S2 s2;
     5:
     6:    if( a >>= s1 ) {
     7:      cout << "Found struct S1" << endl;
     8:      cout << s1.x << endl;
     9:      cout << s1.c << endl;
    10:    }
    11:    if( a >>= s2 ) {
    12:      cout << "Found struct S2" << endl;
    13:      cout << s2.str << endl;
    14:    }
    15:  }
    16:
    17:  int main( int argc, char *argv[] )
    18:  {
    19:    //...
    20:    CORBA::Any a;
    21:
    22:    S2 s2;
    23:    s2.str = (const char *) "Hello";
    24:    a <<= s2;
    25:    show_any( a );
    26:
    27:    S1 s1;
    28:    s1.x = 42;
    29:    s1.c = 'C';
    30:    a <<= s1;
    31:    show_any( a );
    32:  }

The main program first initializes an instance of a `S2` (lines 22-24) and then calls the function `show_any`. Function `show_any` tries to extract the value contained in the any. This example also demonstrates how to tell whether the extraction was successful or not. The operator `>>=` returns true, iff the type of the value contained in the any matches with the type of the variable of the right side of `>>=`. If the any should contain something else than `S1` or `S2`, then `show_any` will fall through both `if`-statements in lines 6 and 11. The complete sources for the above example can be found in `mico/test/idl/14`.

- Unknown Constructed Types
- Subtyping

Next: Unknown Constructed Types Up: C++ mapping Previous: Using strings