Micro template library A library for building device drivers
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

242 řádky
9.5 KiB

  1. /*!
  2. * \file detection.h
  3. * \brief Detection idiom based on WG21's \ref N4502 from Walter E. Brown
  4. *
  5. * \copyright
  6. * Copyright (C) 2018 Christos Choutouridis <christos@choutouridis.net>\n
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.\n
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.\n
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * \anchor N4502 www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4502.pdf
  19. */
  20. #ifndef __utl_meta_detection_h__
  21. #define __utl_meta_detection_h__
  22. #include <utl/core/impl.h>
  23. #include <utl/meta/operations.h>
  24. #include <type_traits>
  25. /*!
  26. * \ingroup meta
  27. * \defgroup detection
  28. * Detection idiom support header.
  29. */
  30. //! @{
  31. namespace utl {
  32. namespace meta {
  33. /*!
  34. * \name void_t implementation
  35. */
  36. //! @{
  37. #if defined(UTL_WORKAROUND_CWG_1558)
  38. template<typename... _Ts>
  39. struct void_ {
  40. using type = void;
  41. };
  42. //! void_t type alias
  43. template<typename... _Ts>
  44. using void_t = eval_t<void_<_Ts...>>;
  45. #else
  46. //! void_ meta-function that maps a sequence of any types to the type void
  47. template <typename...> using void_ = void;
  48. //! void_t meta-function that maps a sequence of any types to the type void
  49. template <typename...> using void_t = void;
  50. #endif
  51. //! @}
  52. /*!
  53. * Not a type to use in detected idiom. This type can
  54. * not be constructed, destructed or copied.
  55. */
  56. struct nat_ {
  57. nat_() = delete;
  58. ~nat_() = delete;
  59. nat_(nat_ const&) = delete;
  60. void operator = (nat_ const&) = delete;
  61. };
  62. //! \name Detector for detection idiom
  63. //! @{
  64. namespace details {
  65. template <typename Default,
  66. typename AlwaysVoid,
  67. template<typename...> class Op, typename... Args>
  68. struct detector {
  69. using detected = false_;
  70. using type = Default;
  71. };
  72. template <typename Default,
  73. template<typename...> class Op, typename... Args>
  74. struct detector <Default, void_t<Op<Args...>>, Op, Args...> {
  75. using detected = true_;
  76. using type = Op<Args...>;
  77. };
  78. //! helper for detected_or_t
  79. template <typename Default,
  80. template<typename...> class Op, typename... Args>
  81. using detected_or = detector<Default, void, Op, Args...>;
  82. } // namespace details
  83. //! @}
  84. /*!
  85. * \name detection interface
  86. */
  87. //! @{
  88. /*!
  89. * Checks if Op<Args...> is a valid expression without evaluating it.
  90. *
  91. * \tparam Op a meta-callback function to pass Args...
  92. * \tparam Args... types to pass to Op for checking
  93. * \return status of the operation [bool_]
  94. * \arg true_ if Op<Args...> is valid expression
  95. * \arg false_ if Op<Args...> is not valid
  96. *
  97. * \code
  98. * // archetypal alias for a copy assignment operation
  99. * template< class T > using copy_assign_t = decltype( declval<T&>() = declval<T const &>() );
  100. *
  101. * template< class T > using is_copy_assignable = is_detected< copy_assign_t, T >;
  102. * \endcode
  103. */
  104. template <template<typename...> class Op, typename... Args>
  105. using is_detected = typename details::detector<nat_, void, Op, Args...>::detected;
  106. //! Detection predicate
  107. template< template<typename...> class Op, typename... Args>
  108. constexpr bool is_detected_v = is_detected<Op, Args...>::value;
  109. /*!
  110. * Detection tool that evaluates to Op<Args...> if it's valid and to nat_ if not
  111. *
  112. * \tparam Op metafunction detector
  113. * \tparam Args... The arguments to pass to \p Op and check if is well formed
  114. * \return The result type
  115. * \arg Op<Args...> if is well formed
  116. * \arg nat_ if Op<Args...> is ill formed
  117. *
  118. * \code
  119. * template <typename T> using try_type = typename T::type; // detector
  120. * template <typename T> using try_ppT = decltype (++(std::declval<T>())); // detector
  121. * static_assert( std::is_same<nat_, detected_t<try_type, A<int>> >(), ""); // detection failed
  122. * static_assert( std::is_same<A<int>&, detected_t<try_ppT, A<int>> >(), ""); // detection succeed
  123. *
  124. * // if mFun<int, int> is well formed
  125. * static_assert(std::is_same< mFun<int, int>, detected_t<mFun, int, int> >(), "");
  126. * \endcode
  127. */
  128. template <template<typename...> class Op, typename... Args>
  129. using detected_t = eval_t <
  130. details::detector<nat_, void, Op, Args...>
  131. >;
  132. /*!
  133. * Detection tool that evaluates to Op<Args...> if it's valid and to \p Default if not
  134. *
  135. * \tparam Default The resulting type if detection fail
  136. * \tparam Op metafunction detector
  137. * \tparam Args... The arguments to pass to \p Op and check if is well formed
  138. * \return The result type
  139. * \arg Op<Args...> if is well formed
  140. * \arg Default if Op<Args...> is ill formed
  141. *
  142. * \code
  143. * template <typename T> using try_type = typename T::type; // detector
  144. * template <typename T> using try_ppT = decltype (++(std::declval<T>())); // detector
  145. * static_assert( std::is_same<Foo, detected_or_t<Foo, try_type, A<int>> >(), ""); // detection failed
  146. * static_assert( std::is_same<A<int>&, detected_or_t<Foo, try_ppT, A<int>> >(), ""); // detection succeed
  147. *
  148. * // if mFun<int, int> is well formed
  149. * static_assert(std::is_same< mFun<int, int>, detected_or_t<void, mFun, int, int> >(), "");
  150. * \endcode
  151. */
  152. template <typename Default,
  153. template<typename...> class Op, typename... Args>
  154. using detected_or_t = eval_t <
  155. details::detected_or<Default, Op, Args...>
  156. >;
  157. /*!
  158. * Detection tool that evaluates to true_ if evaluation of Op<Args...>
  159. * is \p Expected and to false_ if not
  160. *
  161. * \tparam Expected The expected resulting type if detection succeed
  162. * \tparam Op metafunction detector
  163. * \tparam Args... The arguments to pass to \p Op and check if is well formed
  164. * \return The result type
  165. * \arg true_ if Op<Args...> is well formed and evaluate to Expected
  166. * \arg false_ Any other case
  167. *
  168. * \code
  169. * template <typename T> using try_type = typename T::type; // detector
  170. * template <typename T> using try_ppT = decltype (++(std::declval<T>())); // detector
  171. * static_assert( std::is_same<false_, is_detected_exact<int, try_type, A<int>> >(), ""); // detection failed
  172. * static_assert( std::is_same<true_, is_detected_exact<A<int>&, try_ppT, A<int>> >(), ""); // detection succeed
  173. *
  174. * // if mFun<int, int> is well formed
  175. * static_assert(std::is_same< true_, is_detected_exact<mFun<int, int>, mFun, int, int> >(), "");
  176. * \endcode
  177. */
  178. template <typename Expected,
  179. template<typename...> class Op, typename... Args >
  180. using is_detected_exact = eval_t <
  181. same_<Expected, detected_t<Op, Args...>>
  182. >;
  183. //! evaluates to true if evaluation of Op<Args...> is \p Expected and to false if not
  184. template <typename Expected,
  185. template<typename...> class Op, typename... Args >
  186. constexpr bool is_detected_exact_v = is_detected_exact< Expected, Op, Args...>::value;
  187. /*!
  188. * Detection tool that evaluates to true_ if evaluation of Op<Args...> is convertible
  189. * to \p To and to false_ if not
  190. *
  191. * \tparam To The to convert to if detection succeed
  192. * \tparam Op metafunction detector
  193. * \tparam Args... The arguments to pass to \p Op and check if is well formed
  194. * \return The result type
  195. * \arg true_ if Op<Args...> is well formed and convertible to To
  196. * \arg false_ Any other case
  197. *
  198. * \code
  199. * template <typename T> using try_type = typename T::type; // detector
  200. * template <typename T> using try_ppT = decltype (++(std::declval<T>())); // detector
  201. * static_assert( std::is_same<false_, is_detected_convertible<Foo, try_type, A<int>> >(), ""); // detection failed
  202. * static_assert( std::is_same<true_, is_detected_convertible<A<int>&&, try_ppT, A<int>> >(), "");// detection succeed
  203. *
  204. * // if mFun<int, int> is well formed but not convertible to Foo
  205. * static_assert(std::is_same< false_, is_detected_convertible<Foo, mFun, int, int> >(), "");
  206. * \endcode
  207. */
  208. template <typename To,
  209. template<typename...> class Op, typename... Args >
  210. using is_detected_convertible = eval_t <
  211. std::is_convertible< detected_t<Op, Args...>, To >
  212. >;
  213. //! evaluates to true if evaluation of Op<Args...> is convertible to \p To
  214. //! and to false if not
  215. template <typename To,
  216. template<typename...> class Op, typename... Args >
  217. constexpr bool is_detected_convertible_v =
  218. is_detected_convertible<To, Op, Args...>::value;
  219. //! @}
  220. }}
  221. //!@}
  222. #endif /* __utl_meta_detection_h__ */