Introduced make_left, make_right

This commit is contained in:
Sebastian Messmer 2015-06-26 12:41:25 +02:00
parent f3222e361b
commit ab5238549d
2 changed files with 59 additions and 12 deletions

View File

@ -126,17 +126,15 @@ namespace cpputils {
};
enum class Side : unsigned char {left, right} _side;
void _construct_left(const Left &left) {
new(&_left)Left(left);
Either(Side side): _side(side) {}
template<typename... Args>
void _construct_left(Args&&... args) {
new(&_left)Left(std::forward<Args>(args)...);
}
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));
template<typename... Args>
void _construct_right(Args&&... args) {
new(&_right)Right(std::forward<Args>(args)...);
}
void _destruct() {
if (_side == Side::left) {
@ -145,6 +143,12 @@ namespace cpputils {
_right.~Right();
}
}
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>
@ -174,8 +178,19 @@ namespace cpputils {
return stream;
}
//TODO Test make_either<>
//TODO make_either<>
template<typename Left, typename Right, typename... Args>
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>
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;
}
}

View File

@ -10,6 +10,8 @@
using std::ostringstream;
using std::string;
using std::vector;
using std::pair;
using std::make_pair;
using namespace cpputils;
using ::testing::Test;
@ -309,6 +311,36 @@ TEST_F(EitherTest, OutputRight) {
EXPECT_EQ("Right(mystring)", str.str());
}
TEST_F(EitherTest, MakeLeft) {
Either<string, int> var = make_left<string, int>("mystring");
EXPECT_LEFT_IS("mystring", var);
}
TEST_F(EitherTest, MakeLeft_OnlyMoveable) {
Either<OnlyMoveable, int> var = make_left<OnlyMoveable, int>(4);
EXPECT_LEFT_IS(OnlyMoveable(4), var);
}
TEST_F(EitherTest, MakeLeft_MultiParam) {
Either<pair<int, int>, int> var = make_left<pair<int, int>, int>(4, 5);
EXPECT_LEFT_IS(make_pair(4,5), var);
}
TEST_F(EitherTest, MakeRight) {
Either<int, string> var = make_right<int, string>("mystring");
EXPECT_RIGHT_IS("mystring", var);
}
TEST_F(EitherTest, MakeRight_OnlyMoveable) {
Either<int, OnlyMoveable> var = make_right<int, OnlyMoveable>(4);
EXPECT_RIGHT_IS(OnlyMoveable(4), var);
}
TEST_F(EitherTest, MakeRight_MultiParam) {
Either<int, pair<int, int>> var = make_right<int, pair<int, int>>(4, 5);
EXPECT_RIGHT_IS(make_pair(4,5), var);
}
class DestructorCallback {
public:
MOCK_CONST_METHOD0(call, void());