fork download
  1. // ----------------------------------------------------------
  2. // qmetatype.h simplification -------------------------------
  3. // ----------------------------------------------------------
  4.  
  5. template<typename T>
  6. struct metatype
  7. {
  8. enum { defined = 0 };
  9. };
  10.  
  11. template<typename T>
  12. struct metatype2
  13. {
  14. enum { defined = metatype<T>::defined };
  15. static inline int id() { return metatype<T>::id(); }
  16. };
  17.  
  18. template <typename T>
  19. inline int metatypeId(
  20. T * /* dummy */ = 0
  21. )
  22. {
  23. return metatype2<T>::id();
  24. }
  25.  
  26. #define register_meta_type( _type_ ) \
  27.  template<> \
  28.  struct metatype< _type_ > \
  29.  { \
  30.   enum { defined = 1 }; \
  31.   static int id() \
  32.   { \
  33.   /* Run-time registration in Qt */ \
  34.   return __COUNTER__; \
  35.   }; \
  36.  };
  37.  
  38.  
  39.  
  40. // ----------------------------------------------------------
  41. // ----------------------------------------------------------
  42. // ----------------------------------------------------------
  43.  
  44. class TestA {};
  45. register_meta_type(TestA)
  46.  
  47. class TestB {};
  48.  
  49. class TestC {};
  50. register_meta_type(TestC)
  51.  
  52. class TestD {};
  53.  
  54.  
  55. #include <type_traits>
  56.  
  57. struct _test_is_declared_metatype
  58. {
  59. /*
  60.   metatypeId<T>() is always a valid expression. So this overload is
  61.   always taken
  62.  */
  63. template<class T>
  64. static auto test(T* t) -> decltype(metatypeId<T>(), std::true_type());
  65.  
  66. static std::false_type test(...);
  67. };
  68.  
  69. template<class T>
  70. struct is_declared_metatype : decltype(_test_is_declared_metatype::test<T>(0))
  71. {
  72. };
  73.  
  74. #include <iostream>
  75. #define PRINT_DEF( _type_ ) std::cout << #_type_ << " registered ? " << is_declared_metatype< _type_ >::value << "\n";
  76. int main()
  77. {
  78. std::cout << std::boolalpha;
  79. PRINT_DEF(TestA);
  80. PRINT_DEF(TestB);
  81. PRINT_DEF(TestC);
  82. PRINT_DEF(TestD);
  83. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
TestA registered ? true
TestB registered ? true
TestC registered ? true
TestD registered ? true