LeechCraft  0.6.70-13729-g7046a9d2a7
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 <optional>
34 #include <boost/optional.hpp>
35 
36 namespace LC
37 {
38 namespace Util
39 {
40  template<typename T, typename SFINAE = void>
42  {
43  using UndefinedTag = void;
44  };
45 
46  namespace detail
47  {
48  template<typename T>
49  constexpr bool IsMonadPlusImpl (int, typename InstanceMonadPlus<T>::UndefinedTag* = nullptr)
50  {
51  return false;
52  }
53 
54  template<typename T>
55  constexpr bool IsMonadPlusImpl (float)
56  {
57  return true;
58  }
59  }
60 
61  template<typename T>
62  constexpr bool IsMonadPlus ()
63  {
64  return detail::IsMonadPlusImpl<T> (0);
65  }
66 
67  template<typename MP>
68  MP Mzero ()
69  {
71  }
72 
73  const struct
74  {
75  template<typename MP>
76  auto operator() (const MP& m1) const
77  {
78  return [m1] (const MP& m2) { return InstanceMonadPlus<MP>::Mplus (m1, m2); };
79  }
80  } Mplus {};
81 
82  template<typename MP>
83  auto operator+ (const MP& m1, const MP& m2) -> decltype (Mplus (m1) (m2))
84  {
85  return Mplus (m1) (m2);
86  }
87 
88  const struct
89  {
90  template<typename Vec>
91  auto operator() (Vec&& vec) const
92  {
93  using std::begin;
94  using std::end;
95  using MP = typename Vec::value_type;
96  return std::accumulate (begin (vec), end (vec), Mzero<MP> (), &operator+<MP>);
97  }
98 
99  template<typename T>
100  auto operator() (const std::initializer_list<T>& vec) const
101  {
102  using std::begin;
103  using std::end;
104  return std::accumulate (begin (vec), end (vec), Mzero<T> (), &operator+<T>);
105  }
106  } Msum {};
107 
108  template<typename T>
109  struct InstanceMonadPlus<boost::optional<T>>
110  {
111  static boost::optional<T> Mzero ()
112  {
113  return {};
114  }
115 
116  static boost::optional<T> Mplus (const boost::optional<T>& t1, const boost::optional<T>& t2)
117  {
118  return t1 ? t1 : t2;
119  }
120  };
121 
122  template<typename T>
123  struct InstanceMonadPlus<std::optional<T>>
124  {
125  static std::optional<T> Mzero ()
126  {
127  return {};
128  }
129 
130  static std::optional<T> Mplus (const std::optional<T>& t1, const std::optional<T>& t2)
131  {
132  return t1 ? t1 : t2;
133  }
134  };
135 }
136 }
static std::optional< T > Mplus(const std::optional< T > &t1, const std::optional< T > &t2)
Definition: monadplus.h:130
Definition: prelude.h:38
constexpr bool IsMonadPlus()
Definition: monadplus.h:62
const struct LC::Util::@1 Mplus
STL namespace.
const struct LC::Util::@2 Msum
static boost::optional< T > Mplus(const boost::optional< T > &t1, const boost::optional< T > &t2)
Definition: monadplus.h:116
auto operator+(const MP &m1, const MP &m2) -> decltype(Mplus(m1)(m2))
Definition: monadplus.h:83
Definition: constants.h:35
MP Mzero()
Definition: monadplus.h:68
constexpr bool IsMonadPlusImpl(int, typename InstanceMonadPlus< T >::UndefinedTag *=nullptr)
Definition: monadplus.h:49