class template c++ example for many topics

7 posts / 0 new
Last post
Offline
Last seen: 4 years 1 week ago
Joined: 10/28/2010
Posts: 22
class template c++ example for many topics

Hello,

 

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.

Thanks

 

Akram

Offline
Last seen: 7 years 10 months ago
Joined: 10/07/2011
Posts: 2

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

Offline
Last seen: 7 years 10 months ago
Joined: 10/07/2011
Posts: 2

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

Offline
Last seen: 3 years 12 months ago
Joined: 10/17/2011
Posts: 2

Hi,

 

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"

DeclareMyTypeWrapper(Foo);

 

// 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(
        topic_name,
        type_name, TOPIC_QOS_DEFAULT, NULL /* listener */,
        STATUS_MASK_NONE);

    writer = publisher->create_datawriter(
        topic, DDS_DATAWRITER_QOS_DEFAULT,
        NULL, STATUS_MASK_NONE);

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

 

// Elsewhere you could then call

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

 

Best,

Shaun

Fernando Garcia's picture
Offline
Last seen: 1 month 1 week ago
Joined: 05/18/2011
Posts: 194

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::TypeSupport::print_data(&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;
   template_example<Foo>(foo);
   template_example<Bar>(bar);
}
Gerardo Pardo's picture
Offline
Last seen: 2 weeks 17 hours ago
Joined: 06/02/2010
Posts: 587

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.  

Gerardo

Offline
Last seen: 1 week 1 day ago
Joined: 04/02/2013
Posts: 109

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