LeechCraft 0.6.70-14794-g33744ae6ce
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
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 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include <variant>
12#include <optional>
13#include <type_traits>
14#include "visitor.h"
15
16namespace LC
17{
18namespace Util
19{
20 template<typename L, typename R>
21 class Either
22 {
23 using Either_t = std::variant<L, R>;
24 Either_t This_;
25
26 enum { LeftVal, RightVal };
27
28 static_assert (!std::is_same<L, R>::value, "Types cannot be the same.");
29 public:
30 using L_t = L;
31 using R_t = R;
32
33 Either () = delete;
34
35 explicit Either (const L& l)
36 : This_ { l }
37 {
38 }
39
40 explicit Either (const R& r)
41 : This_ { r }
42 {
43 }
44
45 Either (const Either&) = default;
46 Either (Either&&) = default;
47 Either& operator= (const Either&) = default;
48 Either& operator= (Either&&) = default;
49
50 bool IsLeft () const
51 {
52 return This_.index () == LeftVal;
53 }
54
55 bool IsRight () const
56 {
57 return This_.index () == RightVal;
58 }
59
60 const L& GetLeft () const
61 {
62 if (!IsLeft ())
63 throw std::runtime_error { "Tried accessing Left for a Right Either" };
64 return std::get<L> (This_);
65 }
66
67 const R& GetRight () const
68 {
69 if (!IsRight ())
70 throw std::runtime_error { "Tried accessing Right for a Left Either" };
71 return std::get<R> (This_);
72 }
73
74 std::optional<L> MaybeLeft () const
75 {
76 if (!IsLeft ())
77 return {};
78 return GetLeft ();
79 }
80
81 std::optional<R> MaybeRight () const
82 {
83 if (!IsRight ())
84 return {};
85 return GetRight ();
86 }
87
88 std::variant<L, R> AsVariant () const
89 {
90 return This_;
91 }
92
93 template<typename F>
94 R ToRight (F&& f) const
95 {
96 return IsRight () ?
97 GetRight () :
98 f (GetLeft ());
99 }
100
101 template<typename RNew>
102 static Either<L, RNew> FromMaybe (const std::optional<RNew>& maybeRight, const L& left)
103 {
104 return maybeRight ?
105 Either<L, RNew>::Right (*maybeRight) :
107 }
108
109 static Either Left (const L& l)
110 {
111 return Either { l };
112 }
113
114 static Either Right (const R& r)
115 {
116 return Either { r };
117 }
118
119 template<typename... Vars>
120 static Either LeftLift (const std::variant<Vars...>& var)
121 {
122 return Either { std::visit ([] (auto&& arg) { return L { std::forward<decltype (arg)> (arg) }; }, var) };
123 }
124
125 template<typename... Vars>
126 static Either LeftLift (const Either<std::variant<Vars...>, R>& either)
127 {
128 return either.IsRight () ?
129 Right (either.GetRight ()) :
130 LeftLift (either.GetLeft ());
131 }
132
133 template<typename LPrime, typename = std::enable_if_t<std::is_convertible_v<LPrime, L>>>
134 static Either LeftLift (const Either<LPrime, R>& either)
135 {
136 return either.IsRight () ?
137 Right (either.GetRight ()) :
138 Left (either.GetLeft ());
139 }
140
141 template<typename RNew>
142 static std::enable_if_t<!std::is_convertible<RNew, R>::value, Either<L, RNew>> Right (const RNew& r)
143 {
144 return Either<L, RNew>::Right (r);
145 }
146
147 static auto EmbeddingLeft ()
148 {
149 return [] (const auto& other)
150 {
151 static_assert (std::is_convertible<std::decay_t<decltype (other.GetLeft ())>, L>::value,
152 "Other's Either's Left type is not convertible to this Left type.");
153 return other.IsLeft () ?
154 Either<L, R>::Left (other.GetLeft ()) :
155 Either<L, R>::Right (other.GetRight ());
156 };
157 }
158
159 friend bool operator== (const Either& e1, const Either& e2)
160 {
161 return e1.This_ == e2.This_;
162 }
163
164 friend bool operator!= (const Either& e1, const Either& e2)
165 {
166 return !(e1 == e2);
167 }
168 };
169
170 template<typename L, typename R, typename F, typename = std::result_of_t<F ()>>
171 R RightOr (const Either<L, R>& either, F&& f)
172 {
173 return either.IsRight () ?
174 either.GetRight () :
175 f ();
176 }
177
178 template<typename L, typename R>
179 R RightOr (const Either<L, R>& either, const R& r)
180 {
181 return either.IsRight () ?
182 either.GetRight () :
183 r;
184 }
185
186 template<template<typename> class Cont, typename L, typename R>
187 std::pair<Cont<L>, Cont<R>> PartitionEithers (const Cont<Either<L, R>>& eithers)
188 {
189 std::pair<Cont<L>, Cont<R>> result;
190 for (const auto& either : eithers)
191 if (either.IsLeft ())
192 result.first.push_back (either.GetLeft ());
193 else
194 result.second.push_back (either.GetRight ());
195
196 return result;
197 }
198
199 template<typename Left, typename Right, typename... Args>
200 auto Visit (const Either<Left, Right>& either, Args&&... args)
201 {
202 return Visit (either.AsVariant (), std::forward<Args> (args)...);
203 }
204}
205}
friend bool operator!=(const Either &e1, const Either &e2)
Definition: either.h:164
static Either Right(const R &r)
Definition: either.h:114
Either(const L &l)
Definition: either.h:35
Either(Either &&)=default
std::optional< L > MaybeLeft() const
Definition: either.h:74
static auto EmbeddingLeft()
Definition: either.h:147
static Either Left(const L &l)
Definition: either.h:109
Either & operator=(const Either &)=default
Either(const R &r)
Definition: either.h:40
static Either LeftLift(const Either< std::variant< Vars... >, R > &either)
Definition: either.h:126
std::variant< L, R > AsVariant() const
Definition: either.h:88
static std::enable_if_t<!std::is_convertible< RNew, R >::value, Either< L, RNew > > Right(const RNew &r)
Definition: either.h:142
const L & GetLeft() const
Definition: either.h:60
std::optional< R > MaybeRight() const
Definition: either.h:81
static Either LeftLift(const std::variant< Vars... > &var)
Definition: either.h:120
bool IsRight() const
Definition: either.h:55
R ToRight(F &&f) const
Definition: either.h:94
friend bool operator==(const Either &e1, const Either &e2)
Definition: either.h:159
static Either LeftLift(const Either< LPrime, R > &either)
Definition: either.h:134
static Either< L, RNew > FromMaybe(const std::optional< RNew > &maybeRight, const L &left)
Definition: either.h:102
bool IsLeft() const
Definition: either.h:50
const R & GetRight() const
Definition: either.h:67
Either(const Either &)=default
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition: either.h:200
std::pair< Cont< L >, Cont< R > > PartitionEithers(const Cont< Either< L, R > > &eithers)
Definition: either.h:187
R RightOr(const Either< L, R > &either, F &&f)
Definition: either.h:171
Definition: constants.h:15