reflect.hpp 1.19 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
#ifndef MIGRAPH_GUARD_RTGLIB_REFLECT_HPP
#define MIGRAPH_GUARD_RTGLIB_REFLECT_HPP

#include <migraph/functional.hpp>
#include <migraph/rank.hpp>
6
#include <migraph/config.hpp>
Paul's avatar
Paul committed
7
8
#include <functional>

9
10
namespace migraph {
inline namespace MIGRAPH_INLINE_NS {
Paul's avatar
Paul committed
11
12
13

namespace detail {

Paul's avatar
Paul committed
14
template <class T, class Selector>
Paul's avatar
Paul committed
15
16
17
18
19
auto reflect_impl(rank<1>, T& x, Selector f) -> decltype(T::reflect(x, f))
{
    return T::reflect(x, std::move(f));
}

Paul's avatar
Paul committed
20
template <class T, class Selector>
Paul's avatar
Paul committed
21
22
23
24
25
26
27
auto reflect_impl(rank<0>, T&, Selector)
{
    return pack();
}

} // namespace detail

Paul's avatar
Paul committed
28
template <class T, class Selector>
Paul's avatar
Paul committed
29
30
31
32
33
auto reflect(T& x, Selector f)
{
    return detail::reflect_impl(rank<1>{}, x, std::move(f));
}

Paul's avatar
Paul committed
34
template <class T>
Paul's avatar
Paul committed
35
36
auto reflect_tie(T& x)
{
Paul's avatar
Paul committed
37
38
    return reflect(x, [](auto&& y, auto&&...) { return std::ref(y); })(
        [](auto&&... xs) { return std::tie(xs.get()...); });
Paul's avatar
Paul committed
39
40
}

Paul's avatar
Paul committed
41
template <class T, class F>
Paul's avatar
Paul committed
42
43
void reflect_each(T& x, F f)
{
Paul's avatar
Paul committed
44
45
    return reflect(x, [](auto&& y, auto... ys) { return pack(std::ref(y), ys...); })(
        [&](auto&&... xs) {
Paul's avatar
Paul committed
46
            each_args([&](auto p) { p([&](auto&& y, auto... ys) { f(y.get(), ys...); }); }, xs...);
Paul's avatar
Paul committed
47
        });
Paul's avatar
Paul committed
48
49
}

50
} // namespace MIGRAPH_INLINE_NS
Paul's avatar
Paul committed
51
52
53
} // namespace migraph

#endif