MPL11
 All Classes Namespaces Files Typedefs Macros Groups Pages
foldable.hpp
Go to the documentation of this file.
1 
12 #ifndef BOOST_MPL11_FOLDABLE_HPP
13 #define BOOST_MPL11_FOLDABLE_HPP
14 
16 
17 #include <boost/mpl11/core.hpp>
19 #include <boost/mpl11/maybe.hpp>
20 
21 #include <boost/mpl11/bool.hpp> //
22 #include <boost/mpl11/functional.hpp> // required by fwd/foldable.hpp
23 #include <boost/mpl11/logical.hpp> //
24 
25 
26 namespace boost { namespace mpl11 {
27  template <typename Datatype, typename>
28  struct Foldable : false_ { };
29 
30  template <>
31  struct instantiate<Foldable> {
32  private:
33  template <typename f>
34  struct maybe_f {
35  using type = maybe_f;
36  template <typename x, typename y>
37  using apply = just<maybe<x, partial<f, x>, y>>;
38  };
39 
40  public:
41  template <typename Datatype>
42  struct with : true_ {
43  template <typename f, typename structure>
44  using foldr1_impl = from_just<
45  foldr<maybe_f<f>, nothing, box<structure>>
46  >;
47 
48  template <typename f, typename structure>
49  using foldl1_impl = from_just<
50  foldl<flip<maybe_f<flip<f>>>, nothing, box<structure>>
51  >;
52  };
53  };
54 
55  template <typename f, typename state, typename structure>
56  struct foldr :
57  Foldable<typename datatype<typename structure::type>::type>::
58  template foldr_impl<f, state, typename structure::type>
59  { };
60 
61  template <typename f, typename state, typename structure>
62  struct foldl :
63  Foldable<typename datatype<typename structure::type>::type>::
64  template foldl_impl<f, state, typename structure::type>
65  { };
66 
67  namespace foldable_detail {
68  template <typename xs>
69  using is_empty_structure = foldr<always<false_>, true_, xs>;
70 
71  template <typename structure>
72  struct fold1_checks {
73  static_assert(!is_empty_structure<structure>::value,
74  "Invalid usage of foldr1 or foldl1 with an empty structure.");
75  };
76  }
77 
78  template <typename f, typename structure>
79  struct foldr1 :
80  BOOST_MPL11_IF_ASSERTIONS(foldable_detail::fold1_checks<structure>,)
81  Foldable<typename datatype<typename structure::type>::type>::
82  template foldr1_impl<f, typename structure::type>
83  { };
84 
85  template <typename f, typename structure>
86  struct foldl1 :
87  BOOST_MPL11_IF_ASSERTIONS(foldable_detail::fold1_checks<structure>,)
88  Foldable<typename datatype<typename structure::type>::type>::
89  template foldl1_impl<f, typename structure::type>
90  { };
91 }} // end namespace boost::mpl11
92 
93 #endif // !BOOST_MPL11_FOLDABLE_HPP
Manages configurable options of the library and defines utility macros.
Defines the Maybe datatype.
Defines the Logical module.
Variant of foldl that has no base case, and thus may only be applied to non-empty structures...
Definition: foldable.hpp:86
Defines the Core module.
Right-associative fold of a structure using a binary operation.
Definition: foldable.hpp:56
#define BOOST_MPL11_IF_ASSERTIONS(...)
Macro expanding to its argument(s) if BOOST_MPL11_NO_ASSERTIONS is not defined, and to nothing otherw...
Definition: config.hpp:72
Invokes a metafunction class with the given arguments.
Definition: functional.hpp:39
Alias to bool_; provided for convenience.
Definition: integer.hpp:79
Left-associative fold of a structure using a binary operation.
Definition: foldable.hpp:62
Forwards to boost/mpl11/integer.hpp.
Variant of foldr that has no base case, and thus may only be applied to non-empty structures...
Definition: foldable.hpp:79
Defines the Functional module.
Forward declares the Foldable typeclass.