mpl_nested_loop.hh 6.05 KB
Newer Older
1
2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2020 Tiago de Paula Peixoto <tiago@skewed.de>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef NESTED_FOR_LOOP_HH
#define NESTED_FOR_LOOP_HH

#include <boost/mpl/for_each.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/empty.hpp>
#include <boost/any.hpp>

namespace boost
{
namespace mpl
{
// The following is a implementation of a nested for_each loop, which runs a
// given Action functor for each combination of its arguments, given by the type
// ranges, as such:
//
//     struct foo
//     {
//         template<class T1, class T2, class T3>
//         void operator()(T1, T2, T3) const
//         {
//             ...
//         }
//     };
//
//     ...
//
//     typedef mpl::vector<int,float,long> r1;
//     typedef mpl::vector<string,double> r2;
//     typedef mpl::vector<size_t,char> r3;
//
49
50
51
//     any x = float(2);
//     any y = string("foo");
//     any z = size_t(42);
52
//
53
54
55
56
//     bool found = nested_for_each<r1,r2,r3>(foo(), x, y, z);
//
// The code above will run iterate through all combinations of foo::operator(T1,
// T2, T3) and call the one that corresponds to the actual types stored in x, y,
57
58
// and z. If the types are not found during iteration, we have found == true,
// otherwise found == false. This provides a more general compile-time to
59
60
// run-time bridge than the simpler mpl::for_each().

61
62
63

// this is a functor wrapper that will perform an any_cast<> in each in an array
// of arguments according to the called types. If the cast is successful, the
64
// function will be called with those types, and true will be returned.
65
66
template <class Action, std::size_t N>
struct all_any_cast
67
{
68
    all_any_cast(Action a, std::array<any*, N>& args)
69
        : _a(a), _args(args) {}
70
71

    template <class... Ts>
72
    [[gnu::always_inline]]
73
    bool operator()(Ts*... vs) const
74
    {
75
        return dispatch(std::make_index_sequence<sizeof...(Ts)>(), vs...);
76
77
78
    }

    template <class T>
79
    T* try_any_cast(boost::any& a) const
80
    {
81
82
        T * t = any_cast<T>(&a);
        if (t)
83
        {
84
            return t;
85
        }
86
87
88

        std::reference_wrapper<T> * tr = any_cast<std::reference_wrapper<T>>(&a);
        if (tr)
89
        {
90
            return &(tr->get());
91
        }
92
93

        return nullptr;
94
95
    }

96
    template <std::size_t... Idx, class... Ts>
97
    [[gnu::always_inline]]
98
    bool dispatch(std::index_sequence<Idx...>, Ts*...) const
99
    {
100
101
102
103
104
        static_assert(sizeof...(Idx) == N,
                      "all_any_cast: wrong number of arguments");

        std::tuple<std::add_pointer_t<Ts>...> args;
        if (((std::get<Idx>(args) = try_any_cast<Ts>(*_args[Idx])) && ...))
105
        {
106
107
108
            // successful set of casts. Dereference and call action.
            std::apply([this](auto*... arg){ _a(*arg...); }, args);
            return true;
109
        }
110
111

        return false;
112
113
114
    }

    Action _a;
115
    std::array<any*, N>& _args;
116
117
};

118
119
120
// recursion-free variadic version of for_each
template <class...>
struct for_each_variadic;
121

122
123
template <class F, class... Ts>
struct for_each_variadic<F,std::tuple<Ts...>>
124
{
125
    bool operator()(F f)
126
    {
127
128
        auto call = [&](auto&& arg) -> bool {return f(std::forward<decltype(arg)>(arg));};
        return (call(typename std::add_pointer<Ts>::type()) || ...);
129
    }
130
131
};

132
133
134
// convert mpl sequence to std::tuple
template <class T, class R>
struct to_tuple_imp;
135

136
137
template <class... Ts, class X>
struct to_tuple_imp<std::tuple<Ts...>, X>
138
{
139
    typedef std::tuple<Ts..., X> type;
140
};
141

142
143
template <class Seq>
struct to_tuple
144
{
145
146
147
148
149
150
151
152
    typedef typename mpl::fold<Seq, std::tuple<>,
                               to_tuple_imp<mpl::_1, mpl::_2>>::type type;
};

// nested type loops via variadic templates

template <class...>
struct inner_loop {};
153

154
155
template <class Action, class... Ts>
struct inner_loop<Action, std::tuple<Ts...>>
156
{
157
    inner_loop(Action a): _a(a) {}
158

159
    template <class T>
160
    [[gnu::always_inline]]
161
162
163
    bool operator()(T*) const
    { return _a(typename std::add_pointer<Ts>::type()...,
                typename std::add_pointer<T>::type()); }  // innermost loop
164
165
    Action _a;
};
166

167
168
template <class Action, class... Ts, class TR1, class... TRS>
struct inner_loop<Action, std::tuple<Ts...>, TR1, TRS...>
169
170
{
    inner_loop(Action a): _a(a) {}
171

172
    template <class T>
173
    [[gnu::always_inline]]
174
    bool operator()(T*) const
175
    {
176
177
        typedef inner_loop<Action, std::tuple<Ts..., T>, TRS...> inner_loop_t;
        typedef typename to_tuple<TR1>::type tr_tuple;
178
        return for_each_variadic<inner_loop_t, tr_tuple>()(inner_loop_t(_a));
179
180
181
    }
    Action _a;
};
182

183
// final function
184

185
template <class TR1, class... TRS, class Action, class... Args>
186
bool nested_for_each(Action a, Args&&... args)
187
{
188
    std::array<any*, sizeof...(args)> as{{&args...}};
189
    auto b = all_any_cast<Action, sizeof...(args)>(a, as);
190
191
192
193
    typedef decltype(b) action_t;
    typedef typename to_tuple<TR1>::type tr_tuple;
    typedef inner_loop<action_t, std::tuple<>, TRS...> inner_loop_t;
    return for_each_variadic<inner_loop_t, tr_tuple>()(inner_loop_t(b));
194
}
195

196
197
198
template <class TR1, class... TRS, class Action>
void nested_for_each(Action a)
{
199
200
201
    typedef typename to_tuple<TR1>::type tr_tuple;

    // wrap action into a bool-returning function
202
    auto ab = [=](auto*... args) -> bool { a(args...); return false; };
203
204
205

    typedef inner_loop<decltype(ab), std::tuple<>, TRS...> inner_loop_t;
    for_each_variadic<inner_loop_t, tr_tuple>()(inner_loop_t(ab));
206
207
208
}


209
210
211
212
} // mpl namespace
} // boost namespace

#endif //NESTED_FOR_LOOP_HH