Further improve unique_ref

This commit is contained in:
Sebastian Messmer 2017-04-03 15:43:27 +01:00
parent 9ada10db4d
commit 2fb61e7c71
2 changed files with 33 additions and 5 deletions

View File

@ -70,11 +70,13 @@ public:
return _target.get(); return _target.get();
} }
operator std::unique_ptr<element_type>() && noexcept { template<class T2>
operator std::unique_ptr<T2>() && noexcept {
return std::move(_target); return std::move(_target);
} }
operator std::shared_ptr<element_type>() && noexcept { template<class T2>
operator std::shared_ptr<T2>() && noexcept {
return std::move(_target); return std::move(_target);
} }

View File

@ -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);
}