optional_ownership_ptr works with unique_ref

This commit is contained in:
Sebastian Messmer 2015-07-21 15:18:14 +02:00
parent e031aa5ef6
commit e9c461e328
3 changed files with 21 additions and 3 deletions

View File

@ -2,7 +2,7 @@
#ifndef MESSMER_CPP_UTILS_POINTER_OPTIONALOWNERSHIPPOINTER_H_
#define MESSMER_CPP_UTILS_POINTER_OPTIONALOWNERSHIPPOINTER_H_
#include <memory>
#include "unique_ref.h"
#include <functional>
/**
@ -29,6 +29,11 @@ optional_ownership_ptr<T> WithOwnership(std::unique_ptr<T> obj) {
return optional_ownership_ptr<T>(obj.release(), [deleter](T* obj){deleter(obj);});
}
template <typename T>
optional_ownership_ptr<T> WithOwnership(unique_ref<T> obj) {
return WithOwnership(to_unique_ptr(std::move(obj)));
}
template<typename T>
optional_ownership_ptr<T> WithoutOwnership(T *obj) {
return optional_ownership_ptr<T>(obj, [](T*){});

View File

@ -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 <boost/optional/optional_io.hpp>
//gtest/boost::optional workaround for working with optional<unique_ref<T>>
@ -18,4 +20,4 @@ namespace boost {
}
}
#endif //CRYFS_UNIQUE_REF_BOOST_OPTIONAL_GTEST_WORKAROUND_H
#endif

View File

@ -54,14 +54,25 @@ TEST_F(OptionalOwnershipPointerTest, TestIsInitializedCorrectly) {
EXPECT_FALSE(obj.isDestructed());
}
TEST_F(OptionalOwnershipPointerTest, DestructsWhenItHasOwnership) {
TEST_F(OptionalOwnershipPointerTest, DestructsWhenItHasOwnership_UniquePtr) {
{
optional_ownership_ptr<TestObject> ptr = WithOwnership(unique_ptr<TestObject>(obj.get()));
EXPECT_FALSE(obj.isDestructed());
UNUSED(ptr);
}
EXPECT_TRUE(obj.isDestructed());
}
TEST_F(OptionalOwnershipPointerTest, DestructsWhenItHasOwnership_UniqueRef) {
{
optional_ownership_ptr<TestObject> ptr = WithOwnership(cpputils::nullcheck(unique_ptr<TestObject>(obj.get())).value());
EXPECT_FALSE(obj.isDestructed());
UNUSED(ptr);
}
EXPECT_TRUE(obj.isDestructed());
}
TEST_F(OptionalOwnershipPointerTest, DestructsWhenItHasOwnershipAfterAssignment) {
{
optional_ownership_ptr<TestObject> ptr = WithoutOwnership(obj.get());