diff --git a/include/beman/execution/detail/sub_visit.hpp b/include/beman/execution/detail/sub_visit.hpp new file mode 100644 index 00000000..19b9cb59 --- /dev/null +++ b/include/beman/execution/detail/sub_visit.hpp @@ -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 +#include +#include + +// ---------------------------------------------------------------------------- + +namespace beman::execution::detail { +/* + * \brief Helper function creatig thunks for a variant visit. + * \headerfile beman/execution/task.hpp + * \internal + */ +template +void sub_visit_thunks(Fun& fun, Var& var, std::index_sequence) { + using thunk_t = void (*)(Fun&, Var&); + static constexpr thunk_t thunks[]{(+[](Fun& f, Var& v) { f(std::get(v)); })...}; + thunks[var.index() - Start](fun, var); +} + +/* + * \brief Helper function visiting a suffix of variant options + * \headerfile beman/execution/task.hpp + * \internal + */ +template +void sub_visit(auto&& fun, std::variant& v) { + if (v.index() < Start) + return; + sub_visit_thunks(fun, v, std::make_index_sequence{}); +} + +} // namespace beman::execution::detail + +// ---------------------------------------------------------------------------- + +#endif