LeechCraft  0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
either.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 <type_traits>
33 #include <boost/variant.hpp>
34 #include "functor.h"
35 #include "applicative.h"
36 #include "monad.h"
37 #include "visitor.h"
38 
39 namespace LeechCraft
40 {
41 namespace Util
42 {
43  template<typename L, typename R>
44  class Either
45  {
46  using Either_t = boost::variant<L, R>;
47  Either_t This_;
48 
49  enum { LeftVal, RightVal };
50 
51  static_assert (!std::is_same<L, R>::value, "Types cannot be the same.");
52  public:
53  using L_t = L;
54  using R_t = R;
55 
56  Either () = delete;
57 
58  explicit Either (const L& l)
59  : This_ { l }
60  {
61  }
62 
63  explicit Either (const R& r)
64  : This_ { r }
65  {
66  }
67 
68  template<typename LPrime, typename RPrime,
69  typename = std::enable_if_t<std::is_convertible<LPrime, L>::value &&
70  std::is_convertible<RPrime, R>::value>>
72  : This_ { other.AsVariant () }
73  {
74  }
75 
76  Either (const Either&) = default;
77  Either (Either&&) = default;
78  Either& operator= (const Either&) = default;
79  Either& operator= (Either&&) = default;
80 
81  bool IsLeft () const
82  {
83  return This_.which () == LeftVal;
84  }
85 
86  bool IsRight () const
87  {
88  return This_.which () == RightVal;
89  }
90 
91  const L& GetLeft () const
92  {
93  if (!IsLeft ())
94  throw std::runtime_error { "Tried accessing Left for a Right Either" };
95  return boost::get<L> (This_);
96  }
97 
98  const R& GetRight () const
99  {
100  if (!IsRight ())
101  throw std::runtime_error { "Tried accessing Right for a Left Either" };
102  return boost::get<R> (This_);
103  }
104 
105  boost::optional<L> MaybeLeft () const
106  {
107  if (!IsLeft ())
108  return {};
109  return GetLeft ();
110  }
111 
112  boost::optional<R> MaybeRight () const
113  {
114  if (!IsRight ())
115  return {};
116  return GetRight ();
117  }
118 
119  boost::variant<L, R> AsVariant () const
120  {
121  return This_;
122  }
123 
124  template<typename F>
125  R ToRight (F&& f) const
126  {
127  return IsRight () ?
128  GetRight () :
129  f (GetLeft ());
130  }
131 
132  template<typename RNew>
133  static Either<L, RNew> FromMaybe (const boost::optional<RNew>& maybeRight, const L& left)
134  {
135  return maybeRight ?
136  Either<L, RNew>::Right (*maybeRight) :
137  Either<L, RNew>::Left (left);
138  }
139 
140  static Either Left (const L& l)
141  {
142  return Either { l };
143  }
144 
145  static Either Right (const R& r)
146  {
147  return Either { r };
148  }
149 
150  template<typename RNew>
151  static std::enable_if_t<!std::is_convertible<RNew, R>::value, Either<L, RNew>> Right (const RNew& r)
152  {
153  return Either<L, RNew>::Right (r);
154  }
155 
156  static auto EmbeddingLeft ()
157  {
158  return [] (const auto& other)
159  {
160  static_assert (std::is_convertible<std::decay_t<decltype (other.GetLeft ())>, L>::value,
161  "Other's Either's Left type is not convertible to this Left type.");
162  return other.IsLeft () ?
163  Either<L, R>::Left (other.GetLeft ()) :
164  Either<L, R>::Right (other.GetRight ());
165  };
166  }
167 
168  friend bool operator== (const Either& e1, const Either& e2)
169  {
170  return e1.This_ == e2.This_;
171  }
172 
173  friend bool operator!= (const Either& e1, const Either& e2)
174  {
175  return !(e1 == e2);
176  }
177  };
178 
179  template<typename L, typename R, typename F, typename = std::result_of_t<F ()>>
180  R RightOr (const Either<L, R>& either, F&& f)
181  {
182  return either.IsRight () ?
183  either.GetRight () :
184  f ();
185  }
186 
187  template<typename L, typename R>
188  R RightOr (const Either<L, R>& either, const R& r)
189  {
190  return either.IsRight () ?
191  either.GetRight () :
192  r;
193  }
194 
195  template<template<typename> class Cont, typename L, typename R>
196  std::pair<Cont<L>, Cont<R>> PartitionEithers (const Cont<Either<L, R>>& eithers)
197  {
198  std::pair<Cont<L>, Cont<R>> result;
199  for (const auto& either : eithers)
200  if (either.IsLeft ())
201  result.first.push_back (either.GetLeft ());
202  else
203  result.second.push_back (either.GetRight ());
204 
205  return result;
206  }
207 
208  template<typename Left, typename Right, typename... Args>
209  auto Visit (const Either<Left, Right>& either, Args&&... args)
210  {
211  return Visit (either.AsVariant (), std::forward<Args> (args)...);
212  }
213 
214  template<typename L, typename R>
215  struct InstanceFunctor<Either<L, R>>
216  {
217  template<typename F>
219 
220  template<typename F>
221  static FmapResult_t<F> Apply (const Either<L, R>& either, const F& f)
222  {
223  if (either.IsLeft ())
224  return FmapResult_t<F>::Left (either.GetLeft ());
225 
226  return FmapResult_t<F>::Right (f (either.GetRight ()));
227  }
228  };
229 
230  template<typename L, typename R>
232  {
234 
235  template<typename>
236  struct GSLResult;
237 
238  template<typename V>
239  struct GSLResult<Either<L, V>>
240  {
242  };
243 
244  template<typename RP>
245  static Either<L, RP> Pure (const RP& v)
246  {
247  return Either<L, RP>::Right (v);
248  }
249 
250  template<typename AV>
251  static GSLResult_t<Type_t, AV> GSL (const Type_t& f, const AV& v)
252  {
253  using R_t = GSLResult_t<Type_t, AV>;
254 
255  if (f.IsLeft ())
256  return R_t::Left (f.GetLeft ());
257 
258  if (v.IsLeft ())
259  return R_t::Left (v.GetLeft ());
260 
261  return R_t::Right (f.GetRight () (v.GetRight ()));
262  }
263  };
264 
265  template<typename L, typename R>
266  struct InstanceMonad<Either<L, R>>
267  {
268  template<typename F>
269  using BindResult_t = std::result_of_t<F (R)>;
270 
271  template<typename F>
272  static BindResult_t<F> Bind (const Either<L, R>& value, const F& f)
273  {
274  using R_t = BindResult_t<F>;
275 
276  if (value.IsLeft ())
277  return R_t::Left (value.GetLeft ());
278 
279  return f (value.GetRight ());
280  }
281  };
282 }
283 }
static Either Right(const R &r)
Definition: either.h:145
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptr > > f
Definition: oral.h:931
const L & GetLeft() const
Definition: either.h:91
friend bool operator!=(const Either &e1, const Either &e2)
Definition: either.h:173
bool IsLeft() const
Definition: either.h:81
boost::variant< L, R > AsVariant() const
Definition: either.h:119
friend bool operator==(const Either &e1, const Either &e2)
Definition: either.h:168
static BindResult_t< F > Bind(const Either< L, R > &value, const F &f)
Definition: either.h:272
R ToRight(F &&f) const
Definition: either.h:125
static Either< L, RP > Pure(const RP &v)
Definition: either.h:245
boost::optional< L > MaybeLeft() const
Definition: either.h:105
static Either Left(const L &l)
Definition: either.h:140
static std::enable_if_t<!std::is_convertible< RNew, R >::value, Either< L, RNew > > Right(const RNew &r)
Definition: either.h:151
bool IsRight() const
Definition: either.h:86
R RightOr(const Either< L, R > &either, F &&f)
Definition: either.h:180
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition: either.h:209
static FmapResult_t< F > Apply(const Either< L, R > &either, const F &f)
Definition: either.h:221
Either(const R &r)
Definition: either.h:63
Either & operator=(const Either &)=default
std::pair< Cont< L >, Cont< R > > PartitionEithers(const Cont< Either< L, R >> &eithers)
Definition: either.h:196
static GSLResult_t< Type_t, AV > GSL(const Type_t &f, const AV &v)
Definition: either.h:251
Either(const L &l)
Definition: either.h:58
typename InstanceApplicative< AF >::template GSLResult< AV >::Type_t GSLResult_t
Definition: applicative.h:42
static auto EmbeddingLeft()
Definition: either.h:156
boost::optional< R > MaybeRight() const
Definition: either.h:112
static Either< L, RNew > FromMaybe(const boost::optional< RNew > &maybeRight, const L &left)
Definition: either.h:133
const R & GetRight() const
Definition: either.h:98
The Functor class is used for types that can be mapped over.
Definition: functor.h:54
Either(const Either< LPrime, RPrime > &other)
Definition: either.h:71