Publishing data

Data instances can be updated with the TypedDataWriter::write method. This change will be communicated to every DataReader matched with the DataWriter. As a side effect, this operation asserts liveliness on the DataWriter itself, the Publisher and the DomainParticipant.

Using the TypedDataWriter

Safe DDS Usage

In order to provide a type safe API, Safe DDS provides a TypedDataWriter class.

TypedDataWriter class is a specialization of the DataWriter class, and it is obtained by calling the TypedDataWriter::downcast method and its constructed using a Typesupport class that must be previously registered in the DomainParticipant with a Topic name that matches the one used to create the DataWriter.

Once the TypedDataWriter reference is obtained, the write method can be used to publish data.

The write function takes two arguments:

  • A reference to the data instance with the new values.

  • The handler to the instance.

An empty (i.e., default constructed InstanceHandle or HANDLE_NIL) instance handler can be used for the argument handle. This indicates that the identity of the instance should be automatically deduced from the key of the instance data.

If the handle is not empty, then it must correspond to the value obtained with the TypedDataWriter::lookup_instance of the data. Otherwise the write function will fail with PRECONDITION_NOT_MET.

Safe DDS Usage

If Safe DDS Static API is used, there is no need to use the TypedDataWriter::downcast method, since the StaticDataWriter is already a TypedDataWriter.


// Obtain a reference to a TypedDataWriter
auto typedwriter = TypedDataWriter<CustomTypeSupport>::downcast(*datawriter);

// Check if the TypedDataWriter was obtained
if (nullptr == typedwriter)
    std::cout << "Error obtaining TypedDataWriter" << std::endl;

// Create a sample
CustomTypeSupport::DataType sample = {};
sample.index = 1;

// Write the sample
dds::ReturnCode ret = typedwriter->write(sample, HANDLE_NIL);

// Check if the sample was written
if (dds::ReturnCode::OK != ret)
    std::cout << "Error writing sample" << std::endl;