reflect.hpp 1.09 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
9
10
11
#ifndef MIGRAPH_GUARD_RTGLIB_REFLECT_HPP
#define MIGRAPH_GUARD_RTGLIB_REFLECT_HPP

#include <migraph/functional.hpp>
#include <migraph/rank.hpp>
#include <functional>

namespace migraph {

namespace detail {

Paul's avatar
Paul committed
12
template <class T, class Selector>
Paul's avatar
Paul committed
13
14
15
16
17
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
18
template <class T, class Selector>
Paul's avatar
Paul committed
19
20
21
22
23
24
25
auto reflect_impl(rank<0>, T&, Selector)
{
    return pack();
}

} // namespace detail

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

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

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

} // namespace migraph

#endif