Previous Up Next

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. The example code is in the src/examples/anyExample directory in the omniORB distribution. The example 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.

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. For example:

// 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 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.


Previous Up Next