Converting from methods to factory functions (no functional change).

parent cdb30dde
...@@ -75,16 +75,6 @@ struct smart_holder { ...@@ -75,16 +75,6 @@ struct smart_holder {
bool vptr_is_using_builtin_delete : 1; bool vptr_is_using_builtin_delete : 1;
bool vptr_is_external_shared_ptr : 1; bool vptr_is_external_shared_ptr : 1;
void clear() {
rtti_held = nullptr;
rtti_uqp_del = nullptr;
vptr.reset();
vptr_deleter_guard_flag = false;
vptr_is_using_noop_deleter = false;
vptr_is_using_builtin_delete = false;
vptr_is_external_shared_ptr = false;
}
smart_holder() smart_holder()
: rtti_held{nullptr}, : rtti_held{nullptr},
rtti_uqp_del{nullptr}, rtti_uqp_del{nullptr},
...@@ -151,11 +141,13 @@ struct smart_holder { ...@@ -151,11 +141,13 @@ struct smart_holder {
} }
template <typename T> template <typename T>
void from_raw_ptr_unowned(T* raw_ptr) { static smart_holder from_raw_ptr_unowned(T* raw_ptr) {
clear(); smart_holder hld;
rtti_held = &typeid(T); hld.rtti_held = &typeid(T);
vptr_is_using_noop_deleter = true; hld.vptr_is_using_noop_deleter = true;
vptr.reset(raw_ptr, guarded_builtin_delete<T>(&vptr_deleter_guard_flag)); hld.vptr.reset(raw_ptr,
guarded_builtin_delete<T>(&hld.vptr_deleter_guard_flag));
return hld;
} }
template <typename T> template <typename T>
...@@ -174,12 +166,14 @@ struct smart_holder { ...@@ -174,12 +166,14 @@ struct smart_holder {
} }
template <typename T> template <typename T>
void from_raw_ptr_take_ownership(T* raw_ptr) { static smart_holder from_raw_ptr_take_ownership(T* raw_ptr) {
clear(); smart_holder hld;
rtti_held = &typeid(T); hld.rtti_held = &typeid(T);
vptr_deleter_guard_flag = true; hld.vptr_deleter_guard_flag = true;
vptr_is_using_builtin_delete = true; hld.vptr_is_using_builtin_delete = true;
vptr.reset(raw_ptr, guarded_builtin_delete<T>(&vptr_deleter_guard_flag)); hld.vptr.reset(raw_ptr,
guarded_builtin_delete<T>(&hld.vptr_deleter_guard_flag));
return hld;
} }
template <typename T> template <typename T>
...@@ -195,14 +189,15 @@ struct smart_holder { ...@@ -195,14 +189,15 @@ struct smart_holder {
} }
template <typename T> template <typename T>
void from_unique_ptr(std::unique_ptr<T>&& unq_ptr) { static smart_holder from_unique_ptr(std::unique_ptr<T>&& unq_ptr) {
clear(); smart_holder hld;
rtti_held = &typeid(T); hld.rtti_held = &typeid(T);
vptr_deleter_guard_flag = true; hld.vptr_deleter_guard_flag = true;
vptr_is_using_builtin_delete = true; hld.vptr_is_using_builtin_delete = true;
vptr.reset(unq_ptr.get(), hld.vptr.reset(unq_ptr.get(),
guarded_builtin_delete<T>(&vptr_deleter_guard_flag)); guarded_builtin_delete<T>(&hld.vptr_deleter_guard_flag));
unq_ptr.release(); unq_ptr.release();
return hld;
} }
template <typename T> template <typename T>
...@@ -211,14 +206,16 @@ struct smart_holder { ...@@ -211,14 +206,16 @@ struct smart_holder {
} }
template <typename T, typename D> template <typename T, typename D>
void from_unique_ptr_with_deleter(std::unique_ptr<T, D>&& unq_ptr) { static smart_holder from_unique_ptr_with_deleter(
clear(); std::unique_ptr<T, D>&& unq_ptr) {
rtti_held = &typeid(T); smart_holder hld;
rtti_uqp_del = &typeid(D); hld.rtti_held = &typeid(T);
vptr_deleter_guard_flag = true; hld.rtti_uqp_del = &typeid(D);
vptr.reset(unq_ptr.get(), hld.vptr_deleter_guard_flag = true;
guarded_custom_deleter<T, D>(&vptr_deleter_guard_flag)); hld.vptr.reset(unq_ptr.get(),
guarded_custom_deleter<T, D>(&hld.vptr_deleter_guard_flag));
unq_ptr.release(); unq_ptr.release();
return hld;
} }
template <typename T, typename D> template <typename T, typename D>
...@@ -234,11 +231,12 @@ struct smart_holder { ...@@ -234,11 +231,12 @@ struct smart_holder {
} }
template <typename T> template <typename T>
void from_shared_ptr(std::shared_ptr<T> shd_ptr) { static smart_holder from_shared_ptr(std::shared_ptr<T> shd_ptr) {
clear(); smart_holder hld;
rtti_held = &typeid(T); hld.rtti_held = &typeid(T);
vptr_is_external_shared_ptr = true; hld.vptr_is_external_shared_ptr = true;
vptr = std::static_pointer_cast<void>(shd_ptr); hld.vptr = std::static_pointer_cast<void>(shd_ptr);
return hld;
} }
template <typename T> template <typename T>
......
...@@ -19,22 +19,19 @@ struct functor_other_delete : functor_builtin_delete<T> {}; ...@@ -19,22 +19,19 @@ struct functor_other_delete : functor_builtin_delete<T> {};
TEST_CASE("from_raw_ptr_unowned+as_raw_ptr_unowned", "[S]") { TEST_CASE("from_raw_ptr_unowned+as_raw_ptr_unowned", "[S]") {
static int value = 19; static int value = 19;
smart_holder hld; auto hld = smart_holder::from_raw_ptr_unowned(&value);
hld.from_raw_ptr_unowned(&value);
REQUIRE(*hld.as_raw_ptr_unowned<int>() == 19); REQUIRE(*hld.as_raw_ptr_unowned<int>() == 19);
} }
TEST_CASE("from_raw_ptr_unowned+const_value_ref", "[S]") { TEST_CASE("from_raw_ptr_unowned+const_value_ref", "[S]") {
static int value = 19; static int value = 19;
smart_holder hld; auto hld = smart_holder::from_raw_ptr_unowned(&value);
hld.from_raw_ptr_unowned(&value);
REQUIRE(hld.const_value_ref<int>() == 19); REQUIRE(hld.const_value_ref<int>() == 19);
} }
TEST_CASE("from_raw_ptr_unowned+as_raw_ptr_release_ownership", "[E]") { TEST_CASE("from_raw_ptr_unowned+as_raw_ptr_release_ownership", "[E]") {
static int value = 19; static int value = 19;
smart_holder hld; auto hld = smart_holder::from_raw_ptr_unowned(&value);
hld.from_raw_ptr_unowned(&value);
REQUIRE_THROWS_WITH( REQUIRE_THROWS_WITH(
hld.as_raw_ptr_release_ownership<int>(), hld.as_raw_ptr_release_ownership<int>(),
"Cannot disown non-owning holder (as_raw_ptr_release_ownership)."); "Cannot disown non-owning holder (as_raw_ptr_release_ownership).");
...@@ -42,16 +39,14 @@ TEST_CASE("from_raw_ptr_unowned+as_raw_ptr_release_ownership", "[E]") { ...@@ -42,16 +39,14 @@ TEST_CASE("from_raw_ptr_unowned+as_raw_ptr_release_ownership", "[E]") {
TEST_CASE("from_raw_ptr_unowned+as_unique_ptr", "[E]") { TEST_CASE("from_raw_ptr_unowned+as_unique_ptr", "[E]") {
static int value = 19; static int value = 19;
smart_holder hld; auto hld = smart_holder::from_raw_ptr_unowned(&value);
hld.from_raw_ptr_unowned(&value);
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(), REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(),
"Cannot disown non-owning holder (as_unique_ptr)."); "Cannot disown non-owning holder (as_unique_ptr).");
} }
TEST_CASE("from_raw_ptr_unowned+as_unique_ptr_with_deleter", "[E]") { TEST_CASE("from_raw_ptr_unowned+as_unique_ptr_with_deleter", "[E]") {
static int value = 19; static int value = 19;
smart_holder hld; auto hld = smart_holder::from_raw_ptr_unowned(&value);
hld.from_raw_ptr_unowned(&value);
auto condense_for_macro = [](smart_holder& hld) { auto condense_for_macro = [](smart_holder& hld) {
hld.as_unique_ptr_with_deleter<int, helpers::functor_builtin_delete<int>>(); hld.as_unique_ptr_with_deleter<int, helpers::functor_builtin_delete<int>>();
}; };
...@@ -62,21 +57,18 @@ TEST_CASE("from_raw_ptr_unowned+as_unique_ptr_with_deleter", "[E]") { ...@@ -62,21 +57,18 @@ TEST_CASE("from_raw_ptr_unowned+as_unique_ptr_with_deleter", "[E]") {
TEST_CASE("from_raw_ptr_unowned+as_shared_ptr", "[S]") { TEST_CASE("from_raw_ptr_unowned+as_shared_ptr", "[S]") {
static int value = 19; static int value = 19;
smart_holder hld; auto hld = smart_holder::from_raw_ptr_unowned(&value);
hld.from_raw_ptr_unowned(&value);
REQUIRE(*hld.as_shared_ptr<int>() == 19); REQUIRE(*hld.as_shared_ptr<int>() == 19);
} }
TEST_CASE("from_raw_ptr_take_ownership+const_value_ref", "[S]") { TEST_CASE("from_raw_ptr_take_ownership+const_value_ref", "[S]") {
smart_holder hld; auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
hld.from_raw_ptr_take_ownership(new int(19));
REQUIRE(hld.has_pointee()); REQUIRE(hld.has_pointee());
REQUIRE(hld.const_value_ref<int>() == 19); REQUIRE(hld.const_value_ref<int>() == 19);
} }
TEST_CASE("from_raw_ptr_take_ownership+as_raw_ptr_release_ownership1", "[S]") { TEST_CASE("from_raw_ptr_take_ownership+as_raw_ptr_release_ownership1", "[S]") {
smart_holder hld; auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
hld.from_raw_ptr_take_ownership(new int(19));
auto new_owner = auto new_owner =
std::unique_ptr<int>(hld.as_raw_ptr_release_ownership<int>()); std::unique_ptr<int>(hld.as_raw_ptr_release_ownership<int>());
REQUIRE(!hld.has_pointee()); REQUIRE(!hld.has_pointee());
...@@ -84,8 +76,7 @@ TEST_CASE("from_raw_ptr_take_ownership+as_raw_ptr_release_ownership1", "[S]") { ...@@ -84,8 +76,7 @@ TEST_CASE("from_raw_ptr_take_ownership+as_raw_ptr_release_ownership1", "[S]") {
} }
TEST_CASE("from_raw_ptr_take_ownership+as_raw_ptr_release_ownership2", "[E]") { TEST_CASE("from_raw_ptr_take_ownership+as_raw_ptr_release_ownership2", "[E]") {
smart_holder hld; auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
hld.from_raw_ptr_take_ownership(new int(19));
auto shd_ptr = hld.as_shared_ptr<int>(); auto shd_ptr = hld.as_shared_ptr<int>();
REQUIRE_THROWS_WITH( REQUIRE_THROWS_WITH(
hld.as_raw_ptr_release_ownership<int>(), hld.as_raw_ptr_release_ownership<int>(),
...@@ -93,24 +84,21 @@ TEST_CASE("from_raw_ptr_take_ownership+as_raw_ptr_release_ownership2", "[E]") { ...@@ -93,24 +84,21 @@ TEST_CASE("from_raw_ptr_take_ownership+as_raw_ptr_release_ownership2", "[E]") {
} }
TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr1", "[S]") { TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr1", "[S]") {
smart_holder hld; auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
hld.from_raw_ptr_take_ownership(new int(19));
std::unique_ptr<int> new_owner = hld.as_unique_ptr<int>(); std::unique_ptr<int> new_owner = hld.as_unique_ptr<int>();
REQUIRE(!hld.has_pointee()); REQUIRE(!hld.has_pointee());
REQUIRE(*new_owner == 19); REQUIRE(*new_owner == 19);
} }
TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr2", "[E]") { TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr2", "[E]") {
smart_holder hld; auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
hld.from_raw_ptr_take_ownership(new int(19));
auto shd_ptr = hld.as_shared_ptr<int>(); auto shd_ptr = hld.as_shared_ptr<int>();
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(), REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(),
"Cannot disown use_count != 1 (as_unique_ptr)."); "Cannot disown use_count != 1 (as_unique_ptr).");
} }
TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr_with_deleter", "[E]") { TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr_with_deleter", "[E]") {
smart_holder hld; auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
hld.from_raw_ptr_take_ownership(new int(19));
auto condense_for_macro = [](smart_holder& hld) { auto condense_for_macro = [](smart_holder& hld) {
hld.as_unique_ptr_with_deleter<int, helpers::functor_builtin_delete<int>>(); hld.as_unique_ptr_with_deleter<int, helpers::functor_builtin_delete<int>>();
}; };
...@@ -120,8 +108,7 @@ TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr_with_deleter", "[E]") { ...@@ -120,8 +108,7 @@ TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr_with_deleter", "[E]") {
} }
TEST_CASE("from_raw_ptr_take_ownership+as_shared_ptr", "[S]") { TEST_CASE("from_raw_ptr_take_ownership+as_shared_ptr", "[S]") {
smart_holder hld; auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
hld.from_raw_ptr_take_ownership(new int(19));
std::shared_ptr<int> new_owner = hld.as_shared_ptr<int>(); std::shared_ptr<int> new_owner = hld.as_shared_ptr<int>();
REQUIRE(hld.has_pointee()); REQUIRE(hld.has_pointee());
REQUIRE(*new_owner == 19); REQUIRE(*new_owner == 19);
...@@ -129,16 +116,14 @@ TEST_CASE("from_raw_ptr_take_ownership+as_shared_ptr", "[S]") { ...@@ -129,16 +116,14 @@ TEST_CASE("from_raw_ptr_take_ownership+as_shared_ptr", "[S]") {
TEST_CASE("from_unique_ptr+const_value_ref", "[S]") { TEST_CASE("from_unique_ptr+const_value_ref", "[S]") {
std::unique_ptr<int> orig_owner(new int(19)); std::unique_ptr<int> orig_owner(new int(19));
smart_holder hld; auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
hld.from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
REQUIRE(hld.const_value_ref<int>() == 19); REQUIRE(hld.const_value_ref<int>() == 19);
} }
TEST_CASE("from_unique_ptr+as_raw_ptr_release_ownership1", "[S]") { TEST_CASE("from_unique_ptr+as_raw_ptr_release_ownership1", "[S]") {
std::unique_ptr<int> orig_owner(new int(19)); std::unique_ptr<int> orig_owner(new int(19));
smart_holder hld; auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
hld.from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
auto new_owner = auto new_owner =
std::unique_ptr<int>(hld.as_raw_ptr_release_ownership<int>()); std::unique_ptr<int>(hld.as_raw_ptr_release_ownership<int>());
...@@ -148,8 +133,7 @@ TEST_CASE("from_unique_ptr+as_raw_ptr_release_ownership1", "[S]") { ...@@ -148,8 +133,7 @@ TEST_CASE("from_unique_ptr+as_raw_ptr_release_ownership1", "[S]") {
TEST_CASE("from_unique_ptr+as_raw_ptr_release_ownership2", "[E]") { TEST_CASE("from_unique_ptr+as_raw_ptr_release_ownership2", "[E]") {
std::unique_ptr<int> orig_owner(new int(19)); std::unique_ptr<int> orig_owner(new int(19));
smart_holder hld; auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
hld.from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
auto shd_ptr = hld.as_shared_ptr<int>(); auto shd_ptr = hld.as_shared_ptr<int>();
REQUIRE_THROWS_WITH( REQUIRE_THROWS_WITH(
...@@ -159,8 +143,7 @@ TEST_CASE("from_unique_ptr+as_raw_ptr_release_ownership2", "[E]") { ...@@ -159,8 +143,7 @@ TEST_CASE("from_unique_ptr+as_raw_ptr_release_ownership2", "[E]") {
TEST_CASE("from_unique_ptr+as_unique_ptr1", "[S]") { TEST_CASE("from_unique_ptr+as_unique_ptr1", "[S]") {
std::unique_ptr<int> orig_owner(new int(19)); std::unique_ptr<int> orig_owner(new int(19));
smart_holder hld; auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
hld.from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
std::unique_ptr<int> new_owner = hld.as_unique_ptr<int>(); std::unique_ptr<int> new_owner = hld.as_unique_ptr<int>();
REQUIRE(!hld.has_pointee()); REQUIRE(!hld.has_pointee());
...@@ -169,8 +152,7 @@ TEST_CASE("from_unique_ptr+as_unique_ptr1", "[S]") { ...@@ -169,8 +152,7 @@ TEST_CASE("from_unique_ptr+as_unique_ptr1", "[S]") {
TEST_CASE("from_unique_ptr+as_unique_ptr2", "[E]") { TEST_CASE("from_unique_ptr+as_unique_ptr2", "[E]") {
std::unique_ptr<int> orig_owner(new int(19)); std::unique_ptr<int> orig_owner(new int(19));
smart_holder hld; auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
hld.from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
auto shd_ptr = hld.as_shared_ptr<int>(); auto shd_ptr = hld.as_shared_ptr<int>();
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(), REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(),
...@@ -179,8 +161,7 @@ TEST_CASE("from_unique_ptr+as_unique_ptr2", "[E]") { ...@@ -179,8 +161,7 @@ TEST_CASE("from_unique_ptr+as_unique_ptr2", "[E]") {
TEST_CASE("from_unique_ptr+as_unique_ptr_with_deleter", "[E]") { TEST_CASE("from_unique_ptr+as_unique_ptr_with_deleter", "[E]") {
std::unique_ptr<int> orig_owner(new int(19)); std::unique_ptr<int> orig_owner(new int(19));
smart_holder hld; auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
hld.from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
auto condense_for_macro = [](smart_holder& hld) { auto condense_for_macro = [](smart_holder& hld) {
hld.as_unique_ptr_with_deleter<int, helpers::functor_builtin_delete<int>>(); hld.as_unique_ptr_with_deleter<int, helpers::functor_builtin_delete<int>>();
...@@ -192,8 +173,7 @@ TEST_CASE("from_unique_ptr+as_unique_ptr_with_deleter", "[E]") { ...@@ -192,8 +173,7 @@ TEST_CASE("from_unique_ptr+as_unique_ptr_with_deleter", "[E]") {
TEST_CASE("from_unique_ptr+as_shared_ptr", "[S]") { TEST_CASE("from_unique_ptr+as_shared_ptr", "[S]") {
std::unique_ptr<int> orig_owner(new int(19)); std::unique_ptr<int> orig_owner(new int(19));
smart_holder hld; auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
hld.from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
std::shared_ptr<int> new_owner = hld.as_shared_ptr<int>(); std::shared_ptr<int> new_owner = hld.as_shared_ptr<int>();
REQUIRE(hld.has_pointee()); REQUIRE(hld.has_pointee());
...@@ -203,8 +183,7 @@ TEST_CASE("from_unique_ptr+as_shared_ptr", "[S]") { ...@@ -203,8 +183,7 @@ TEST_CASE("from_unique_ptr+as_shared_ptr", "[S]") {
TEST_CASE("from_unique_ptr_with_deleter+const_value_ref", "[S]") { TEST_CASE("from_unique_ptr_with_deleter+const_value_ref", "[S]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner( std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(
new int(19)); new int(19));
smart_holder hld; auto hld = smart_holder::from_unique_ptr_with_deleter(std::move(orig_owner));
hld.from_unique_ptr_with_deleter(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
REQUIRE(hld.const_value_ref<int>() == 19); REQUIRE(hld.const_value_ref<int>() == 19);
} }
...@@ -212,8 +191,7 @@ TEST_CASE("from_unique_ptr_with_deleter+const_value_ref", "[S]") { ...@@ -212,8 +191,7 @@ TEST_CASE("from_unique_ptr_with_deleter+const_value_ref", "[S]") {
TEST_CASE("from_unique_ptr_with_deleter+as_raw_ptr_release_ownership", "[E]") { TEST_CASE("from_unique_ptr_with_deleter+as_raw_ptr_release_ownership", "[E]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner( std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(
new int(19)); new int(19));
smart_holder hld; auto hld = smart_holder::from_unique_ptr_with_deleter(std::move(orig_owner));
hld.from_unique_ptr_with_deleter(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
REQUIRE_THROWS_WITH( REQUIRE_THROWS_WITH(
hld.as_raw_ptr_release_ownership<int>(), hld.as_raw_ptr_release_ownership<int>(),
...@@ -223,8 +201,7 @@ TEST_CASE("from_unique_ptr_with_deleter+as_raw_ptr_release_ownership", "[E]") { ...@@ -223,8 +201,7 @@ TEST_CASE("from_unique_ptr_with_deleter+as_raw_ptr_release_ownership", "[E]") {
TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr", "[E]") { TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr", "[E]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner( std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(
new int(19)); new int(19));
smart_holder hld; auto hld = smart_holder::from_unique_ptr_with_deleter(std::move(orig_owner));
hld.from_unique_ptr_with_deleter(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(), REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(),
"Cannot disown custom deleter (as_unique_ptr)."); "Cannot disown custom deleter (as_unique_ptr).");
...@@ -233,8 +210,7 @@ TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr", "[E]") { ...@@ -233,8 +210,7 @@ TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr", "[E]") {
TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr_with_deleter1", "[S]") { TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr_with_deleter1", "[S]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner( std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(
new int(19)); new int(19));
smart_holder hld; auto hld = smart_holder::from_unique_ptr_with_deleter(std::move(orig_owner));
hld.from_unique_ptr_with_deleter(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
std::unique_ptr<int, helpers::functor_builtin_delete<int>> new_owner = std::unique_ptr<int, helpers::functor_builtin_delete<int>> new_owner =
hld.as_unique_ptr_with_deleter<int, hld.as_unique_ptr_with_deleter<int,
...@@ -246,8 +222,7 @@ TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr_with_deleter1", "[S]") { ...@@ -246,8 +222,7 @@ TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr_with_deleter1", "[S]") {
TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr_with_deleter2", "[E]") { TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr_with_deleter2", "[E]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner( std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(
new int(19)); new int(19));
smart_holder hld; auto hld = smart_holder::from_unique_ptr_with_deleter(std::move(orig_owner));
hld.from_unique_ptr_with_deleter(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
auto condense_for_macro = [](smart_holder& hld) { auto condense_for_macro = [](smart_holder& hld) {
hld.as_unique_ptr_with_deleter<int, helpers::functor_other_delete<int>>(); hld.as_unique_ptr_with_deleter<int, helpers::functor_other_delete<int>>();
...@@ -260,8 +235,7 @@ TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr_with_deleter2", "[E]") { ...@@ -260,8 +235,7 @@ TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr_with_deleter2", "[E]") {
TEST_CASE("from_unique_ptr_with_deleter+as_shared_ptr", "[S]") { TEST_CASE("from_unique_ptr_with_deleter+as_shared_ptr", "[S]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner( std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(
new int(19)); new int(19));
smart_holder hld; auto hld = smart_holder::from_unique_ptr_with_deleter(std::move(orig_owner));
hld.from_unique_ptr_with_deleter(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
std::shared_ptr<int> new_owner = hld.as_shared_ptr<int>(); std::shared_ptr<int> new_owner = hld.as_shared_ptr<int>();
REQUIRE(hld.has_pointee()); REQUIRE(hld.has_pointee());
...@@ -270,15 +244,13 @@ TEST_CASE("from_unique_ptr_with_deleter+as_shared_ptr", "[S]") { ...@@ -270,15 +244,13 @@ TEST_CASE("from_unique_ptr_with_deleter+as_shared_ptr", "[S]") {
TEST_CASE("from_shared_ptr+const_value_ref", "[S]") { TEST_CASE("from_shared_ptr+const_value_ref", "[S]") {
std::shared_ptr<int> orig_owner(new int(19)); std::shared_ptr<int> orig_owner(new int(19));
smart_holder hld; auto hld = smart_holder::from_shared_ptr(orig_owner);
hld.from_shared_ptr(orig_owner);
REQUIRE(hld.const_value_ref<int>() == 19); REQUIRE(hld.const_value_ref<int>() == 19);
} }
TEST_CASE("from_shared_ptr+as_raw_ptr_release_ownership", "[E]") { TEST_CASE("from_shared_ptr+as_raw_ptr_release_ownership", "[E]") {
std::shared_ptr<int> orig_owner(new int(19)); std::shared_ptr<int> orig_owner(new int(19));
smart_holder hld; auto hld = smart_holder::from_shared_ptr(orig_owner);
hld.from_shared_ptr(orig_owner);
REQUIRE_THROWS_WITH( REQUIRE_THROWS_WITH(
hld.as_raw_ptr_release_ownership<int>(), hld.as_raw_ptr_release_ownership<int>(),
"Cannot disown external shared_ptr (as_raw_ptr_release_ownership)."); "Cannot disown external shared_ptr (as_raw_ptr_release_ownership).");
...@@ -286,16 +258,14 @@ TEST_CASE("from_shared_ptr+as_raw_ptr_release_ownership", "[E]") { ...@@ -286,16 +258,14 @@ TEST_CASE("from_shared_ptr+as_raw_ptr_release_ownership", "[E]") {
TEST_CASE("from_shared_ptr+as_unique_ptr", "[E]") { TEST_CASE("from_shared_ptr+as_unique_ptr", "[E]") {
std::shared_ptr<int> orig_owner(new int(19)); std::shared_ptr<int> orig_owner(new int(19));
smart_holder hld; auto hld = smart_holder::from_shared_ptr(orig_owner);
hld.from_shared_ptr(orig_owner);
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(), REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(),
"Cannot disown external shared_ptr (as_unique_ptr)."); "Cannot disown external shared_ptr (as_unique_ptr).");
} }
TEST_CASE("from_shared_ptr+as_unique_ptr_with_deleter", "[E]") { TEST_CASE("from_shared_ptr+as_unique_ptr_with_deleter", "[E]") {
std::shared_ptr<int> orig_owner(new int(19)); std::shared_ptr<int> orig_owner(new int(19));
smart_holder hld; auto hld = smart_holder::from_shared_ptr(orig_owner);
hld.from_shared_ptr(orig_owner);
auto condense_for_macro = [](smart_holder& hld) { auto condense_for_macro = [](smart_holder& hld) {
hld.as_unique_ptr_with_deleter<int, helpers::functor_builtin_delete<int>>(); hld.as_unique_ptr_with_deleter<int, helpers::functor_builtin_delete<int>>();
}; };
...@@ -306,8 +276,7 @@ TEST_CASE("from_shared_ptr+as_unique_ptr_with_deleter", "[E]") { ...@@ -306,8 +276,7 @@ TEST_CASE("from_shared_ptr+as_unique_ptr_with_deleter", "[E]") {
TEST_CASE("from_shared_ptr+as_shared_ptr", "[S]") { TEST_CASE("from_shared_ptr+as_shared_ptr", "[S]") {
std::shared_ptr<int> orig_owner(new int(19)); std::shared_ptr<int> orig_owner(new int(19));
smart_holder hld; auto hld = smart_holder::from_shared_ptr(orig_owner);
hld.from_shared_ptr(orig_owner);
REQUIRE(*hld.as_shared_ptr<int>() == 19); REQUIRE(*hld.as_shared_ptr<int>() == 19);
} }
...@@ -319,15 +288,13 @@ TEST_CASE("error_unpopulated_holder", "[E]") { ...@@ -319,15 +288,13 @@ TEST_CASE("error_unpopulated_holder", "[E]") {
TEST_CASE("error_incompatible_type", "[E]") { TEST_CASE("error_incompatible_type", "[E]") {
static int value = 19; static int value = 19;
smart_holder hld; auto hld = smart_holder::from_raw_ptr_unowned(&value);
hld.from_raw_ptr_unowned(&value);
REQUIRE_THROWS_WITH(hld.as_unique_ptr<std::string>(), REQUIRE_THROWS_WITH(hld.as_unique_ptr<std::string>(),
"Incompatible type (as_unique_ptr)."); "Incompatible type (as_unique_ptr).");
} }
TEST_CASE("error_disowned_holder", "[E]") { TEST_CASE("error_disowned_holder", "[E]") {
smart_holder hld; auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
hld.from_raw_ptr_take_ownership(new int(19));
hld.as_unique_ptr<int>(); hld.as_unique_ptr<int>();
REQUIRE_THROWS_WITH(hld.const_value_ref<int>(), REQUIRE_THROWS_WITH(hld.const_value_ref<int>(),
"Disowned holder (const_value_ref)."); "Disowned holder (const_value_ref).");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment