1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
// Originally taken from
// https://github.com/cryfs/cryfs/blob/14ad22570ddacef22d5ff139cdff68a54fc8234d/src/cpp-utils/either.h
#pragma once
#include <c10/macros/Macros.h>
#include <c10/util/C++17.h>
#include <c10/util/Optional.h>
namespace c10 {
/**
* either<A, B> is a tagged union that holds either an object of type A
* or an object of type B.
*/
template <class Left, class Right>
class either final {
public:
template <
class Head,
class... Tail,
std::enable_if_t<
std::is_constructible<Left, Head, Tail...>::value &&
!std::is_constructible<Right, Head, Tail...>::value>* = nullptr>
either(Head&& construct_left_head_arg, Tail&&... construct_left_tail_args)
: _side(Side::left) {
_construct_left(
std::forward<Head>(construct_left_head_arg),
std::forward<Tail>(construct_left_tail_args)...);
}
template <
class Head,
class... Tail,
std::enable_if_t<
!std::is_constructible<Left, Head, Tail...>::value &&
std::is_constructible<Right, Head, Tail...>::value>* = nullptr>
either(Head&& construct_right_head_arg, Tail&&... construct_right_tail_args)
: _side(Side::right) {
_construct_right(
std::forward<Head>(construct_right_head_arg),
std::forward<Tail>(construct_right_tail_args)...);
}
either(const either<Left, Right>& rhs) : _side(rhs._side) {
if (_side == Side::left) {
_construct_left(
rhs._left); // NOLINT(cppcoreguidelines-pro-type-union-access)
} else {
_construct_right(
rhs._right); // NOLINT(cppcoreguidelines-pro-type-union-access)
}
}
either(either<Left, Right>&& rhs) noexcept : _side(rhs._side) {
if (_side == Side::left) {
_construct_left(std::move(
rhs._left)); // NOLINT(cppcoreguidelines-pro-type-union-access)
} else {
_construct_right(std::move(
rhs._right)); // NOLINT(cppcoreguidelines-pro-type-union-access)
}
}
~either() {
_destruct();
}
either<Left, Right>& operator=(const either<Left, Right>& rhs) {
_destruct();
_side = rhs._side;
if (_side == Side::left) {
_construct_left(
rhs._left); // NOLINT(cppcoreguidelines-pro-type-union-access)
} else {
_construct_right(
rhs._right); // NOLINT(cppcoreguidelines-pro-type-union-access)
}
return *this;
}
either<Left, Right>& operator=(either<Left, Right>&& rhs) {
_destruct();
_side = rhs._side;
if (_side == Side::left) {
_construct_left(std::move(
rhs._left)); // NOLINT(cppcoreguidelines-pro-type-union-access)
} else {
_construct_right(std::move(
rhs._right)); // NOLINT(cppcoreguidelines-pro-type-union-access)
}
return *this;
}
bool is_left() const noexcept {
return _side == Side::left;
}
bool is_right() const noexcept {
return _side == Side::right;
}
const Left& left() const& {
if (C10_UNLIKELY(!is_left())) {
throw std::logic_error(
"Tried to get left side of an either which is right.");
}
return _left; // NOLINT(cppcoreguidelines-pro-type-union-access)
}
Left& left() & {
return const_cast<Left&>(
const_cast<const either<Left, Right>*>(this)->left());
}
Left&& left() && {
return std::move(left());
}
const Right& right() const& {
if (C10_UNLIKELY(!is_right())) {
throw std::logic_error(
"Tried to get right side of an either which is left.");
}
return _right; // NOLINT(cppcoreguidelines-pro-type-union-access)
}
Right& right() & {
return const_cast<Right&>(
const_cast<const either<Left, Right>*>(this)->right());
}
Right&& right() && {
return std::move(right());
}
template <class Result, class LeftFoldFunc, class RightFoldFunc>
Result fold(LeftFoldFunc&& leftFoldFunc, RightFoldFunc&& rightFoldFunc)
const {
if (Side::left == _side) {
return std::forward<LeftFoldFunc>(leftFoldFunc)(_left);
} else {
return std::forward<RightFoldFunc>(rightFoldFunc)(_right);
}
}
private:
union {
Left _left;
Right _right;
};
enum class Side : uint8_t { left, right } _side;
explicit either(Side side) noexcept : _side(side) {}
template <typename... Args>
void _construct_left(Args&&... args) {
new (&_left) Left(std::forward<Args>(
args)...); // NOLINT(cppcoreguidelines-pro-type-union-access)
}
template <typename... Args>
void _construct_right(Args&&... args) {
new (&_right) Right(std::forward<Args>(
args)...); // NOLINT(cppcoreguidelines-pro-type-union-access)
}
void _destruct() noexcept {
if (_side == Side::left) {
_left.~Left(); // NOLINT(cppcoreguidelines-pro-type-union-access)
} else {
_right.~Right(); // NOLINT(cppcoreguidelines-pro-type-union-access)
}
}
template <typename Left_, typename Right_, typename... Args>
friend either<Left_, Right_> make_left(Args&&... args);
template <typename Left_, typename Right_, typename... Args>
friend either<Left_, Right_> make_right(Args&&... args);
};
template <class Left, class Right>
inline bool operator==(
const either<Left, Right>& lhs,
const either<Left, Right>& rhs) {
if (lhs.is_left() != rhs.is_left()) {
return false;
}
if (lhs.is_left()) {
return lhs.left() == rhs.left();
} else {
return lhs.right() == rhs.right();
}
}
template <class Left, class Right>
inline bool operator!=(
const either<Left, Right>& lhs,
const either<Left, Right>& rhs) {
return !operator==(lhs, rhs);
}
template <class Left, class Right>
inline std::ostream& operator<<(
std::ostream& stream,
const either<Left, Right>& value) {
if (value.is_left()) {
stream << "Left(" << value.left() << ")";
} else {
stream << "Right(" << value.right() << ")";
}
return stream;
}
template <typename Left, typename Right, typename... Args>
inline either<Left, Right> make_left(Args&&... args) {
either<Left, Right> result(either<Left, Right>::Side::left);
result._construct_left(std::forward<Args>(args)...);
return result;
}
template <typename Left, typename Right, typename... Args>
inline either<Left, Right> make_right(Args&&... args) {
either<Left, Right> result(either<Left, Right>::Side::right);
result._construct_right(std::forward<Args>(args)...);
return result;
}
} // namespace c10
|