summaryrefslogtreecommitdiff
path: root/boost/spirit/home/support/algorithm/any_if.hpp
blob: 0ce6d3ff300a0c87b82d27f277f15779ee5ffddc (plain)
1
2
3
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*=============================================================================
    Copyright (c) 2001-2011 Hartmut Kaiser
    Copyright (c) 2001-2011 Joel de Guzman

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#if !defined(BOOST_SPIRIT_ANY_IF_MARCH_30_2007_1220PM)
#define BOOST_SPIRIT_ANY_IF_MARCH_30_2007_1220PM

#if defined(_MSC_VER)
#pragma once
#endif

#include <boost/fusion/include/equal_to.hpp>
#include <boost/fusion/include/next.hpp>
#include <boost/fusion/include/deref.hpp>
#include <boost/fusion/include/value_of.hpp>
#include <boost/fusion/include/begin.hpp>
#include <boost/fusion/include/end.hpp>
#include <boost/fusion/include/is_sequence.hpp>
#include <boost/fusion/include/any.hpp>
#include <boost/spirit/home/support/unused.hpp>

#include <boost/mpl/bool.hpp>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/and.hpp>
#include <boost/mpl/not.hpp>

namespace boost { namespace spirit
{
    ///////////////////////////////////////////////////////////////////////////
    //  This is a special version for a binary fusion::any. The predicate
    //  is used to decide whether to advance the second iterator or not.
    //  This is needed for sequences containing components with unused
    //  attributes. The second iterator is advanced only if the attribute
    //  of the corresponding component iterator is not unused.
    ///////////////////////////////////////////////////////////////////////////
    namespace detail
    {
        ///////////////////////////////////////////////////////////////////////
        template <typename Iterator, typename Pred>
        struct apply_predicate
          : mpl::apply1<Pred, typename fusion::result_of::value_of<Iterator>::type>
        {};

        ///////////////////////////////////////////////////////////////////////
        //  if the predicate is true, attribute_next returns next(Iterator2),
        //  otherwise Iterator2
        namespace result_of
        {
            template <
                typename Iterator1, typename Iterator2, typename Last2
              , typename Pred>
            struct attribute_next
            {
                typedef mpl::and_<
                    apply_predicate<Iterator1, Pred>
                  , mpl::not_<fusion::result_of::equal_to<Iterator2, Last2> >
                > pred;

                typedef typename
                    mpl::eval_if<
                        pred
                      , fusion::result_of::next<Iterator2>
                      , mpl::identity<Iterator2>
                    >::type
                type;

                template <typename Iterator>
                static type
                call(Iterator const& i, mpl::true_)
                {
                    return fusion::next(i);
                }

                template <typename Iterator>
                static type
                call(Iterator const& i, mpl::false_)
                {
                    return i;
                }

                template <typename Iterator>
                static type
                call(Iterator const& i)
                {
                    return call(i, pred());
                }
            };
        }

        template <
            typename Pred, typename Iterator1, typename Last2
          , typename Iterator2>
        inline typename
            result_of::attribute_next<Iterator1, Iterator2, Last2, Pred
        >::type const
        attribute_next(Iterator2 const& i)
        {
            return result_of::attribute_next<
                Iterator1, Iterator2, Last2, Pred>::call(i);
        }

        ///////////////////////////////////////////////////////////////////////
        //  if the predicate is true, attribute_value returns deref(Iterator2),
        //  otherwise unused
        namespace result_of
        {
            template <
                typename Iterator1, typename Iterator2, typename Last2
              , typename Pred>
            struct attribute_value
            {
                typedef mpl::and_<
                    apply_predicate<Iterator1, Pred>
                  , mpl::not_<fusion::result_of::equal_to<Iterator2, Last2> >
                > pred;

                typedef typename
                    mpl::eval_if<
                        pred
                      , fusion::result_of::deref<Iterator2>
                      , mpl::identity<unused_type const>
                    >::type
                type;

                template <typename Iterator>
                static type
                call(Iterator const& i, mpl::true_)
                {
                    return fusion::deref(i);
                }

                template <typename Iterator>
                static type
                call(Iterator const&, mpl::false_)
                {
                    return unused;
                }

                template <typename Iterator>
                static type
                call(Iterator const& i)
                {
                    return call(i, pred());
                }
            };
        }

        template <
            typename Pred, typename Iterator1, typename Last2
          , typename Iterator2>
        inline typename
            result_of::attribute_value<Iterator1, Iterator2, Last2, Pred
        >::type
        attribute_value(Iterator2 const& i)
        {
            return result_of::attribute_value<
                Iterator1, Iterator2, Last2, Pred>::call(i);
        }

        ///////////////////////////////////////////////////////////////////////
        template <
            typename Pred, typename First1, typename Last1, typename First2
          , typename Last2, typename F>
        inline bool
        any_if (First1 const&, First2 const&, Last1 const&, Last2 const&
          , F const&, mpl::true_)
        {
            return false;
        }

        template <
            typename Pred, typename First1, typename Last1, typename First2
          , typename Last2, typename F>
        inline bool
        any_if (First1 const& first1, First2 const& first2, Last1 const& last1
          , Last2 const& last2, F& f, mpl::false_)
        {
            typename result_of::attribute_value<First1, First2, Last2, Pred>::type
                attribute = attribute_value<Pred, First1, Last2>(first2);

            return f(*first1, attribute) ||
                detail::any_if<Pred>(
                    fusion::next(first1)
                  , attribute_next<Pred, First1, Last2>(first2)
                  , last1, last2
                  , f
                  , fusion::result_of::equal_to<
                        typename fusion::result_of::next<First1>::type, Last1>());
        }
    }

    template <typename Pred, typename Sequence1, typename Sequence2, typename F>
    inline bool
    any_if(Sequence1 const& seq1, Sequence2& seq2, F f, Pred)
    {
        return detail::any_if<Pred>(
                fusion::begin(seq1), fusion::begin(seq2)
              , fusion::end(seq1), fusion::end(seq2)
              , f
              , fusion::result_of::equal_to<
                    typename fusion::result_of::begin<Sequence1>::type
                  , typename fusion::result_of::end<Sequence1>::type>());
    }

    template <typename Pred, typename Sequence, typename F>
    inline bool
    any_if(Sequence const& seq, unused_type const, F f, Pred)
    {
        return fusion::any(seq, f);
    }

}}

#endif