Sumant Tambe, Ph.D.
Senior Software Research Engineer and Microsoft MVP
Real-Time Innovations, Inc.
www.rti.com
SILICON VALLEY
10/2/2013 © 2012 RTI 2
Data-Centric Communication Model in DDS
Data Distribution Service
10/2/2013 © 2012 RTI 3
• Data Distribution Service
– Middleware knows the schema
– Messages represent update to data-objects
– Data-Objects identified by a key
– Middleware maintains state of each object
– Objects are cached. Applications can read at leisure
– Smart QoS (Reliability, Ownership, History (per key), Deadline, and 18 more!)
Streaming
Data
Sensors Events
Real-Time
Applications
Enterprise
Applications
Actuators
Data-Centric  Typed All the Way!
10/2/2013 © 2013 RTI 4
Standard
Mapping
Java
C/C++
C#
Type Descriptions
in DDS-XTypes
(IDL, XSD, Java etc.)
Code
Generator
• Types description similar to an OO language
– Primitives, strings, arrays and sequences, maps, structures,
enumerations, aliases (typedefs), unions, modules, etc.
• Type versioning and assignability
• Meta-Data
– Unique ID for each member
– Annotations (like in Java)
– Key/non-key
– Optional/required members
– Sharable/non-sharable
DDS X-Types Standard
• Type System: DDS data objects have a type and obey the rules described
herein
• Language Binding for Object Manipulation
– Generate code when type-descriptions are available at compile-time
– Otherwise, create/inspect objects of any type at run-time
• Language Binding for Type Manipulation
– add/remove/query data members types (think Java reflection)
• Data Representation: Objects can be serialized for file storage and
network transmission
• Type Representation: Types can be serialized for file storage and network
transmission (using TypeObject) 5
Type System
Type
Representation
Language
Binding
Data
Representation
Example
6
Type
Representation
Language
Binding
Data
Representation
IDL: Foo.idl
struct Foo {
string name;
@key long ssn;
@optional string
phone;
};
IDL to C++ Language Mapping:
struct Foo {
std::string name;
int ssn;
std::string * phone;
// ...
};
IDL to CDR:
00000006
68656C6C
6F000000
00000002
Two Faces of DDS
10/2/2013 © 2013 RTI 7
• Application Integration
– Many applications, many programming languages, many
contractors necessitate a common format for sharing types
– IDL serves well
– Related technologies: CORBA Interfaces, schema definitions (DB),
XML document structure (XSD)
• Messaging
– Typically, a single (distributed) application
– Likely developed using a single programming language
– Overall system under single administration
– Data already stored in application data structures
– Just send it from point A to B
– Related technologies: ActiveMQ, JMS
– IDL not so much desirable
IDL may be a Distraction (for messaging apps)
• “… But I already have my headers…”
– Maintaining redundant IDL type descriptions is a burden
– Integrating generated code in app build system could be burdensome
– Must write glue code
• Or sometimes it is an overhead
– Copy data back and forth between app data types and IDL data types
• Application level data structures may not be captured
accurately
– Think XSD (e.g., sequence of choices)
10/2/2013 © 2013 RTI 8
Solution: A Pure C++11 Library-based Approach
Example:
10/2/2013 © 2012 RTI 9
// shape.h
struct ShapeType
{
std::string color;
int x;
int y;
unsigned shapesize;
};
// shape.h
RTI_ADAPT_STRUCT(
ShapeType,
(std::string, color, KEY)
(int, x)
(int, y)
(unsigned, shapesize))
DDSDomainParticipant * participant = ...
GenericDataWriter<ShapeType> shapes_writer(participant, "Square");
for(;;) {
ShapeType shape {“BLUE”, x, y, 30};
shapes_writer.write(shape);
}
Solution: A Pure C++11 Library-based Approach
Example:
10/4/2013 © 2012 RTI 10
// shape.h
struct ShapeType
{
std::string color;
int x;
int y;
unsigned shapesize;
};
// shape.h
RTI_ADAPT_STRUCT(
ShapeType,
(std::string, color, KEY)
(int, x)
(int, y)
(unsigned, shapesize))
@Extensibility(EXTENSIBLE)
struct ShapeType {
@key string color;
long x;
long y;
unsigned long shapesize;
};
Instead
IDL
A substitute for lack of reflection in C++
Read/Take Shapes (note C++11)
10/2/2013 © 2013 RTI 11
class MyShapesListener : public GenericDataReaderListener<ShapeType> {
public:
void on_data_available(GenericDataReader<ShapeType> & dr) override {
std::vector<ShapeType> samples = dr.take();
for (auto const &shape : samples) {
std::cout << “color = " << shape.color << “n”
<< "x = " << shape.x << “n”
<< "y = " << shape.y << “n”
<< "shapesize = " << shape.shapesize << “n”
<< std::endl;
}
}
};
DDSDomainParticipant * participant = ...
MyShapesListener listener;
std::string topic = “Square”;
GenericDataReader<ShapeType> shapes_reader(participant, &listener, topic);
for(;;)
sleep(1);
Slightly Complex Example
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 12
struct VetoTDCHit {
int hit_index;
int pmt_index; // not sent
float pmt_theta;
float pmt_phi;
};
typedef std::list<VetoTDCHit> VetoTDCHits;
struct VetoTruth
{
int sim_event;
VetoTDCHits hits;
};
enum STATUS_FLAGS { NORMAL=0,
ID_MISMATCH=1,
BAD_TIMESTAMP=2 };
struct EventInfo {
VetoTruth truth;
int event_id;
STATUS_FLAGS status;
};
RTI_ADAPT_STRUCT( // same header
VetoTDCHit,
(int, hit_index)
(float, pmt_theta)
(float, pmt_phi))
RTI_ADAPT_STRUCT(
VetoTruth,
(int, sim_event)
(VetoTDCHits, hits))
RTI_ADAPT_ENUM(
STATUS_FLAGS,
(NORMAL, 0)
(ID_MISMATCH, 1)
(BAD_TIMESTAMP, 2))
RTI_ADAPT_STRUCT(
EventInfo,
(VetoTruth, truth)
(int, event_id, KEY)
(TATUS_FLAGS, status))
Equivalent Type at Run-time
10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 13
@Extensibility(EXTENSIBLE_EXTENSIBILITY)
enum STATUS_FLAGS {
NORMAL = 0,
ID_MISMATCH = 1,
BAD_TIMESTAMP = 2,
};
@Extensibility(EXTENSIBLE_EXTENSIBILITY)
struct VetoTDCHit {
long hit_index;
float pmt_theta;
float pmt_phi;
};
@Extensibility(EXTENSIBLE_EXTENSIBILITY)
struct VetoTruth {
long sim_event;
sequence<VetoTDCHit> hits;
};
@Extensibility(EXTENSIBLE_EXTENSIBILITY)
struct EventInfo {
VetoTruth truth;
@key long event_id;
STATUS_FLAGS status;
};
Type representation
is binary at runtime
but shown here
using the IDL syntax
for readability
So, How does it work?
• It uses:
– C++11
– Overloading in overdrive!
• SFINAE: Substitution Failure Is Not An Error
– Generic/Generative programming (templates)
– Template meta-programming
– Some (macro) preprocessor tricks
• For code generation
– C++ Standard Library
• tuple, pair, vector, list, set, map, array, type_traits etc.
– Boost
• Fusion, Optional, Variant, and Preprocessor
– TypeCode API
– Dynamic Data API
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 14
What is Supported? (C++ point of view)
• Built-in types, arrays, enumerations
• Nested structs/classes (with public members)
• STL
– string, vector, list, set, map, array, tuple, pair, iterators, etc.
• User-defined/custom containers
– my_special_container_with_iterators
– On-the-fly container adapters (views e.g., boost.Iterators)
• C++11 compilers
– Visual Studio 2013 Preview
– gcc 4.9
– Clang 3.0, 3.2
• In future…
– Raw Pointers, smart pointers
– Classes with public get/set methods?
– Inheritance?
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 15
What is Supported? (IDL point of view)
• Basic types/enumerations/strings
• Arrays
• Sequences of strings/basic types/enumerations
• Bounded sequences/strings
• Structures
• Unions (including cases with defaults, multiple discriminators, and enumerations)
• Sparse Types
• Sequences of sequences (of sequences… and so on…)
• Sequences of structures
• Multidimensional arrays of strings, structures, sequences,…
• Nested structures
• Nested unions
• But, no bit-fields and inheritance, so far…
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 16
Architecture
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 17
Application
GenericDataReader<T>
GenericDataReaderListener<T>
GenericDataWriter<T>
Generic Functions
(MakeTypecode, FillDD, ExtractDD, etc.)
Type-driven access (overloading, template
meta-programming, etc.)
RTI_ADAPT
Macros
TypeCode, Dynamic Data API, DynamicDataTypeSupport
Dynamic Data Type Plugin callbacks and CDR API
DDS Core
Extremely Simplified
DynamicData API
class DDS_TypeCode {};
class DDS_DynamicData
{
public:
explicit DDS_DynamicData(const DDS_TypeCode &) {}
void set_short (int id, int16_t) {}
void set_long (int id, int32_t) {}
void set_longlong (int id, uint64_t) {}
void set_octet (int id, uint8_t) {}
void set_ushort (int id, uint16_t) {}
void set_ulong (int id, uint32_t) {}
void set_ulonglong (int id, uint64_t) {}
void set_float (int id, float) {}
void set_double (int id, double) {}
void set_longdouble(int id, long double) {}
void set_string (int id, const char *) {}
void set_complex (int id, const DDS_DynamicData &) {}
};
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 18
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 19
Overloading for Fundamental Types
void set_value(DDS_DynamicData & d, int id, bool v) {
d.set_bool(id, v); }
void set_value(DDS_DynamicData & d, int id, char v) {
d.set_char(id, v); }
void set_value(DDS_DynamicData & d, int id, int16_t v) {
d.set_short(id, v); }
void set_value(DDS_DynamicData & d, int id, int32_t v) {
d.set_long(id, v); }
void set_value(DDS_DynamicData & d, int id, float v) {
d.set_float(id, v); }
void set_value(DDS_DynamicData & d, int id, double v) {
d.set_double(id, v); }
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 20
Overloading set_value for std::string
void set_value(DDS_DynamicData & d,
int id,
const std::string & str)
{
d.set_string(id, str.c_str());
}
Sequences
(simplified DDS DynamicData API, again)
class DDS_DynamicData
{
public:
void set_boolean_seq (int id, const DDS_BooleanSeq &) {}
void set_long_seq (int id, const DDS_LongSeq &) {}
void set_float_seq (int id, const DDS_FloatSeq &) {}
void set_double_seq (int id, const DDS_DoubleSeq &) {}
void set_longdouble_seq (int id, const DDS_LongDoubleSeq &) {}
};
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 21
• Sequences are wrappers over C-style arrays
• Flexible: May own memory or loan/unloan from the user
– RAII is used when memory is owned
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 22
Overloading for std::vector of primitives
void set_value(DDS_DynamicData & d,
int id,
const std::vector<float> & v)
{
DDS_FloatSeq seq;
seq.loan_contiguous(&v[0], v.size(), v.capacity());
d.set_float_seq(id, seq);
seq.unloan();
}
// and many many more similar looking functions for
// int8_t, int16_t, int32_t, int64_t, char, double, ...
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 23
set_value Template for std::vector of primitives
template <class T>
void set_value(DDS_DynamicData & d,
int id,
const std::vector<T> & v)
{
typename DynamicDataSeqTraits<T>::type seq;
seq.loan_contiguous(&v[0], v.size(), v.capacity());
typename DynamicDataSeqTraits<T>::SetSeqFuncPtr fn =
DynamicDataSeqTraits<T>::set_seq_func_ptr;
d.*fn(id, seq);
seq.unloan();
}
• One template for all vectors of primitives
• Traits to get the function pointer
• Traits written using macros!
• Isn’t that just overloading and static method dispatch? . . . Yes it is!
Static
method
dispatch
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 24
But set_value[T=bool] is outright wrong!
template <class T>
void set_value(DDS_DynamicData & d,
int id,
const std::vector<T> & v)
{
typename DynamicDataSeqTraits<T>::type seq;
seq.loan_contiguous(&v[0], v.size(), v.capacity());
typename DynamicDataSeqTraits<T>::SetSeqFuncPtr fn =
DynamicDataSeqTraits<T>::set_seq_func_ptr;
d.*fn(id, seq);
seq.unloan();
}
std::vector<bool>
layout incompatible
• std::vector<bool> is often a space-efficient data structure
• Each element occupies a single bit instead of a single byte
• Does not follow many other std::vector invariants
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 25
Add std::vector<bool> overload of set_value
template <class T>
void set_value(DDS_DynamicData & d,
int id,
const std::vector<T> & v) {
typename DynamicDataSeqTraits<T>::type seq;
seq.loan_contiguous(&v[0], v.size(), v.size());
typename DynamicDataSeqTraits<T>::SetSeqFuncPtr fn =
DynamicDataSeqTraits<T>::set_seq_func_ptr;
d.*fn(id, seq);
seq.unloan();
}
void set_value(DDS_DynamicData & d,
int id,
const std::vector<bool> & v) {
DDS_BooleanSeq seq;
seq.ensure_length(v.size(), v.size());
std::copy(begin(v), end(v), &seq[0]);
d.set_boolean_seq(id, seq);
}
Overload for Classic Enumerations
• C++03 enumerations implicitly convert to an integer
– User C++11’s type-safe enums if you don’t want that
• The following overload works ... for now!
• set_value(DDS_DynamicData &, int id, int32_t)
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 26
enums go here
Overload for user-defined structs: “Foo”
10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 27
template <class T>
void set_value(DDS_DynamicData & d,
int id,
const T & t)
{
DDS_DynamicData inner;
// Use boost.fusion to iterate over the public members of T
// and use set_value recursively to populate the inner object.
// Effectively equivalent to
set_value(inner, 0, at<0>(t));
set_value(inner, 1, at<1>(t));
set_value(inner, 2, at<2>(t)); // and so on...
d.set_complex(id, inner);
}
Enumerations are user-defined types too!
• Addition of the template for Foo breaks enumerations
• Solution:
– Function Overloading Based on Arbitrary Properties of
Types
– Powered by SFINAE!
• Substitution Failure Is Not An Error
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 28
template <class T>
void set_value(DDS_DynamicData & d,
int id,
const T & t);
All enums and user-
defined structs resolve to
this overload
(that’s not what we want)
A SFINAE Primer
• Instantiating a function template is a two-phase process
– Deduce template arguments
– Substitute the deduced type in all occurrences of the template
parameter
• If the substitution process leads to an invalid type,
argument deduction fails
• The function is removed from the overload resolution set
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 29
int negate(int i) { return -i; }
template <class F>
typename F::result_type
negate(const F& f) { return -f(); }
negate(1);
Function Enablers and Disablers
• enable_if<true> keeps the template instantiation in the overload set
• enable_if<false> removes the template instantiation from the overload
set
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 30
template <bool B, class T = void>
struct enable_if {
typedef T type;
};
template <class T>
struct enable_if<false, T> {};
template <class T>
typename enable_if<is_arithmetic<T>::value, T>::type
foo(T t);
template <class T>
T bar (T t,
typename enable_if<is_arithmetic<T>::value>::type* = 0);
Using enable_if with type_traits
• enable_if combined with type_traits is very powerful
• Many type_traits are implemented using compiler intrinsics
– Think of it as compile-time reflection in library form
– E.g., is_union, is_pod, is_enum, any many many more
– C++11 type_traits library is portable
10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 31
template <class T>
void set_value(DDS_DynamicData & d,
int id,
const T & t,
typename enable_if<std::is_enum<T>::value>::type * = 0);
template <class T>
void set_value(DDS_DynamicData & d,
int id,
const T & t,
typename enable_if<std::is_class<T>::value>::type * = 0);
Recap set_value overloads so far
10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 32
Overloads #
void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &);[T=fundamental]
2
void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 3
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<std::is_enum<T>::value>::type * = 0);
4
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<std::is_class<T>::value>::type * = 0);
5
• Works with
– 18 fundamental types, std::vector of those, std::vector<bool>, scalar
enumerations, and scalar user-defined structs.
• No match for
– std::vector<USER-DEFINED-STRUCTS>
More overloads
10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 33
Overloads #
void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &
enable_if<is_fundamental<T>::value>::type * = 0);
2
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
disable_if<is_fundamental<T>::value>::type * = 0);
3
void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 4
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_enum<T>::value>::type * = 0);
5
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_class<T>::value>::type * = 0);
6
• Works with
– std::vector<USER-DEFINED-STRUCT>, std::vector<bool>, scalar enumerations, scalar user-defined structs, 18
fundamental types, std::vector of those
• Resolves incorrectly for std::vector<ENUMS>
– Resolves to overload #3 but that’s wrong because enums are not fundamental types
More overloads!
10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 34
Overloads #
void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &
enable_if<is_fundamental<T>::value>::type * = 0);
2
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
disable_if<is_fundamental<T>::value>::type * = 0);
3
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
enable_if<is_enum<T>::value>::type * = 0);
4
void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_enum<T>::value>::type * = 0);
6
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_class<T>::value>::type * = 0);
7
• std::vector<ENUMS> does not work due to ambiguity
• Overload #3 and #4 are both viable for std::vector<ENUMS>
More overloads!
10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 35
Overloads #
void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &
enable_if<is_fundamental<T>::value>::type * = 0);
2
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
enable_if<is_class<T>::value>::type * = 0);
3
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
enable_if<is_enum<T>::value>::type * = 0);
4
void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_enum<T>::value>::type * = 0);
6
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_class<T>::value>::type * = 0);
7
• Support built-in arrays
– Without array-specific overloads arrays map to the bool overload!
10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 36
Overloads #
void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &
enable_if<is_fundamental<T>::value>::type * = 0);
2
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
enable_if<is_class<T>::value>::type * = 0);
3
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
enable_if<is_enum<T>::value>::type * = 0);
4
void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_enum<T>::value>::type * = 0);
6
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_class<T>::value>::type * = 0);
7
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_fundamental<typename remove_all_extents<T>::type>::value>::type * = 0);
8
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_enum<typename remove_all_extents<T>::type>::value>::type * = 0);
9
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_class<typename remove_all_extents<T>::type>::value>::type * = 0);
10
set_value Overloads for built-in arrays
Overloads for std::vector<T[N]> ?
10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 37
Overloads #
void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &
enable_if<is_fundamental<T>::value>::type * = 0);
2
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
enable_if<is_class<T>::value>::type * = 0);
3
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
enable_if<is_enum<T>::value>::type * = 0);
4
void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_enum<T>::value>::type * = 0);
6
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_class<T>::value>::type * = 0);
7
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_fundamental<typename remove_all_extents<T>::type>::value>::type * = 0);
8
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_enum<typename remove_all_extents<T>::type>::value>::type * = 0);
9
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_class<typename remove_all_extents<T>::type>::value>::type * = 0);
10
Works for vector
of structs but not
for vector of
built-in arrays
Overloads for std::vector<T[N]>
10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 38
Overloads #
void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &
enable_if<is_fundamental<T>::value>::type * = 0);
2
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
disable_if<is_fundamental<T>::value ||
is_enum<T>::value>::type * = 0);
3
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
enable_if<is_enum<T>::value>::type * = 0);
4
void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_enum<T>::value>::type * = 0);
6
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_class<T>::value>::type * = 0);
7
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_fundamental<typename remove_all_extents<T>::type>::value>::type * = 0);
8
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_enum<typename remove_all_extents<T>::type>::value>::type * = 0);
9
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_class<typename remove_all_extents<T>::type>::value>::type * = 0);
10
Works for vector
of structs/built-in
arrays/nested
vectors
The Overloads are Mutually Recursive
10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 39
Overloads #
void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &
enable_if<is_fundamental<T>::value>::type * = 0);
2
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
disable_if<is_fundamental<T>::value ||
is_enum<T>::value>::type * = 0);
3
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
enable_if<is_enum<T>::value>::type * = 0);
4
void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_enum<T>::value>::type * = 0);
6
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_class<T>::value>::type * = 0);
7
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_fundamental<typename remove_all_extents<T>::type>::value>::type * = 0);
8
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_enum<typename remove_all_extents<T>::type>::value>::type * = 0);
9
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_class<typename remove_all_extents<T>::type>::value>::type * = 0);
10
May call
The Overloads are Mutually Recursive
10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 40
Overloads #
void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &
enable_if<is_fundamental<T>::value>::type * = 0);
2
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
disable_if<is_fundamental<T>::value ||
is_enum<T>::value>::type * = 0);
3
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
enable_if<is_enum<T>::value>::type * = 0);
4
void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_enum<T>::value>::type * = 0);
6
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_class<T>::value>::type * = 0);
7
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_fundamental<typename remove_all_extents<T>::type>::value>::type * = 0);
8
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_enum<typename remove_all_extents<T>::type>::value>::type * = 0);
9
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_class<typename remove_all_extents<T>::type>::value>::type * = 0);
10
Works for
Multidimensional
array or array of
complex types
Overloading for Other STL Containers!
10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 41
Overloads #
void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &
enable_if<is_fundamental<T>::value>::type * = 0);
2
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
enable_if<is_class<T>::value>::type * = 0);
3
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &,
enable_if<is_enum<T>::value>::type * = 0);
4
void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_enum<T>::value>::type * = 0);
6
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_class<T>::value>::type * = 0);
7
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_fundamental<typename remove_all_extents<T>::type>::value>::type * = 0);
8
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_enum<typename remove_all_extents<T>::type>::value>::type * = 0);
9
template <class T, unsigned N>
void set_value(DDS_DynamicData &, int, const T(&arr)[N],
typename enable_if<is_class<typename remove_all_extents<T>::type>::value>::type * = 0);
10
What about
standard list, set,
deque, unordered,
etc.?
Overloading for Other STL Containers!
10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 42
template <class T>
struct is_container : std::false_type {};
template <class T, class Alloc>
struct is_container<std::vector<T, Alloc>> : std::true_type {};
template <class T, class Alloc>
struct is_container<std::list<T, Alloc>> : std::true_type {};
template <class T, class Comp, class Alloc>
struct is_container<std::set<T, Comp, Alloc>> : std::true_type {};
template <class Key, class Value, class Comp, class Alloc>
struct is_container<std::map<Key, Value, Comp, Alloc>> : std::true_type {};
// and few more ...
Overloading for Other STL Containers!
10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 43
Overloads #
template <class T>
void set_value(DDS_DynamicData &, int, const std::vector<T> &
enable_if<is_fundamental<T>::value>::type * = 0);
1
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename disable_if<!is_container<T>::value ||
std::is_fundamental<typename T::value_type>::value ||
std::is_enum<typename T::value_type>::value >::type * = 0)
2
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_container<T>::value &&
(is_fundamental<typename T::value_type>::value ||
std::is_enum<typename T::value_type>::value) &&
(is_vector<T>::value?
is_bool_or_enum<typename T::value_type>::value :
true)>::type * = 0);
3
void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 4
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<!is_container<T>::value &&
is_class<T>::value>::type * = 0);
5
template <class T>
void set_value(DDS_DynamicData &, int, const T &,
typename enable_if<is_enum<T>::value>::type * = 0);
6
Overloads for fundamental scalar types and built-in arrays are not shown
T is a std container
of primitives but
not vector
<primitives except
(bool & enums)>
T is a std container of
complex type
Scalar complex type
(Foo)
Scalar Enums
vector<fundamental>
Using set_value overloads with Boost.Fusion
struct SetValue {
SetValue(DDS_DynamicData &dd);
// ...
template <typename T>
void operator()(T& t) const {
set_value(dd, ++id, t);
}
};
ShapeType shape(“BLUE”, x, y, 30);
DDS_DynamicData dd(get_typecode<ShapeType>());
boost::fusion::for_each(shape, SetValue(dd));
10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 44
// shape.h
struct ShapeType
{
std::string color;
int x;
int y;
unsigned shapesize;
};
// shape.h
RTI_ADAPT_STRUCT(
ShapeType,
(std::string, color, KEY)
(int, x)
(int, y)
(unsigned, shapesize))
More Info
• OMG DDS
– http://portals.omg.org/dds/
• RTI Connext™ DDS
– http://www.rti.com/products/dds/
• Example Code
– https://cpptruths.googlecode.com/svn/trunk/cpp
0x/overloading-overdrive.cpp
10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 45
Thank you!
© 2012 RTI • COMPANY CONFIDENTIAL

More Related Content

PDF
Reactive Stream Processing Using DDS and Rx
PPTX
Systematic Generation Data and Types in C++
PDF
Property-based Testing and Generators (Lua)
PDF
Java collections the force awakens
PDF
Generics Past, Present and Future
PDF
Generics Past, Present and Future (Latest)
PDF
Generics past, present and future
PDF
Performance and predictability
Reactive Stream Processing Using DDS and Rx
Systematic Generation Data and Types in C++
Property-based Testing and Generators (Lua)
Java collections the force awakens
Generics Past, Present and Future
Generics Past, Present and Future (Latest)
Generics past, present and future
Performance and predictability

What's hot (20)

PDF
Apache Spark Machine Learning
PPTX
Compilers Are Databases
PPTX
Core concepts of C++
PDF
Performance and predictability (1)
PDF
Map, flatmap and reduce are your new best friends (javaone, svcc)
PPTX
Dplyr packages
PDF
Introduction to Machine Learning with Spark
PDF
Ge aviation spark application experience porting analytics into py spark ml p...
PDF
Spark schema for free with David Szakallas
PDF
Deep Anomaly Detection from Research to Production Leveraging Spark and Tens...
PDF
Statistical Learning and Text Classification with NLTK and scikit-learn
PDF
An Introduction to Higher Order Functions in Spark SQL with Herman van Hovell
PPTX
Concurrency Constructs Overview
PPTX
Scalable and Flexible Machine Learning With Scala @ LinkedIn
PDF
Groovy On Trading Desk (2010)
PPTX
Survey of Spark for Data Pre-Processing and Analytics
PPTX
Toub parallelism tour_oct2009
PDF
PDF
VelocityGraph Introduction
Apache Spark Machine Learning
Compilers Are Databases
Core concepts of C++
Performance and predictability (1)
Map, flatmap and reduce are your new best friends (javaone, svcc)
Dplyr packages
Introduction to Machine Learning with Spark
Ge aviation spark application experience porting analytics into py spark ml p...
Spark schema for free with David Szakallas
Deep Anomaly Detection from Research to Production Leveraging Spark and Tens...
Statistical Learning and Text Classification with NLTK and scikit-learn
An Introduction to Higher Order Functions in Spark SQL with Herman van Hovell
Concurrency Constructs Overview
Scalable and Flexible Machine Learning With Scala @ LinkedIn
Groovy On Trading Desk (2010)
Survey of Spark for Data Pre-Processing and Analytics
Toub parallelism tour_oct2009
VelocityGraph Introduction
Ad

Viewers also liked (8)

PDF
C++11 Idioms @ Silicon Valley Code Camp 2012
PPTX
Fun with Lambdas: C++14 Style (part 2)
PDF
Reactive Stream Processing in Industrial IoT using DDS and Rx
PPTX
C++ Generators and Property-based Testing
PDF
O caml2014 leroy-slides
PDF
Getting Started with DDS in C++, Java and Scala
PDF
The Present and Future of DDS
PDF
Deep C
C++11 Idioms @ Silicon Valley Code Camp 2012
Fun with Lambdas: C++14 Style (part 2)
Reactive Stream Processing in Industrial IoT using DDS and Rx
C++ Generators and Property-based Testing
O caml2014 leroy-slides
Getting Started with DDS in C++, Java and Scala
The Present and Future of DDS
Deep C
Ad

Similar to Overloading in Overdrive: A Generic Data-Centric Messaging Library for DDS (20)

PDF
DDS ISO C++ PSM
PPT
Extensible and Dynamic Topic Types For DDS (out of date)
PDF
mm-ADT: A Multi-Model Abstract Data Type
PPTX
DDS Advanced Tutorial - OMG June 2013 Berlin Meeting
PPTX
7 DDS Innovations to Improve your Next Distributed System
PDF
DDS Programming with IDL to C++11 tutorial
PPTX
Distributed Systems: How to connect your real-time applications
PDF
DFDL and Apache Daffodil(tm) Overview from Owl Cyber Defense
PPTX
SolidSource Portfolio
PDF
DDS-PSM-Cxx and simd-cxx
PDF
Standardizing the Data Distribution Service (DDS) API for Modern C++
PPTX
Overview of the DDS-XRCE specification
PDF
Introducing the New MagicDraw Plug-In for RTI Connext DDS: Industrial IoT Mee...
PDF
IoT Protocols Integration with Vortex Gateway
DOC
Resume_Dimitri_Dey_LTE_Android_Gmail
PDF
Vital AI MetaQL: Queries Across NoSQL, SQL, Sparql, and Spark
PPTX
Scam 08
PPT
Sw Software Design
PDF
Distributed Algorithms with DDS
PDF
Reactive Data Centric Architectures with DDS
DDS ISO C++ PSM
Extensible and Dynamic Topic Types For DDS (out of date)
mm-ADT: A Multi-Model Abstract Data Type
DDS Advanced Tutorial - OMG June 2013 Berlin Meeting
7 DDS Innovations to Improve your Next Distributed System
DDS Programming with IDL to C++11 tutorial
Distributed Systems: How to connect your real-time applications
DFDL and Apache Daffodil(tm) Overview from Owl Cyber Defense
SolidSource Portfolio
DDS-PSM-Cxx and simd-cxx
Standardizing the Data Distribution Service (DDS) API for Modern C++
Overview of the DDS-XRCE specification
Introducing the New MagicDraw Plug-In for RTI Connext DDS: Industrial IoT Mee...
IoT Protocols Integration with Vortex Gateway
Resume_Dimitri_Dey_LTE_Android_Gmail
Vital AI MetaQL: Queries Across NoSQL, SQL, Sparql, and Spark
Scam 08
Sw Software Design
Distributed Algorithms with DDS
Reactive Data Centric Architectures with DDS

More from Sumant Tambe (13)

PDF
Kafka tiered-storage-meetup-2022-final-presented
PPTX
Tuning kafka pipelines
PPTX
New Tools for a More Functional C++
PPTX
C++ Coroutines
PDF
RPC over DDS Beta 1
PDF
Remote Log Analytics Using DDS, ELK, and RxJS
PDF
Reactive Stream Processing for Data-centric Publish/Subscribe
PDF
Fun with Lambdas: C++14 Style (part 1)
PDF
An Extensible Architecture for Avionics Sensor Health Assessment Using DDS
PDF
Communication Patterns Using Data-Centric Publish/Subscribe
PPTX
Retargeting Embedded Software Stack for Many-Core Systems
PPTX
Ph.D. Dissertation
PDF
Native XML processing in C++ (BoostCon'11)
Kafka tiered-storage-meetup-2022-final-presented
Tuning kafka pipelines
New Tools for a More Functional C++
C++ Coroutines
RPC over DDS Beta 1
Remote Log Analytics Using DDS, ELK, and RxJS
Reactive Stream Processing for Data-centric Publish/Subscribe
Fun with Lambdas: C++14 Style (part 1)
An Extensible Architecture for Avionics Sensor Health Assessment Using DDS
Communication Patterns Using Data-Centric Publish/Subscribe
Retargeting Embedded Software Stack for Many-Core Systems
Ph.D. Dissertation
Native XML processing in C++ (BoostCon'11)

Recently uploaded (20)

PDF
CEH Module 2 Footprinting CEH V13, concepts
PDF
EIS-Webinar-Regulated-Industries-2025-08.pdf
PDF
Introduction to MCP and A2A Protocols: Enabling Agent Communication
PDF
Decision Optimization - From Theory to Practice
PDF
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
PDF
Planning-an-Audit-A-How-To-Guide-Checklist-WP.pdf
PDF
zbrain.ai-Scope Key Metrics Configuration and Best Practices.pdf
PDF
giants, standing on the shoulders of - by Daniel Stenberg
PPTX
Presentation - Principles of Instructional Design.pptx
PDF
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
PDF
Ensemble model-based arrhythmia classification with local interpretable model...
PDF
The AI Revolution in Customer Service - 2025
PDF
Altius execution marketplace concept.pdf
PDF
Data Virtualization in Action: Scaling APIs and Apps with FME
PDF
Electrocardiogram sequences data analytics and classification using unsupervi...
PDF
Build Real-Time ML Apps with Python, Feast & NoSQL
PDF
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
PDF
4 layer Arch & Reference Arch of IoT.pdf
PDF
substrate PowerPoint Presentation basic one
PDF
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
CEH Module 2 Footprinting CEH V13, concepts
EIS-Webinar-Regulated-Industries-2025-08.pdf
Introduction to MCP and A2A Protocols: Enabling Agent Communication
Decision Optimization - From Theory to Practice
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
Planning-an-Audit-A-How-To-Guide-Checklist-WP.pdf
zbrain.ai-Scope Key Metrics Configuration and Best Practices.pdf
giants, standing on the shoulders of - by Daniel Stenberg
Presentation - Principles of Instructional Design.pptx
“The Future of Visual AI: Efficient Multimodal Intelligence,” a Keynote Prese...
Ensemble model-based arrhythmia classification with local interpretable model...
The AI Revolution in Customer Service - 2025
Altius execution marketplace concept.pdf
Data Virtualization in Action: Scaling APIs and Apps with FME
Electrocardiogram sequences data analytics and classification using unsupervi...
Build Real-Time ML Apps with Python, Feast & NoSQL
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
4 layer Arch & Reference Arch of IoT.pdf
substrate PowerPoint Presentation basic one
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC

Overloading in Overdrive: A Generic Data-Centric Messaging Library for DDS

  • 1. Sumant Tambe, Ph.D. Senior Software Research Engineer and Microsoft MVP Real-Time Innovations, Inc. www.rti.com SILICON VALLEY
  • 2. 10/2/2013 © 2012 RTI 2 Data-Centric Communication Model in DDS
  • 3. Data Distribution Service 10/2/2013 © 2012 RTI 3 • Data Distribution Service – Middleware knows the schema – Messages represent update to data-objects – Data-Objects identified by a key – Middleware maintains state of each object – Objects are cached. Applications can read at leisure – Smart QoS (Reliability, Ownership, History (per key), Deadline, and 18 more!) Streaming Data Sensors Events Real-Time Applications Enterprise Applications Actuators
  • 4. Data-Centric  Typed All the Way! 10/2/2013 © 2013 RTI 4 Standard Mapping Java C/C++ C# Type Descriptions in DDS-XTypes (IDL, XSD, Java etc.) Code Generator • Types description similar to an OO language – Primitives, strings, arrays and sequences, maps, structures, enumerations, aliases (typedefs), unions, modules, etc. • Type versioning and assignability • Meta-Data – Unique ID for each member – Annotations (like in Java) – Key/non-key – Optional/required members – Sharable/non-sharable
  • 5. DDS X-Types Standard • Type System: DDS data objects have a type and obey the rules described herein • Language Binding for Object Manipulation – Generate code when type-descriptions are available at compile-time – Otherwise, create/inspect objects of any type at run-time • Language Binding for Type Manipulation – add/remove/query data members types (think Java reflection) • Data Representation: Objects can be serialized for file storage and network transmission • Type Representation: Types can be serialized for file storage and network transmission (using TypeObject) 5 Type System Type Representation Language Binding Data Representation
  • 6. Example 6 Type Representation Language Binding Data Representation IDL: Foo.idl struct Foo { string name; @key long ssn; @optional string phone; }; IDL to C++ Language Mapping: struct Foo { std::string name; int ssn; std::string * phone; // ... }; IDL to CDR: 00000006 68656C6C 6F000000 00000002
  • 7. Two Faces of DDS 10/2/2013 © 2013 RTI 7 • Application Integration – Many applications, many programming languages, many contractors necessitate a common format for sharing types – IDL serves well – Related technologies: CORBA Interfaces, schema definitions (DB), XML document structure (XSD) • Messaging – Typically, a single (distributed) application – Likely developed using a single programming language – Overall system under single administration – Data already stored in application data structures – Just send it from point A to B – Related technologies: ActiveMQ, JMS – IDL not so much desirable
  • 8. IDL may be a Distraction (for messaging apps) • “… But I already have my headers…” – Maintaining redundant IDL type descriptions is a burden – Integrating generated code in app build system could be burdensome – Must write glue code • Or sometimes it is an overhead – Copy data back and forth between app data types and IDL data types • Application level data structures may not be captured accurately – Think XSD (e.g., sequence of choices) 10/2/2013 © 2013 RTI 8
  • 9. Solution: A Pure C++11 Library-based Approach Example: 10/2/2013 © 2012 RTI 9 // shape.h struct ShapeType { std::string color; int x; int y; unsigned shapesize; }; // shape.h RTI_ADAPT_STRUCT( ShapeType, (std::string, color, KEY) (int, x) (int, y) (unsigned, shapesize)) DDSDomainParticipant * participant = ... GenericDataWriter<ShapeType> shapes_writer(participant, "Square"); for(;;) { ShapeType shape {“BLUE”, x, y, 30}; shapes_writer.write(shape); }
  • 10. Solution: A Pure C++11 Library-based Approach Example: 10/4/2013 © 2012 RTI 10 // shape.h struct ShapeType { std::string color; int x; int y; unsigned shapesize; }; // shape.h RTI_ADAPT_STRUCT( ShapeType, (std::string, color, KEY) (int, x) (int, y) (unsigned, shapesize)) @Extensibility(EXTENSIBLE) struct ShapeType { @key string color; long x; long y; unsigned long shapesize; }; Instead IDL A substitute for lack of reflection in C++
  • 11. Read/Take Shapes (note C++11) 10/2/2013 © 2013 RTI 11 class MyShapesListener : public GenericDataReaderListener<ShapeType> { public: void on_data_available(GenericDataReader<ShapeType> & dr) override { std::vector<ShapeType> samples = dr.take(); for (auto const &shape : samples) { std::cout << “color = " << shape.color << “n” << "x = " << shape.x << “n” << "y = " << shape.y << “n” << "shapesize = " << shape.shapesize << “n” << std::endl; } } }; DDSDomainParticipant * participant = ... MyShapesListener listener; std::string topic = “Square”; GenericDataReader<ShapeType> shapes_reader(participant, &listener, topic); for(;;) sleep(1);
  • 12. Slightly Complex Example 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 12 struct VetoTDCHit { int hit_index; int pmt_index; // not sent float pmt_theta; float pmt_phi; }; typedef std::list<VetoTDCHit> VetoTDCHits; struct VetoTruth { int sim_event; VetoTDCHits hits; }; enum STATUS_FLAGS { NORMAL=0, ID_MISMATCH=1, BAD_TIMESTAMP=2 }; struct EventInfo { VetoTruth truth; int event_id; STATUS_FLAGS status; }; RTI_ADAPT_STRUCT( // same header VetoTDCHit, (int, hit_index) (float, pmt_theta) (float, pmt_phi)) RTI_ADAPT_STRUCT( VetoTruth, (int, sim_event) (VetoTDCHits, hits)) RTI_ADAPT_ENUM( STATUS_FLAGS, (NORMAL, 0) (ID_MISMATCH, 1) (BAD_TIMESTAMP, 2)) RTI_ADAPT_STRUCT( EventInfo, (VetoTruth, truth) (int, event_id, KEY) (TATUS_FLAGS, status))
  • 13. Equivalent Type at Run-time 10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 13 @Extensibility(EXTENSIBLE_EXTENSIBILITY) enum STATUS_FLAGS { NORMAL = 0, ID_MISMATCH = 1, BAD_TIMESTAMP = 2, }; @Extensibility(EXTENSIBLE_EXTENSIBILITY) struct VetoTDCHit { long hit_index; float pmt_theta; float pmt_phi; }; @Extensibility(EXTENSIBLE_EXTENSIBILITY) struct VetoTruth { long sim_event; sequence<VetoTDCHit> hits; }; @Extensibility(EXTENSIBLE_EXTENSIBILITY) struct EventInfo { VetoTruth truth; @key long event_id; STATUS_FLAGS status; }; Type representation is binary at runtime but shown here using the IDL syntax for readability
  • 14. So, How does it work? • It uses: – C++11 – Overloading in overdrive! • SFINAE: Substitution Failure Is Not An Error – Generic/Generative programming (templates) – Template meta-programming – Some (macro) preprocessor tricks • For code generation – C++ Standard Library • tuple, pair, vector, list, set, map, array, type_traits etc. – Boost • Fusion, Optional, Variant, and Preprocessor – TypeCode API – Dynamic Data API 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 14
  • 15. What is Supported? (C++ point of view) • Built-in types, arrays, enumerations • Nested structs/classes (with public members) • STL – string, vector, list, set, map, array, tuple, pair, iterators, etc. • User-defined/custom containers – my_special_container_with_iterators – On-the-fly container adapters (views e.g., boost.Iterators) • C++11 compilers – Visual Studio 2013 Preview – gcc 4.9 – Clang 3.0, 3.2 • In future… – Raw Pointers, smart pointers – Classes with public get/set methods? – Inheritance? 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 15
  • 16. What is Supported? (IDL point of view) • Basic types/enumerations/strings • Arrays • Sequences of strings/basic types/enumerations • Bounded sequences/strings • Structures • Unions (including cases with defaults, multiple discriminators, and enumerations) • Sparse Types • Sequences of sequences (of sequences… and so on…) • Sequences of structures • Multidimensional arrays of strings, structures, sequences,… • Nested structures • Nested unions • But, no bit-fields and inheritance, so far… 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 16
  • 17. Architecture 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 17 Application GenericDataReader<T> GenericDataReaderListener<T> GenericDataWriter<T> Generic Functions (MakeTypecode, FillDD, ExtractDD, etc.) Type-driven access (overloading, template meta-programming, etc.) RTI_ADAPT Macros TypeCode, Dynamic Data API, DynamicDataTypeSupport Dynamic Data Type Plugin callbacks and CDR API DDS Core
  • 18. Extremely Simplified DynamicData API class DDS_TypeCode {}; class DDS_DynamicData { public: explicit DDS_DynamicData(const DDS_TypeCode &) {} void set_short (int id, int16_t) {} void set_long (int id, int32_t) {} void set_longlong (int id, uint64_t) {} void set_octet (int id, uint8_t) {} void set_ushort (int id, uint16_t) {} void set_ulong (int id, uint32_t) {} void set_ulonglong (int id, uint64_t) {} void set_float (int id, float) {} void set_double (int id, double) {} void set_longdouble(int id, long double) {} void set_string (int id, const char *) {} void set_complex (int id, const DDS_DynamicData &) {} }; 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 18
  • 19. 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 19 Overloading for Fundamental Types void set_value(DDS_DynamicData & d, int id, bool v) { d.set_bool(id, v); } void set_value(DDS_DynamicData & d, int id, char v) { d.set_char(id, v); } void set_value(DDS_DynamicData & d, int id, int16_t v) { d.set_short(id, v); } void set_value(DDS_DynamicData & d, int id, int32_t v) { d.set_long(id, v); } void set_value(DDS_DynamicData & d, int id, float v) { d.set_float(id, v); } void set_value(DDS_DynamicData & d, int id, double v) { d.set_double(id, v); }
  • 20. 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 20 Overloading set_value for std::string void set_value(DDS_DynamicData & d, int id, const std::string & str) { d.set_string(id, str.c_str()); }
  • 21. Sequences (simplified DDS DynamicData API, again) class DDS_DynamicData { public: void set_boolean_seq (int id, const DDS_BooleanSeq &) {} void set_long_seq (int id, const DDS_LongSeq &) {} void set_float_seq (int id, const DDS_FloatSeq &) {} void set_double_seq (int id, const DDS_DoubleSeq &) {} void set_longdouble_seq (int id, const DDS_LongDoubleSeq &) {} }; 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 21 • Sequences are wrappers over C-style arrays • Flexible: May own memory or loan/unloan from the user – RAII is used when memory is owned
  • 22. 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 22 Overloading for std::vector of primitives void set_value(DDS_DynamicData & d, int id, const std::vector<float> & v) { DDS_FloatSeq seq; seq.loan_contiguous(&v[0], v.size(), v.capacity()); d.set_float_seq(id, seq); seq.unloan(); } // and many many more similar looking functions for // int8_t, int16_t, int32_t, int64_t, char, double, ...
  • 23. 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 23 set_value Template for std::vector of primitives template <class T> void set_value(DDS_DynamicData & d, int id, const std::vector<T> & v) { typename DynamicDataSeqTraits<T>::type seq; seq.loan_contiguous(&v[0], v.size(), v.capacity()); typename DynamicDataSeqTraits<T>::SetSeqFuncPtr fn = DynamicDataSeqTraits<T>::set_seq_func_ptr; d.*fn(id, seq); seq.unloan(); } • One template for all vectors of primitives • Traits to get the function pointer • Traits written using macros! • Isn’t that just overloading and static method dispatch? . . . Yes it is! Static method dispatch
  • 24. 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 24 But set_value[T=bool] is outright wrong! template <class T> void set_value(DDS_DynamicData & d, int id, const std::vector<T> & v) { typename DynamicDataSeqTraits<T>::type seq; seq.loan_contiguous(&v[0], v.size(), v.capacity()); typename DynamicDataSeqTraits<T>::SetSeqFuncPtr fn = DynamicDataSeqTraits<T>::set_seq_func_ptr; d.*fn(id, seq); seq.unloan(); } std::vector<bool> layout incompatible • std::vector<bool> is often a space-efficient data structure • Each element occupies a single bit instead of a single byte • Does not follow many other std::vector invariants
  • 25. 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 25 Add std::vector<bool> overload of set_value template <class T> void set_value(DDS_DynamicData & d, int id, const std::vector<T> & v) { typename DynamicDataSeqTraits<T>::type seq; seq.loan_contiguous(&v[0], v.size(), v.size()); typename DynamicDataSeqTraits<T>::SetSeqFuncPtr fn = DynamicDataSeqTraits<T>::set_seq_func_ptr; d.*fn(id, seq); seq.unloan(); } void set_value(DDS_DynamicData & d, int id, const std::vector<bool> & v) { DDS_BooleanSeq seq; seq.ensure_length(v.size(), v.size()); std::copy(begin(v), end(v), &seq[0]); d.set_boolean_seq(id, seq); }
  • 26. Overload for Classic Enumerations • C++03 enumerations implicitly convert to an integer – User C++11’s type-safe enums if you don’t want that • The following overload works ... for now! • set_value(DDS_DynamicData &, int id, int32_t) 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 26 enums go here
  • 27. Overload for user-defined structs: “Foo” 10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 27 template <class T> void set_value(DDS_DynamicData & d, int id, const T & t) { DDS_DynamicData inner; // Use boost.fusion to iterate over the public members of T // and use set_value recursively to populate the inner object. // Effectively equivalent to set_value(inner, 0, at<0>(t)); set_value(inner, 1, at<1>(t)); set_value(inner, 2, at<2>(t)); // and so on... d.set_complex(id, inner); }
  • 28. Enumerations are user-defined types too! • Addition of the template for Foo breaks enumerations • Solution: – Function Overloading Based on Arbitrary Properties of Types – Powered by SFINAE! • Substitution Failure Is Not An Error 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 28 template <class T> void set_value(DDS_DynamicData & d, int id, const T & t); All enums and user- defined structs resolve to this overload (that’s not what we want)
  • 29. A SFINAE Primer • Instantiating a function template is a two-phase process – Deduce template arguments – Substitute the deduced type in all occurrences of the template parameter • If the substitution process leads to an invalid type, argument deduction fails • The function is removed from the overload resolution set 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 29 int negate(int i) { return -i; } template <class F> typename F::result_type negate(const F& f) { return -f(); } negate(1);
  • 30. Function Enablers and Disablers • enable_if<true> keeps the template instantiation in the overload set • enable_if<false> removes the template instantiation from the overload set 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 30 template <bool B, class T = void> struct enable_if { typedef T type; }; template <class T> struct enable_if<false, T> {}; template <class T> typename enable_if<is_arithmetic<T>::value, T>::type foo(T t); template <class T> T bar (T t, typename enable_if<is_arithmetic<T>::value>::type* = 0);
  • 31. Using enable_if with type_traits • enable_if combined with type_traits is very powerful • Many type_traits are implemented using compiler intrinsics – Think of it as compile-time reflection in library form – E.g., is_union, is_pod, is_enum, any many many more – C++11 type_traits library is portable 10/2/2013 © 2012 RTI • COMPANY CONFIDENTIAL 31 template <class T> void set_value(DDS_DynamicData & d, int id, const T & t, typename enable_if<std::is_enum<T>::value>::type * = 0); template <class T> void set_value(DDS_DynamicData & d, int id, const T & t, typename enable_if<std::is_class<T>::value>::type * = 0);
  • 32. Recap set_value overloads so far 10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 32 Overloads # void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &);[T=fundamental] 2 void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 3 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<std::is_enum<T>::value>::type * = 0); 4 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<std::is_class<T>::value>::type * = 0); 5 • Works with – 18 fundamental types, std::vector of those, std::vector<bool>, scalar enumerations, and scalar user-defined structs. • No match for – std::vector<USER-DEFINED-STRUCTS>
  • 33. More overloads 10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 33 Overloads # void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> & enable_if<is_fundamental<T>::value>::type * = 0); 2 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, disable_if<is_fundamental<T>::value>::type * = 0); 3 void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 4 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_enum<T>::value>::type * = 0); 5 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_class<T>::value>::type * = 0); 6 • Works with – std::vector<USER-DEFINED-STRUCT>, std::vector<bool>, scalar enumerations, scalar user-defined structs, 18 fundamental types, std::vector of those • Resolves incorrectly for std::vector<ENUMS> – Resolves to overload #3 but that’s wrong because enums are not fundamental types
  • 34. More overloads! 10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 34 Overloads # void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> & enable_if<is_fundamental<T>::value>::type * = 0); 2 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, disable_if<is_fundamental<T>::value>::type * = 0); 3 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, enable_if<is_enum<T>::value>::type * = 0); 4 void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_enum<T>::value>::type * = 0); 6 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_class<T>::value>::type * = 0); 7 • std::vector<ENUMS> does not work due to ambiguity • Overload #3 and #4 are both viable for std::vector<ENUMS>
  • 35. More overloads! 10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 35 Overloads # void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> & enable_if<is_fundamental<T>::value>::type * = 0); 2 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, enable_if<is_class<T>::value>::type * = 0); 3 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, enable_if<is_enum<T>::value>::type * = 0); 4 void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_enum<T>::value>::type * = 0); 6 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_class<T>::value>::type * = 0); 7 • Support built-in arrays – Without array-specific overloads arrays map to the bool overload!
  • 36. 10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 36 Overloads # void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> & enable_if<is_fundamental<T>::value>::type * = 0); 2 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, enable_if<is_class<T>::value>::type * = 0); 3 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, enable_if<is_enum<T>::value>::type * = 0); 4 void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_enum<T>::value>::type * = 0); 6 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_class<T>::value>::type * = 0); 7 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_fundamental<typename remove_all_extents<T>::type>::value>::type * = 0); 8 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_enum<typename remove_all_extents<T>::type>::value>::type * = 0); 9 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_class<typename remove_all_extents<T>::type>::value>::type * = 0); 10 set_value Overloads for built-in arrays
  • 37. Overloads for std::vector<T[N]> ? 10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 37 Overloads # void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> & enable_if<is_fundamental<T>::value>::type * = 0); 2 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, enable_if<is_class<T>::value>::type * = 0); 3 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, enable_if<is_enum<T>::value>::type * = 0); 4 void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_enum<T>::value>::type * = 0); 6 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_class<T>::value>::type * = 0); 7 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_fundamental<typename remove_all_extents<T>::type>::value>::type * = 0); 8 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_enum<typename remove_all_extents<T>::type>::value>::type * = 0); 9 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_class<typename remove_all_extents<T>::type>::value>::type * = 0); 10 Works for vector of structs but not for vector of built-in arrays
  • 38. Overloads for std::vector<T[N]> 10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 38 Overloads # void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> & enable_if<is_fundamental<T>::value>::type * = 0); 2 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, disable_if<is_fundamental<T>::value || is_enum<T>::value>::type * = 0); 3 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, enable_if<is_enum<T>::value>::type * = 0); 4 void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_enum<T>::value>::type * = 0); 6 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_class<T>::value>::type * = 0); 7 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_fundamental<typename remove_all_extents<T>::type>::value>::type * = 0); 8 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_enum<typename remove_all_extents<T>::type>::value>::type * = 0); 9 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_class<typename remove_all_extents<T>::type>::value>::type * = 0); 10 Works for vector of structs/built-in arrays/nested vectors
  • 39. The Overloads are Mutually Recursive 10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 39 Overloads # void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> & enable_if<is_fundamental<T>::value>::type * = 0); 2 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, disable_if<is_fundamental<T>::value || is_enum<T>::value>::type * = 0); 3 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, enable_if<is_enum<T>::value>::type * = 0); 4 void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_enum<T>::value>::type * = 0); 6 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_class<T>::value>::type * = 0); 7 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_fundamental<typename remove_all_extents<T>::type>::value>::type * = 0); 8 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_enum<typename remove_all_extents<T>::type>::value>::type * = 0); 9 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_class<typename remove_all_extents<T>::type>::value>::type * = 0); 10 May call
  • 40. The Overloads are Mutually Recursive 10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 40 Overloads # void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> & enable_if<is_fundamental<T>::value>::type * = 0); 2 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, disable_if<is_fundamental<T>::value || is_enum<T>::value>::type * = 0); 3 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, enable_if<is_enum<T>::value>::type * = 0); 4 void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_enum<T>::value>::type * = 0); 6 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_class<T>::value>::type * = 0); 7 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_fundamental<typename remove_all_extents<T>::type>::value>::type * = 0); 8 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_enum<typename remove_all_extents<T>::type>::value>::type * = 0); 9 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_class<typename remove_all_extents<T>::type>::value>::type * = 0); 10 Works for Multidimensional array or array of complex types
  • 41. Overloading for Other STL Containers! 10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 41 Overloads # void set_value(DDS_DynamicData &, int, FundamentalTypes v); [18 overloads] 1 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> & enable_if<is_fundamental<T>::value>::type * = 0); 2 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, enable_if<is_class<T>::value>::type * = 0); 3 template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> &, enable_if<is_enum<T>::value>::type * = 0); 4 void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 5 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_enum<T>::value>::type * = 0); 6 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_class<T>::value>::type * = 0); 7 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_fundamental<typename remove_all_extents<T>::type>::value>::type * = 0); 8 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_enum<typename remove_all_extents<T>::type>::value>::type * = 0); 9 template <class T, unsigned N> void set_value(DDS_DynamicData &, int, const T(&arr)[N], typename enable_if<is_class<typename remove_all_extents<T>::type>::value>::type * = 0); 10 What about standard list, set, deque, unordered, etc.?
  • 42. Overloading for Other STL Containers! 10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 42 template <class T> struct is_container : std::false_type {}; template <class T, class Alloc> struct is_container<std::vector<T, Alloc>> : std::true_type {}; template <class T, class Alloc> struct is_container<std::list<T, Alloc>> : std::true_type {}; template <class T, class Comp, class Alloc> struct is_container<std::set<T, Comp, Alloc>> : std::true_type {}; template <class Key, class Value, class Comp, class Alloc> struct is_container<std::map<Key, Value, Comp, Alloc>> : std::true_type {}; // and few more ...
  • 43. Overloading for Other STL Containers! 10/3/2013 © 2012 RTI • COMPANY CONFIDENTIAL 43 Overloads # template <class T> void set_value(DDS_DynamicData &, int, const std::vector<T> & enable_if<is_fundamental<T>::value>::type * = 0); 1 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename disable_if<!is_container<T>::value || std::is_fundamental<typename T::value_type>::value || std::is_enum<typename T::value_type>::value >::type * = 0) 2 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_container<T>::value && (is_fundamental<typename T::value_type>::value || std::is_enum<typename T::value_type>::value) && (is_vector<T>::value? is_bool_or_enum<typename T::value_type>::value : true)>::type * = 0); 3 void set_value(DDS_DynamicData &, int, const std::vector<bool> &); 4 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<!is_container<T>::value && is_class<T>::value>::type * = 0); 5 template <class T> void set_value(DDS_DynamicData &, int, const T &, typename enable_if<is_enum<T>::value>::type * = 0); 6 Overloads for fundamental scalar types and built-in arrays are not shown T is a std container of primitives but not vector <primitives except (bool & enums)> T is a std container of complex type Scalar complex type (Foo) Scalar Enums vector<fundamental>
  • 44. Using set_value overloads with Boost.Fusion struct SetValue { SetValue(DDS_DynamicData &dd); // ... template <typename T> void operator()(T& t) const { set_value(dd, ++id, t); } }; ShapeType shape(“BLUE”, x, y, 30); DDS_DynamicData dd(get_typecode<ShapeType>()); boost::fusion::for_each(shape, SetValue(dd)); 10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 44 // shape.h struct ShapeType { std::string color; int x; int y; unsigned shapesize; }; // shape.h RTI_ADAPT_STRUCT( ShapeType, (std::string, color, KEY) (int, x) (int, y) (unsigned, shapesize))
  • 45. More Info • OMG DDS – http://portals.omg.org/dds/ • RTI Connext™ DDS – http://www.rti.com/products/dds/ • Example Code – https://cpptruths.googlecode.com/svn/trunk/cpp 0x/overloading-overdrive.cpp 10/4/2013 © 2012 RTI • COMPANY CONFIDENTIAL 45
  • 46. Thank you! © 2012 RTI • COMPANY CONFIDENTIAL