Skip to content

Instantly share code, notes, and snippets.

@randvoorhies
Created April 26, 2012 06:53
Show Gist options
  • Save randvoorhies/2496915 to your computer and use it in GitHub Desktop.
Save randvoorhies/2496915 to your computer and use it in GitHub Desktop.
NRT Message Split Test
#include <iostream>
#include <cxxabi.h>
#include <boost/preprocessor/variadic/size.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/variadic/to_seq.hpp>
#include <boost/preprocessor/stringize.hpp>
int indent = 1;
template<class T>
std::string demangledName()
{
int status;
return std::string(abi::__cxa_demangle(typeid(T).name(), 0, 0, &status));
}
struct SplittableType { };
template<class T>
typename std::enable_if<std::is_base_of<SplittableType, T>::value, void>::type
safe_enumerate() { T::enumerate(); }
template<class T>
typename std::enable_if<!std::is_base_of<SplittableType, T>::value, void>::type
safe_enumerate() { }
#define PRINT_IT(r, data, elem) \
std::cout << "Data Element "; \
std::cout << std::string(indent, '>') << " " << demangledName<decltype(elem)>() << " " << BOOST_PP_STRINGIZE(elem) << std::endl; \
if(std::is_base_of<SplittableType, decltype(elem)>::value) \
{ \
indent+=1; \
safe_enumerate<decltype(elem)>(); \
indent-=1; \
}
#define NRT_MESSAGE_DATA(...) \
static void enumerate() \
{ \
BOOST_PP_SEQ_FOR_EACH(PRINT_IT, , BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) \
}
struct Image {};
struct Matrix {};
struct CalibratedImageMessage : SplittableType
{
Image img;
Matrix calibration;
NRT_MESSAGE_DATA (img, calibration);
};
struct StereoImageMessage : SplittableType
{
CalibratedImageMessage left;
CalibratedImageMessage right;
NRT_MESSAGE_DATA (left, right);
};
struct StampedStereoImageMessage : SplittableType
{
StereoImageMessage stereo;
double time;
NRT_MESSAGE_DATA(stereo, time);
};
int main()
{
StampedStereoImageMessage msg;
std::cout << "Data Members for " << demangledName<decltype(msg)>() << std::endl;
msg.enumerate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment