Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Advanced CORBA Programming wit C++ - M. Henning, S. Vinoski.pdf
Скачиваний:
65
Добавлен:
24.05.2014
Размер:
5 Mб
Скачать

IT-SC book: Advanced CORBA® Programming with C++

The IDL compiler generates a separate overloaded <<= operator for every system and user exception, so you can insert an exception just as you insert any other data type. Both copying and consuming insertion is provided:

CORBA::Any a;

bp;

// Exception on the stack

CORBA::BAD_PARAM

CORBA::PERSIST_STORE *

ps_p;

// Pointer to exception

a <= bp;

//

Copying insertion

ps_p = new CORBA::PERSIST_STORE;

// Exception on the heap

a <= ps_p;

//

Consuming insertion

As with other complex data types, extraction from an Any is by pointer:

CORBA::Any a;

 

CORBA::BAD_PARAM bp;

// Insert exception

a <= bp;

CORBA::BAD_PARAM * ep;

// Extract it again

a >>= ep;

The usual rules for data extracted by pointer apply: you must treat the extracted pointer as read-only, and the Any retains ownership of the exception.

You can also insert exceptions generically as the CORBA::Exception base type:

try {

// ...

}

catch (const CORBA::Exception & e) {

CORBA::Any a;

// Insert caught exception

a <= e;

}

 

If you insert an exception as the base type CORBA::Exception, the actual type of the exception is preserved by the Any's type code. For example, if the actual type of the inserted exception is CORBA::BAD_PARAM, you can later extract it as that type. (Generic insertion of exceptions is provided by the C++ mapping to support servers using the DSI.) Note that you cannot extract an exception as a base type, such as

CORBA::Exception, because it does not make sense: CORBA::Exception is an abstract base class that cannot be instantiated.

15.4 Pitfalls in Type Definitions

The C++ mapping currently does not allow you to control the precise type code if an IDL definition contains type definitions. For example, the climate control system contains the following type definitions:

module CCS {

ModelType;

typedef string

typedef string

LocType;

598

IT-SC book: Advanced CORBA® Programming with C++

// ...

};

Now consider the following C++ code fragment, which inserts model and location strings into an Any:

CCS::ModelType model = "BFG9000";

CCS::LocType location = "Room 414";

CORBA::Any

model_any;

 

CORBA::Any

location_any;

 

model_any <<= model;

// Insert model

location_any <<= location;

// Insert location

if (model_any >>= location)

// Succeeds!

// ...

 

// Succeeds!

if (location_any >>= model)

// ...

 

 

The problem here is that we can successfully extract a model string as a location and extract a location string as a model. This happens because the C++ mapping maps both ModelType and LocationType to char *. Model and location strings are therefore both inserted by the same single overloaded operator. (It must be this way because C++ does not permit overloading on types that are typedefs to the same underlying type.) The Any into which we insert the strings therefore contains a type code that indicates "string" and contains no information as to whether the inserted string originally was a model or a location.

You can insert values into an Any so that aliases are preserved if you use the DynAny interface. It is also possible to distinguish whether an Any contains a ModelType or a LocationType during extraction. We show an example of how to do this in Section 16.7.

15.5 Summary

Type any permits type-safe insertion and extraction of arbitrary types. Using type any lets you create generic interfaces with operations that permit arbitrary types to be passed. In addition, you can use type any to simulate variable-length parameter lists for operations. A major use of type any is in the OMG Event Service, which we discuss in

Chapter 20.

599