[Enterprise Extensions only]

Any::operator<<

Overview Inserts data into an Any.
Original class CORBA::Any


Intended Usage

This operator is intended to be used for type-safe insertion of a data value into an Any. The C++ type of the data being inserted determines what TypeCode is automatically created and stored in the Any. The operators are type-safe in that they insure that an Any is not created with a TypeCode that doesn't match the value it holds.

When inserting a value into an Any, the previous value held by the Any is deallocated.

The from_boolean, from_char, and from_octet helper types are used to distinguish between the IDL types boolean, char, and octet, since these IDL types are mapped to the same C++ type. To insert a boolean, octet, or char into an Any, construct a helper object (by passing the data to be inserted to the helper's constructor), then use the helper object with the corresponding Any insertion operator.

The from_string helper type is used to insert a bounded or unbounded string into an Any, since both IDL types are mapped to char* in C++. (Unbounded strings are signified by constructing the from_string helper with a bound of zero.) The nocopy_flag of the from_string helper is used for non-copying insertion of a string into an Any (in which the Any assumes ownership of the string). Unbounded strings can also be inserted into an Any without the use of the from_string helper type.

In addition to the insertion operators defined in the Any class, corresponding to the basic IDL data types, the emitters generate global insertion operators for all types defined in IDL. This allows any type that can be defined in IDL to be inserted into an Any in a type-safe manner. Both copying and non-copying insertion operators are defined in the bindings.

To insert an array into an Any, the <array-name>_forany helper type (defined in the emitted C++ bindings) should be used. In the C++ bindings, an array within a function argument list decays into a pointer to the first element, thus Any-insertion operators cannot be overloaded to distinguish between arrays of different sizes. Instead, Any-insertion operators are provided for each distinct <array-name>_forany type. To insert an array into an Any, create an appropriate <array-name>_forany object, initializing it from the array to be inserted, then use the global operator<< (the Any insertion operator) defined for that <array-name>_forany type. There is no from_object helper type corresponding to the to_object helper type.

IDL Syntax

  void operator<<= (CORBA::Short data);
  void operator<<= (CORBA::UShort data);
  void operator<<= (CORBA::Long data);
  void operator<<= (CORBA::ULong) data;
  void operator<<= (CORBA::Float data;
  void operator<<= (CORBA::Double data);
  void operator<<= (const CORBA::Any &data);
  void operator<<= (const char* data);
  void operator<<= (const WChar* data);
  void operator<<= (CORBA::Any::from_boolean data);
  void operator<<= (CORBA::Any::from_char data);
  void operator<<= (CORBA::Any::from_octet data);
  void operator<<= (CORBA::Any::from_string data);

Parameters

data
The data to be inserted into the Any.

Example

  #include "corba.h"
  #include   ...
  /* Assert a value of short type properly insert or
     extract from an Any
   */
  CORBA::Any any;
  CORBA::Short s1 = 1, s2 = 2;
  /* s1 extracted from any */
  any <<= s2 /* insert s2 into any */ any>>= s1
  assert(s1 == s2);
 
  /* Assert a value of the from/to helper type methods
     which properly inserted or extracted
   */
  CORBA::Any anyc;
  char my_char = 'z';
  CORBA::Char x_char = 'u';
  /* insert my_char into anyc */
  anyc <<= corba::any::from_char(my_char);
  /* x_char extracted from anyc */
  anyc>>= CORBA::Any::to_char(x_char);
  assert(x_char == my_char);