Документ взят из кэша поисковой машины. Адрес оригинального документа : http://rtm-cs.sinp.msu.ru/manual/mico/doc/node54.html
Дата изменения: Mon Jun 7 21:54:59 1999
Дата индексирования: Mon Oct 1 21:22:31 2012
Кодировка:
Untyped values next up previous
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.

For some IDL types two different >>= and <<= operators are provided: a copying and a non-copying version. The copying version of the <<= operator takes a reference to the IDL type and inserts a copy of it into the Any. The non-copying version takes a pointer to the IDL type and moves it into the Any without making a copy. The user must not access the inserted value afterwards. The copying version of the >>= operator takes a reference to the IDL type and copies the value of the Any into it. The non-copying version takes a reference to a pointer to the IDL type and points it to the value in the Any. The user must not free the returned value. Here are some examples:

  // IDL
  struct foo {
    long l;
    short s;
  };

  // C++
  CORBA::Any a;

  // copying <<=
  foo f;
  a <<= f;

  // non-copying <<=
  foo *f = new foo;
  a <<= f;
  // do not touch 'f' here ...

  // copying >>=
  foo f;
  a >>= f;

  // non-copying >>=
  foo *f;
  a >>= f;
  // do not free 'f'
  // changing 'a' invalidates 'f'

Table 5.1 gives an overview of the operators provided for each IDL type (nc. means non-copying).

 
IDL type <<= nc. <<= >>= nc. >>=
base type + +
enum + +
any + + + +
fixed + + + +
string + + +
wstring + + +
sequence + + + +
array + + +
struct + + + +
union + + + +
interface + + +
pseudo objs + + +
valuetype + + +
Table 5.1: Any insertion and extraction operators

 




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

Arno Puder
Mon Jun 7 10:53:40 PDT 1999