c++ - Inheritance reflection in tags -
i'm working on kind of publisher/subscriber block. subscribers subscribe messages tag(s). messages in system organized in hierarchy, i'd tags reflect hierarchy. i've made small piece of code, how i'd see system
class tmsg {}; class tmsg1: public tmsg {}; class tmsg2: public tmsg {}; template<class t> struct tag {}; template<> struct tag<tmsg1>: public tag<tmsg> {}; template<> struct tag<tmsg2>: public tag<tmsg> {}; template<class t> void processmessage(t &t) { tag<t> ti; procmsgimpl(ti); } void procmsgimpl(tag<tmsg>&) { std::cout << "f" << std::endl; } void procmsgimpl(tag<tmsg1>&) { std::cout << "f1" << std::endl; } int main() { tmsg t; tmsg1 t1; tmsg2 t2; processmessage(t); processmessage(t1); processmessage(t2); return 0; }
what want avoid in snippet explicit definition inheritance of tags struct tag<tmsg1>: public tag<tmsg> {};
, if tmsg base class of tmsg1, i'd tags keep information implicitly.
would work you? idea make tmsg classes inherit wrapper , have generic tag:
#include <iostream> // use wrapper template <class b> struct base : public b { using base_type = b; }; class tmsg {}; // inherit wrapper class tmsg1: public base<tmsg> {}; // inherit wrapper class tmsg2: public base<tmsg> {}; // generic tag template<class t> struct tag : public tag<typename t::base_type>{}; // full specialization base class template <> struct tag<tmsg> {}; template<class t> void processmessage(t &t) { tag<t> ti; procmsgimpl(ti); } void procmsgimpl(tag<tmsg>&) { std::cout << "f" << std::endl; } void procmsgimpl(tag<tmsg1>&) { std::cout << "f1" << std::endl; } int main() { tmsg t; tmsg1 t1; tmsg2 t2; processmessage(t); processmessage(t1); processmessage(t2); return 0; }
Comments
Post a Comment