From e9c461e32806e419bcf8dae26529b433175c0614 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Tue, 21 Jul 2015 15:18:14 +0200 Subject: [PATCH] optional_ownership_ptr works with unique_ref --- pointer/optional_ownership_ptr.h | 7 ++++++- .../unique_ref_boost_optional_gtest_workaround.h | 4 +++- test/pointer/optional_ownership_ptr_test.cpp | 13 ++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/pointer/optional_ownership_ptr.h b/pointer/optional_ownership_ptr.h index b67b4134..3f321661 100644 --- a/pointer/optional_ownership_ptr.h +++ b/pointer/optional_ownership_ptr.h @@ -2,7 +2,7 @@ #ifndef MESSMER_CPP_UTILS_POINTER_OPTIONALOWNERSHIPPOINTER_H_ #define MESSMER_CPP_UTILS_POINTER_OPTIONALOWNERSHIPPOINTER_H_ -#include +#include "unique_ref.h" #include /** @@ -29,6 +29,11 @@ optional_ownership_ptr WithOwnership(std::unique_ptr obj) { return optional_ownership_ptr(obj.release(), [deleter](T* obj){deleter(obj);}); } +template +optional_ownership_ptr WithOwnership(unique_ref obj) { + return WithOwnership(to_unique_ptr(std::move(obj))); +} + template optional_ownership_ptr WithoutOwnership(T *obj) { return optional_ownership_ptr(obj, [](T*){}); diff --git a/pointer/unique_ref_boost_optional_gtest_workaround.h b/pointer/unique_ref_boost_optional_gtest_workaround.h index 80723aa0..d92376cf 100644 --- a/pointer/unique_ref_boost_optional_gtest_workaround.h +++ b/pointer/unique_ref_boost_optional_gtest_workaround.h @@ -7,6 +7,8 @@ * Without including this file, the linker will fail. */ +//TODO Test that this solves the problem (add test unit file that doesn't compile without) + #include "unique_ref.h" #include //gtest/boost::optional workaround for working with optional> @@ -18,4 +20,4 @@ namespace boost { } } -#endif //CRYFS_UNIQUE_REF_BOOST_OPTIONAL_GTEST_WORKAROUND_H +#endif diff --git a/test/pointer/optional_ownership_ptr_test.cpp b/test/pointer/optional_ownership_ptr_test.cpp index 3e87121b..43889186 100644 --- a/test/pointer/optional_ownership_ptr_test.cpp +++ b/test/pointer/optional_ownership_ptr_test.cpp @@ -54,14 +54,25 @@ TEST_F(OptionalOwnershipPointerTest, TestIsInitializedCorrectly) { EXPECT_FALSE(obj.isDestructed()); } -TEST_F(OptionalOwnershipPointerTest, DestructsWhenItHasOwnership) { +TEST_F(OptionalOwnershipPointerTest, DestructsWhenItHasOwnership_UniquePtr) { { optional_ownership_ptr ptr = WithOwnership(unique_ptr(obj.get())); + EXPECT_FALSE(obj.isDestructed()); UNUSED(ptr); } EXPECT_TRUE(obj.isDestructed()); } +TEST_F(OptionalOwnershipPointerTest, DestructsWhenItHasOwnership_UniqueRef) { + { + optional_ownership_ptr ptr = WithOwnership(cpputils::nullcheck(unique_ptr(obj.get())).value()); + EXPECT_FALSE(obj.isDestructed()); + UNUSED(ptr); + } + EXPECT_TRUE(obj.isDestructed()); +} + + TEST_F(OptionalOwnershipPointerTest, DestructsWhenItHasOwnershipAfterAssignment) { { optional_ownership_ptr ptr = WithoutOwnership(obj.get());