class template c++ example for many topics

7 posts / 0 new
Last post
Last seen: 5 years 6 months ago
Joined: 10/28/2010
Posts: 22
class template c++ example for many topics



Does anyone could give help about using c++ template class  to create several topics, because I need to create more then 30 topics with different fields (struct elements) and with different  settings (QoS), doing this repeatedly is error prone, so it looks like templates can help.




Last seen: 9 years 4 months ago
Joined: 10/07/2011
Posts: 2

have a look at SIMD dds api. They specify topic as a template class.

Last seen: 9 years 4 months ago
Joined: 10/07/2011
Posts: 2

have a look at SIMD dds api. They specify topic as a template class.

Last seen: 5 years 6 months ago
Joined: 10/17/2011
Posts: 2



The biggest problem I've seen when trying to use types generically is decoupling use code from the syntactic name of the data type.  That is, how do you avoid writing "FooDataWriter" or "SensorDataDataWriter" everywhere?  Here's a snippet that may help:


// Hide macro concatatenation in template

template <typename T> struct MyTypeWrapper { };

#define MYWTYPEDEF(type) typedef type##DataWriter Writer
#define MYRTYPEDEF(type) typedef type##DataReader Reader
#define MYSTYPEDEF(type) typedef type##TypeSupport Support
#define MYQTYPEDEF(type) typedef type##Seq Seq

#define DeclareMyTypeWrapper(t)        \
template <> struct MyTypeWrapper <t> { \
    MYWTYPEDEF(t);                     \
    MYRTYPEDEF(t);                     \
    MYSTYPEDEF(t);                     \
    MYQTYPEDEF(t);                     \
    typedef t Sample;                  \


// Include generated code and declare type-specific class

#include "Foo.h"
#include "FooPlugin.h"
#include "FooSupport.h"



// Generic code, error checking ommitted


template<typename T>
typename MyTypeWrapper<T>::Writer *
make_writer(DomainParticipant *participant, Publisher *publisher,
            const char* topic_name) {
    ReturnCode_t retcode;
    Topic *topic = NULL;
    DataWriter *writer = NULL;
    typename MyTypeWrapper<T>::Writer *typed_writer = NULL;
    const char *type_name = NULL;

    type_name = MyTypeWrapper<T>::Support::get_type_name();
    retcode = MyTypeWrapper<T>::Support::register_type(
        participant, type_name);

    topic = participant->create_topic(
        type_name, TOPIC_QOS_DEFAULT, NULL /* listener */,

    writer = publisher->create_datawriter(

    typed_writer = MyTypeWrapper<T>::Writer::narrow(writer);
    return typed_writer;


// Elsewhere you could then call

make_writer<SensorData>(participant, publisher, "SensorData Topic");




Fernando Garcia's picture
Last seen: 3 months 2 weeks ago
Joined: 05/18/2011
Posts: 199

In the upcoming release, 4.5f, we are going to add support for the use of C++ traits on each top level type generated by rtiddsgen. The use of this new feature will allow definitions like:

template <typename T>
void template_example(const T& data)
   T::DataWriter * writer = publisher->create_datawriter(...);
   writer->write(data, ...);
   T::Sequence dataSeq;
   T::DataReader reader;
   reader->take(dataSeq, ...);

void main() 
   Foo foo;
   Bar bar;
Gerardo Pardo's picture
Last seen: 4 months 3 days ago
Joined: 06/02/2010
Posts: 594

Hello Akram,

In addition to Fernando's suggestion.  Have you looked into the DDS Application Creation using XML feature that was included in RTI Connext DDS version 5.0.0? This feature allows you to specify all the DataWriters (and DataReaders for that matter) you want to create in an XML file. Once you do this they will be created automatically at the time you create your DomainParticipant.  


Last seen: 1 week 4 days ago
Joined: 04/02/2013
Posts: 158

Since this topic was posted RTI has released a brand-new modern C++ API that makes generic programming much easier.