Create a Subscriber

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

Mandatory arguments are:

Optional arguments are:

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

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

DomainParticipant::create_subscriber 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 Subscriber QoS
SubscriberQos subscriber_qos{};

// Instantiation of a custom SubscriberListener
CustomSubscriberListener subscriber_listener{};

// Creation of a StatusMask
StatusMask subscriber_mask = ANY_STATUS_MASK;

// Remove a unwanted status
status_mask_unset(subscriber_mask, SUBSCRIPTION_MATCHED_STATUS);

// Creation of a Subscriber
Subscriber* subscriber = participant->create_subscriber(
    subscriber_qos,
    &subscriber_listener,
    subscriber_mask);

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

Static memory example

In order to create a Subscriber that does not use any platform allocation a StaticSubscriber can be instantiated:

Note

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

StaticSubscriber static_subscriber(
    static_participant,
    subscriber_qos,
    &subscriber_listener,
    subscriber_mask);