visitor_registration.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /************************************************************************************
  2. * *
  3. * Copyright (c) 2014 - 2018 Axel Menzel <info@rttr.org> *
  4. * *
  5. * This file is part of RTTR (Run Time Type Reflection) *
  6. * License: MIT License *
  7. * *
  8. * Permission is hereby granted, free of charge, to any person obtaining *
  9. * a copy of this software and associated documentation files (the "Software"), *
  10. * to deal in the Software without restriction, including without limitation *
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  12. * and/or sell copies of the Software, and to permit persons to whom the *
  13. * Software is furnished to do so, subject to the following conditions: *
  14. * *
  15. * The above copyright notice and this permission notice shall be included in *
  16. * all copies or substantial portions of the Software. *
  17. * *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
  24. * SOFTWARE. *
  25. * *
  26. *************************************************************************************/
  27. #ifndef RTTR_VISITOR_REGISTRATION_H_
  28. #define RTTR_VISITOR_REGISTRATION_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/type_list.h"
  31. #include <type_traits>
  32. namespace rttr
  33. {
  34. namespace detail
  35. {
  36. /* This is a slightly modified version (>= VS2015 compatible) of the code from Raul Ramos. */
  37. /*
  38. MIT License
  39. Copyright (c) 2018 Raul Ramos
  40. */
  41. // basic operations
  42. template<typename T, typename TL>
  43. struct push_back;
  44. template<typename T, typename...TS>
  45. struct push_back<T, type_list<TS...>> {
  46. using type = type_list<TS..., T>;
  47. };
  48. template<typename T, typename Enable = void>
  49. struct is_class_complete_impl : std::false_type {};
  50. template<typename T>
  51. struct is_class_complete_impl<T, enable_if_t<std::is_object<T>::value &&
  52. !std::is_pointer<T>::value &&
  53. (sizeof(T) > 0)
  54. >
  55. > : std::true_type {};
  56. template<typename T>
  57. using is_class_complete = std::integral_constant<bool, is_class_complete_impl<T>::value>;
  58. } // end namespace detail
  59. } // end namespace rttr
  60. #define DECLARE_TL(_name) DECLARE_TL_IMPL(_name, __COUNTER__)
  61. #define DECLARE_TL_IMPL(_name, _start)\
  62. /* Declare the type list meta-variable and initialize with an empty list */\
  63. template<size_t IDX>\
  64. struct _name##_history;\
  65. \
  66. template<> struct _name##_history<_start> {\
  67. using type = rttr::type_list<>;\
  68. };\
  69. \
  70. /* Check if the entry at "IDX" exists */\
  71. template <size_t IDX>\
  72. using _name##_is_defined = rttr::detail::is_class_complete<_name##_history<IDX>>;\
  73. \
  74. \
  75. /* Read from an index IDX */\
  76. template<size_t IDX, bool = std::is_same<std::true_type, _name##_is_defined<IDX>>::value>\
  77. struct _name##_read;\
  78. \
  79. template<size_t IDX>\
  80. struct _name##_read<IDX, true> {\
  81. using type = typename _name##_history<IDX>::type;\
  82. };\
  83. \
  84. template<size_t IDX>\
  85. struct _name##_read<IDX, false> {\
  86. using type = typename std::conditional< \
  87. (IDX > _start), /* Should we stop searching? */\
  88. typename _name##_read<IDX-1>::type, /* No */\
  89. rttr::type_list<> /* Yes => failed => empty type_list */\
  90. >::type;\
  91. }
  92. #define READ_TL(_name) typename _name##_read<__COUNTER__ - 1>::type
  93. #define ADD_TL_IMPL(_name, _class, _idx)\
  94. /* Define the current type_list at index _idx */\
  95. template<>\
  96. struct _name##_history<_idx> {\
  97. using previous = typename _name##_read<_idx - 1>::type;\
  98. using type = typename rttr::detail::push_back<_class, previous>::type;\
  99. }
  100. #define ADD_TL(_name, _class) ADD_TL_IMPL(_name, _class, __COUNTER__)
  101. DECLARE_TL(rttr_visitor_list);
  102. #define RTTR_REGISTER_VISITOR(_class) ADD_TL(rttr_visitor_list, _class)
  103. #endif // RTTR_VISITOR_REGISTRATION_H_