adapter.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_ADAPTER_HPP_INCLUDED
5 #define LEXY_CALLBACK_ADAPTER_HPP_INCLUDED
6 
7 #include <lexy/callback/base.hpp>
8 
9 namespace lexy
10 {
11 template <typename ReturnType, typename... Fns>
12 struct _callback : _overloaded<Fns...>
13 {
14  using return_type = ReturnType;
15 
16  constexpr explicit _callback(Fns... fns) : _overloaded<Fns...>(LEXY_MOV(fns)...) {}
17 };
18 
19 template <typename ReturnType, typename... Fns>
21 {
22  using return_type = ReturnType;
23 
24  template <typename State>
25  struct _with_state
26  {
28  State& _state;
29 
30  template <typename... Args>
31  constexpr return_type operator()(Args&&... args) const&&
32  {
33  return _cb(_state, LEXY_FWD(args)...);
34  }
35  };
36 
37  constexpr explicit _callback_with_state(Fns... fns) : _overloaded<Fns...>(LEXY_MOV(fns)...) {}
38 
39  template <typename State>
40  constexpr auto operator[](State& state) const
41  {
42  return _with_state<State>{*this, state};
43  }
44 };
45 
47 template <typename... Fns>
48 constexpr auto callback(Fns&&... fns)
49 {
50  if constexpr ((lexy::is_callback<std::decay_t<Fns>> && ...))
52  std::decay_t<Fns>...>(LEXY_FWD(fns)...);
53  else
54  return _callback<void, std::decay_t<Fns>...>(LEXY_FWD(fns)...);
55 }
56 template <typename ReturnType, typename... Fns>
57 constexpr auto callback(Fns&&... fns)
58 {
60 }
61 
63 template <typename... Fns>
64 constexpr auto callback_with_state(Fns&&... fns)
65 {
66  if constexpr ((lexy::is_callback<std::decay_t<Fns>> && ...))
68  std::decay_t<Fns>...>(LEXY_FWD(fns)...);
69  else
71 }
72 template <typename ReturnType, typename... Fns>
73 constexpr auto callback_with_state(Fns&&... fns)
74 {
76 }
77 
78 template <typename Sink>
80 {
81  Sink _sink;
82 
84  using return_type = typename _cb::return_type;
85 
86  template <typename... Args>
87  constexpr auto operator()(Args&&... args) const
88  -> decltype((LEXY_DECLVAL(_cb&)(LEXY_FWD(args)), ..., LEXY_DECLVAL(_cb&&).finish()))
89  {
90  auto cb = _sink.sink();
91  (cb(LEXY_FWD(args)), ...);
92  return LEXY_MOV(cb).finish();
93  }
94 };
95 
97 template <typename Sink, typename = lexy::sink_callback<Sink>>
98 constexpr auto callback(Sink&& sink)
99 {
101 }
102 } // namespace lexy
103 
104 namespace lexy
105 {
106 template <typename MemFn>
107 struct _mem_fn_traits // MemFn is member data
108 {
109  using return_type = MemFn;
110 };
111 
112 #define LEXY_MAKE_MEM_FN_TRAITS(...) \
113  template <typename ReturnType, typename... Args> \
114  struct _mem_fn_traits<ReturnType(Args...) __VA_ARGS__> \
115  { \
116  using return_type = ReturnType; \
117  }; \
118  template <typename ReturnType, typename... Args> \
119  struct _mem_fn_traits<ReturnType(Args..., ...) __VA_ARGS__> \
120  { \
121  using return_type = ReturnType; \
122  };
123 
124 #define LEXY_MAKE_MEM_FN_TRAITS_CV(...) \
125  LEXY_MAKE_MEM_FN_TRAITS(__VA_ARGS__) \
126  LEXY_MAKE_MEM_FN_TRAITS(const __VA_ARGS__) \
127  LEXY_MAKE_MEM_FN_TRAITS(volatile __VA_ARGS__) \
128  LEXY_MAKE_MEM_FN_TRAITS(const volatile __VA_ARGS__)
129 
130 #define LEXY_MAKE_MEM_FN_TRAITS_CV_REF(...) \
131  LEXY_MAKE_MEM_FN_TRAITS_CV(__VA_ARGS__) \
132  LEXY_MAKE_MEM_FN_TRAITS_CV(&__VA_ARGS__) \
133  LEXY_MAKE_MEM_FN_TRAITS_CV(&&__VA_ARGS__)
134 
137 
138 #undef LEXY_MAKE_MEM_FN_TRAITS_CV_REF
139 #undef LEXY_MAKE_MEM_FN_TRAITS_CV
140 #undef LEXY_MAKE_MEM_FN_TRAITS
141 
142 template <typename Fn>
143 struct _mem_fn;
144 template <typename MemFn, typename T>
145 struct _mem_fn<MemFn T::*>
146 {
147  MemFn T::*_fn;
148 
150 
151  template <typename... Args>
152  constexpr auto operator()(Args&&... args) const
153  -> decltype(_detail::_mem_invoker<MemFn T::*>::invoke(_fn, LEXY_FWD(args)...))
154  {
156  }
157 };
158 
160 template <typename MemFn, typename T>
161 constexpr auto mem_fn(MemFn T::*fn)
162 {
163  return _mem_fn<MemFn T::*>{fn};
164 }
165 } // namespace lexy
166 
167 #endif // LEXY_CALLBACK_ADAPTER_HPP_INCLUDED
168 
LEXY_MOV
#define LEXY_MOV(...)
Definition: config.hpp:29
lexy::_callback_with_state
Definition: adapter.hpp:20
lexy::_mem_fn_traits::return_type
MemFn return_type
Definition: adapter.hpp:109
lexy::_callback_with_state::_with_state
Definition: adapter.hpp:25
base.hpp
lexy::_callback_with_state::return_type
ReturnType return_type
Definition: adapter.hpp:22
lexy::sink_callback
decltype(LEXY_DECLVAL(Sink).sink(LEXY_DECLVAL(Args)...)) sink_callback
Returns the type of the .sink() function.
Definition: callback/base.hpp:39
lexy::callback_with_state
constexpr auto callback_with_state(Fns &&... fns)
Creates a callback that also receives the parse state.
Definition: adapter.hpp:64
lexy::_callback::return_type
ReturnType return_type
Definition: adapter.hpp:14
lexy::_cb_from_sink::return_type
typename _cb::return_type return_type
Definition: adapter.hpp:84
lexy::_mem_fn< MemFn T::* >::_fn
MemFn T::* _fn
Definition: adapter.hpp:147
lexy::_overloaded
Definition: callback/base.hpp:74
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:30
lexy::_mem_fn< MemFn T::* >
Definition: adapter.hpp:145
lexy
Definition: any_ref.hpp:12
lexy::is_callback
constexpr bool is_callback
Definition: callback/base.hpp:16
lexy::_callback_with_state::_callback_with_state
constexpr _callback_with_state(Fns... fns)
Definition: adapter.hpp:37
lexy::_callback
Definition: adapter.hpp:12
lexy::_cb_from_sink::operator()
constexpr auto operator()(Args &&... args) const -> decltype((LEXY_DECLVAL(_cb &)(LEXY_FWD(args)),..., LEXY_DECLVAL(_cb &&).finish()))
Definition: adapter.hpp:87
lexy::callback
constexpr auto callback(Fns &&... fns)
Creates a callback.
Definition: adapter.hpp:48
LEXY_MAKE_MEM_FN_TRAITS_CV_REF
#define LEXY_MAKE_MEM_FN_TRAITS_CV_REF(...)
Definition: adapter.hpp:130
lexy::_mem_fn< MemFn T::* >::operator()
constexpr auto operator()(Args &&... args) const -> decltype(_detail::_mem_invoker< MemFn T::* >::invoke(_fn, LEXY_FWD(args)...))
Definition: adapter.hpp:152
lexy::_callback_with_state::_with_state::_cb
const _callback_with_state & _cb
Definition: adapter.hpp:27
lexy::_mem_fn
Definition: adapter.hpp:143
lexy::mem_fn
constexpr auto mem_fn(MemFn T::*fn)
Creates a callback from a member function.
Definition: adapter.hpp:161
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::_callback::_callback
constexpr _callback(Fns... fns)
Definition: adapter.hpp:16
lexy::_callback_with_state::_with_state::_state
State & _state
Definition: adapter.hpp:28
lexy::_mem_fn< MemFn T::* >::return_type
typename _mem_fn_traits< MemFn >::return_type return_type
Definition: adapter.hpp:149
lexy::_detail::_mem_invoker
Definition: invoke.hpp:12
lexy::_mem_fn_traits
Definition: adapter.hpp:107
lexy::_cb_from_sink::_cb
lexy::sink_callback< Sink > _cb
Definition: adapter.hpp:83
LEXY_DECLVAL
#define LEXY_DECLVAL(...)
Definition: config.hpp:32
lexy::_callback_with_state::_with_state::operator()
constexpr return_type operator()(Args &&... args) const &&
Definition: adapter.hpp:31
lexy::_cb_from_sink::_sink
Sink _sink
Definition: adapter.hpp:81
lexy::_callback_with_state::operator[]
constexpr auto operator[](State &state) const
Definition: adapter.hpp:40
lexy::_cb_from_sink
Definition: adapter.hpp:79


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