Support Class Library
A set of tools providing classes and utility
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
scl::utils::Either< Lhs, Rhs > Class Template Reference

Alternative type that stores one or the other. More...

#include "Either.h"

Collaboration diagram for scl::utils::Either< Lhs, Rhs >:
Collaboration graph
[legend]

Classes

struct  lhs_tag
 Tag used to construct the LHS member (in case both are convertible types) More...
 
struct  payload_t
 Payload type that encapsulates both alternatives. More...
 
struct  rhs_tag
 Tag used to construct the RHS member (in case both are convertible types) More...
 

Public Types

using left_type = Lhs
 The type of the left alternative. More...
 
using right_type = Rhs
 The type of the right alternative. More...
 

Public Member Functions

 Either ()=delete
 
template<class = scl::tools::meta ::void_t< scl::tools::meta ::enable_if_t<is_copyable()> >>
 Either (const Either &other)
 
template<class = scl::tools::meta ::void_t< scl::tools::meta ::enable_if_t<is_copyable()> >>
Eitheroperator= (const Either &other)
 
template<class = scl::tools::meta ::void_t< scl::tools::meta ::enable_if_t<is_movable()> >>
 Either (Either &&other) noexcept
 
template<class = scl::tools::meta ::void_t< scl::tools::meta ::enable_if_t<is_movable()> >>
Eitheroperator= (Either &&other) noexcept
 
bool hasLeft () const
 Determines whether or not LHS is the active alternative. More...
 
bool hasRight () const
 Determines whether or not RHS is the active alternative. More...
 
const Lhs & getLeft () const
 Attempts to get the LHS value. More...
 
const Rhs & getRight () const
 Attempts to get the RHS value. More...
 
template<class LeftVisitor , class RightVisitor >
const Eithervisit (LeftVisitor visitLeft, RightVisitor visitRight) const
 Visit this Either. More...
 
template<class LeftVisitor >
const EitherdoIfLeft (LeftVisitor visitLeft) const
 Execute a callback if LHS is the active alternative. More...
 
template<class RightVisitor >
const EitherdoIfRight (RightVisitor visitRight) const
 Execute a callback if RHS is the active alternative. More...
 
template<class NewLhs , class Mapper >
Either< NewLhs, Rhs > mapLeftTo (Mapper mapper) const
 Maps the LHS if there's any. More...
 
template<class NewRhs , class Mapper >
Either< Lhs, NewRhs > mapRightTo (Mapper mapper) const
 Maps the RHS if there's any. More...
 
template<class NewLhs , class NewRhs , class MapperLeft , class MapperRight >
Either< NewLhs, NewRhs > mapTo (MapperLeft mapLeft, MapperRight mapRight) const
 Maps both alternatives. More...
 
Lhs leftOr (const Lhs &defaultValue) const
 Tries to get the LHS value and fallback to default if not. More...
 
Rhs rightOr (const Rhs &defaultValue) const
 Tries to get the RHS value and fallback to default if not. More...
 

Static Public Member Functions

template<class L >
static Either left (L &&lhs)
 Construct the LHS. More...
 
template<class L >
static Either Left (L &&lhs)
 Alias for Either::left. More...
 
template<class... Args>
static Either emplaceLeft (Args &&... args)
 Constructs a Lhs in place. More...
 
template<class... Args>
static Either emplaceRight (Args &&... args)
 Constructs a Rhs in place. More...
 
template<class R >
static Either right (R &&rhs)
 Construct the RHS. More...
 
template<class R >
static Either Right (R &&rhs)
 Alias for Either::right. More...
 

Protected Member Functions

template<class = scl::tools::meta ::enable_if_t< scl::tools::meta ::is_movable<Lhs>() >>
 Either (lhs_tag, Lhs &&lhs)
 Construct the LHS alternative (rvalue) More...
 
template<class = scl::tools::meta ::enable_if_t< scl::tools::meta ::is_copyable<Lhs>() >>
 Either (lhs_tag, const Lhs &lhs)
 Construct the LHS alternative (lvalue) More...
 
template<class = scl::tools::meta ::void_t< scl::tools::meta ::enable_if_t< scl::tools::meta ::is_movable<Rhs>() >>>
 Either (rhs_tag, Rhs &&rhs)
 Construct the RHS alternative (rvalue) More...
 
template<class = scl::tools::meta ::void_t< scl::tools::meta ::enable_if_t< scl::tools::meta ::is_copyable<Rhs>() >>>
 Either (rhs_tag, const Rhs &rhs)
 Construct the RHS alternative (lvalue) More...
 

Static Protected Member Functions

static constexpr bool is_copyable ()
 
static constexpr bool is_movable ()
 
static void leftVoidVisitor (const Lhs &lhs)
 Handy visitor that does nothing on the LHS. More...
 
static void rightVoidVisitor (const Rhs &rhs)
 Handy visitor that does nothing on the RHS. More...
 

Protected Attributes

bool lhs = false
 Determines whether or ot the LHS member is the current alternative. More...
 
payload_t payload = {}
 Stores the actual alternative payload. More...
 

Detailed Description

template<class Lhs, class Rhs>
class scl::utils::Either< Lhs, Rhs >

Alternative type that stores one or the other.

Template Parameters
Lhsbeing the left value type
Rhsbeing the right value type
Warning
Either defines move and/or copy semantics only if both the underlying types do so

Definition at line 22 of file Either.h.

Member Typedef Documentation

◆ left_type

template<class Lhs, class Rhs>
scl::utils::Either< Lhs, Rhs >::left_type

The type of the left alternative.

Definition at line 28 of file Either.h.

◆ right_type

template<class Lhs, class Rhs>
scl::utils::Either< Lhs, Rhs >::right_type

The type of the right alternative.

Definition at line 34 of file Either.h.

Constructor & Destructor Documentation

◆ Either() [1/7]

template<class Lhs, class Rhs>
template<class = scl::tools::meta ::enable_if_t< scl::tools::meta ::is_movable<Lhs>() >>
scl::utils::Either< Lhs, Rhs >::Either ( lhs_tag  ,
Lhs &&  lhs 
)
inlineprotected

Construct the LHS alternative (rvalue)

Parameters
lhsbeing the LHS value to construct from

Definition at line 134 of file Either.h.

◆ Either() [2/7]

template<class Lhs, class Rhs>
template<class = scl::tools::meta ::enable_if_t< scl::tools::meta ::is_copyable<Lhs>() >>
scl::utils::Either< Lhs, Rhs >::Either ( lhs_tag  ,
const Lhs &  lhs 
)
inlineprotected

Construct the LHS alternative (lvalue)

Parameters
lhsbeing the LHS value to construct from

Definition at line 145 of file Either.h.

◆ Either() [3/7]

template<class Lhs, class Rhs>
template<class = scl::tools::meta ::void_t< scl::tools::meta ::enable_if_t< scl::tools::meta ::is_movable<Rhs>() >>>
scl::utils::Either< Lhs, Rhs >::Either ( rhs_tag  ,
Rhs &&  rhs 
)
inlineprotected

Construct the RHS alternative (rvalue)

Parameters
rhsbeing the RHS value to construct from

Definition at line 156 of file Either.h.

◆ Either() [4/7]

template<class Lhs, class Rhs>
template<class = scl::tools::meta ::void_t< scl::tools::meta ::enable_if_t< scl::tools::meta ::is_copyable<Rhs>() >>>
scl::utils::Either< Lhs, Rhs >::Either ( rhs_tag  ,
const Rhs &  rhs 
)
inlineprotected

Construct the RHS alternative (lvalue)

Parameters
rhsbeing the RHS value to construct from

Definition at line 167 of file Either.h.

◆ Either() [5/7]

template<class Lhs, class Rhs>
scl::utils::Either< Lhs, Rhs >::Either ( )
delete

◆ Either() [6/7]

template<class Lhs, class Rhs>
template<class = scl::tools::meta ::void_t< scl::tools::meta ::enable_if_t<is_copyable()> >>
scl::utils::Either< Lhs, Rhs >::Either ( const Either< Lhs, Rhs > &  other)
inline

Definition at line 189 of file Either.h.

◆ Either() [7/7]

template<class Lhs, class Rhs>
template<class = scl::tools::meta ::void_t< scl::tools::meta ::enable_if_t<is_movable()> >>
scl::utils::Either< Lhs, Rhs >::Either ( Either< Lhs, Rhs > &&  other)
inlinenoexcept

Definition at line 212 of file Either.h.

Member Function Documentation

◆ doIfLeft()

template<class Lhs, class Rhs>
template<class LeftVisitor >
const Either& scl::utils::Either< Lhs, Rhs >::doIfLeft ( LeftVisitor  visitLeft) const
inline

Execute a callback if LHS is the active alternative.

Template Parameters
LeftVisitorbeing the type of the callback
Parameters
visitLeftbeing the callback
Returns
a reference to this Either

Definition at line 354 of file Either.h.

◆ doIfRight()

template<class Lhs, class Rhs>
template<class RightVisitor >
const Either& scl::utils::Either< Lhs, Rhs >::doIfRight ( RightVisitor  visitRight) const
inline

Execute a callback if RHS is the active alternative.

Template Parameters
RightVisitorbeing the type of the callback
Parameters
visitRightbeing the callback
Returns
a reference to this Either

Definition at line 365 of file Either.h.

◆ emplaceLeft()

template<class Lhs, class Rhs>
template<class... Args>
static Either scl::utils::Either< Lhs, Rhs >::emplaceLeft ( Args &&...  args)
inlinestatic

Constructs a Lhs in place.

Template Parameters
Argsbeing the types of the arguments for the constructor
Parameters
argsbeing the arguments for the constructor
Returns
a Either<Lhs, Rhs> where Lhs is the active alternative

Definition at line 257 of file Either.h.

◆ emplaceRight()

template<class Lhs, class Rhs>
template<class... Args>
static Either scl::utils::Either< Lhs, Rhs >::emplaceRight ( Args &&...  args)
inlinestatic

Constructs a Rhs in place.

Template Parameters
Argsbeing the types of the arguments for the constructor
Parameters
argsbeing the arguments for the constructor
Returns
a Either<Lhs, Rhs> where Rhs is the active alternative

Definition at line 268 of file Either.h.

◆ getLeft()

template<class Lhs, class Rhs>
const Lhs& scl::utils::Either< Lhs, Rhs >::getLeft ( ) const
inline

Attempts to get the LHS value.

Returns
a const& to the LHS value
Exceptions
exceptions::InvalidEitherAccessif LHS is not the active alternative

Definition at line 307 of file Either.h.

◆ getRight()

template<class Lhs, class Rhs>
const Rhs& scl::utils::Either< Lhs, Rhs >::getRight ( ) const
inline

Attempts to get the RHS value.

Returns
a const& to the RHS value
Exceptions
exceptions::InvalidEitherAccessif RHS is not the active alternative

Definition at line 319 of file Either.h.

◆ hasLeft()

template<class Lhs, class Rhs>
bool scl::utils::Either< Lhs, Rhs >::hasLeft ( ) const
inline

Determines whether or not LHS is the active alternative.

Returns
TRUE if it is, FALSE otherwise

Definition at line 294 of file Either.h.

◆ hasRight()

template<class Lhs, class Rhs>
bool scl::utils::Either< Lhs, Rhs >::hasRight ( ) const
inline

Determines whether or not RHS is the active alternative.

Returns
TRUE if it is, FALSE otherwise

Definition at line 300 of file Either.h.

◆ is_copyable()

template<class Lhs, class Rhs>
static constexpr bool scl::utils::Either< Lhs, Rhs >::is_copyable ( )
inlinestaticprotected

Definition at line 37 of file Either.h.

◆ is_movable()

template<class Lhs, class Rhs>
static constexpr bool scl::utils::Either< Lhs, Rhs >::is_movable ( )
inlinestaticprotected

Definition at line 42 of file Either.h.

◆ left()

template<class Lhs, class Rhs>
template<class L >
static Either scl::utils::Either< Lhs, Rhs >::left ( L &&  lhs)
inlinestatic

Construct the LHS.

Parameters
lhsbeing the value to construct from
Returns
a Either<Lhs, Rhs> where the Lhs is the active alternative

Definition at line 238 of file Either.h.

◆ Left()

template<class Lhs, class Rhs>
template<class L >
static Either scl::utils::Either< Lhs, Rhs >::Left ( L &&  lhs)
inlinestatic

Alias for Either::left.

Definition at line 246 of file Either.h.

◆ leftOr()

template<class Lhs, class Rhs>
Lhs scl::utils::Either< Lhs, Rhs >::leftOr ( const Lhs &  defaultValue) const
inline

Tries to get the LHS value and fallback to default if not.

Parameters
defaultValuebeing the default value to use if LHS is not the current alternative
Returns
Lhs

Definition at line 430 of file Either.h.

◆ leftVoidVisitor()

template<class Lhs, class Rhs>
static void scl::utils::Either< Lhs, Rhs >::leftVoidVisitor ( const Lhs &  lhs)
inlinestaticprotected

Handy visitor that does nothing on the LHS.

Parameters
lhsbeing the LHS value of the alternative

Definition at line 175 of file Either.h.

◆ mapLeftTo()

template<class Lhs, class Rhs>
template<class NewLhs , class Mapper >
Either<NewLhs, Rhs> scl::utils::Either< Lhs, Rhs >::mapLeftTo ( Mapper  mapper) const
inline

Maps the LHS if there's any.

Template Parameters
NewLhsbeing the new LHS value type
Mapperbeing Lhs -> NewLhs
Parameters
mapperbeing the mapper function
Returns
a correctly mapper Either<NewLhs, Rhs>

Definition at line 377 of file Either.h.

◆ mapRightTo()

template<class Lhs, class Rhs>
template<class NewRhs , class Mapper >
Either<Lhs, NewRhs> scl::utils::Either< Lhs, Rhs >::mapRightTo ( Mapper  mapper) const
inline

Maps the RHS if there's any.

Template Parameters
NewRhsbeing the new RHS value type
Mapperbeing Rhs -> NewRhs
Parameters
mapperbeing the mapper function
Returns
a correctly mapper Either<Lhs, NewRhs>

Definition at line 394 of file Either.h.

◆ mapTo()

template<class Lhs, class Rhs>
template<class NewLhs , class NewRhs , class MapperLeft , class MapperRight >
Either<NewLhs, NewRhs> scl::utils::Either< Lhs, Rhs >::mapTo ( MapperLeft  mapLeft,
MapperRight  mapRight 
) const
inline

Maps both alternatives.

Template Parameters
NewLhsbeing the new LHS value type
NewRhsbeing the new RHS value type
MapperLeftbeing Lhs -> NewLhs
MapperRightbeing Rhs -> NewRhs
Parameters
mapLeftbeing the LHS mapper function
mapRightbeing the RHS mapper function
Returns
the mapped Either<NewLhs, NewRhs>

Definition at line 414 of file Either.h.

◆ operator=() [1/2]

template<class Lhs, class Rhs>
template<class = scl::tools::meta ::void_t< scl::tools::meta ::enable_if_t<is_copyable()> >>
Either& scl::utils::Either< Lhs, Rhs >::operator= ( const Either< Lhs, Rhs > &  other)
inline

Definition at line 199 of file Either.h.

◆ operator=() [2/2]

template<class Lhs, class Rhs>
template<class = scl::tools::meta ::void_t< scl::tools::meta ::enable_if_t<is_movable()> >>
Either& scl::utils::Either< Lhs, Rhs >::operator= ( Either< Lhs, Rhs > &&  other)
inlinenoexcept

Definition at line 222 of file Either.h.

◆ right()

template<class Lhs, class Rhs>
template<class R >
static Either scl::utils::Either< Lhs, Rhs >::right ( R &&  rhs)
inlinestatic

Construct the RHS.

Parameters
rhsbeing the value to construct from
Returns
a Either<Lhs, Rhs> where the Rhs is the active alternative

Definition at line 278 of file Either.h.

◆ Right()

template<class Lhs, class Rhs>
template<class R >
static Either scl::utils::Either< Lhs, Rhs >::Right ( R &&  rhs)
inlinestatic

Alias for Either::right.

Definition at line 286 of file Either.h.

◆ rightOr()

template<class Lhs, class Rhs>
Rhs scl::utils::Either< Lhs, Rhs >::rightOr ( const Rhs &  defaultValue) const
inline

Tries to get the RHS value and fallback to default if not.

Parameters
defaultValuebeing the default value to use if LHS is not the current alternative
Returns
Rhs

Definition at line 439 of file Either.h.

◆ rightVoidVisitor()

template<class Lhs, class Rhs>
static void scl::utils::Either< Lhs, Rhs >::rightVoidVisitor ( const Rhs &  rhs)
inlinestaticprotected

Handy visitor that does nothing on the RHS.

Parameters
rhsbeing the RHS value of the alternative

Definition at line 181 of file Either.h.

◆ visit()

template<class Lhs, class Rhs>
template<class LeftVisitor , class RightVisitor >
const Either& scl::utils::Either< Lhs, Rhs >::visit ( LeftVisitor  visitLeft,
RightVisitor  visitRight 
) const
inline

Visit this Either.

Template Parameters
LeftVisitorbeing the type of the visitor for the LHS
RightVisitorbeing the type of the visitor for the RHS
Parameters
visitLeftbeing the visitor for the LHS
visitRightbeing the visitor for the RHS
Returns
a reference to this Either

Definition at line 335 of file Either.h.

Member Data Documentation

◆ lhs

template<class Lhs, class Rhs>
scl::utils::Either< Lhs, Rhs >::lhs = false
protected

Determines whether or ot the LHS member is the current alternative.

Definition at line 117 of file Either.h.

◆ payload

template<class Lhs, class Rhs>
scl::utils::Either< Lhs, Rhs >::payload = {}
protected

Stores the actual alternative payload.

Definition at line 123 of file Either.h.


The documentation for this class was generated from the following file: