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
have a look at SIMD dds api. They specify topic as a template class.
have a look at SIMD dds api. They specify topic as a template class.
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
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:
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
Since this topic was posted RTI has released a brand-new modern C++ API that makes generic programming much easier.