libmoost
/home/mhx/git/github/libmoost/include/moost/mpl/search.hpp
Go to the documentation of this file.
00001 /* vim:set ts=3 sw=3 sts=3 et: */
00030 #ifndef MOOST_MPL_SEARCH_HPP
00031 #define MOOST_MPL_SEARCH_HPP
00032 
00033 #include <boost/mpl/assert.hpp>
00034 #include <boost/mpl/deref.hpp>
00035 #include <boost/mpl/is_sequence.hpp>
00036 #include <boost/mpl/next.hpp>
00037 
00038 namespace moost { namespace mpl {
00039 
00040 namespace detail {
00041 
00042 template <bool = true>
00043 struct search_impl
00044 {
00045    template <typename Iterator, typename End, typename Policy>
00046    static typename Policy::return_type exec(const Policy& policy)
00047    {
00048       return policy.not_found();
00049    }
00050 };
00051 
00052 template <>
00053 struct search_impl<false>
00054 {
00055    template <typename Iterator, typename End, typename Policy>
00056    static typename Policy::return_type exec(const Policy& policy)
00057    {
00058       typedef typename boost::mpl::deref<Iterator>::type item;
00059       typedef typename boost::mpl::next<Iterator>::type iter;
00060 
00061       return policy.template test<item>()
00062                 ? policy.template found<item>()
00063                 : search_impl<boost::is_same<iter, End>::value>::template exec<iter, End>(policy);
00064    }
00065 };
00066 
00067 }
00068 
00119 template <typename Sequence, typename Policy>
00120 inline typename Policy::return_type search(const Policy& policy)
00121 {
00122    BOOST_MPL_ASSERT((boost::mpl::is_sequence<Sequence>));
00123 
00124    typedef typename boost::mpl::begin<Sequence>::type first;
00125    typedef typename boost::mpl::end<Sequence>::type last;
00126 
00127    return detail::search_impl<boost::is_same<first, last>::value>::template exec<first, last>(policy);
00128 }
00129 
00130 }}
00131 
00132 #endif