Create a DataReader
A DataReader always belongs to a Subscriber.
Creation of a DataReader is done with the Subscriber::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.
Optional arguments are:
A Listener derived from DataReader Listener, implementing the callbacks that will be triggered in response to events and state changes on the DataReader.
A
StatusMaskthat activates or deactivates triggering of individual callbacks on the DataReaderListener.
Subscriber::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.
Example
// Definition of a DataReader QoS for a reliable DataReader
DataReaderQos datareader_qos{};
datareader_qos.reliability().kind = ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS;
// Instantiation of a custom DataReaderListener
CustomDataReaderListener datareader_listener;
// Creation of a StatusMask
StatusMask datareader_mask = ANY_STATUS_MASK;
// Remove a unwanted status
status_mask_unset(datareader_mask, REQUESTED_INCOMPATIBLE_QOS_STATUS);
// Creation of a DataReader
DataReader* datareader = subscriber->create_datareader(
*topic,
datareader_qos,
&datareader_listener,
datareader_mask);
// Check if the DataReader was created
if (nullptr == datareader)
{
std::cout << "Error creating DataReader" << std::endl;
}
Static memory example
In order to create a DataReader that does not use any platform allocation a StaticDataReader can be instantiated:
Note
When using Safe DDS Static memory API, any Policy related with PreallocMemoryConfig in DataReaderQos will be ignored.
constexpr uint32_t DATAREADER_MAX_REMOTE_WRITERS = 1;
constexpr uint32_t DATAREADER_MAX_LOCAL_WRITERS = 1;
constexpr uint32_t DATAREADER_MAX_INSTANCES = 10;
constexpr uint32_t DATAREADER_MAX_SAMPLES_PER_INSTANCE = 100;
StaticDataReader<
CustomTypeSupport,
DATAREADER_MAX_REMOTE_WRITERS,
DATAREADER_MAX_LOCAL_WRITERS,
DATAREADER_MAX_INSTANCES,
DATAREADER_MAX_SAMPLES_PER_INSTANCE>
static_datareader(
static_subscriber,
static_topic,
datareader_qos,
&datareader_listener,
datareader_mask);