Create a Publisher

A Publisher always belongs to a DomainParticipant. Creation of a Publisher is done with the DomainParticipant::create_publisher member function on the DomainParticipant instance, that acts as a factory for the Publisher.

Mandatory arguments are:

  • The PublisherQos describing the behavior of the Publisher.

Optional arguments are:

  • A Listener derived from Publisher Listener, implementing the callbacks that will be triggered in response to events and state changes on the Publisher.

  • A StatusMask that activates or deactivates triggering of individual callbacks on the PublisherListener.

DomainParticipant::create_publisher 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 Publisher QoS
PublisherQos publisher_qos{};

// Instantiation of a custom PublisherListener
CustomPublisherListener publisher_listener{};

// Creation of a StatusMask
StatusMask publisher_mask = ANY_STATUS_MASK;

// Remove a unwanted status
status_mask_unset(publisher_mask, PUBLICATION_MATCHED_STATUS);

// Creation of a Publisher
Publisher* publisher = participant->create_publisher(
    publisher_qos,
    &publisher_listener,
    publisher_mask);

// Check if the Publisher was created
if (nullptr == publisher)
{
    std::cout << "Error creating Publisher" << std::endl;
}

Static memory example

In order to create a Publisher that does not perform any allocation a StaticPublisher can be instantiated:

Note

When using Safe DDS Static memory API, any Policy related with PreallocMemoryConfig in PublisherQos will be ignored.

StaticPublisher static_publisher(
    static_participant,
    publisher_qos,
    &publisher_listener,
    publisher_mask);