LeechCraft  0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
monadplus.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #pragma once
31 
32 #include <numeric>
33 #include <boost/optional.hpp>
34 
35 namespace LeechCraft
36 {
37 namespace Util
38 {
39  template<typename T, typename SFINAE = void>
41  {
42  using UndefinedTag = void;
43  };
44 
45  namespace detail
46  {
47  template<typename T>
48  constexpr bool IsMonadPlusImpl (int, typename InstanceMonadPlus<T>::UndefinedTag* = nullptr)
49  {
50  return false;
51  }
52 
53  template<typename T>
54  constexpr bool IsMonadPlusImpl (float)
55  {
56  return true;
57  }
58  }
59 
60  template<typename T>
61  constexpr bool IsMonadPlus ()
62  {
63  return detail::IsMonadPlusImpl<T> (0);
64  }
65 
66  template<typename MP>
67  MP Mzero ()
68  {
70  }
71 
72  const struct
73  {
74  template<typename MP>
75  auto operator() (const MP& m1) const
76  {
77  return [m1] (const MP& m2) { return InstanceMonadPlus<MP>::Mplus (m1, m2); };
78  }
79  } Mplus {};
80 
81  template<typename MP>
82  auto operator+ (const MP& m1, const MP& m2) -> decltype (Mplus (m1) (m2))
83  {
84  return Mplus (m1) (m2);
85  }
86 
87  const struct
88  {
89  template<typename Vec>
90  auto operator() (Vec&& vec) const
91  {
92  using std::begin;
93  using std::end;
94  using MP = typename Vec::value_type;
95  return std::accumulate (begin (vec), end (vec), Mzero<MP> (), &operator+<MP>);
96  }
97 
98  template<typename T>
99  auto operator() (const std::initializer_list<T>& vec) const
100  {
101  using std::begin;
102  using std::end;
103  return std::accumulate (begin (vec), end (vec), Mzero<T> (), &operator+<T>);
104  }
105  } Msum {};
106 
107  template<typename T>
108  struct InstanceMonadPlus<boost::optional<T>>
109  {
110  static boost::optional<T> Mzero ()
111  {
112  return {};
113  }
114 
115  static boost::optional<T> Mplus (const boost::optional<T>& t1, const boost::optional<T>& t2)
116  {
117  return t1 ? t1 : t2;
118  }
119  };
120 }
121 }
static boost::optional< T > Mplus(const boost::optional< T > &t1, const boost::optional< T > &t2)
Definition: monadplus.h:115
Definition: prelude.h:39
constexpr bool IsMonadPlus()
Definition: monadplus.h:61
auto operator+(const MP &m1, const MP &m2) -> decltype(Mplus(m1)(m2))
Definition: monadplus.h:82
const struct LeechCraft::Util::@2 Msum
constexpr bool IsMonadPlusImpl(int, typename InstanceMonadPlus< T >::UndefinedTag *=nullptr)
Definition: monadplus.h:48
const struct LeechCraft::Util::@1 Mplus