di 0.1.0
Loading...
Searching...
No Matches
dispatcher.h
Go to the documentation of this file.
1#pragma once
2
6#include "di/meta/algorithm.h"
7#include "di/meta/constexpr.h"
8#include "di/meta/util.h"
10
11namespace di::any {
12namespace detail {
13 template<typename Tag, typename Sig, typename... Tags>
14 struct DispatcherImpl;
15
16 template<typename Tag, typename R, concepts::DecaysTo<This> Self, typename... Args, typename... Tags>
17 struct DispatcherImpl<Tag, R(Self, Args...), Tags...> {
18 using Type = Method<Tag, R(Self, Args...)>;
19
20 template<typename T, typename F>
21 constexpr static bool is_invocable = concepts::InvocableTo<F const&, R, meta::Like<Self, T>, Args...>;
22
23 template<typename T>
24 struct Invocable {
25 template<typename F>
26 using Invoke = Constexpr<is_invocable<T, F>>;
27 };
28
29 template<typename T>
30 requires(concepts::TagInvocableTo<Tag, R, meta::Like<Self, T>, Args...> ||
31 (concepts::InvocableTo<Tags const&, R, meta::Like<Self, T>, Args...> || ...))
32 constexpr auto operator()(T&& self, Args... args) const -> R {
33 if constexpr (concepts::TagInvocableTo<Tag, R, meta::Like<Self, T>, Args...>) {
34 auto const tag = Tag {};
35 return function::tag_invoke(tag, util::forward_like<Self>(self), util::forward<Args>(args)...);
36 } else {
37 using Matching = meta::Filter<meta::List<Tags...>, Invocable<T>>;
38 using Choice = meta::Front<Matching>;
39 auto const tag = Choice {};
40 return function::invoke_r<R>(tag, util::forward_like<Self>(self), util::forward<Args>(args)...);
41 }
42 }
43 };
44}
45
46template<typename Self, concepts::LanguageFunction Sig, typename... Tags>
47using Dispatcher = detail::DispatcherImpl<Self, Sig, Tags...>;
48}
49
50namespace di {
51using any::Dispatcher;
52}
Definition any.h:18
detail::DispatcherImpl< Self, Sig, Tags... > Dispatcher
Definition dispatcher.h:47
constexpr tag_invoke_detail::TagInvokeFn tag_invoke
Definition tag_invoke.h:22
constexpr auto invoke_r
Definition invoke.h:103
T::Type Type
Definition core.h:26
Fold< List, meta::List<>, detail::FilterReducer< Pred > > Filter
Definition algorithm.h:152
T::Front Front
Definition list.h:108
Type< Defer< Fun::template Invoke, Args... > > Invoke
Definition function.h:43
di::meta::Decay< decltype(T)> Tag
Definition tag_invoke.h:28
constexpr auto forward_like(U &&value) -> decltype(auto)
Definition forward_like.h:8
Definition any_storable.h:9