Create a DataWriter¶
A DataWriter always belongs to a Publisher.
Creation of a DataWriter is done with the Publisher::create_datawriter member function on the Publisher instance, that acts as a factory for the DataWriter.
Mandatory arguments are:
A Topic bound to the data type that will be transmitted.
The DataWriterQos describing the behavior of the DataWriter.
Optional arguments are:
A Listener derived from DataWriter Listener, implementing the callbacks that will be triggered in response to events and state changes on the DataWriter.
A
StatusMaskthat activates or deactivates triggering of individual callbacks on the DataWriterListener.
Publisher::create_datawriter 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 DataWriter QoS for a reliable DataWriter
DataWriterQos datawriter_qos{};
datawriter_qos.reliability().kind = ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS;
// Instantiation of a custom DataWriterListener
CustomDataWriterListener datawriter_listener{};
// Creation of a StatusMask
StatusMask datawriter_mask = ANY_STATUS_MASK;
// Remove a unwanted status
status_mask_unset(datawriter_mask, OFFERED_INCOMPATIBLE_QOS_STATUS);
// Creation of a DataWriter
DataWriter* datawriter = publisher->create_datawriter(
*topic,
datawriter_qos,
&datawriter_listener,
datawriter_mask);
// Check if the DataWriter was created
if (nullptr == datawriter)
{
std::cout << "Error creating DataWriter" << std::endl;
}
Static memory example¶
In order to create a DataWriter that does not perform any allocation a StaticDataWriter can be instantiated:
Note
When using Safe DDS Static memory API, any Policy related with PreallocMemoryConfig in DataWriterQos will be ignored.
constexpr uint32_t DATAWRITER_MAX_REMOTE_READERS = 1;
constexpr uint32_t DATAWRITER_MAX_INSTANCES = 10;
constexpr uint32_t DATAWRITER_MAX_SAMPLES_PER_INSTANCE = 100;
StaticDataWriter<
CustomTypeSupport,
DATAWRITER_MAX_REMOTE_READERS,
DATAWRITER_MAX_INSTANCES,
DATAWRITER_MAX_SAMPLES_PER_INSTANCE>
static_datawriter(
static_publisher,
static_topic,
datawriter_qos,
&datawriter_listener,
datawriter_mask);