Cyclone ISO C++ API Reference Guide
ISO C++ example code snippets

Below you will find an assortment of small code examples which show you how to do various things with the ISO C++ API. Most of the snippets are written in a way that they would be compilable as one program and as such concepts already demonstrated are not repeated in subsequent snippets.

Using the DataWriter

Creating a DataWriter with default QoS

dds::domain::DomainParticipant dp(org::eclipse::cyclonedds::domain::default_id());
dds::topic::Topic<Foo::Type> topic(dp, "TypeTopic");

Creating a DataWriter with your own QoS

Getting and setting DataWriter QoS

writer >> dwqos;
writer << dwqos;
//or
writer.qos(dwqos);

Writing a sample

Foo::Type sample;
sample.long_1()=1;
sample.long_2()=2;
writer << sample;
//or
writer.write(sample);

Writing with other items (timestamps, instance handles)

dds::core::Time t1(20,0);
writer.write(sample, t1);
//or
writer << std::make_pair(sample, t1);

Writing from your own container

std::vector<Foo::Type> userSamples;
userSamples.push_back(sample);
writer.write(userSamples.begin(), userSamples.end());

Writing from your own samples container with your own instance handles

std::vector<dds::core::InstanceHandle> handles;
handles.push_back(writer.register_instance(sample));
writer.write(userSamples.begin(), userSamples.end(), handles.begin(), handles.end());

Register and instance handle and a write sample using it

dds::core::InstanceHandle ih(writer.register_instance(sample));
writer << std::make_pair(sample, ih);

Using the DataReader

Creating a DataReader with the default QoS

Creating a DataReader with your own QoS

dds::sub::qos::DataReaderQos drqos(topic.qos());
dds::sub::DataReader<Foo::Type> reader2(sub, topic, drqos);

Reading a sample

dds::sub::LoanedSamples<Foo::Type> samples = reader.read();
//Or
reader >> samples;

Reading samples into your own containers (Forward inserting Iterator)

std::vector<dds::sub::Sample<Foo::Type> > mySamples;
mySamples.resize(30);
unsigned int numRead = reader.read(mySamples.begin(), 30);

Reading samples into your own containers (Back inserting Iterator)

numRead = reader.read(back_inserter(mySamples));

Reading with a InstanceHandle (lookup key from sample)

ih = reader.lookup_instance(sample);
samples = reader.select().
instance(ih).
.read();

Reading samples with the Selector

dds::sub::LoanedSamples<Foo::Type> FooSamples = reader.select()
.instance(ih)
.read();

Reading samples with the Manipulator Selector

reader >> dds::sub::instance(ih) >> dds::sub::state(dds::sub::status::DataState::any_data()) >> samples;
//Take
reader.take();
//Or with the ManipulatorSelector
reader >> dds::sub::take >> samples;

Accessing the samples

sample < samples.end(); ++sample)
{
if (sample->info().valid())
{
std::cout << sample->data().long_1() << std::endl;
}
}

Accessing the samples (C++11)

for(auto s : reader.read())
{
std::cout << s->data().long_1() << std::endl;
}

Accessing the samples (C++11 lambda)

auto s = reader.read();
std::for_each(s.begin(), s.end(), [&]{ std::cout << s->data().long_1() << std::endl; });

Miscellaneous

Getting QoS values

dds::core::Duration lease_duration = drqos.policy<dds::core::policy::Liveliness>().lease_duration();

Finding entities (retained or built in)

//Finding one DataReader (Forward inserting iterator)
std::vector<dds::sub::AnyDataReader> rv;
rv.push_back(dds::sub::AnyDataReader(dds::core::null));
dds::sub::find<dds::sub::DataReader<Foo::Type> >(sub, topic.name(), rv.begin(), 1);
samples = rv[0].get<Foo::Type>().take();
//Finding all the DataReaders (Back inserting iterator)
dds::sub::find<dds::sub::DataReader<Foo::Type> >(sub, topic.name(), back_inserter(rv));
dds::sub::DataReader<Foo::Type> adr(rv[0].get<Foo::Type>());
adr >> dds::sub::take >> samples;

Using a subset of listeners

class ExampleDataReaderListener :
public virtual dds::sub::DataReaderListener<Foo::Type>,
public virtual dds::sub::NoOpDataReaderListener<Foo::Type>
{
...

Extending/inheriting a entity

template <class T>
class MyExtendedDataReader : public dds::sub::DataReader<T>
{
public:
MyExtendedDataReader() : dds::sub::DataReader<T>(dds::core::null) {}
...
dds::sub::Subscriber
A Subscriber is the object responsible for the actual reception of the data resulting from its subscr...
Definition: Subscriber.hpp:53
dds::sub::NoOpDataReaderListener
DataReader events Listener.
Definition: DataReaderListener.hpp:32
dds::sub::status::DataState::any_data
static DataState any_data()
Definition: DataState.hpp:648
dds::pub::qos::DataWriterQos
This object provides the basic mechanism for an application to specify Quality of Service attributes ...
Definition: DataWriterQos.hpp:68
dds::core::Duration
Definition: Duration.hpp:42
dds::core::InstanceHandle
Class to hold the handle associated with in sample instance.
Definition: InstanceHandle.hpp:40
dds::sub::LoanedSamples
This class encapsulates and automates the management of loaned samples.
Definition: LoanedSamples.hpp:87
dds::pub::Publisher
The Publisher acts on the behalf of one or several DataWriter objects that belong to it.
Definition: Publisher.hpp:55
dds::pub::DataWriter
DataWriter allows the application to set the value of the sample to be published under a given Topic.
Definition: DataWriter.hpp:89
dds
Definition: array.hpp:30
dds::sub::AnyDataReader
Typeless base class for the typed DataReader.
Definition: AnyDataReader.hpp:50
dds::sub::DataReaderListener
DataReader events Listener.
Definition: DataReader.hpp:37
dds::core::policy::Durability::Volatile
static Durability Volatile()
dds::core::policy::WriterDataLifecycle::ManuallyDisposeUnregisteredInstances
static WriterDataLifecycle ManuallyDisposeUnregisteredInstances()
dds::core::policy::Deadline
Definition: CorePolicy.hpp:410
dds::core::policy::Liveliness
Definition: CorePolicy.hpp:1230
dds::sub::DataReader
DataReader allows the applicatin to access published sample data.
Definition: DataReader.hpp:34
dds::topic::Topic
Topic is the most basic description of the data to be published and subscribed.
Definition: Topic.hpp:36
dds::sub::qos::DataReaderQos
This class provides the basic mechanism for an application to specify Quality of Service attributes f...
Definition: DataReaderQos.hpp:66
dds::sub::LoanedSamples::const_iterator
DELEGATE< T >::const_iterator const_iterator
Definition: LoanedSamples.hpp:98
dds::core::Time
Definition: Time.hpp:42
dds::core::policy::ResourceLimits
Definition: CorePolicy.hpp:1159
dds::core::Duration::from_microsecs
static const Duration from_microsecs(int64_t microseconds)
dds::domain::DomainParticipant
A DomainParticipant represents the local membership of the application in a Domain.
Definition: DomainParticipant.hpp:65