21 template <
class Lhs,
class Rhs>
38 return META::is_copyable<Lhs>()
39 && META::is_copyable<Rhs>();
43 return META::is_movable<Lhs>()
44 && META::is_movable<Rhs>();
85 this->right = p.
right;
95 this->left = std::move(p.left);
96 this->right = std::move(p.right);
132 META::is_movable<Lhs>()
135 this->payload.left = std::move(lhs);
143 META::is_copyable<Lhs>()
145 Either(lhs_tag,
const Lhs& lhs) : lhs{
true} {
146 this->payload.left =
lhs;
154 META::is_movable<Rhs>()
156 Either(rhs_tag, Rhs&& rhs) : lhs{
false} {
157 this->payload.right = std::move(rhs);
165 META::is_copyable<Rhs>()
167 Either(rhs_tag,
const Rhs& rhs) : lhs{
false} {
168 this->payload.right = rhs;
187 META::enable_if_t<is_copyable()>
191 this->payload.left = other.getLeft();
193 this->payload.right = other.getRight();
197 META::enable_if_t<is_copyable()>
200 this->lhs = other.
lhs;
202 this->payload.left = other.
getLeft();
204 this->payload.right = other.
getRight();
210 META::enable_if_t<is_movable()>
214 this->payload.left = std::move(other.payload.left);
216 this->payload.right = std::move(other.payload.right);
220 META::enable_if_t<is_movable()>
223 this->lhs = other.
lhs;
225 this->payload.left = std::move(other.payload.left);
227 this->payload.right = std::move(other.payload.right);
239 return Either(lhs_tag{}, std::forward<L>(
lhs));
247 return left(std::forward<L>(lhs));
256 template <
class... Args>
258 return left(Lhs{std::forward<Args>(args)...});
267 template <
class... Args>
269 return right(Rhs{std::forward<Args>(args)...});
279 return Either(rhs_tag{}, std::forward<R>(rhs));
287 return right(std::forward<R>(rhs));
311 return this->payload.left.get();
323 return this->payload.right.get();
334 template <
class LeftVisitor,
class RightVisitor>
335 const Either&
visit(LeftVisitor visitLeft, RightVisitor visitRight)
const{
353 template <
class LeftVisitor>
364 template <
class RightVisitor>
376 template <
class NewLhs,
class Mapper>
393 template <
class NewRhs,
class Mapper>
413 template <
class NewLhs,
class NewRhs,
class MapperLeft,
class MapperRight>
430 Lhs
leftOr(
const Lhs& defaultValue)
const{
431 return this->payload.left.orElse(defaultValue);
440 return this->payload.right.orElse(defaultValue);
Either(lhs_tag, Lhs &&lhs)
Construct the LHS alternative (rvalue)
value_type left_type
The type of the left alternative.
payload_t & operator=(const payload_t &p)
error_type right_type
The type of the right alternative.
static void rightVoidVisitor(const Rhs &rhs)
Handy visitor that does nothing on the RHS.
Tag used to construct the LHS member (in case both are convertible types)
const Either & doIfRight(RightVisitor visitRight) const
Execute a callback if RHS is the active alternative.
Global namespace of the SCL.
static constexpr bool is_copyable()
bool hasLeft() const
Determines whether or not LHS is the active alternative.
static Either emplaceLeft(Args &&... args)
Constructs a Lhs in place.
Lhs leftOr(const Lhs &defaultValue) const
Tries to get the LHS value and fallback to default if not.
Either(const Either &other)
Rhs rightOr(const Rhs &defaultValue) const
Tries to get the RHS value and fallback to default if not.
static Either left(L &&lhs)
Construct the LHS.
payload_t payload
Stores the actual alternative payload.
payload_t & operator=(payload_t &&p)
static InvalidEitherAccess leftWhenRight()
Helper function that creates an InvaidEitherAccess when trying to access Lhs when Rhs is active...
payload_t(const payload_t &p)
bool lhs
Determines whether or ot the LHS member is the current alternative.
Optional< Lhs > left
The holder of the left alternative.
Alternative type that stores one or the other.
Either(Either &&other) noexcept
Either(rhs_tag, const Rhs &rhs)
Construct the RHS alternative (lvalue)
static Either Right(R &&rhs)
Alias for Either::right.
static Either right(R &&rhs)
Construct the RHS.
Either & operator=(const Either &other)
const Either & visit(LeftVisitor visitLeft, RightVisitor visitRight) const
Visit this Either.
Either & operator=(Either &&other) noexcept
const Rhs & getRight() const
Attempts to get the RHS value.
Either< Lhs, NewRhs > mapRightTo(Mapper mapper) const
Maps the RHS if there's any.
static Either Left(L &&lhs)
Alias for Either::left.
Either< NewLhs, Rhs > mapLeftTo(Mapper mapper) const
Maps the LHS if there's any.
Either< NewLhs, NewRhs > mapTo(MapperLeft mapLeft, MapperRight mapRight) const
Maps both alternatives.
Either(rhs_tag, Rhs &&rhs)
Construct the RHS alternative (rvalue)
const Either & doIfLeft(LeftVisitor visitLeft) const
Execute a callback if LHS is the active alternative.
static InvalidEitherAccess rightWhenLeft()
Helper function that creates an InvaidEitherAccess when trying to access Rhs when Lhs is active...
const Lhs & getLeft() const
Attempts to get the LHS value.
Tag used to construct the RHS member (in case both are convertible types)
static void leftVoidVisitor(const Lhs &lhs)
Handy visitor that does nothing on the LHS.
static constexpr bool is_movable()
static Either emplaceRight(Args &&... args)
Constructs a Rhs in place.
bool hasRight() const
Determines whether or not RHS is the active alternative.
Either(lhs_tag, const Lhs &lhs)
Construct the LHS alternative (lvalue)
Optional< Rhs > right
The holder of the right alternative.
Payload type that encapsulates both alternatives.