Type Any and TypeCode

# Chapter 11 Type Any and TypeCode

The CORBA specification provides for a type that can hold the value of any OMG IDL type. This type is known as type Any. The OMG also specifies a pseudo-object, TypeCode, that can encode a description of any type specifiable in OMG IDL.

In this chapter, an example demonstrating the use of type Any is presented. This is followed by sections describing the behaviour of type Any and TypeCode in omniORB. For further information on type Any, refer to the C++ Mapping specification., and for more information on TypeCode, refer to the Interface Repository chapter in the CORBA core section of the CORBA specification.

## 11.1 Example using type Any

Before going through this example, you should make sure that you have read and understood the examples in chapter 2. The source code for this example is included in the omniORB distribution, in the directory `src/examples/anyExample`. A listing of the source code is provided at the end of this chapter.

### 11.1.1 Type Any in IDL

Type Any allows one to delay the decision on the type used in an operation until run-time. To use type any in IDL, use the keyword `any`, as in the following example:

// IDL interface anyExample { any testOp(in any mesg); };

The operation `testOp()()` in this example can now take any value expressible in OMG IDL as an argument, and can also return any type expressible in OMG IDL.

Type Any is mapped into C++ as the type `CORBA::Any`. When passed as an argument or as a result of an operation, the following rules apply:

In | InOut | Out | Return
`const CORBA::Any&` | `CORBA::Any&` | `CORBA::Any*&` | `CORBA::Any*`

So, the above IDL would map to the following C++:

// C++ class anyExample_i : public virtual POA_anyExample { public: anyExample_i() { } virtual ~anyExample_i() { } virtual CORBA::Any* testOp(const CORBA::Any& a); };

### 11.1.2 Inserting and Extracting Basic Types from an Any

The question now arises as to how values are inserted into and removed from an Any. This is achieved using two overloaded operators: `<<=` and `>>=`.

To insert a value into an Any, the `<<=` operator is used, as in this example:

// C++ CORBA::Any an_any; CORBA::Long l = 100; an_any <<= l;

Note that the overloaded `<<=` operator has a return type of `void`.

To extract a value, the `>>=` operator is used, as in this example (where the Any contains a long):

// C++ CORBA::Long l; an_any >>= l; cout << "This is a long: " << l << endl;

The overloaded `>>=` operator returns a `CORBA::Boolean`. If an attempt is made to extract a value from an Any when it contains a different type of value (e.g. an attempt to extract a long from an Any containing a double), the overloaded `>>=` operator will return False; otherwise it will return True. Thus, a common tactic to extract values from an Any is as follows:

// C++ CORBA::Long l; CORBA::Double d; const char* str; if (an_any >>= l) { cout << "Long: " << l << endl; } else if (an_any >>= d) { cout << "Double: " << d << endl; } else if (an_any >>= str) { cout << "String: " << str << endl; // The storage of the extracted string is still owned by the any. } else { cout << "Unknown value." << endl; }

### 11.1.3 Inserting and Extracting Constructed Types from an Any

It is also possible to insert and extract constructed types and object references from an Any. omniidl will generate insertion and extraction operators for the constructed type. Note that it is necessary to specify the `-WBa` command-line flag when running omniidl in order to generate these operators. The following example illustrates the use of constructed types with type Any:

// IDL struct testStruct { long l; short s; }; interface anyExample { any testOp(in any mesg); };

Upon compiling the above IDL with `omniidl -bcxx -Wba`, the following overloaded operators are generated:

1. `void operator<<=(CORBA::Any&, const testStruct&)`
2. `void operator<<=(CORBA::Any&, testStruct*)`
3. `CORBA::Boolean operator>>=(const CORBA::Any&,`
   `const testStruct*&)`

Operators of this form are generated for all constructed types, and for interfaces.

The first operator, (1), copies the constructed type, and inserts it into the Any. The second operator, (2), inserts the constructed type into the Any, and then manages it. Note that if the second operator is used, the Any consumes the constructed type, and the caller should not use the pointer to access the data after insertion. The following is an example of how to insert a value into an Any using operator (1):

// C++ CORBA::Any an_any; testStruct t; t.l = 456; t.s = 8; an_any <<= t;

The third operator, (3), is used to extract the constructed type from the Any, and can be used as follows:

const testStruct* tp; if (an_any >>= tp) { cout << "testStruct: l: " << tp->l << endl; cout << " s: " << tp->s << endl; } else { cout << "Unknown value contained in Any." << endl; }

As with basic types, if an attempt is made to extract a type from an Any that does not contain a value of that type, the extraction operator returns False. If the Any does contain that type, the extraction operator returns True. If the extraction is successful, the caller’s pointer will point to memory managed by the Any. The caller must not delete or otherwise change this storage, and should not use this storage after the contents of the Any are replaced (either by insertion or assignment), or after the Any has been destroyed. In particular, management of the pointer should not be assigned to a `_var` type.

If the extraction fails, the caller’s pointer will be set to point to null.

Note that there are special rules for inserting and extracting arrays (using the `_forany` types), and for inserting and extracting bounded strings, booleans, chars, and octets. Please refer to the C++ Mapping specification for further information.

## 11.2 Type Any in omniORB

This section contains some notes on the use and behaviour of type Any in omniORB.

### 11.2.1 Generating Insertion and Extraction Operators.

To generate type Any insertion and extraction operators for constructed types and interfaces, the `-Wba` command line flag should be specified when running omniidl.

### 11.2.2 TypeCode comparison when extracting from an Any.

When an attempt is made to extract a type from an Any, the TypeCode of the type is checked for equivalence with the TypeCode of the type stored by the Any. The `equivalent()` test in the TypeCode interface is used for this purpose.

Examples:

// IDL 1 typedef double Double1; struct Test1 { Double1 a; };
// IDL 2 typedef double Double2; struct Test1 { Double2 a; };

If an attempt is made to extract the type `Test1` defined in IDL 1 from an Any containing the `Test1` defined in IDL 2, this will succeed (and vice-versa), as the two types differ only by an alias.

### 11.2.3 Top-level aliases.

When a type is inserted into an Any, the Any stores both the value of the type and the TypeCode for that type. However, in some cases, a top-level alias can be lost due to the details of the C++ mapping. For example, consider these IDL definitions:

// IDL 3 typedef sequence<double> seqDouble1; typedef sequence<double> seqDouble2; typedef seqDouble2 seqDouble3;

omniidl generates distinct types for `seqDouble1` and `seqDouble2`, and therefore each has its own set of C++ operators for Any insertion and extraction. That means inserting a `seqDouble1` into an Any sets the Any’s TypeCode to include the alias ‘seqDouble1’, and inserting a `seqDouble2` sets the TypeCode to the alias ‘seqDouble2’.

However, in the C++ mapping, `seqDouble3` is required to be just a C++ typedef to `seqDouble2`, so the C++ compiler uses the Any insertion operator for `seqDouble2`. Therefore, inserting a `seqDouble3` sets the Any’s TypeCode to the `seqDouble2` alias. If this is not desirable, you can use the member function ‘`void type(TypeCode_ptr)`’ of the Any interface to explicitly set the TypeCode to the correct one.

### 11.2.4 Removing aliases from TypeCodes.

Some ORBs (such as old versions of Orbix) will not accept TypeCodes containing `tk_alias` TypeCodes. When using type Any while interoperating with these ORBs, it is necessary to remove `tk_alias` TypeCodes from throughout the TypeCode representing a constructed type.

To remove all `tk_alias` TypeCodes from TypeCodes transmitted in Anys, supply the `-ORBtcAliasExpand 1` command-line flag when running an omniORB executable. There will be some (small) performance penalty when transmitting Any values.

Note that the `_tc_` TypeCodes generated for all constructed types will contain the complete TypeCode for the type (including any `tk_alias` TypeCodes), regardless of whether the `-ORBtcAliasExpand` flag is set to 1 or not. It is only when Anys are transmitted that the aliases are stripped.

### 11.2.5 Recursive TypeCodes.

omniORB supports recursive TypeCodes. This means that types such as the following can be inserted or extracted from an Any:

// IDL 4 struct Test4 { sequence<Test4> a; };

### 11.2.6 Threads and type Any.

Inserting and extracting simultaneously from the same Any (in 2 different threads) results in undefined behaviour.

In versions of omniORB before 4.0, extracting simultaneously from the same Any (in 2 or more different threads) also led to undefined behaviour. That is no longer the case—Any extraction is now thread safe.

## 11.3 TypeCode in omniORB

This section contains some notes on the use and behaviour of TypeCode in omniORB

### 11.3.1 TypeCodes in IDL.

When using TypeCodes in IDL, note that they are defined in the CORBA scope. Therefore, `CORBA::TypeCode` should be used. Example:

// IDL 5 struct Test5 { long length; CORBA::TypeCode desc; };

### 11.3.2 orb.idl

The CORBA specification says that IDL using `CORBA::TypeCode` must include the file `orb.idl`. That is not required in omniORB, but a suitable `orb.idl` is available.

### 11.3.3 Generating TypeCodes for constructed types.

To generate a TypeCode for constructed types, specify the `-Wba` command-line flag when running omniidl. This will generate a `_tc_` TypeCode describing the type, at the same scope as the type. Example:

// IDL 6 struct Test6 { double a; sequence<long> b; };

A TypeCode, `_tc_Test6`, will be generated to describe the struct `Test6`. The operations defined in the TypeCode interface can be used to query the TypeCode about the type it represents.

## 11.4 Source Listing

### 11.4.1 anyExample_impl.cc

// anyExample_impl.cc - This is the source code of the example used in // Chapter 9 "Type Any and TypeCode" of the omniORB // users guide. // // This is the object implementation. // // Usage: anyExample_impl // // On startup, the object reference is printed to cout as a // stringified IOR. This string should be used as the argument to // anyExample_clt. // #include <anyExample.hh> #ifdef HAVE_STD # include <iostream> using namespace std; #else # include <iostream.h> #endif class anyExample_i : public POA_anyExample { public: inline anyExample_i() {} virtual ~anyExample_i() {} virtual CORBA::Any* testOp(const CORBA::Any& a); }; CORBA::Any* anyExample_i::testOp(const CORBA::Any& a) { cout << "Any received, containing: " << endl; #ifndef NO_FLOAT CORBA::Double d; #endif CORBA::Long l; const char* str; testStruct* tp; if (a >>= l) { cout << "Long: " << l << endl; } #ifndef NO_FLOAT // XXX - should we provide stream ops for _CORBA_Double_ and // _CORBA_Float_on VMS?? else if (a >>= d) { cout << "Double: " << (double)d << endl; } #endif else if (a >>= str) { cout << "String: " << str << endl; } else if (a >>= tp) { cout << "testStruct: l: " << tp->l << endl; cout << " s: " << tp->s << endl; } else { cout << "Unknown value." << endl; } CORBA::Any* ap = new CORBA::Any; *ap <<= (CORBA::ULong) 314; cout << "Returning Any containing: ULong: 314\n" << endl; return ap; } ////////////////////////////////////////////////////////////////////// int main(int argc, char** argv) { try { CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); PortableServer::POA_var poa = PortableServer::POA::_narrow(obj); anyExample_i* myobj = new anyExample_i(); PortableServer::ObjectId_var myobjid = poa->activate_object(myobj); obj = myobj->_this(); CORBA::String_var sior(orb->object_to_string(obj)); cout << (char*)sior << endl; myobj->_remove_ref(); PortableServer::POAManager_var pman = poa->the_POAManager(); pman->activate(); orb->run(); orb->destroy(); } catch(CORBA::SystemException& ex) { cerr << "Caught CORBA::" << ex._name() << endl; } catch(CORBA::Exception& ex) { cerr << "Caught CORBA::Exception: " << ex._name() << endl; } catch(omniORB::fatalException& fe) { cerr << "Caught omniORB::fatalException:" << endl; cerr << " file: " << fe.file() << endl; cerr << " line: " << fe.line() << endl; cerr << " mesg: " << fe.errmsg() << endl; } return 0; }

### 11.4.2 anyExample_clt.cc

// anyExample_clt.cc - This is the source code of the example used in // Chapter 9 "Type Any and TypeCode" of the omniORB // users guide. // // This is the client. // // Usage: anyExample_clt <object reference> // #include <anyExample.hh> #ifdef HAVE_STD # include <iostream> using namespace std; #else # include <iostream.h> #endif static void invokeOp(anyExample_ptr& tobj, const CORBA::Any& a) { CORBA::Any_var bp; cout << "Invoking operation." << endl; bp = tobj->testOp(a); cout << "Operation completed. Returned Any: "; CORBA::ULong ul; if (bp >>= ul) { cout << "ULong: " << ul << "\n" << endl; } else { cout << "Unknown value." << "\n" << endl; } } static void hello(anyExample_ptr tobj) { CORBA::Any a; // Sending Long CORBA::Long l = 100; a <<= l; cout << "Sending Any containing Long: " << l << endl; invokeOp(tobj,a); // Sending Double #ifndef NO_FLOAT CORBA::Double d = 1.2345; a <<= d; cout << "Sending Any containing Double: " << d << endl; invokeOp(tobj,a); #endif // Sending String const char* str = "Hello"; a <<= str; cout << "Sending Any containing String: " << str << endl; invokeOp(tobj,a); // Sending testStruct [Struct defined in IDL] testStruct t; t.l = 456; t.s = 8; a <<= t; cout << "Sending Any containing testStruct: l: " << t.l << endl; cout << " s: " << t.s << endl; invokeOp(tobj,a); } ////////////////////////////////////////////////////////////////////// int main(int argc, char** argv) { try { CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); if( argc != 2 ) { cerr << "usage: anyExample_clt <object reference>" << endl; return 1; } { CORBA::Object_var obj = orb->string_to_object(argv[1]); anyExample_var ref = anyExample::_narrow(obj); if( CORBA::is_nil(ref) ) { cerr << "Can't narrow reference to type anyExample (or it was nil)." << endl; return 1; } hello(ref); } orb->destroy(); } catch(CORBA::TRANSIENT&) { cerr << "Caught system exception TRANSIENT -- unable to contact the " << "server." << endl; } catch(CORBA::SystemException& ex) { cerr << "Caught a CORBA::" << ex._name() << endl; } catch(CORBA::Exception& ex) { cerr << "Caught CORBA::Exception: " << ex._name() << endl; } catch(omniORB::fatalException& fe) { cerr << "Caught omniORB::fatalException:" << endl; cerr << " file: " << fe.file() << endl; cerr << " line: " << fe.line() << endl; cerr << " mesg: " << fe.errmsg() << endl; } return 0; }