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
Creating a DataWriter with your own QoS
Getting and setting DataWriter QoS
writer >> dwqos;
writer << dwqos;
writer.qos(dwqos);
Writing a sample
Foo::Type sample;
sample.long_1()=1;
sample.long_2()=2;
writer << sample;
writer.write(sample);
Writing with other items (timestamps, instance handles)
writer.write(sample, t1);
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
writer << std::make_pair(sample, ih);
Using the DataReader
Creating a DataReader with the default QoS
Creating a DataReader with your own QoS
Reading a sample
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
Reading samples with the Manipulator Selector
reader.take();
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
Finding entities (retained or built in)
std::vector<dds::sub::AnyDataReader> rv;
dds::sub::find<dds::sub::DataReader<Foo::Type> >(sub, topic.name(), rv.begin(), 1);
samples = rv[0].get<Foo::Type>().take();
dds::sub::find<dds::sub::DataReader<Foo::Type> >(sub, topic.name(), back_inserter(rv));
adr >> dds::sub::take >> samples;
Using a subset of listeners
class ExampleDataReaderListener :
{
...
Extending/inheriting a entity
template <class T>
{
public:
MyExtendedDataReader() :
dds::sub::DataReader<T>(
dds::core::null) {}
...