register_wrapper_mapper_conversion.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /************************************************************************************
  2. * *
  3. * Copyright (c) 2014, 2015 - 2017 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_REGISTER_WRAPPER_MAPPER_CONVERSION_H_
  28. #define RTTR_REGISTER_WRAPPER_MAPPER_CONVERSION_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/detail/misc/misc_type_traits.h"
  31. #include <type_traits>
  32. namespace rttr
  33. {
  34. template<typename... U> struct type_list;
  35. namespace detail
  36. {
  37. /*!
  38. * Determine if the given type \a T has a wrapper_mapper method called `convert`.
  39. */
  40. template <typename T>
  41. class has_conversion_function_impl
  42. {
  43. typedef char YesType[1];
  44. typedef char NoType[2];
  45. template <typename U> static YesType& check(decltype(&wrapper_mapper<U>::convert));
  46. template <typename U> static NoType& check(...);
  47. public:
  48. static RTTR_CONSTEXPR_OR_CONST bool value = (sizeof(check<raw_type_t<T>>(0)) != sizeof(YesType));
  49. };
  50. /*!
  51. * If \a T has a wrapper_mapper function `convert` then is the same like `std::true_type`, otherwise inherits from `std::false_type`.
  52. */
  53. template<typename T>
  54. using has_wrapper_conv_func = std::integral_constant<bool, has_conversion_function_impl<T>::value>;
  55. /////////////////////////////////////////////////////////////////////////////////////
  56. /*!
  57. * Registers for the given type \p DerivedClass a conversion function
  58. * from \ref wrapper_mapper<T> from and to the types in the list `T...`.
  59. */
  60. template<typename DerivedClass, typename... T>
  61. struct create_wrapper_conversion;
  62. template<typename DerivedClass>
  63. struct create_wrapper_conversion<DerivedClass>
  64. {
  65. static RTTR_INLINE void perform() { }
  66. };
  67. template<typename DerivedClass, typename BaseClass, typename... U>
  68. struct create_wrapper_conversion<DerivedClass, BaseClass, U...>
  69. {
  70. static RTTR_INLINE void perform()
  71. {
  72. static_assert(has_base_class_list<BaseClass>::value, "The parent class has no base class list defined - please use the macro RTTR_ENABLE");
  73. type::register_converter_func(wrapper_mapper<DerivedClass>::template convert<BaseClass>);
  74. using return_type = typename function_traits<decltype(&wrapper_mapper<DerivedClass>::template convert<BaseClass>)>::return_type;
  75. // TO DO: remove raw_type_t, std::shared_ptr<const T> should also be converted, when necessary
  76. using wrapped_derived_t = raw_type_t<wrapper_mapper_t<DerivedClass>>;
  77. type::register_converter_func(wrapper_mapper<return_type>::template convert<wrapped_derived_t>);
  78. create_wrapper_conversion<DerivedClass, typename BaseClass::base_class_list>::perform();
  79. // continue with the rest
  80. create_wrapper_conversion<DerivedClass, U...>::perform();
  81. }
  82. };
  83. template<typename DerivedClass, class... BaseClassList>
  84. struct create_wrapper_conversion<DerivedClass, type_list<BaseClassList...>> : create_wrapper_conversion<DerivedClass, BaseClassList...> { };
  85. /////////////////////////////////////////////////////////////////////////////////////
  86. template<typename T, typename Enable = void>
  87. struct reg_wrapper_converter_for_base_classes
  88. {
  89. reg_wrapper_converter_for_base_classes()
  90. {
  91. }
  92. };
  93. template<typename T>
  94. struct reg_wrapper_converter_for_base_classes<T, typename std::enable_if<is_wrapper<T>::value && has_base_class_list<raw_type_t<wrapper_mapper_t<T>>>::value>::type>
  95. {
  96. reg_wrapper_converter_for_base_classes()
  97. {
  98. create_wrapper_conversion<T, typename raw_type_t<wrapper_mapper_t<T>>::base_class_list>::perform();
  99. }
  100. };
  101. /////////////////////////////////////////////////////////////////////////////////////
  102. } // end namespace detail
  103. } // end namespace rttr
  104. #endif // RTTR_REGISTER_WRAPPER_MAPPER_CONVERSION_H_