3.4.6. Creating a DataReader¶
A DataReader always belongs to a Subscriber.
Creation of a DataReader is done with the create_datareader()
member function on the
Subscriber instance, that acts as a factory for the DataReader.
Mandatory arguments are:
A Topic bound to the data type that will be transmitted.
The DataReaderQos describing the behavior of the DataReader. If the provided value is
DATAREADER_QOS_DEFAULT
, the value of the Default DataReaderQos is used.
Optional arguments are:
A Listener derived from DataReaderListener, implementing the callbacks that will be triggered in response to events and state changes on the DataReader. By default empty callbacks are used.
A
StatusMask
that activates or deactivates triggering of individual callbacks on the DataReaderListener. By default all events are enabled.
create_datareader()
will return a null pointer if there was an error during the operation, e.g.
if the provided QoS is not compatible or is not supported.
It is advisable to check that the returned value is a valid pointer.
// Create a DataReader with default DataReaderQos and no Listener
// The value DATAREADER_QOS_DEFAULT is used to denote the default QoS.
DataReader* data_reader_with_default_qos =
subscriber->create_datareader(topic, DATAREADER_QOS_DEFAULT);
if (nullptr == data_reader_with_default_qos)
{
// Error
return;
}
// A custom DataReaderQos can be provided to the creation method
DataReaderQos custom_qos;
// Modify QoS attributes
// (...)
DataReader* data_reader_with_custom_qos =
subscriber->create_datareader(topic, custom_qos);
if (nullptr == data_reader_with_custom_qos)
{
// Error
return;
}
// Create a DataReader with default QoS and a custom Listener.
// CustomDataReaderListener inherits from DataReaderListener.
// The value DATAREADER_QOS_DEFAULT is used to denote the default QoS.
CustomDataReaderListener custom_listener;
DataReader* data_reader_with_default_qos_and_custom_listener =
subscriber->create_datareader(topic, DATAREADER_QOS_DEFAULT, &custom_listener);
if (nullptr == data_reader_with_default_qos_and_custom_listener)
{
// Error
return;
}
3.4.6.1. Profile based creation of a DataReader¶
Instead of using a DataReaderQos, the name of a profile
can be used to create a DataReader with the create_datareader_with_profile()
member function on the Subscriber instance.
Mandatory arguments are:
A Topic bound to the data type that will be transmitted.
A string with the name that identifies the DataReader.
Optional arguments are:
A Listener derived from DataReaderListener, implementing the callbacks that will be triggered in response to events and state changes on the DataReader. By default empty callbacks are used.
A
StatusMask
that activates or deactivates triggering of individual callbacks on the DataReaderListener. By default all events are enabled.
create_datareader_with_profile()
will return a null pointer if there was an error during the operation,
e.g. if the provided QoS is not compatible or is not supported.
It is advisable to check that the returned value is a valid pointer.
Note
XML profiles must have been loaded previously. See Loading profiles from an XML file.
// First load the XML with the profiles
DomainParticipantFactory::get_instance()->load_XML_profiles_file("profiles.xml");
// Create a DataReader using a profile and no Listener
DataReader* data_reader_with_profile =
subscriber->create_datareader_with_profile(topic, "data_reader_profile");
if (nullptr == data_reader_with_profile)
{
// Error
return;
}
// Create a DataReader using a profile and a custom Listener.
// CustomDataReaderListener inherits from DataReaderListener.
CustomDataReaderListener custom_listener;
DataReader* data_reader_with_profile_and_custom_listener =
subscriber->create_datareader_with_profile(topic, "data_reader_profile", &custom_listener);
if (nullptr == data_reader_with_profile_and_custom_listener)
{
// Error
return;
}
3.4.6.2. Deleting a DataReader¶
A DataReader can be deleted with the delete_datareader()
member function on the
Subscriber instance where the DataReader was created.
// Create a DataReader
DataReader* data_reader =
subscriber->create_datareader(topic, DATAREADER_QOS_DEFAULT);
if (nullptr == data_reader)
{
// Error
return;
}
// Use the DataReader to communicate
// (...)
// Delete the DataReader
if (subscriber->delete_datareader(data_reader) != ReturnCode_t::RETCODE_OK)
{
// Error
return;
}