wrapper_mapper_impl.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_WRAPPER_MAPPER_IMPL_H_
  28. #define RTTR_WRAPPER_MAPPER_IMPL_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include "rttr/detail/misc/std_type_traits.h"
  31. #include "rttr/detail/misc/utility.h"
  32. #include <type_traits>
  33. #include <memory>
  34. namespace rttr
  35. {
  36. class type;
  37. //////////////////////////////////////////////////////////////////////////////////////
  38. template<typename T>
  39. struct wrapper_mapper<std::shared_ptr<T>>
  40. {
  41. using wrapped_type = decltype(std::shared_ptr<T>().get());
  42. using type = std::shared_ptr<T>;
  43. static RTTR_INLINE wrapped_type get(const type& obj)
  44. {
  45. return obj.get();
  46. }
  47. static RTTR_INLINE type create(const wrapped_type& t)
  48. {
  49. return type(t);
  50. }
  51. template<typename U>
  52. static std::shared_ptr<U> convert(const type& source, bool& ok)
  53. {
  54. if (auto p = rttr_cast<typename std::shared_ptr<U>::element_type*>(source.get()))
  55. {
  56. ok = true;
  57. return std::shared_ptr<U>(source, p);
  58. }
  59. else
  60. {
  61. ok = false;
  62. return std::shared_ptr<U>();
  63. }
  64. }
  65. };
  66. //////////////////////////////////////////////////////////////////////////////////////
  67. template<typename T>
  68. struct wrapper_mapper<std::reference_wrapper<T>>
  69. {
  70. using wrapped_type = decltype(std::declval<std::reference_wrapper<T>>().get());
  71. using type = std::reference_wrapper<T>;
  72. static RTTR_INLINE wrapped_type get(const type& obj)
  73. {
  74. return obj.get();
  75. }
  76. static RTTR_INLINE type create(const wrapped_type& t)
  77. {
  78. return type(t);
  79. }
  80. };
  81. //////////////////////////////////////////////////////////////////////////////////////
  82. template<typename T>
  83. struct wrapper_mapper<std::unique_ptr<T>>
  84. {
  85. using wrapped_type = decltype(std::declval<std::unique_ptr<T>>().get());
  86. using type = std::unique_ptr<T>;
  87. static RTTR_INLINE wrapped_type get(const type& obj)
  88. {
  89. return obj.get();
  90. }
  91. static RTTR_INLINE type create(const wrapped_type& t)
  92. {
  93. return type(t);
  94. }
  95. };
  96. //////////////////////////////////////////////////////////////////////////////////////
  97. template<typename T>
  98. struct wrapper_mapper<std::weak_ptr<T>>
  99. {
  100. using wrapped_type = decltype(std::declval<std::weak_ptr<T>>().lock().get());
  101. using type = std::weak_ptr<T>;
  102. static RTTR_INLINE wrapped_type get(const type& obj)
  103. {
  104. return obj.lock().get();
  105. }
  106. // there is no create method because, weak pointer can only be created by a referencing a shared_ptr.
  107. // And a tmp shared_ptr which goes out of scope immediately, result always in an empty weak_ptr.
  108. };
  109. namespace detail
  110. {
  111. //////////////////////////////////////////////////////////////////////////////////////
  112. template<typename T>
  113. using wrapper_mapper_t = typename wrapper_mapper<typename std::remove_cv<typename std::remove_reference<T>::type
  114. >::type>::wrapped_type;
  115. //////////////////////////////////////////////////////////////////////////////////////
  116. template<typename T>
  117. using is_wrapper = std::integral_constant<bool, !std::is_same<invalid_wrapper_type, wrapper_mapper_t<T>>::value >;
  118. //////////////////////////////////////////////////////////////////////////////////////
  119. template<typename T>
  120. using wrapper_address_return_type_t = conditional_t<is_wrapper<T>::value,
  121. raw_addressof_return_type_t< wrapper_mapper_t< T > >,
  122. raw_addressof_return_type_t<T>>;
  123. //////////////////////////////////////////////////////////////////////////////////////
  124. template<typename T>
  125. typename std::enable_if<is_wrapper<T>::value, raw_addressof_return_type_t< wrapper_mapper_t<T>> >::type wrapped_raw_addressof(T& obj)
  126. {
  127. using raw_wrapper_type = remove_cv_t<remove_reference_t<T>>;
  128. wrapper_mapper_t<T> value = wrapper_mapper<raw_wrapper_type>::get(obj);
  129. return raw_addressof(value);
  130. }
  131. //////////////////////////////////////////////////////////////////////////////////////
  132. template<typename T>
  133. typename std::enable_if<!is_wrapper<T>::value, raw_addressof_return_type_t<T>>::type wrapped_raw_addressof(T& obj)
  134. {
  135. return raw_addressof(obj);
  136. }
  137. //////////////////////////////////////////////////////////////////////////////////////
  138. /*!
  139. * Determine if the given type \a T is a wrapper and has the member method
  140. * 'wrapper create(const wrapper_type&)' declared.
  141. */
  142. template <typename T, typename Tp = typename std::remove_cv<typename std::remove_reference<T>::type>::type>
  143. class has_create_wrapper_func_impl
  144. {
  145. using YesType = char[1];
  146. using NoType = char[2];
  147. template <typename U, typename V, U (*)(const V&)>
  148. class check { };
  149. template <typename C>
  150. static YesType& f(check<C, wrapper_mapper_t<C>, &wrapper_mapper<C>::create>*);
  151. template <typename C>
  152. static NoType& f(...);
  153. public:
  154. static RTTR_CONSTEXPR_OR_CONST bool value = (sizeof(f<Tp>(0)) == sizeof(YesType));
  155. };
  156. template<typename T>
  157. using has_create_wrapper_func = std::integral_constant<bool, has_create_wrapper_func_impl<T>::value>;
  158. //////////////////////////////////////////////////////////////////////////////////////
  159. } // end namespace detail
  160. } // end namespace rttr
  161. #endif // RTTR_WRAPPER_MAPPER_IMPL_H_