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