fork download
  1. #include <cstdlib>
  2.  
  3. #include <iostream>
  4. #include <typeinfo>
  5.  
  6. #include <cxxabi.h>
  7.  
  8. class AutoRegister
  9. {
  10. class InitializerPrivate
  11. {
  12. };
  13.  
  14. public:
  15. explicit AutoRegister(const InitializerPrivate&)
  16. {
  17. }
  18.  
  19. template <class Child>
  20. class Initializer : public InitializerPrivate
  21. {
  22. class StaticInit
  23. {
  24. public:
  25. StaticInit()
  26. {
  27. int status;
  28. char *childName = abi::__cxa_demangle(typeid(Child).name(), 0, 0, &status);
  29. std::cout << "Registering child: " << childName << std::endl;
  30. // XXX: qRegisterMetaType<EventFoo>("EventFoo");
  31. std::free(childName);
  32. }
  33.  
  34. void nop() const
  35. {
  36.  
  37. }
  38. };
  39.  
  40. static const StaticInit _init;
  41.  
  42. public:
  43. explicit Initializer()
  44. {
  45. _init.nop();
  46. }
  47. };
  48. };
  49.  
  50. class BaseEvent : protected virtual AutoRegister
  51. {
  52. public:
  53. explicit BaseEvent() : AutoRegister(Initializer<BaseEvent>())
  54. {
  55. }
  56. };
  57.  
  58. class EventFoo : public BaseEvent
  59. {
  60. public:
  61. explicit EventFoo() : AutoRegister(Initializer<EventFoo>())
  62. {
  63. }
  64. };
  65.  
  66. class EventBar : public BaseEvent
  67. {
  68. public:
  69. explicit EventBar() : AutoRegister(Initializer<EventBar>())
  70. {
  71. }
  72. };
  73.  
  74. template<class T>
  75. const typename AutoRegister::Initializer<T>::StaticInit AutoRegister::Initializer<T>::_init = typename AutoRegister::Initializer<T>::StaticInit();
  76.  
  77. int main(void)
  78. {
  79. return 0;
  80. }
  81.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Registering child: BaseEvent
Registering child: EventFoo
Registering child: EventBar