CoreDX DDS Modern C++ API
DataWriter Use Cases

Setup required for the following code snippets:

#include <dds/domain/ddsdomain.hpp>
#include <dds/pub/ddspub.hpp>
#include "foo.hh" // declares the foo types used in the examples
dds::topic::Topic< foo::ExampleType > topic( participant, "ExampleTopic" );
dds::pub::Publisher publisher( participant );

Create a DataWriter

Create a DataWriter with all default QoS policies and no listener.

dds::pub::DataWriter< foo::ExampleType > writer( publisher, topic );

Create a DataWriter with specified QoS policies and no listener.

Create a DataWriter with specified QoS policies and a listener.

template<typename T>
class ExampleDWListener : public dds::pub::NoOpDataWriterListener<foo::ExampleType> {
public:
{ std::cout << "Offered Deadline Missed" << std::endl; }
};
ExampleDWListener<foo::ExampleType> dw_listener;
dds::pub::DataWriter< foo::ExampleType > writer( publisher, topic, dw_qos, &dw_listener, dds::core::status::StatusMask::all() );
// NOTE: remove the listener before the listener goes out of scope because it lives on the stack in this example
writer.listener( NULL, dds::core::status::StatusMask::none() );

Using a DataWriter

Register an instance:

foo::ExampleType data;
data.theKey = 100;
dds::core::InstanceHandle ihandle = writer.register_instance( data );

Get the values of the 'key' fields of an instance

foo::ExampleType data_key;
writer.key_value( data_key, ihandle );
std::cout << "Key value: " << data_key.theKey << std::endl;
// Or, using TopicInstance:
dds::topic::TopicInstance<foo::ExampleType> instance_key;
writer.key_value( instance_key, ihandle );
// then access the key member[s] ('theKey', in this example) like this:
std::cout << "Key value: " << instance_key.sample().theKey << std::endl;

Unregister an instance:

writer.unregister_instance( ihandle );

Dispose an instance:

writer.dispose_instance( ihandle );

Sending a sample:

// write a sample, middleware will apply the current source timestamp
writer.write( data );
// write a sample with the provided timestamp
dds::core::Time timestamp( 10 );
writer.write( data, timestamp );
// write a sample with handle
// note: if the handle does not match 'data', then the middleware
// may raise and InvalidArgumentError exception
writer.write( data, handle );
// write a sample with handle and timestamp
writer.write( data, handle, dds::core::Time(124) );
// or, the same, using TopicInstance:
dds::topic::TopicInstance<foo::ExampleType> ti(handle, data);
writer.write( ti, dds::core::Time(124) );

Sending multiple samples:

// write a vector of samples
std::vector<foo::ExampleType> samples( 3, foo::ExampleType() );
samples[0].theKey = 1;
samples[1].theKey = 2;
samples[2].theKey = 3;
writer.write( samples.begin(), samples.end() );
// write a vector of samples with a timestamp
writer.write( samples.begin(), samples.end(), dds::core::Time(125) );
// create a matching vector of 'handles' and include that in the write
std::vector<dds::core::InstanceHandle> handles( 3 );
handles[0] = test_dw.register_instance( samples[0] );
handles[1] = test_dw.register_instance( samples[1] );
handles[2] = test_dw.register_instance( samples[2] );
writer.write( samples.begin(), samples.end(),
handles.begin(), handles.end(), dds::core::Time(126) );

Write via the stream operator:

// stream write, using current timestamp
writer << data;
// stream write with provided timestamp
writer << std::make_pair( data, dds::core::Time(127) );
// stream write with provided handle
writer << std::make_pair( msg, ihandle );

Tracking writer status with a listener:

template <typename T>
class ExampleDWListener : public dds::pub::NoOpDataWriterListener<T>
{
public:
ExampleDWListener() {}
public:
{
std::cout << "on_offered_deadline_missed callback" << std::endl;
std::cout << "total_count : " << status.total_count() << std::endl;
std::cout << "total_count_change : " << status.total_count_change() << std::endl;
std::cout << "last_instance_handle: " << status.last_instance_handle() << std::endl;
}
{
std::cout << "on_offered_incompatible_qos callback" << std::endl;
std::cout << "total_count : " << status.total_count() << std::endl;
std::cout << "total_count_change: " << status.total_count_change() << std::endl;
std::cout << "last_policy_id : " << status.last_policy_id() << std::endl;
for (const auto& policy : status.policies()) {
std::cout << " policy_id : " << policy.policy_id() << ": " << policy.count() << std::endl;
}
}
{
std::cout << "on_liveliness_lost callback" << std::endl;
std::cout << "total_count : " << status.total_count() << std::endl;
std::cout << "total_count_change: " << status.total_count_change() << std::endl;
}
{
std::cout << "on_publication_matched callback" << std::endl;
std::cout << "total_count : " << status.total_count() << std::endl;
std::cout << "total_count_change : " << status.total_count_change() << std::endl;
std::cout << "current_count : " << status.current_count() << std::endl;
std::cout << "current_count_change : " << status.current_count_change() << std::endl;
std::cout << "last_subscription_handle: " << status.last_subscription_handle() << std::endl;
}
};
// examples of creating a writer with a listener were shown above.
// install a listener on an existing writer:
ExampleDWListener< foo::ExampleType > * listener = new ExampleDWListener<foo::ExampleType>();
writer.listener( listener, dds::core::status::StatusMask::all() );

© 2009-2020 Twin Oaks Computing, Inc
Castle Rock, CO 80104
All rights reserved.