Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions include/beman/execution/detail/sub_visit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// include/beman/execution/detail/sub_visit.hpp -*-C++-*-
// ----------------------------------------------------------------------------
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// ----------------------------------------------------------------------------

#ifndef INCLUDED_BEMAN_EXECUTION_DETAIL_SUB_VISIT
#define INCLUDED_BEMAN_EXECUTION_DETAIL_SUB_VISIT

#include <utility>
#include <variant>
#include <cstddef>

// ----------------------------------------------------------------------------

namespace beman::execution::detail {
/*
* \brief Helper function creatig thunks for a variant visit.
* \headerfile beman/execution/task.hpp <beman/execution/task.hpp>
* \internal
*/
template <std::size_t Start, typename Fun, typename Var, std::size_t... I>
void sub_visit_thunks(Fun& fun, Var& var, std::index_sequence<I...>) {
using thunk_t = void (*)(Fun&, Var&);
static constexpr thunk_t thunks[]{(+[](Fun& f, Var& v) { f(std::get<Start + I>(v)); })...};
thunks[var.index() - Start](fun, var);
}

/*
* \brief Helper function visiting a suffix of variant options
* \headerfile beman/execution/task.hpp <beman/execution/task.hpp>
* \internal
*/
template <std::size_t Start, typename... T>
void sub_visit(auto&& fun, std::variant<T...>& v) {
if (v.index() < Start)
return;
sub_visit_thunks<Start>(fun, v, std::make_index_sequence<sizeof...(T) - Start>{});
}

} // namespace beman::execution::detail

// ----------------------------------------------------------------------------

#endif
Loading