Add assignment operators
This commit is contained in:
parent
d5dadc74e7
commit
396dbcfc1c
71
either.h
71
either.h
@ -11,48 +11,61 @@ namespace cpputils {
|
||||
public:
|
||||
//TODO Try allowing construction with any type that std::is_convertible to Left or Right.
|
||||
Either(const Left &left): _side(Side::left) {
|
||||
new(&_left)Left(left);
|
||||
_construct_left(left);
|
||||
}
|
||||
Either(Left &&left): _side(Side::left) {
|
||||
new(&_left)Left(std::move(left));
|
||||
_construct_left(std::move(left));
|
||||
}
|
||||
Either(const Right &right): _side(Side::right) {
|
||||
new(&_right)Right(right);
|
||||
_construct_right(right);
|
||||
}
|
||||
Either(Right &&right): _side(Side::right) {
|
||||
new(&_right)Right(std::move(right));
|
||||
_construct_right(std::move(right));
|
||||
}
|
||||
//TODO Try allowing copy-construction when Left/Right types are std::is_convertible
|
||||
Either(const Either<Left, Right> &rhs): _side(rhs._side) {
|
||||
if(_side == Side::left) {
|
||||
new(&_left)Left(rhs._left);
|
||||
_construct_left(rhs._left);
|
||||
} else {
|
||||
new(&_right)Right(rhs._right);
|
||||
_construct_right(rhs._right);
|
||||
}
|
||||
}
|
||||
Either(Either<Left, Right> &&rhs): _side(rhs._side) {
|
||||
if(_side == Side::left) {
|
||||
new(&_left)Left(std::move(rhs._left));
|
||||
_construct_left(std::move(rhs._left));
|
||||
} else {
|
||||
new(&_right)Right(std::move(rhs._right));
|
||||
_construct_right(std::move(rhs._right));
|
||||
}
|
||||
}
|
||||
|
||||
~Either() {
|
||||
if (_side == Side::left) {
|
||||
_left.~Left();
|
||||
} else {
|
||||
_right.~Right();
|
||||
}
|
||||
_destruct();
|
||||
}
|
||||
|
||||
//TODO Test copy assignment operator
|
||||
//TODO Copy assignment operator
|
||||
//TODO Test destruction after copy assignment
|
||||
//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;
|
||||
if (_side == Side::left) {
|
||||
_construct_left(rhs._left);
|
||||
} else {
|
||||
_construct_right(rhs._right);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
//TODO Test move assignment operator
|
||||
//TODO Move assignment operator
|
||||
//TODO Test destruction after move assignment
|
||||
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;
|
||||
}
|
||||
|
||||
//TODO fold, map_left, map_right, left_or_else(val), right_or_else(val), left_or_else(func), right_or_else(func)
|
||||
|
||||
//TODO Test operator<<
|
||||
//TODO operator<<(ostream)
|
||||
@ -115,6 +128,26 @@ namespace cpputils {
|
||||
Right _right;
|
||||
};
|
||||
enum class Side : unsigned char {left, right} _side;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<class Left, class Right>
|
||||
|
@ -173,6 +173,34 @@ TEST_F(EitherTest, RightCanBeMoved) {
|
||||
EXPECT_EQ(5, val2.right().value);
|
||||
}
|
||||
|
||||
TEST_F(EitherTest, LeftCanBeAssigned) {
|
||||
Either<string, int> val = string("string");
|
||||
Either<string, int> val2 = string("otherstring");
|
||||
val2 = val;
|
||||
EXPECT_LEFT_IS("string", val2);
|
||||
}
|
||||
|
||||
TEST_F(EitherTest, RightCanBeAssigned) {
|
||||
Either<int, string> val = string("string");
|
||||
Either<int, string> val2 = string("otherstring");
|
||||
val2 = val;
|
||||
EXPECT_RIGHT_IS("string", val2);
|
||||
}
|
||||
|
||||
TEST_F(EitherTest, LeftCanBeMoveAssigned) {
|
||||
Either<OnlyMoveable, int> val = OnlyMoveable(3);
|
||||
Either<OnlyMoveable, int> val2 = OnlyMoveable(4);
|
||||
val2 = std::move(val);
|
||||
EXPECT_EQ(3, val2.left().value);
|
||||
}
|
||||
|
||||
TEST_F(EitherTest, RightCanBeMoveAssigned) {
|
||||
Either<int, OnlyMoveable> val = OnlyMoveable(3);
|
||||
Either<int, OnlyMoveable> val2 = OnlyMoveable(4);
|
||||
val2 = std::move(val);
|
||||
EXPECT_EQ(3, val2.right().value);
|
||||
}
|
||||
|
||||
TEST_F(EitherTest, ModifyLeft) {
|
||||
Either<string, int> val = string("mystring1");
|
||||
val.left() = "mystring2";
|
||||
@ -245,10 +273,15 @@ TEST_F(EitherTest, LeftNotEqualsRight) {
|
||||
class DestructorCallback {
|
||||
public:
|
||||
MOCK_CONST_METHOD0(call, void());
|
||||
|
||||
void EXPECT_CALLED(int times = 1) {
|
||||
EXPECT_CALL(*this, call()).Times(times);
|
||||
}
|
||||
};
|
||||
class ClassWithDestructorCallback {
|
||||
public:
|
||||
ClassWithDestructorCallback(const DestructorCallback *destructorCallback) : _destructorCallback(destructorCallback) { }
|
||||
ClassWithDestructorCallback(const DestructorCallback *destructorCallback) : _destructorCallback(destructorCallback) {}
|
||||
ClassWithDestructorCallback(const ClassWithDestructorCallback &rhs): _destructorCallback(rhs._destructorCallback) {}
|
||||
|
||||
~ClassWithDestructorCallback() {
|
||||
_destructorCallback->call();
|
||||
@ -272,50 +305,108 @@ private:
|
||||
};
|
||||
|
||||
class EitherTest_Destructor: public EitherTest {
|
||||
public:
|
||||
DestructorCallback destructorCallback;
|
||||
|
||||
void EXPECT_DESTRUCTOR_CALLED(int times = 1) {
|
||||
EXPECT_CALL(destructorCallback, call()).Times(times);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(EitherTest_Destructor, LeftDestructorIsCalled) {
|
||||
DestructorCallback destructorCallback;
|
||||
destructorCallback.EXPECT_CALLED(2); //Once for the temp object, once when the either class destructs
|
||||
|
||||
ClassWithDestructorCallback temp(&destructorCallback);
|
||||
Either<ClassWithDestructorCallback, string> var = temp;
|
||||
EXPECT_DESTRUCTOR_CALLED(2); //Once for the temp object, once when the either class destructs
|
||||
}
|
||||
|
||||
TEST_F(EitherTest_Destructor, RightDestructorIsCalled) {
|
||||
DestructorCallback destructorCallback;
|
||||
destructorCallback.EXPECT_CALLED(2); //Once for the temp object, once when the either class destructs
|
||||
|
||||
ClassWithDestructorCallback temp(&destructorCallback);
|
||||
Either<string, ClassWithDestructorCallback> var = temp;
|
||||
EXPECT_DESTRUCTOR_CALLED(2); //Once for the temp object, once when the either class destructs
|
||||
}
|
||||
|
||||
TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterCopying) {
|
||||
DestructorCallback destructorCallback;
|
||||
destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2
|
||||
|
||||
ClassWithDestructorCallback temp(&destructorCallback);
|
||||
Either<ClassWithDestructorCallback, string> var1 = temp;
|
||||
Either<ClassWithDestructorCallback, string> var2 = var1;
|
||||
EXPECT_DESTRUCTOR_CALLED(3); //Once for the temp object, once for var1 and once for var2
|
||||
}
|
||||
|
||||
TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterCopying) {
|
||||
DestructorCallback destructorCallback;
|
||||
destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2
|
||||
|
||||
ClassWithDestructorCallback temp(&destructorCallback);
|
||||
Either<string, ClassWithDestructorCallback> var1 = temp;
|
||||
Either<string, ClassWithDestructorCallback> var2 = var1;
|
||||
EXPECT_DESTRUCTOR_CALLED(3); //Once for the temp object, once for var1 and once for var2
|
||||
}
|
||||
|
||||
TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterMoving) {
|
||||
DestructorCallback destructorCallback;
|
||||
destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2
|
||||
|
||||
OnlyMoveableClassWithDestructorCallback temp(&destructorCallback);
|
||||
Either<OnlyMoveableClassWithDestructorCallback, string> var1 = std::move(temp);
|
||||
Either<OnlyMoveableClassWithDestructorCallback, string> var2 = std::move(var1);
|
||||
EXPECT_DESTRUCTOR_CALLED(3); //Once for the temp object, once for var1 and once for var2
|
||||
}
|
||||
|
||||
TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterMoving) {
|
||||
DestructorCallback destructorCallback;
|
||||
destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2
|
||||
|
||||
OnlyMoveableClassWithDestructorCallback temp(&destructorCallback);
|
||||
Either<string, OnlyMoveableClassWithDestructorCallback> var1 = std::move(temp);
|
||||
Either<string, OnlyMoveableClassWithDestructorCallback> var2 = std::move(var1);
|
||||
EXPECT_DESTRUCTOR_CALLED(3); //Once for the temp object, once for var1 and once for var2
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterAssignment) {
|
||||
DestructorCallback destructorCallback1;
|
||||
DestructorCallback destructorCallback2;
|
||||
destructorCallback1.EXPECT_CALLED(2); //Once for the temp1 object, once at the assignment
|
||||
destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1
|
||||
|
||||
ClassWithDestructorCallback temp1(&destructorCallback1);
|
||||
Either<ClassWithDestructorCallback, string> var1 = temp1;
|
||||
ClassWithDestructorCallback temp2(&destructorCallback2);
|
||||
Either<ClassWithDestructorCallback, string> var2 = temp2;
|
||||
var1 = var2;
|
||||
}
|
||||
|
||||
TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterAssignment) {
|
||||
DestructorCallback destructorCallback1;
|
||||
DestructorCallback destructorCallback2;
|
||||
destructorCallback1.EXPECT_CALLED(2); //Once for the temp1 object, once at the assignment
|
||||
destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1
|
||||
|
||||
ClassWithDestructorCallback temp1(&destructorCallback1);
|
||||
Either<string, ClassWithDestructorCallback> var1 = temp1;
|
||||
ClassWithDestructorCallback temp2(&destructorCallback2);
|
||||
Either<string, ClassWithDestructorCallback> var2 = temp2;
|
||||
var1 = var2;
|
||||
}
|
||||
|
||||
TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterMoveAssignment) {
|
||||
DestructorCallback destructorCallback1;
|
||||
DestructorCallback destructorCallback2;
|
||||
destructorCallback1.EXPECT_CALLED(2); //Once for the temp1 object, once at the assignment
|
||||
destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1
|
||||
|
||||
OnlyMoveableClassWithDestructorCallback temp1(&destructorCallback1);
|
||||
Either<OnlyMoveableClassWithDestructorCallback, string> var1 = std::move(temp1);
|
||||
OnlyMoveableClassWithDestructorCallback temp2(&destructorCallback2);
|
||||
Either<OnlyMoveableClassWithDestructorCallback, string> var2 = std::move(temp2);
|
||||
var1 = std::move(var2);
|
||||
}
|
||||
|
||||
TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterMoveAssignment) {
|
||||
DestructorCallback destructorCallback1;
|
||||
DestructorCallback destructorCallback2;
|
||||
destructorCallback1.EXPECT_CALLED(2); //Once for the temp1 object, once at the assignment
|
||||
destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1
|
||||
|
||||
OnlyMoveableClassWithDestructorCallback temp1(&destructorCallback1);
|
||||
Either<string, OnlyMoveableClassWithDestructorCallback> var1 = std::move(temp1);
|
||||
OnlyMoveableClassWithDestructorCallback temp2(&destructorCallback2);
|
||||
Either<string, OnlyMoveableClassWithDestructorCallback> var2 = std::move(temp2);
|
||||
var1 = std::move(var2);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user