view_adaptor.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // Copyright (C) 2022 T. Zachary Laine
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_STL_INTERFACES_VIEW_ADAPTOR_HPP
  7. #define BOOST_STL_INTERFACES_VIEW_ADAPTOR_HPP
  8. #include <boost/stl_interfaces/config.hpp>
  9. #include <boost/stl_interfaces/detail/view_closure.hpp>
  10. #include <boost/type_traits/is_detected.hpp>
  11. #include <tuple>
  12. #include <type_traits>
  13. #if !defined(BOOST_STL_INTERFACES_DOXYGEN)
  14. #if defined(__cpp_lib_ranges) && 202202L <= __cpp_lib_ranges
  15. #define BOOST_STL_INTERFACES_USE_CPP23_STD_RANGE_ADAPTOR_CLOSURE 1
  16. #else
  17. #define BOOST_STL_INTERFACES_USE_CPP23_STD_RANGE_ADAPTOR_CLOSURE 0
  18. #endif
  19. #if !BOOST_STL_INTERFACES_USE_CPP23_STD_RANGE_ADAPTOR_CLOSURE && \
  20. BOOST_STL_INTERFACES_USE_CONCEPTS && defined(__GNUC__) && 12 <= __GNUC__
  21. #define BOOST_STL_INTERFACES_USE_LIBSTDCPP_GCC12_RANGE_ADAPTOR_CLOSURE 1
  22. #else
  23. #define BOOST_STL_INTERFACES_USE_LIBSTDCPP_GCC12_RANGE_ADAPTOR_CLOSURE 0
  24. #endif
  25. #if !BOOST_STL_INTERFACES_USE_CPP23_STD_RANGE_ADAPTOR_CLOSURE && \
  26. defined(_MSC_VER) && _MSC_VER <= 1929
  27. #define BOOST_STL_INTERFACES_NEED_VS_COMPATIBLE_RANGE_ADAPTOR_CLOSURE 1
  28. #else
  29. #define BOOST_STL_INTERFACES_NEED_VS_COMPATIBLE_RANGE_ADAPTOR_CLOSURE 0
  30. #endif
  31. #if !BOOST_STL_INTERFACES_USE_CPP23_STD_RANGE_ADAPTOR_CLOSURE && \
  32. !BOOST_STL_INTERFACES_USE_LIBSTDCPP_GCC12_RANGE_ADAPTOR_CLOSURE && \
  33. !BOOST_STL_INTERFACES_NEED_VS_COMPATIBLE_RANGE_ADAPTOR_CLOSURE
  34. #define BOOST_STL_INTERFACES_DEFINE_CUSTOM_RANGE_ADAPTOR_CLOSURE 1
  35. #else
  36. #define BOOST_STL_INTERFACES_DEFINE_CUSTOM_RANGE_ADAPTOR_CLOSURE 0
  37. #endif
  38. #endif
  39. namespace boost { namespace stl_interfaces {
  40. namespace detail {
  41. template<typename F, typename... Args>
  42. using invocable_expr =
  43. decltype(std::declval<F>()(std::declval<Args>()...));
  44. template<typename F, typename... Args>
  45. constexpr bool is_invocable_v =
  46. is_detected_v<invocable_expr, F, Args...>;
  47. template<typename Func, typename... CapturedArgs>
  48. struct bind_back_t
  49. {
  50. static_assert(std::is_move_constructible<Func>::value, "");
  51. #if defined(__cpp_fold_expressions)
  52. static_assert(
  53. (std::is_move_constructible<CapturedArgs>::value && ...), "");
  54. #endif
  55. template<typename F, typename... Args>
  56. explicit constexpr bind_back_t(int, F && f, Args &&... args) :
  57. f_((F &&) f), bound_args_((Args &&) args...)
  58. {
  59. static_assert(sizeof...(Args) == sizeof...(CapturedArgs), "");
  60. }
  61. template<typename... Args>
  62. constexpr decltype(auto) operator()(Args &&... args) &
  63. {
  64. return call_impl(*this, indices(), (Args &&) args...);
  65. }
  66. template<typename... Args>
  67. constexpr decltype(auto) operator()(Args &&... args) const &
  68. {
  69. return call_impl(*this, indices(), (Args &&) args...);
  70. }
  71. template<typename... Args>
  72. constexpr decltype(auto) operator()(Args &&... args) &&
  73. {
  74. return call_impl(
  75. std::move(*this), indices(), (Args &&) args...);
  76. }
  77. template<typename... Args>
  78. constexpr decltype(auto) operator()(Args &&... args) const &&
  79. {
  80. return call_impl(
  81. std::move(*this), indices(), (Args &&) args...);
  82. }
  83. private:
  84. using indices = std::index_sequence_for<CapturedArgs...>;
  85. template<typename T, size_t... I, typename... Args>
  86. static constexpr decltype(auto)
  87. call_impl(T && this_, std::index_sequence<I...>, Args &&... args)
  88. {
  89. return ((T &&) this_)
  90. .f_((Args &&) args...,
  91. std::get<I>(((T &&) this_).bound_args_)...);
  92. }
  93. Func f_;
  94. std::tuple<CapturedArgs...> bound_args_;
  95. };
  96. template<typename Func, typename... Args>
  97. using bind_back_result =
  98. bind_back_t<std::decay_t<Func>, std::decay_t<Args>...>;
  99. }
  100. /** An implementation of `std::bind_back()` from C++23. */
  101. template<typename Func, typename... Args>
  102. constexpr auto bind_back(Func && f, Args &&... args)
  103. {
  104. return detail::bind_back_result<Func, Args...>(
  105. 0, (Func &&) f, (Args &&) args...);
  106. }
  107. #if BOOST_STL_INTERFACES_DEFINE_CUSTOM_RANGE_ADAPTOR_CLOSURE || \
  108. defined(BOOST_STL_INTERFACES_DOXYGEN)
  109. /** A backwards-compatible implementation of C++23's
  110. `std::ranges::range_adaptor_closure`. `range_adaptor_closure` may be
  111. a struct template or may be an alias, as required to maintain
  112. compatability with the standard library's view adaptors. */
  113. #if BOOST_STL_INTERFACES_USE_CONCEPTS
  114. template<typename D>
  115. requires std::is_class_v<D> && std::same_as<D, std::remove_cv_t<D>>
  116. #else
  117. template<
  118. typename D,
  119. typename Enable = std::enable_if_t<
  120. std::is_class<D>::value &&
  121. std::is_same<D, std::remove_cv_t<D>>::value>>
  122. #endif
  123. struct range_adaptor_closure;
  124. namespace detail {
  125. #if BOOST_STL_INTERFACES_USE_CONCEPTS
  126. template<typename T>
  127. concept range_adaptor_closure_ = std::derived_from<
  128. std::remove_cvref_t<T>,
  129. range_adaptor_closure<std::remove_cvref_t<T>>>;
  130. #else
  131. template<typename T>
  132. using range_adaptor_closure_tag_expr = typename range_adaptor_closure<
  133. T>::inheritance_tag_with_an_unlikely_name_;
  134. template<typename T>
  135. constexpr bool range_adaptor_closure_ =
  136. is_detected_v<range_adaptor_closure_tag_expr, remove_cvref_t<T>>;
  137. #endif
  138. }
  139. #endif
  140. #if BOOST_STL_INTERFACES_USE_CPP23_STD_RANGE_ADAPTOR_CLOSURE
  141. template<typename D>
  142. using range_adaptor_closure = std::ranges::range_adaptor_closure<D>;
  143. #elif BOOST_STL_INTERFACES_USE_LIBSTDCPP_GCC12_RANGE_ADAPTOR_CLOSURE
  144. template<typename D>
  145. using range_adaptor_closure = std::views::__adaptor::_RangeAdaptorClosure;
  146. #elif BOOST_STL_INTERFACES_NEED_VS_COMPATIBLE_RANGE_ADAPTOR_CLOSURE
  147. template<typename D>
  148. using range_adaptor_closure = detail::pipeable<D>;
  149. #else
  150. #if BOOST_STL_INTERFACES_USE_CONCEPTS
  151. template<typename D>
  152. requires std::is_class_v<D> && std::same_as<D, std::remove_cv_t<D>>
  153. #else
  154. template<typename D, typename>
  155. #endif
  156. struct range_adaptor_closure
  157. {
  158. #if BOOST_STL_INTERFACES_USE_CONCEPTS
  159. template<typename T>
  160. requires std::invocable<D, T>
  161. #else
  162. template<
  163. typename T,
  164. typename Enable = std::enable_if_t<detail::is_invocable_v<D, T>>>
  165. #endif
  166. [[nodiscard]] friend constexpr decltype(auto) operator|(T && t, D && d)
  167. {
  168. return std::move(d)((T &&) t);
  169. }
  170. #if BOOST_STL_INTERFACES_USE_CONCEPTS
  171. template<typename T>
  172. requires std::invocable<D const &, T>
  173. #else
  174. template<
  175. typename T,
  176. typename Enable =
  177. std::enable_if_t<detail::is_invocable_v<D const &, T>>>
  178. #endif
  179. [[nodiscard]] friend constexpr decltype(auto)
  180. operator|(T && t, D const & d)
  181. {
  182. return d((T &&) t);
  183. }
  184. using inheritance_tag_with_an_unlikely_name_ = int;
  185. };
  186. #endif
  187. //[closure_defn
  188. /** An invocable consisting of a contained invocable `f`. Calling
  189. `operator()` with some argument `t` calls `f(t)` and returns the
  190. result. This type is typically used to capture a the result of a call
  191. to `bind_back()`. */
  192. template<typename F>
  193. struct closure : range_adaptor_closure<closure<F>>
  194. {
  195. constexpr closure(F f) : f_(f) {}
  196. #if BOOST_STL_INTERFACES_USE_CONCEPTS
  197. template<typename T>
  198. requires std::invocable<F const &, T>
  199. #else
  200. template<
  201. typename T,
  202. typename Enable =
  203. std::enable_if_t<detail::is_invocable_v<F const &, T>>>
  204. #endif
  205. constexpr decltype(auto) operator()(T && t) const &
  206. {
  207. return f_((T &&) t);
  208. }
  209. #if BOOST_STL_INTERFACES_USE_CONCEPTS
  210. template<typename T>
  211. requires std::invocable<F &&, T>
  212. #else
  213. template<
  214. typename T,
  215. typename Enable = std::enable_if_t<detail::is_invocable_v<F &&, T>>>
  216. #endif
  217. constexpr decltype(auto) operator()(T && t) &&
  218. {
  219. return std::move(f_)((T &&) t);
  220. }
  221. private:
  222. F f_;
  223. };
  224. //]
  225. namespace detail {
  226. #if !BOOST_STL_INTERFACES_USE_CONCEPTS
  227. template<typename F, bool Invocable, typename... Args>
  228. struct adaptor_impl
  229. {
  230. static constexpr decltype(auto) call(F const & f, Args &&... args)
  231. {
  232. return f((Args &&) args...);
  233. }
  234. };
  235. template<typename F, typename... Args>
  236. struct adaptor_impl<F, false, Args...>
  237. {
  238. static constexpr auto call(F const & f, Args &&... args)
  239. {
  240. using closure_func =
  241. std::decay_t<decltype(stl_interfaces::bind_back(
  242. f, (Args &&) args...))>;
  243. return closure<closure_func>(
  244. stl_interfaces::bind_back(f, (Args &&) args...));
  245. }
  246. };
  247. #endif
  248. }
  249. //[adaptor_defn
  250. /** Adapts an invocable `f` as a view adaptor. Calling
  251. `operator(args...)` will either: call `f(args...)` and return the
  252. result, if `f(args...)` is well-formed; or return
  253. `closure(stl_interfaces::bind_back(f, args...))` otherwise. */
  254. template<typename F>
  255. struct adaptor
  256. {
  257. constexpr adaptor(F f) : f_(f) {}
  258. // clang-format off
  259. template<typename... Args>
  260. constexpr auto operator()(Args &&... args) const
  261. // clang-format on
  262. {
  263. #if BOOST_STL_INTERFACES_USE_CONCEPTS
  264. if constexpr (std::is_invocable_v<F const &, Args...>) {
  265. return f_((Args &&) args...);
  266. } else {
  267. return closure(
  268. stl_interfaces::bind_back(f_, (Args &&) args...));
  269. }
  270. #else
  271. return detail::adaptor_impl<
  272. F const &,
  273. detail::is_invocable_v<F const &, Args...>,
  274. Args...>::call(f_, (Args &&) args...);
  275. #endif
  276. }
  277. private:
  278. F f_;
  279. };
  280. //]
  281. }}
  282. #endif