PageRenderTime 50ms CodeModel.GetById 16ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/fusion/algorithm/query/ext_/find_if_s.hpp

http://hadesmem.googlecode.com/
C++ Header | 222 lines | 187 code | 28 blank | 7 comment | 0 complexity | 287d16238a9249fc1c5e564db8b889de MD5 | raw file
  1/*=============================================================================
  2    Copyright (c) 2006 Eric Niebler
  3
  4    Distributed under the Boost Software License, Version 1.0. (See accompanying 
  5    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6==============================================================================*/
  7#if !defined(FIND_IF_S_05152006_1027)
  8#define FIND_IF_S_05152006_1027
  9
 10#include <boost/mpl/not.hpp>
 11#include <boost/mpl/assert.hpp>
 12#include <boost/mpl/eval_if.hpp>
 13#include <boost/type_traits/is_const.hpp>
 14#include <boost/utility/enable_if.hpp>
 15#include <boost/type_traits/is_same.hpp>
 16#include <boost/fusion/algorithm/query/find_if.hpp>
 17#include <boost/fusion/container/list/cons.hpp>
 18#include <boost/fusion/sequence/intrinsic/ext_/segments.hpp>
 19#include <boost/fusion/view/ext_/segmented_iterator.hpp>
 20#include <boost/fusion/view/ext_/segmented_iterator_range.hpp>
 21#include <boost/fusion/support/ext_/is_segmented.hpp>
 22
 23// fwd declarations
 24namespace boost { namespace fusion
 25{
 26    namespace detail
 27    {
 28        template<typename Sequence, typename Pred, bool IsSegmented = traits::is_segmented<Sequence>::value>
 29        struct static_find_if_s_recurse;
 30    }
 31
 32    namespace result_of
 33    {
 34        template <typename Sequence, typename Pred>
 35        struct find_if_s;
 36    }
 37}}
 38
 39namespace boost { namespace fusion { namespace detail
 40{
 41
 42    template<typename Sequence, typename Where, bool IsSegmented = traits::is_segmented<Sequence>::value>
 43    struct is_found
 44      : mpl::not_<result_of::equal_to<Where, typename result_of::end<Sequence>::type> >
 45    {};
 46
 47    template<typename Sequence, typename Cons>
 48    struct is_found<Sequence, Cons, true>
 49      : mpl::not_<is_same<nil, Cons> >
 50    {};
 51
 52    template<
 53        typename SegmentedRange
 54      , typename Where
 55      , typename Sequence = typename remove_reference<
 56            typename result_of::deref<
 57                typename SegmentedRange::iterator_type
 58            >::type
 59        >::type
 60      , bool IsSegmented = traits::is_segmented<Sequence>::value
 61    >
 62    struct as_segmented_cons
 63    {
 64        typedef cons<
 65            SegmentedRange
 66          , cons<segmented_range<Sequence, Where, false> >
 67        > type;
 68
 69        static type call(SegmentedRange const &range, Where const &where)
 70        {
 71            return fusion::make_cons(
 72                range
 73              , fusion::make_cons(
 74                    segmented_range<Sequence, Where, false>(*fusion::begin(range), where)
 75                )
 76            );
 77        }
 78    };
 79
 80    template<
 81        typename SegmentedRange
 82      , typename Where
 83      , typename Sequence
 84    >
 85    struct as_segmented_cons<SegmentedRange, Where, Sequence, true>
 86    {
 87        typedef cons<SegmentedRange, Where> type;
 88
 89        static type call(SegmentedRange const &range, Where const &where)
 90        {
 91            return fusion::make_cons(range, where);
 92        }
 93    };
 94
 95    template<
 96        typename SegmentedRange
 97      , typename Pred
 98      , bool IsEmpty = is_empty<SegmentedRange>::value
 99    >
100    struct static_find_if_s_seg
101    {
102        typedef typename SegmentedRange::iterator_type first;
103        typedef typename result_of::deref<first>::type segment_ref;
104        typedef typename remove_reference<segment_ref>::type segment;
105        typedef static_find_if_s_recurse<segment, Pred> where;
106        typedef range_next<SegmentedRange> next;
107        typedef is_found<segment, typename where::type> is_found;
108        typedef as_segmented_cons<SegmentedRange, typename where::type> found;
109        typedef static_find_if_s_seg<typename next::type, Pred> not_found;
110        typedef typename mpl::eval_if<is_found, found, not_found>::type type;
111
112        static type call(SegmentedRange const &range)
113        {
114            return call_(range, is_found());
115        }
116
117    private:
118        static type call_(SegmentedRange const &range, mpl::true_)
119        {
120            return found::call(range, where::call(*range.where_));
121        }
122
123        static type call_(SegmentedRange const &range, mpl::false_)
124        {
125            return not_found::call(next::call(range));
126        }
127    };
128
129    template<
130        typename SegmentedRange
131      , typename Pred
132    >
133    struct static_find_if_s_seg<SegmentedRange, Pred, true>
134    {
135        typedef nil type;
136
137        static type call(SegmentedRange const &)
138        {
139            return nil();
140        }
141    };
142
143    template<typename Sequence, typename Pred>
144    struct static_find_if_s_recurse<Sequence, Pred, true>
145    {
146        typedef typename as_segmented_range<Sequence>::type range;
147        typedef static_find_if_s_seg<range, Pred> find_if;
148        typedef typename find_if::type type;
149
150        static type call(Sequence &seq)
151        {
152            return find_if::call(range(fusion::segments(seq)));
153        }
154    };
155
156    template<typename Sequence, typename Pred>
157    struct static_find_if_s_recurse<Sequence, Pred, false>
158    {
159        typedef typename result_of::find_if<Sequence, Pred>::type type;
160
161        static type call(Sequence &seq)
162        {
163            return fusion::find_if<Pred>(seq);
164        }
165    };
166
167    template<typename Sequence, typename Pred, bool IsSegmented = traits::is_segmented<Sequence>::value>
168    struct static_find_if_s
169      : static_find_if_s_recurse<Sequence, Pred, IsSegmented>
170    {};
171    
172    template<typename Sequence, typename Pred>
173    struct static_find_if_s<Sequence, Pred, true>
174    {
175        typedef typename as_segmented_range<Sequence>::type range;
176        typedef static_find_if_s_recurse<Sequence, Pred> find_if;
177        typedef typename find_if::type found;
178
179        typedef segmented_iterator<typename reverse_cons<found>::type> type;
180
181        static type call(Sequence &seq)
182        {
183            return type(reverse_cons<found>::call(find_if::call(seq)));
184        }
185    };
186}}}
187
188namespace boost { namespace fusion
189{
190    namespace result_of
191    {
192        template <typename Sequence, typename Pred>
193        struct find_if_s
194        {
195            typedef typename
196                detail::static_find_if_s<
197                    Sequence
198                  , Pred
199                >::type
200            type;
201        };
202    }
203
204    template <typename Pred, typename Sequence>
205    typename lazy_disable_if<
206        is_const<Sequence>
207      , result_of::find_if_s<Sequence, Pred>
208    >::type
209    find_if_s(Sequence& seq)
210    {
211        return detail::static_find_if_s<Sequence, Pred>::call(seq);
212    }
213
214    template <typename Pred, typename Sequence>
215    typename result_of::find_if_s<Sequence const, Pred>::type
216    find_if_s(Sequence const& seq)
217    {
218        return detail::static_find_if_s<Sequence const, Pred>::call(seq);
219    }
220}}
221
222#endif