Micro template library A library for building device drivers
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

227 行
8.7 KiB

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