flexiblesusy is hosted by Hepforge, IPPP Durham
FlexibleSUSY
find_if.hpp
Go to the documentation of this file.
1// ====================================================================
2// This file is part of FlexibleSUSY.
3//
4// FlexibleSUSY is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published
6// by the Free Software Foundation, either version 3 of the License,
7// or (at your option) any later version.
8//
9// FlexibleSUSY is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with FlexibleSUSY. If not, see
16// <http://www.gnu.org/licenses/>.
17// ====================================================================
18
19#ifndef FS_FIND_IF_H
20#define FS_FIND_IF_H
21
22#include <type_traits>
23#include <utility>
24
25#include <boost/mpl/begin_end.hpp>
26#include <boost/mpl/deref.hpp>
27#include <boost/mpl/next.hpp>
28
29namespace flexiblesusy
30{
31namespace meta
32{
33namespace detail
34{
35template <class Iterator, class End, template <typename> class Predicate,
36 template <typename> class F, class State>
37bool find_if_impl(State&& state, std::true_type)
38{
39 return false;
40}
41
42template <class Iterator, class End, template <typename> class Predicate,
43 template <typename> class F, class State>
44bool find_if_impl(State&& state, std::false_type)
45{
46 if (Predicate<typename boost::mpl::deref<Iterator>::type>{}(state)) {
47 F<typename boost::mpl::deref<Iterator>::type>{}(state);
48 return true;
49 }
50
51 using NextIterator = typename boost::mpl::next<Iterator>::type;
52
53 return find_if_impl<NextIterator, End, Predicate, F>(
54 std::forward<State>(state),
55 typename std::is_same<NextIterator, End>::type{});
56}
57} // namespace detail
58
59template <class Sequence, template <typename> class Predicate,
60 template <typename> class F, class State>
61bool find_if(State&& state)
62{
63 return detail::find_if_impl<typename boost::mpl::begin<Sequence>::type,
64 typename boost::mpl::end<Sequence>::type,
65 Predicate, F>(
66 std::forward<State>(state),
67 typename std::is_same<typename boost::mpl::begin<Sequence>::type,
68 typename boost::mpl::end<Sequence>::type>::type{});
69}
70} // namespace meta
71} // namespace flexiblesusy
72
73#endif
bool find_if_impl(State &&state, std::true_type)
Definition: find_if.hpp:37
bool find_if(State &&state)
Definition: find_if.hpp:61