Merge branch 'feature/optional_fix' into feature/either2

This commit is contained in:
Sebastian Messmer 2018-12-25 19:54:10 +01:00
commit 8c02968898
2 changed files with 40 additions and 40 deletions

View File

@ -27,18 +27,18 @@ namespace cpputils {
either(const either<Left, Right> &rhs) noexcept(noexcept(std::declval<either<Left, Right>>()._construct_left(rhs._left)) && noexcept(std::declval<either<Left, Right>>()._construct_right(rhs._right)))
: _side(rhs._side) {
if(_side == Side::left) {
_construct_left(rhs._left);
_construct_left(rhs._left); // NOLINT(cppcoreguidelines-pro-type-union-access)
} else {
_construct_right(rhs._right);
_construct_right(rhs._right); // NOLINT(cppcoreguidelines-pro-type-union-access)
}
}
either(either<Left, Right> &&rhs) noexcept(noexcept(_construct_left(std::move(rhs._left))) && noexcept(_construct_right(std::move(rhs._right))))
: _side(rhs._side) {
if(_side == Side::left) {
_construct_left(std::move(rhs._left));
_construct_left(std::move(rhs._left)); // NOLINT(cppcoreguidelines-pro-type-union-access)
} else {
_construct_right(std::move(rhs._right));
_construct_right(std::move(rhs._right)); // NOLINT(cppcoreguidelines-pro-type-union-access)
}
}
@ -51,9 +51,9 @@ namespace cpputils {
_destruct();
_side = rhs._side;
if (_side == Side::left) {
_construct_left(rhs._left);
_construct_left(rhs._left); // NOLINT(cppcoreguidelines-pro-type-union-access)
} else {
_construct_right(rhs._right);
_construct_right(rhs._right); // NOLINT(cppcoreguidelines-pro-type-union-access)
}
return *this;
}
@ -62,9 +62,9 @@ namespace cpputils {
_destruct();
_side = rhs._side;
if (_side == Side::left) {
_construct_left(std::move(rhs._left));
_construct_left(std::move(rhs._left)); // NOLINT(cppcoreguidelines-pro-type-union-access)
} else {
_construct_right(std::move(rhs._right));
_construct_right(std::move(rhs._right)); // NOLINT(cppcoreguidelines-pro-type-union-access)
}
return *this;
}
@ -83,7 +83,7 @@ namespace cpputils {
if (!is_left()) {
throw std::logic_error("Tried to get left side of an either which is right.");
}
return _left;
return _left; // NOLINT(cppcoreguidelines-pro-type-union-access)
}
Left &left() & {
return const_cast<Left&>(const_cast<const either<Left, Right>*>(this)->left());
@ -96,7 +96,7 @@ namespace cpputils {
if (!is_right()) {
throw std::logic_error("Tried to get right side of an either which is left.");
}
return _right;
return _right; // NOLINT(cppcoreguidelines-pro-type-union-access)
}
Right &right() & {
return const_cast<Right&>(const_cast<const either<Left, Right>*>(this)->right());
@ -107,14 +107,14 @@ namespace cpputils {
boost::optional<const Left&> left_opt() const& noexcept {
if (_side == Side::left) {
return _left;
return _left; // NOLINT(cppcoreguidelines-pro-type-union-access)
} else {
return boost::none;
}
}
boost::optional<Left&> left_opt() & noexcept {
if (_side == Side::left) {
return _left;
return _left; // NOLINT(cppcoreguidelines-pro-type-union-access)
} else {
return boost::none;
}
@ -123,14 +123,14 @@ namespace cpputils {
boost::optional<const Right&> right_opt() const& noexcept {
if (_side == Side::right) {
return _right;
return _right; // NOLINT(cppcoreguidelines-pro-type-union-access)
} else {
return boost::none;
}
}
boost::optional<Right&> right_opt() & noexcept {
if (_side == Side::right) {
return _right;
return _right; // NOLINT(cppcoreguidelines-pro-type-union-access)
} else {
return boost::none;
}
@ -148,17 +148,17 @@ namespace cpputils {
template<typename... Args>
void _construct_left(Args&&... args) noexcept(noexcept(new Left(std::forward<Args>(args)...))) {
new(&_left)Left(std::forward<Args>(args)...);
new(&_left)Left(std::forward<Args>(args)...); // NOLINT(cppcoreguidelines-pro-type-union-access)
}
template<typename... Args>
void _construct_right(Args&&... args) noexcept(noexcept(new Right(std::forward<Args>(args)...))) {
new(&_right)Right(std::forward<Args>(args)...);
new(&_right)Right(std::forward<Args>(args)...); // NOLINT(cppcoreguidelines-pro-type-union-access)
}
void _destruct() noexcept {
if (_side == Side::left) {
_left.~Left();
_left.~Left(); // NOLINT(cppcoreguidelines-pro-type-union-access)
} else {
_right.~Right();
_right.~Right(); // NOLINT(cppcoreguidelines-pro-type-union-access)
}
}

View File

@ -341,7 +341,7 @@ TEST(EitherTest, givenLeftMoveConstructedFromValue_thenOldIsCorrect) {
[] (const auto& test) {
MovableOnly a(3);
either<MovableOnly, int> b(std::move(a));
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}
},
EXPECT_IS<MovableOnly>(MovableOnly(0)) // 0 is moved-from value
@ -365,7 +365,7 @@ TEST(EitherTest, givenRightMoveConstructedFromValue_thenOldIsCorrect) {
[] (const auto& test) {
MovableOnly a(3);
either<int, MovableOnly> b(std::move(a));
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}
},
EXPECT_IS<MovableOnly>(MovableOnly(0)) // 0 is moved-from value
@ -468,12 +468,12 @@ TEST(EitherTest, givenLeftMoveAssignedFromValue_thenOldIsCorrect) {
MovableOnly a(3);
either<MovableOnly, string> b("2");
b = std::move(a);
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}, [] (const auto& test) {
MovableOnly a(3);
either<MovableOnly, string> b(MovableOnly(0));
b = std::move(a);
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}
},
EXPECT_IS<MovableOnly>(MovableOnly(0))
@ -504,12 +504,12 @@ TEST(EitherTest, givenRightMoveAssignedFromValue_thenOldIsCorrect) {
MovableOnly a(3);
either<string, MovableOnly> b("2");
b = std::move(a);
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}, [] (const auto& test) {
MovableOnly a(3);
either<string, MovableOnly> b(MovableOnly(2));
b = std::move(a);
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}
},
EXPECT_IS<MovableOnly>(MovableOnly(0)) // 0 is moved-from value
@ -631,7 +631,7 @@ TEST(EitherTest, givenLeftMoveConstructed_thenOldIsCorrect) {
[] (const auto& test) {
either<MovableOnly, int> a(MovableOnly(3));
either<MovableOnly, int> b(std::move(a));
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}
},
EXPECT_IS_LEFT<MovableOnly, int>(MovableOnly(0)) // 0 is moved-from value
@ -655,7 +655,7 @@ TEST(EitherTest, givenLeftMoveConstructed_withSameType_thenOldIsCorrect) {
[] (const auto& test) {
either<MovableOnly, MovableOnly> a = make_left<MovableOnly, MovableOnly>(MovableOnly(3));
either<MovableOnly, MovableOnly> b(std::move(a));
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}
},
EXPECT_IS_LEFT<MovableOnly, MovableOnly>(MovableOnly(0)) // 0 is moved-from value
@ -679,7 +679,7 @@ TEST(EitherTest, givenRightMoveConstructed_thenOldIsCorrect) {
[] (const auto& test) {
either<int, MovableOnly> a(MovableOnly(3));
either<int, MovableOnly> b(std::move(a));
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}
},
EXPECT_IS_RIGHT<int, MovableOnly>(MovableOnly(0)) // 0 is moved-from value
@ -703,7 +703,7 @@ TEST(EitherTest, givenRightMoveConstructed_withSameType_thenOldIsCorrect) {
[] (const auto& test) {
either<MovableOnly, MovableOnly> a = make_right<MovableOnly, MovableOnly>(MovableOnly(3));
either<MovableOnly, MovableOnly> b(std::move(a));
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}
},
EXPECT_IS_RIGHT<MovableOnly, MovableOnly>(MovableOnly(0)) // 0 is moved-from value
@ -878,12 +878,12 @@ TEST(EitherTest, givenLeftMoveAssigned_thenOldIsCorrect) {
either<MovableOnly, string> a(MovableOnly(3));
either<MovableOnly, string> b(2);
b = std::move(a);
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}, [] (const auto& test) {
either<MovableOnly, string> a(MovableOnly(3));
either<MovableOnly, string> b(MovableOnly(2));
b = std::move(a);
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}
},
EXPECT_IS_LEFT<MovableOnly, string>(MovableOnly(0)) // 0 is moved-from value
@ -914,12 +914,12 @@ TEST(EitherTest, givenLeftMoveAssigned_withSameType_thenOldIsCorrect) {
either<MovableOnly, MovableOnly> a = make_left<MovableOnly, MovableOnly>(3);
either<MovableOnly, MovableOnly> b = make_right<MovableOnly, MovableOnly>(2);
b = std::move(a);
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}, [] (const auto& test) {
either<MovableOnly, MovableOnly> a = make_left<MovableOnly, MovableOnly>(3);
either<MovableOnly, MovableOnly> b = make_left<MovableOnly, MovableOnly>(2);
b = std::move(a);
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}
},
EXPECT_IS_LEFT<MovableOnly, MovableOnly>(MovableOnly(0)) // 0 is moved-from value
@ -950,12 +950,12 @@ TEST(EitherTest, givenRightMoveAssigned_thenOldIsCorrect) {
either<string, MovableOnly> a(MovableOnly(3));
either<string, MovableOnly> b("2");
b = std::move(a);
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}, [] (const auto& test) {
either<string, MovableOnly> a(MovableOnly(3));
either<string, MovableOnly> b(MovableOnly(2));
b = std::move(a);
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}
},
EXPECT_IS_RIGHT<string, MovableOnly>(MovableOnly(0)) // 0 is moved-from value
@ -986,12 +986,12 @@ TEST(EitherTest, givenRightMoveAssigned_withSameType_thenOldIsCorrect) {
either<MovableOnly, MovableOnly> a = make_right<MovableOnly, MovableOnly>(3);
either<MovableOnly, MovableOnly> b = make_left<MovableOnly, MovableOnly>(2);
b = std::move(a);
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}, [] (const auto& test) {
either<MovableOnly, MovableOnly> a = make_right<MovableOnly, MovableOnly>(3);
either<MovableOnly, MovableOnly> b = make_right<MovableOnly, MovableOnly>(2);
b = std::move(a);
test(a);
test(a); // NOLINT(bugprone-use-after-move)
}
},
EXPECT_IS_RIGHT<MovableOnly, MovableOnly>(MovableOnly(0)) // 0 is moved-from value
@ -1033,22 +1033,22 @@ TEST(EitherTest, givenRight_whenModified_thenValueIsChanged) {
TEST(EitherTest, canEmplaceConstructLeft) {
test_with_matrix({
[] (const auto& test) {
either<tuple<int, int>, tuple<int, int, int>> a(2, 3);
either<tuple<int, int>, tuple<int, string, int>> a(2, 3);
test(a);
}
},
EXPECT_IS_LEFT<tuple<int, int>, tuple<int, int, int>>(tuple<int, int>(2, 3))
EXPECT_IS_LEFT<tuple<int, int>, tuple<int, string, int>>(tuple<int, int>(2, 3))
);
}
TEST(EitherTest, canEmplaceConstructRight) {
test_with_matrix({
[] (const auto& test) {
either<tuple<int, int>, tuple<int, int, int>> a(2, 3, 4);
either<tuple<int, int>, tuple<int, string, int>> a(2, "3", 4);
test(a);
}
},
EXPECT_IS_RIGHT<tuple<int, int>, tuple<int, int, int>>(tuple<int, int, int>(2, 3, 4))
EXPECT_IS_RIGHT<tuple<int, int>, tuple<int, string, int>>(tuple<int, string, int>(2, "3", 4))
);
}