adapter.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2023 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 
20 template <typename... Fns>
21 constexpr auto callback(Fns&&... fns)
22 {
23  if constexpr ((lexy::is_callback<std::decay_t<Fns>> && ...))
25  std::decay_t<Fns>...>(LEXY_FWD(fns)...);
26  else
27  return _callback<void, std::decay_t<Fns>...>(LEXY_FWD(fns)...);
28 }
29 
31 template <typename ReturnType, typename... Fns>
32 constexpr auto callback(Fns&&... fns)
33 {
35 }
36 
37 template <typename Sink>
39 {
40  Sink _sink;
41 
43  using return_type = typename _cb::return_type;
44 
45  template <typename... Args>
46  constexpr auto operator()(Args&&... args) const
47  -> decltype((LEXY_DECLVAL(_cb&)(LEXY_FWD(args)), ..., LEXY_DECLVAL(_cb&&).finish()))
48  {
49  auto cb = _sink.sink();
50  (cb(LEXY_FWD(args)), ...);
51  return LEXY_MOV(cb).finish();
52  }
53 };
54 
56 template <typename Sink, typename = lexy::sink_callback<Sink>>
57 constexpr auto callback(Sink&& sink)
58 {
60 }
61 } // namespace lexy
62 
63 namespace lexy
64 {
65 template <typename MemFn>
66 struct _mem_fn_traits // MemFn is member data
67 {
68  using return_type = MemFn;
69 };
70 
71 #define LEXY_MAKE_MEM_FN_TRAITS(...) \
72  template <typename ReturnType, typename... Args> \
73  struct _mem_fn_traits<ReturnType(Args...) __VA_ARGS__> \
74  { \
75  using return_type = ReturnType; \
76  }; \
77  template <typename ReturnType, typename... Args> \
78  struct _mem_fn_traits<ReturnType(Args..., ...) __VA_ARGS__> \
79  { \
80  using return_type = ReturnType; \
81  };
82 
83 #define LEXY_MAKE_MEM_FN_TRAITS_CV(...) \
84  LEXY_MAKE_MEM_FN_TRAITS(__VA_ARGS__) \
85  LEXY_MAKE_MEM_FN_TRAITS(const __VA_ARGS__) \
86  LEXY_MAKE_MEM_FN_TRAITS(volatile __VA_ARGS__) \
87  LEXY_MAKE_MEM_FN_TRAITS(const volatile __VA_ARGS__)
88 
89 #define LEXY_MAKE_MEM_FN_TRAITS_CV_REF(...) \
90  LEXY_MAKE_MEM_FN_TRAITS_CV(__VA_ARGS__) \
91  LEXY_MAKE_MEM_FN_TRAITS_CV(&__VA_ARGS__) \
92  LEXY_MAKE_MEM_FN_TRAITS_CV(&&__VA_ARGS__)
93 
96 
97 #undef LEXY_MAKE_MEM_FN_TRAITS_CV_REF
98 #undef LEXY_MAKE_MEM_FN_TRAITS_CV
99 #undef LEXY_MAKE_MEM_FN_TRAITS
100 
101 template <typename Fn>
102 struct _mem_fn;
103 template <typename MemFn, typename T>
104 struct _mem_fn<MemFn T::*>
105 {
106  MemFn T::*_fn;
107 
109 
110  template <typename... Args>
111  constexpr auto operator()(Args&&... args) const
112  -> decltype(_detail::_mem_invoker<MemFn T::*>::invoke(_fn, LEXY_FWD(args)...))
113  {
115  }
116 };
117 
119 template <typename MemFn, typename T>
120 constexpr auto mem_fn(MemFn T::*fn)
121 {
122  return _mem_fn<MemFn T::*>{fn};
123 }
124 } // namespace lexy
125 
126 #endif // LEXY_CALLBACK_ADAPTER_HPP_INCLUDED
127 
LEXY_MOV
#define LEXY_MOV(...)
Definition: config.hpp:21
lexy::_mem_fn_traits::return_type
MemFn return_type
Definition: adapter.hpp:68
base.hpp
lexy::sink_callback
decltype(LEXY_DECLVAL(Sink).sink(LEXY_DECLVAL(Args)...)) sink_callback
Returns the type of the .sink() function.
Definition: callback/base.hpp:32
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:43
lexy::_mem_fn< MemFn T::* >::_fn
MemFn T::* _fn
Definition: adapter.hpp:106
lexy::_overloaded
Definition: callback/base.hpp:67
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:22
lexy::_mem_fn< MemFn T::* >
Definition: adapter.hpp:104
lexy
Definition: any_ref.hpp:12
lexy::is_callback
constexpr bool is_callback
Definition: callback/base.hpp:16
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:46
lexy::callback
constexpr auto callback(Fns &&... fns)
Creates a callback.
Definition: adapter.hpp:21
LEXY_MAKE_MEM_FN_TRAITS_CV_REF
#define LEXY_MAKE_MEM_FN_TRAITS_CV_REF(...)
Definition: adapter.hpp:89
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:111
lexy::_mem_fn
Definition: adapter.hpp:102
lexy::mem_fn
constexpr auto mem_fn(MemFn T::*fn)
Creates a callback from a member function.
Definition: adapter.hpp:120
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::_mem_fn< MemFn T::* >::return_type
typename _mem_fn_traits< MemFn >::return_type return_type
Definition: adapter.hpp:108
lexy::_detail::_mem_invoker
Definition: invoke.hpp:12
lexy::_mem_fn_traits
Definition: adapter.hpp:66
lexy::_cb_from_sink::_cb
lexy::sink_callback< Sink > _cb
Definition: adapter.hpp:42
LEXY_DECLVAL
#define LEXY_DECLVAL(...)
Definition: config.hpp:24
lexy::_cb_from_sink::_sink
Sink _sink
Definition: adapter.hpp:40
lexy::_cb_from_sink
Definition: adapter.hpp:38


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07