optional_ownership_ptr works with unique_ref
This commit is contained in:
parent
e031aa5ef6
commit
e9c461e328
@ -2,7 +2,7 @@
|
|||||||
#ifndef MESSMER_CPP_UTILS_POINTER_OPTIONALOWNERSHIPPOINTER_H_
|
#ifndef MESSMER_CPP_UTILS_POINTER_OPTIONALOWNERSHIPPOINTER_H_
|
||||||
#define MESSMER_CPP_UTILS_POINTER_OPTIONALOWNERSHIPPOINTER_H_
|
#define MESSMER_CPP_UTILS_POINTER_OPTIONALOWNERSHIPPOINTER_H_
|
||||||
|
|
||||||
#include <memory>
|
#include "unique_ref.h"
|
||||||
#include <functional>
|
#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);});
|
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>
|
template<typename T>
|
||||||
optional_ownership_ptr<T> WithoutOwnership(T *obj) {
|
optional_ownership_ptr<T> WithoutOwnership(T *obj) {
|
||||||
return optional_ownership_ptr<T>(obj, [](T*){});
|
return optional_ownership_ptr<T>(obj, [](T*){});
|
||||||
|
@ -7,6 +7,8 @@
|
|||||||
* Without including this file, the linker will fail.
|
* 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 "unique_ref.h"
|
||||||
#include <boost/optional/optional_io.hpp>
|
#include <boost/optional/optional_io.hpp>
|
||||||
//gtest/boost::optional workaround for working with optional<unique_ref<T>>
|
//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
|
||||||
|
@ -54,14 +54,25 @@ TEST_F(OptionalOwnershipPointerTest, TestIsInitializedCorrectly) {
|
|||||||
EXPECT_FALSE(obj.isDestructed());
|
EXPECT_FALSE(obj.isDestructed());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(OptionalOwnershipPointerTest, DestructsWhenItHasOwnership) {
|
TEST_F(OptionalOwnershipPointerTest, DestructsWhenItHasOwnership_UniquePtr) {
|
||||||
{
|
{
|
||||||
optional_ownership_ptr<TestObject> ptr = WithOwnership(unique_ptr<TestObject>(obj.get()));
|
optional_ownership_ptr<TestObject> ptr = WithOwnership(unique_ptr<TestObject>(obj.get()));
|
||||||
|
EXPECT_FALSE(obj.isDestructed());
|
||||||
UNUSED(ptr);
|
UNUSED(ptr);
|
||||||
}
|
}
|
||||||
EXPECT_TRUE(obj.isDestructed());
|
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) {
|
TEST_F(OptionalOwnershipPointerTest, DestructsWhenItHasOwnershipAfterAssignment) {
|
||||||
{
|
{
|
||||||
optional_ownership_ptr<TestObject> ptr = WithoutOwnership(obj.get());
|
optional_ownership_ptr<TestObject> ptr = WithoutOwnership(obj.get());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user