fold.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2024 Jonathan Müller and lexy contributors
2 // SPDX-License-Identifier: BSL-1.0
3 
4 #ifndef LEXY_CALLBACK_FOLD_HPP_INCLUDED
5 #define LEXY_CALLBACK_FOLD_HPP_INCLUDED
6 
7 #include <lexy/callback/base.hpp>
8 
9 namespace lexy
10 {
11 template <bool Inplace>
12 struct _fold_sfinae;
13 template <>
14 struct _fold_sfinae<true>
15 {
16  template <typename Op, typename T, typename... Args>
17  using type = decltype(void(
19 };
20 template <>
21 struct _fold_sfinae<false>
22 {
23  template <typename Op, typename T, typename... Args>
24  using type = decltype(void(
26 };
27 
28 template <typename T, typename Arg, bool Inplace, typename Op>
29 struct _fold
30 {
31  Arg _init;
33 
35  {
37  Op _op;
38 
39  using return_type = T;
40 
41  template <typename... Args>
42  constexpr auto operator()(Args&&... args) ->
43  typename _fold_sfinae<Inplace>::template type<Op, T, Args&&...>
44  {
45  if constexpr (Inplace)
46  _detail::invoke(_op, _result, LEXY_FWD(args)...);
47  else
49  }
50 
51  constexpr T finish() &&
52  {
53  return LEXY_MOV(_result);
54  }
55  };
56 
57  constexpr auto sink() const
58  {
59  if constexpr (std::is_constructible_v<T, Arg>)
60  return _sink_callback{T(_init), _op};
61  else
62  return _sink_callback{_init(), _op};
63  }
64 };
65 
67 template <typename T, typename Arg = T, typename... Op>
68 constexpr auto fold(Arg&& init, Op&&... op)
69 {
70  auto fn = _make_overloaded(LEXY_FWD(op)...);
71  return _fold<T, std::decay_t<Arg>, false, decltype(fn)>{LEXY_FWD(init), LEXY_MOV(fn)};
72 }
73 
76 template <typename T, typename Arg = T, typename... Op>
77 constexpr auto fold_inplace(Arg&& init, Op&&... op)
78 {
79  auto fn = _make_overloaded(LEXY_FWD(op)...);
80  return _fold<T, std::decay_t<Arg>, true, decltype(fn)>{LEXY_FWD(init), LEXY_MOV(fn)};
81 }
82 } // namespace lexy
83 
84 namespace lexy
85 {
87 constexpr auto count
88  = fold_inplace<std::size_t>(0u, [](std::size_t& result, auto&&...) { ++result; });
89 } // namespace lexy
90 
91 #endif // LEXY_CALLBACK_FOLD_HPP_INCLUDED
92 
LEXY_MOV
#define LEXY_MOV(...)
Definition: config.hpp:29
base.hpp
lexy::_fold_sfinae< true >::type
decltype(void(_detail::invoke(LEXY_DECLVAL(Op), LEXY_DECLVAL(T &), LEXY_DECLVAL(Args)...))) type
Definition: fold.hpp:18
lexy::fold_inplace
constexpr auto fold_inplace(Arg &&init, Op &&... op)
Definition: fold.hpp:77
lexy::_fold_sfinae
Definition: fold.hpp:12
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:30
lexy::_fold
Definition: fold.hpp:29
lexy
Definition: any_ref.hpp:12
lexy::_op
Definition: operator.hpp:24
lexy::_fold::sink
constexpr auto sink() const
Definition: fold.hpp:57
lexy::_fold_sfinae< false >::type
decltype(void(_detail::invoke(LEXY_DECLVAL(Op), LEXY_DECLVAL(T &&), LEXY_DECLVAL(Args)...))) type
Definition: fold.hpp:25
lexy::_fold::_op
LEXY_EMPTY_MEMBER Op _op
Definition: fold.hpp:32
lexy::count
constexpr auto count
Sink that counts all arguments.
Definition: fold.hpp:88
lexy::_fold::_sink_callback::_result
T _result
Definition: fold.hpp:36
lexy::_fold::_sink_callback::_op
Op _op
Definition: fold.hpp:37
lexy::_fold::_init
Arg _init
Definition: fold.hpp:31
lexy::_detail::invoke
constexpr auto invoke(F ClassT::*f, Args &&... args) -> decltype(_mem_invoker< F ClassT::* >::invoke(f, LEXY_FWD(args)...))
Definition: invoke.hpp:56
lexy::_fold::_sink_callback
Definition: fold.hpp:34
lexy::op
typename LEXY_DECAY_DECLTYPE(Operator)::op_tag_type op
Definition: operator.hpp:41
lexy::_fold::_sink_callback::return_type
T return_type
Definition: fold.hpp:39
lexy::_make_overloaded
constexpr auto _make_overloaded(Op &&... op)
Definition: callback/base.hpp:82
lexy::_fold::_sink_callback::operator()
constexpr auto operator()(Args &&... args) -> typename _fold_sfinae< Inplace >::template type< Op, T, Args &&... >
Definition: fold.hpp:42
LEXY_EMPTY_MEMBER
#define LEXY_EMPTY_MEMBER
Definition: config.hpp:193
LEXY_DECLVAL
#define LEXY_DECLVAL(...)
Definition: config.hpp:32
lexy::_fold::_sink_callback::finish
constexpr T finish() &&
Definition: fold.hpp:51
lexy::fold
constexpr auto fold(Arg &&init, Op &&... op)
Sink that folds all the arguments with the binary operation op.
Definition: fold.hpp:68


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:16