Go to the documentation of this file.
4 #ifndef LEXY_CALLBACK_CONTAINER_HPP_INCLUDED
5 #define LEXY_CALLBACK_CONTAINER_HPP_INCLUDED
13 template <
typename Container>
15 template <
typename Container>
16 constexpr
auto _has_reserve = _detail::is_detected<_detect_reserve, Container>;
18 template <
typename Container>
20 template <
typename Container>
21 constexpr
auto _has_append = _detail::is_detected<_detect_append, Container>;
27 template <
typename Container>
34 template <
typename C = Container,
typename U>
40 template <
typename C = Container,
typename... Args>
53 template <
typename Container,
typename AllocFn>
60 template <
typename State>
66 constexpr Container
operator()(Container&& container)
const
75 template <
typename... Args>
81 if constexpr (_has_reserve<Container>)
82 result.reserve(
sizeof...(args));
83 (result.emplace_back(
LEXY_FWD(args)), ...);
88 template <
typename State>
94 template <
typename State>
95 constexpr
auto sink(State& state)
const
101 template <
typename Container>
115 template <
typename... Args>
121 if constexpr (_has_reserve<Container>)
122 result.reserve(
sizeof...(args));
123 (result.emplace_back(
LEXY_FWD(args)), ...);
126 template <
typename C = Container,
typename... Args>
131 if constexpr (_has_reserve<Container>)
132 result.reserve(
sizeof...(args));
133 (result.emplace_back(
LEXY_FWD(args)), ...);
141 template <
typename C = Container>
147 template <
typename AllocFn>
154 return allocator([](
const auto& alloc) {
return alloc; });
160 template <
typename Container>
167 template <
typename Container>
174 template <
typename C = Container,
typename U>
180 template <
typename C = Container,
typename... Args>
193 template <
typename Container,
typename AllocFn>
200 template <
typename State>
215 template <
typename... Args>
221 if constexpr (_has_reserve<Container>)
222 result.reserve(
sizeof...(args));
223 (result.emplace(
LEXY_FWD(args)), ...);
228 template <
typename State>
234 template <
typename State>
235 constexpr
auto sink(State& state)
const
241 template <
typename Container>
255 template <
typename... Args>
261 if constexpr (_has_reserve<Container>)
262 result.reserve(
sizeof...(args));
263 (result.emplace(
LEXY_FWD(args)), ...);
267 template <
typename C = Container,
typename... Args>
272 if constexpr (_has_reserve<Container>)
273 result.reserve(
sizeof...(args));
274 (result.emplace(
LEXY_FWD(args)), ...);
282 template <
typename C = Container>
288 template <
typename AllocFn>
295 return allocator([](
const auto& alloc) {
return alloc; });
301 template <
typename T>
308 template <
typename Container>
318 template <
typename... Tail>
319 constexpr Container
_call(Container&& head, Tail&&... tail)
const
321 if constexpr (
sizeof...(Tail) == 0)
325 if constexpr (_has_reserve<Container>)
327 auto total_size = (head.size() + ... + tail.size());
328 head.reserve(total_size);
331 auto append = [&head](Container&& container) {
332 if constexpr (_has_append<Container>)
338 for (
auto& elem : container)
348 template <
typename... Args>
368 else if constexpr (_has_append<Container>)
374 if constexpr (_has_reserve<Container>)
376 auto capacity =
_result.capacity();
377 auto total_size =
_result.size() + container.size();
378 if (total_size > capacity)
381 auto exp_capacity = 2 * capacity;
382 if (total_size > exp_capacity)
389 for (
auto& elem : container)
406 template <
typename Container>
413 template <
typename Container,
typename Callback>
418 template <
typename C = Container>
425 template <
typename... Args>
441 template <
typename Callback>
450 template <
typename... Args>
468 template <
typename Container,
typename Callback>
478 template <
typename C = Container>
479 constexpr
auto sink(
const typename C::allocator_type& allocator)
const
490 template <
typename Callback>
493 using callback_t = std::decay_t<Callback>;
494 static_assert(std::is_void_v<typename callback_t::return_type>,
495 "need to specify a container to collect into for non-void callbacks");
500 template <
typename Container,
typename Callback>
503 using callback_t = std::decay_t<Callback>;
504 static_assert(!std::is_void_v<typename callback_t::return_type>,
505 "cannot collect a void callback into a container");
510 #endif // LEXY_CALLBACK_CONTAINER_HPP_INCLUDED
constexpr _collect_sink(Callback callback)
constexpr auto operator()(Args &&... args) const -> LEXY_DECAY_DECLTYPE((LEXY_DECLVAL(Container &).insert(LEXY_FWD(args)),...), LEXY_DECLVAL(Container))
constexpr auto operator()(U &&obj) -> decltype(LEXY_DECLVAL(C &).insert(LEXY_FWD(obj)))
constexpr auto collect(Callback &&callback)
constexpr auto operator()(Args &&... args) const -> LEXY_DECAY_DECLTYPE((LEXY_DECLVAL(Container &).push_back(LEXY_FWD(args)),...), LEXY_DECLVAL(Container))
constexpr auto sink(const typename C::allocator_type &allocator) const
constexpr _collect_sink(Callback callback, const typename C::allocator_type &allocator)
constexpr auto allocator() const
constexpr auto _has_append
constexpr Container operator()(Container &&container) const
constexpr auto allocator() const
constexpr Container operator()(Container &&container) const
constexpr auto as_collection
constexpr auto operator()(Args &&... args) -> decltype(void(LEXY_DECLVAL(Callback)(LEXY_FWD(args)...)))
constexpr Container operator()(nullopt &&) const
constexpr auto operator()(const typename C::allocator_type &allocator, Args &&... args) const -> decltype((LEXY_DECLVAL(C &).push_back(LEXY_FWD(args)),...), C(allocator))
constexpr auto sink() const
constexpr auto callback(Fns &&... fns)
Creates a callback.
constexpr Container && finish() &&
constexpr auto sink() const
constexpr auto operator[](State &state) const
constexpr Container operator()(nullopt &&) const
constexpr auto operator()(Args &&... args) const -> decltype(_call(Container(LEXY_FWD(args))...))
constexpr auto operator()(Args &&... args) -> decltype(LEXY_DECLVAL(C &).emplace(LEXY_FWD(args)...))
LEXY_EMPTY_MEMBER Callback _callback
constexpr auto allocator(AllocFn alloc_fn) const
constexpr _collect_sink(Callback callback)
constexpr auto sink(const typename C::allocator_type &allocator) const
constexpr auto sink(State &state) const
#define LEXY_DECAY_DECLTYPE(...)
LEXY_EMPTY_MEMBER Callback _callback
constexpr auto sink() const
constexpr auto invoke(F ClassT::*f, Args &&... args) -> decltype(_mem_invoker< F ClassT::* >::invoke(f, LEXY_FWD(args)...))
constexpr Container operator()(Container &&container) const
constexpr auto operator()(U &&obj) -> decltype(LEXY_DECLVAL(C &).push_back(LEXY_FWD(obj)))
constexpr void operator()(Container &&container)
constexpr Container operator()(nullopt &&) const
constexpr Container && finish() &&
decltype(LEXY_DECLVAL(Container &).reserve(std::size_t())) _detect_reserve
constexpr auto _has_reserve
constexpr Container operator()(nullopt &&) const
constexpr auto operator()(Args &&... args) -> decltype(void(LEXY_DECLVAL(Callback)(LEXY_FWD(args)...)))
constexpr auto operator[](State &state) const
constexpr auto sink(State &state) const
decltype(LEXY_DECLVAL(Container &).append(LEXY_DECLVAL(Container &&))) _detect_append
constexpr Container && finish() &&
constexpr Container _call(Container &&head, Tail &&... tail) const
constexpr auto finish() &&
constexpr _collect(Callback callback)
constexpr auto sink() const
constexpr auto allocator(AllocFn alloc_fn) const
constexpr auto sink(const typename C::allocator_type &allocator) const
constexpr Container operator()(nullopt &&) const
constexpr auto operator()(Args &&... args) -> decltype(LEXY_DECLVAL(C &).emplace_back(LEXY_FWD(args)...))
constexpr auto operator()(Args &&... args) const -> LEXY_DECAY_DECLTYPE((LEXY_DECLVAL(Container &).push_back(LEXY_FWD(args)),...), LEXY_DECLVAL(Container))
constexpr auto operator()(Args &&... args) const -> LEXY_DECAY_DECLTYPE((LEXY_DECLVAL(Container &).insert(LEXY_FWD(args)),...), LEXY_DECLVAL(Container))
#define LEXY_EMPTY_MEMBER
LEXY_EMPTY_MEMBER Callback _callback
#define LEXY_DECLVAL(...)
constexpr Container operator()(Container &&container) const
constexpr auto operator()(const typename C::allocator_type &allocator, Args &&... args) const -> decltype((LEXY_DECLVAL(C &).insert(LEXY_FWD(args)),...), C(allocator))
constexpr auto finish() &&