Setup required for the following code snippets:
#include <dds/domain/ddsdomain.hpp>
#include <dds/pub/ddspub.hpp>
#include "foo.hh"
Create a DataWriter
Create a DataWriter with all default QoS policies and no listener.
Create a DataWriter with specified QoS policies and no listener.
Create a DataWriter with specified QoS policies and a listener.
template<typename T>
public:
{ std::cout << "Offered Deadline Missed" << std::endl; }
};
ExampleDWListener<foo::ExampleType> dw_listener;
Using a DataWriter
Register an instance:
foo::ExampleType data;
data.theKey = 100;
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;
dds::topic::TopicInstance<foo::ExampleType> instance_key;
writer.key_value( instance_key, ihandle );
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:
writer.write( data );
writer.write( data, timestamp );
writer.write( data, handle );
dds::topic::TopicInstance<foo::ExampleType> ti(handle, data);
Sending multiple 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() );
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(),
Write via the stream operator:
writer << data;
writer << std::make_pair( msg, ihandle );
Tracking writer status with a listener:
template <typename 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;
}
};
ExampleDWListener< foo::ExampleType > * listener = new ExampleDWListener<foo::ExampleType>();