What is the purpose of the narrow() method created for a type by rtiddsgen?

Note: Applies to RTI Connext 4.x and above

The narrow method is a class-scope method that implements a safe down-cast for a pointer to an RTI Connext entity. It is a type-safe way to cast a pointer. The Java API does not require this method because Java casts are always type-safe.

This method takes a DDSEntity ( DDSDataWriter, DDSDataReader, etc.) pointer and casts it to a specific type pointer ( FooDataWriter, FooDataReader, etc.). In this case, Foo is the related data type. The return value from narrow will either be a valid pointer to the requested type, or NULL if the cast is invalid. This will happen for example if FooDataReader::narrow() is called with a DDSDataReader pointer to an object that is actually of class BarDataReader.

The most common usage is in an on_data_available callback for the DataReader: 

FooReaderListener::on_data_available(DDSDataReader *reader, ...);

FooDataReader *foo_reader = FooDataReader::narrow(reader);
... 

Note that there were problems with the narrow method for the built-in data reader classes in the 4.0g release; these were fixed in 4.0i.

Programming Language: