2015-06-25 15:46:53 +02:00
|
|
|
#pragma once
|
|
|
|
#ifndef MESSMER_CPP_UTILS_EITHER_H
|
|
|
|
#define MESSMER_CPP_UTILS_EITHER_H
|
|
|
|
|
2015-06-26 01:07:46 +02:00
|
|
|
#include <boost/optional.hpp>
|
2015-06-25 16:27:26 +02:00
|
|
|
|
2015-06-25 15:46:53 +02:00
|
|
|
namespace cpputils {
|
|
|
|
|
|
|
|
template<class Left, class Right>
|
|
|
|
class Either final {
|
|
|
|
public:
|
2015-06-25 16:27:26 +02:00
|
|
|
//TODO Try allowing construction with any type that std::is_convertible to Left or Right.
|
2015-06-25 15:46:53 +02:00
|
|
|
Either(const Left &left): _side(Side::left) {
|
2015-06-26 01:59:29 +02:00
|
|
|
_construct_left(left);
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
Either(Left &&left): _side(Side::left) {
|
2015-06-26 01:59:29 +02:00
|
|
|
_construct_left(std::move(left));
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
Either(const Right &right): _side(Side::right) {
|
2015-06-26 01:59:29 +02:00
|
|
|
_construct_right(right);
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
Either(Right &&right): _side(Side::right) {
|
2015-06-26 01:59:29 +02:00
|
|
|
_construct_right(std::move(right));
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
2015-06-25 16:27:26 +02:00
|
|
|
//TODO Try allowing copy-construction when Left/Right types are std::is_convertible
|
2015-06-25 15:46:53 +02:00
|
|
|
Either(const Either<Left, Right> &rhs): _side(rhs._side) {
|
|
|
|
if(_side == Side::left) {
|
2015-06-26 01:59:29 +02:00
|
|
|
_construct_left(rhs._left);
|
2015-06-25 15:46:53 +02:00
|
|
|
} else {
|
2015-06-26 01:59:29 +02:00
|
|
|
_construct_right(rhs._right);
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Either(Either<Left, Right> &&rhs): _side(rhs._side) {
|
|
|
|
if(_side == Side::left) {
|
2015-06-26 01:59:29 +02:00
|
|
|
_construct_left(std::move(rhs._left));
|
2015-06-25 15:46:53 +02:00
|
|
|
} else {
|
2015-06-26 01:59:29 +02:00
|
|
|
_construct_right(std::move(rhs._right));
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-25 16:27:26 +02:00
|
|
|
|
|
|
|
~Either() {
|
2015-06-26 01:59:29 +02:00
|
|
|
_destruct();
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO Try allowing copy-assignment when Left/Right types are std::is_convertible
|
|
|
|
Either<Left, Right> &operator=(const Either<Left, Right> &rhs) {
|
|
|
|
_destruct();
|
|
|
|
_side = rhs._side;
|
2015-06-25 16:27:26 +02:00
|
|
|
if (_side == Side::left) {
|
2015-06-26 01:59:29 +02:00
|
|
|
_construct_left(rhs._left);
|
2015-06-25 16:27:26 +02:00
|
|
|
} else {
|
2015-06-26 01:59:29 +02:00
|
|
|
_construct_right(rhs._right);
|
2015-06-25 16:27:26 +02:00
|
|
|
}
|
2015-06-26 01:59:29 +02:00
|
|
|
return *this;
|
2015-06-25 16:27:26 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 01:59:29 +02:00
|
|
|
Either<Left, Right> &operator=(Either<Left, Right> &&rhs) {
|
|
|
|
_destruct();
|
|
|
|
_side = rhs._side;
|
|
|
|
if (_side == Side::left) {
|
|
|
|
_construct_left(std::move(rhs._left));
|
|
|
|
} else {
|
|
|
|
_construct_right(std::move(rhs._right));
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2015-06-25 15:46:53 +02:00
|
|
|
|
2015-06-26 01:59:29 +02:00
|
|
|
//TODO fold, map_left, map_right, left_or_else(val), right_or_else(val), left_or_else(func), right_or_else(func)
|
2015-06-25 15:46:53 +02:00
|
|
|
|
|
|
|
bool is_left() const {
|
|
|
|
return _side == Side::left;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_right() const {
|
|
|
|
return _side == Side::right;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Left &left() const {
|
|
|
|
return _left;
|
|
|
|
}
|
2015-06-26 01:07:46 +02:00
|
|
|
Left &left() {
|
|
|
|
return const_cast<Left&>(const_cast<const Either<Left, Right>*>(this)->left());
|
|
|
|
}
|
2015-06-25 15:46:53 +02:00
|
|
|
|
|
|
|
const Right &right() const {
|
|
|
|
return _right;
|
|
|
|
}
|
|
|
|
Right &right() {
|
|
|
|
return const_cast<Right&>(const_cast<const Either<Left, Right>*>(this)->right());
|
|
|
|
}
|
2015-06-26 01:07:46 +02:00
|
|
|
|
|
|
|
boost::optional<const Left&> left_opt() const {
|
|
|
|
if (_side == Side::left) {
|
|
|
|
return _left;
|
|
|
|
} else {
|
|
|
|
return boost::none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
boost::optional<Left&> left_opt() {
|
|
|
|
if (_side == Side::left) {
|
|
|
|
return _left;
|
|
|
|
} else {
|
|
|
|
return boost::none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<const Right&> right_opt() const {
|
|
|
|
if (_side == Side::right) {
|
|
|
|
return _right;
|
|
|
|
} else {
|
|
|
|
return boost::none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
boost::optional<Right&> right_opt() {
|
|
|
|
if (_side == Side::right) {
|
|
|
|
return _right;
|
|
|
|
} else {
|
|
|
|
return boost::none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 15:46:53 +02:00
|
|
|
private:
|
|
|
|
union {
|
|
|
|
Left _left;
|
|
|
|
Right _right;
|
|
|
|
};
|
|
|
|
enum class Side : unsigned char {left, right} _side;
|
2015-06-26 01:59:29 +02:00
|
|
|
|
|
|
|
void _construct_left(const Left &left) {
|
|
|
|
new(&_left)Left(left);
|
|
|
|
}
|
|
|
|
void _construct_left(Left &&left) {
|
|
|
|
new(&_left)Left(std::move(left));
|
|
|
|
}
|
|
|
|
void _construct_right(const Right &right) {
|
|
|
|
new(&_right)Right(right);
|
|
|
|
}
|
|
|
|
void _construct_right(Right &&right) {
|
|
|
|
new(&_right)Right(std::move(right));
|
|
|
|
}
|
|
|
|
void _destruct() {
|
|
|
|
if (_side == Side::left) {
|
|
|
|
_left.~Left();
|
|
|
|
} else {
|
|
|
|
_right.~Right();
|
|
|
|
}
|
|
|
|
}
|
2015-06-25 15:46:53 +02:00
|
|
|
};
|
|
|
|
|
2015-06-25 16:27:26 +02:00
|
|
|
template<class Left, class Right>
|
|
|
|
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>
|
|
|
|
bool operator!=(const Either<Left, Right> &lhs, const Either<Left, Right> &rhs) {
|
|
|
|
return !operator==(lhs, rhs);
|
|
|
|
}
|
|
|
|
|
2015-06-26 02:17:08 +02:00
|
|
|
template<class Left, class Right>
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-06-25 15:46:53 +02:00
|
|
|
//TODO Test make_either<>
|
|
|
|
//TODO make_either<>
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|