Further improve unique_ref
This commit is contained in:
parent
9ada10db4d
commit
2fb61e7c71
@ -45,7 +45,7 @@ public:
|
|||||||
from._target = nullptr;
|
from._target = nullptr;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class U> unique_ref& operator=(unique_ref<U>&& from) noexcept {
|
template<class U> unique_ref& operator=(unique_ref<U>&& from) noexcept {
|
||||||
_target = std::move(from._target);
|
_target = std::move(from._target);
|
||||||
from._target = nullptr;
|
from._target = nullptr;
|
||||||
@ -70,12 +70,14 @@ public:
|
|||||||
return _target.get();
|
return _target.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
operator std::unique_ptr<element_type>() && noexcept {
|
template<class T2>
|
||||||
return std::move(_target);
|
operator std::unique_ptr<T2>() && noexcept {
|
||||||
|
return std::move(_target);
|
||||||
}
|
}
|
||||||
|
|
||||||
operator std::shared_ptr<element_type>() && noexcept {
|
template<class T2>
|
||||||
return std::move(_target);
|
operator std::shared_ptr<T2>() && noexcept {
|
||||||
|
return std::move(_target);
|
||||||
}
|
}
|
||||||
|
|
||||||
void swap(unique_ref& rhs) noexcept {
|
void swap(unique_ref& rhs) noexcept {
|
||||||
|
@ -79,6 +79,16 @@ TEST(MakeUniqueRefTest, CanAssignToSharedPtr) {
|
|||||||
EXPECT_EQ(3, var->v);
|
EXPECT_EQ(3, var->v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(MakeUniqueRefTest, CanAssignToBaseClassUniquePtr) {
|
||||||
|
std::unique_ptr<SomeBaseClass> var = make_unique_ref<SomeChildClass>(3);
|
||||||
|
EXPECT_EQ(3, var->v);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(MakeUniqueRefTest, CanAssignToBaseClassSharedPtr) {
|
||||||
|
std::shared_ptr<SomeBaseClass> var = make_unique_ref<SomeChildClass>(3);
|
||||||
|
EXPECT_EQ(3, var->v);
|
||||||
|
}
|
||||||
|
|
||||||
TEST(NullcheckTest, PrimitiveNullptr) {
|
TEST(NullcheckTest, PrimitiveNullptr) {
|
||||||
boost::optional<unique_ref<int>> var = nullcheck(std::unique_ptr<int>(nullptr));
|
boost::optional<unique_ref<int>> var = nullcheck(std::unique_ptr<int>(nullptr));
|
||||||
EXPECT_FALSE((bool)var);
|
EXPECT_FALSE((bool)var);
|
||||||
@ -579,3 +589,19 @@ TEST_F(UniqueRefTest, givenUniqueRefToChildClass_whenMoveAssignedToBaseClass_the
|
|||||||
base = std::move(child);
|
base = std::move(child);
|
||||||
EXPECT_EQ(3, base->v);
|
EXPECT_EQ(3, base->v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(UniqueRefTest, givenUniqueRefToChildClass_whenCastedToBaseClassUniquePtr_thenWorksAsExpected) {
|
||||||
|
unique_ref<SomeChildClass> child = make_unique_ref<SomeChildClass>(3);
|
||||||
|
std::unique_ptr<SomeBaseClass> base = std::make_unique<SomeBaseClass>(10);
|
||||||
|
base = std::move(child);
|
||||||
|
EXPECT_FALSE(child.isValid());
|
||||||
|
EXPECT_EQ(3, base->v);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(UniqueRefTest, givenUniqueRefToChildClass_whenCastedToBaseClassSharedPtr_thenWorksAsExpected) {
|
||||||
|
unique_ref<SomeChildClass> child = make_unique_ref<SomeChildClass>(3);
|
||||||
|
std::shared_ptr<SomeBaseClass> base = std::make_unique<SomeBaseClass>(10);
|
||||||
|
base = std::move(child);
|
||||||
|
EXPECT_FALSE(child.isValid());
|
||||||
|
EXPECT_EQ(3, base->v);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user