Move smart_holder POC code to tests/pure_cpp directory. (#5315)

* Rename detail/smart_holder_poc.h -> struct_smart_holder.h

* Establish (empty) tests/pure_cpp/smart_holder_poc.h

* Move code guarded by `PYBIND11_TESTS_PURE_CPP_SMART_HOLDER_POC_TEST_CPP` from struct_smart_holder.h to tests/pure_cpp/smart_holder_poc.h
This commit is contained in:
Ralf W. Grosse-Kunstleve 2024-08-18 04:02:39 +07:00 committed by GitHub
parent 205da0d7c1
commit 01b6ccb7fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 97 additions and 98 deletions

View File

@ -133,7 +133,7 @@ set(PYBIND11_HEADERS
include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h
include/pybind11/detail/init.h include/pybind11/detail/init.h
include/pybind11/detail/internals.h include/pybind11/detail/internals.h
include/pybind11/detail/smart_holder_poc.h include/pybind11/detail/struct_smart_holder.h
include/pybind11/detail/type_caster_base.h include/pybind11/detail/type_caster_base.h
include/pybind11/detail/typeid.h include/pybind11/detail/typeid.h
include/pybind11/detail/using_smart_holder.h include/pybind11/detail/using_smart_holder.h

View File

@ -1,4 +1,4 @@
// Copyright (c) 2020-2021 The Pybind Development Team. // Copyright (c) 2020-2024 The Pybind Development Team.
// All rights reserved. Use of this source code is governed by a // All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file. // BSD-style license that can be found in the LICENSE file.
@ -44,16 +44,6 @@ Details:
* The `void_cast_raw_ptr` option is needed to make the `smart_holder` `vptr` * The `void_cast_raw_ptr` option is needed to make the `smart_holder` `vptr`
member invisible to the `shared_from_this` mechanism, in case the lifetime member invisible to the `shared_from_this` mechanism, in case the lifetime
of a `PyObject` is tied to the pointee. of a `PyObject` is tied to the pointee.
* Regarding `PYBIND11_TESTS_PURE_CPP_SMART_HOLDER_POC_TEST_CPP` below:
This define serves as a marker for code that is NOT used
from smart_holder_type_casters.h, but is exercised only from
tests/pure_cpp/smart_holder_poc_test.cpp. The marked code was useful
mainly for bootstrapping the smart_holder work. At this stage, with
smart_holder_type_casters.h in production use (at Google) since around
February 2021, it could be moved from here to tests/pure_cpp/ (help welcome).
It will probably be best in most cases to add tests for new functionality
under test/test_class_sh_*.
*/ */
#pragma once #pragma once
@ -256,26 +246,6 @@ struct smart_holder {
return static_cast<T *>(vptr.get()); return static_cast<T *>(vptr.get());
} }
#ifdef PYBIND11_TESTS_PURE_CPP_SMART_HOLDER_POC_TEST_CPP // See comment near top.
template <typename T>
T &as_lvalue_ref() const {
static const char *context = "as_lvalue_ref";
ensure_is_populated(context);
ensure_has_pointee(context);
return *as_raw_ptr_unowned<T>();
}
#endif
#ifdef PYBIND11_TESTS_PURE_CPP_SMART_HOLDER_POC_TEST_CPP // See comment near top.
template <typename T>
T &&as_rvalue_ref() const {
static const char *context = "as_rvalue_ref";
ensure_is_populated(context);
ensure_has_pointee(context);
return std::move(*as_raw_ptr_unowned<T>());
}
#endif
template <typename T> template <typename T>
static smart_holder from_raw_ptr_take_ownership(T *raw_ptr, bool void_cast_raw_ptr = false) { static smart_holder from_raw_ptr_take_ownership(T *raw_ptr, bool void_cast_raw_ptr = false) {
ensure_pointee_is_destructible<T>("from_raw_ptr_take_ownership"); ensure_pointee_is_destructible<T>("from_raw_ptr_take_ownership");
@ -319,16 +289,6 @@ struct smart_holder {
release_disowned(); release_disowned();
} }
#ifdef PYBIND11_TESTS_PURE_CPP_SMART_HOLDER_POC_TEST_CPP // See comment near top.
template <typename T>
T *as_raw_ptr_release_ownership(const char *context = "as_raw_ptr_release_ownership") {
ensure_can_release_ownership(context);
T *raw_ptr = as_raw_ptr_unowned<T>();
release_ownership();
return raw_ptr;
}
#endif
template <typename T, typename D> template <typename T, typename D>
static smart_holder from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr, static smart_holder from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr,
void *void_ptr = nullptr) { void *void_ptr = nullptr) {
@ -351,19 +311,6 @@ struct smart_holder {
return hld; return hld;
} }
#ifdef PYBIND11_TESTS_PURE_CPP_SMART_HOLDER_POC_TEST_CPP // See comment near top.
template <typename T, typename D = std::default_delete<T>>
std::unique_ptr<T, D> as_unique_ptr() {
static const char *context = "as_unique_ptr";
ensure_compatible_rtti_uqp_del<T, D>(context);
ensure_use_count_1(context);
T *raw_ptr = as_raw_ptr_unowned<T>();
release_ownership();
// KNOWN DEFECT (see PR #4850): Does not copy the deleter.
return std::unique_ptr<T, D>(raw_ptr);
}
#endif
template <typename T> template <typename T>
static smart_holder from_shared_ptr(std::shared_ptr<T> shd_ptr) { static smart_holder from_shared_ptr(std::shared_ptr<T> shd_ptr) {
smart_holder hld; smart_holder hld;

View File

@ -789,7 +789,7 @@ struct load_helper : value_and_holder_helper {
auto *gd = std::get_deleter<pybindit::memory::guarded_delete>(holder().vptr); auto *gd = std::get_deleter<pybindit::memory::guarded_delete>(holder().vptr);
if (gd && gd->use_del_fun) { // Note the ensure_compatible_rtti_uqp_del<T, D>() call above. if (gd && gd->use_del_fun) { // Note the ensure_compatible_rtti_uqp_del<T, D>() call above.
// In smart_holder_poc, a custom deleter is always stored in a guarded delete. // In struct_smart_holder, a custom deleter is always stored in a guarded delete.
// The guarded delete's std::function<void(void*)> actually points at the // The guarded delete's std::function<void(void*)> actually points at the
// custom_deleter type, so we can verify it is of the custom deleter type and // custom_deleter type, so we can verify it is of the custom deleter type and
// finally extract its deleter. // finally extract its deleter.

View File

@ -10,7 +10,7 @@
#include <type_traits> #include <type_traits>
#ifdef PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT #ifdef PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT
# include "smart_holder_poc.h" # include "struct_smart_holder.h"
#endif #endif
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)

View File

@ -58,7 +58,7 @@ detail_headers = {
"include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h", "include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h",
"include/pybind11/detail/init.h", "include/pybind11/detail/init.h",
"include/pybind11/detail/internals.h", "include/pybind11/detail/internals.h",
"include/pybind11/detail/smart_holder_poc.h", "include/pybind11/detail/struct_smart_holder.h",
"include/pybind11/detail/type_caster_base.h", "include/pybind11/detail/type_caster_base.h",
"include/pybind11/detail/typeid.h", "include/pybind11/detail/typeid.h",
"include/pybind11/detail/using_smart_holder.h", "include/pybind11/detail/using_smart_holder.h",

View File

@ -0,0 +1,51 @@
// Copyright (c) 2020-2024 The Pybind Development Team.
// All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#pragma once
#include "pybind11/detail/struct_smart_holder.h"
namespace pybindit {
namespace memory {
namespace smart_holder_poc { // Proof-of-Concept implementations.
template <typename T>
T &as_lvalue_ref(const smart_holder &hld) {
static const char *context = "as_lvalue_ref";
hld.ensure_is_populated(context);
hld.ensure_has_pointee(context);
return *hld.as_raw_ptr_unowned<T>();
}
template <typename T>
T &&as_rvalue_ref(const smart_holder &hld) {
static const char *context = "as_rvalue_ref";
hld.ensure_is_populated(context);
hld.ensure_has_pointee(context);
return std::move(*hld.as_raw_ptr_unowned<T>());
}
template <typename T>
T *as_raw_ptr_release_ownership(smart_holder &hld,
const char *context = "as_raw_ptr_release_ownership") {
hld.ensure_can_release_ownership(context);
T *raw_ptr = hld.as_raw_ptr_unowned<T>();
hld.release_ownership();
return raw_ptr;
}
template <typename T, typename D = std::default_delete<T>>
std::unique_ptr<T, D> as_unique_ptr(smart_holder &hld) {
static const char *context = "as_unique_ptr";
hld.ensure_compatible_rtti_uqp_del<T, D>(context);
hld.ensure_use_count_1(context);
T *raw_ptr = hld.as_raw_ptr_unowned<T>();
hld.release_ownership();
// KNOWN DEFECT (see PR #4850): Does not copy the deleter.
return std::unique_ptr<T, D>(raw_ptr);
}
} // namespace smart_holder_poc
} // namespace memory
} // namespace pybindit

View File

@ -1,6 +1,4 @@
#define PYBIND11_TESTS_PURE_CPP_SMART_HOLDER_POC_TEST_CPP #include "smart_holder_poc.h"
#include "pybind11/detail/smart_holder_poc.h"
#include <functional> #include <functional>
#include <memory> #include <memory>
@ -16,6 +14,7 @@
#include "catch.hpp" #include "catch.hpp"
using pybindit::memory::smart_holder; using pybindit::memory::smart_holder;
namespace poc = pybindit::memory::smart_holder_poc;
namespace helpers { namespace helpers {
@ -70,14 +69,14 @@ TEST_CASE("from_raw_ptr_unowned+as_raw_ptr_unowned", "[S]") {
TEST_CASE("from_raw_ptr_unowned+as_lvalue_ref", "[S]") { TEST_CASE("from_raw_ptr_unowned+as_lvalue_ref", "[S]") {
static int value = 19; static int value = 19;
auto hld = smart_holder::from_raw_ptr_unowned(&value); auto hld = smart_holder::from_raw_ptr_unowned(&value);
REQUIRE(hld.as_lvalue_ref<int>() == 19); REQUIRE(poc::as_lvalue_ref<int>(hld) == 19);
} }
TEST_CASE("from_raw_ptr_unowned+as_rvalue_ref", "[S]") { TEST_CASE("from_raw_ptr_unowned+as_rvalue_ref", "[S]") {
helpers::movable_int orig(19); helpers::movable_int orig(19);
{ {
auto hld = smart_holder::from_raw_ptr_unowned(&orig); auto hld = smart_holder::from_raw_ptr_unowned(&orig);
helpers::movable_int othr(hld.as_rvalue_ref<helpers::movable_int>()); helpers::movable_int othr(poc::as_rvalue_ref<helpers::movable_int>(hld));
REQUIRE(othr.valu == 19); REQUIRE(othr.valu == 19);
REQUIRE(orig.valu == 91); REQUIRE(orig.valu == 91);
} }
@ -86,21 +85,21 @@ TEST_CASE("from_raw_ptr_unowned+as_rvalue_ref", "[S]") {
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;
auto hld = smart_holder::from_raw_ptr_unowned(&value); auto hld = smart_holder::from_raw_ptr_unowned(&value);
REQUIRE_THROWS_WITH(hld.as_raw_ptr_release_ownership<int>(), REQUIRE_THROWS_WITH(poc::as_raw_ptr_release_ownership<int>(hld),
"Cannot disown non-owning holder (as_raw_ptr_release_ownership)."); "Cannot disown non-owning holder (as_raw_ptr_release_ownership).");
} }
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;
auto hld = smart_holder::from_raw_ptr_unowned(&value); auto hld = smart_holder::from_raw_ptr_unowned(&value);
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(), REQUIRE_THROWS_WITH(poc::as_unique_ptr<int>(hld),
"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;
auto hld = smart_holder::from_raw_ptr_unowned(&value); auto hld = smart_holder::from_raw_ptr_unowned(&value);
REQUIRE_THROWS_WITH((hld.as_unique_ptr<int, helpers::functor_builtin_delete<int>>()), REQUIRE_THROWS_WITH((poc::as_unique_ptr<int, helpers::functor_builtin_delete<int>>(hld)),
"Missing unique_ptr deleter (as_unique_ptr)."); "Missing unique_ptr deleter (as_unique_ptr).");
} }
@ -113,12 +112,12 @@ TEST_CASE("from_raw_ptr_unowned+as_shared_ptr", "[S]") {
TEST_CASE("from_raw_ptr_take_ownership+as_lvalue_ref", "[S]") { TEST_CASE("from_raw_ptr_take_ownership+as_lvalue_ref", "[S]") {
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
REQUIRE(hld.has_pointee()); REQUIRE(hld.has_pointee());
REQUIRE(hld.as_lvalue_ref<int>() == 19); REQUIRE(poc::as_lvalue_ref<int>(hld) == 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]") {
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
auto new_owner = std::unique_ptr<int>(hld.as_raw_ptr_release_ownership<int>()); auto new_owner = std::unique_ptr<int>(poc::as_raw_ptr_release_ownership<int>(hld));
REQUIRE(!hld.has_pointee()); REQUIRE(!hld.has_pointee());
REQUIRE(*new_owner == 19); REQUIRE(*new_owner == 19);
} }
@ -126,13 +125,13 @@ 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]") {
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); auto hld = smart_holder::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_raw_ptr_release_ownership<int>(), REQUIRE_THROWS_WITH(poc::as_raw_ptr_release_ownership<int>(hld),
"Cannot disown use_count != 1 (as_raw_ptr_release_ownership)."); "Cannot disown use_count != 1 (as_raw_ptr_release_ownership).");
} }
TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr1", "[S]") { TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr1", "[S]") {
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); auto hld = smart_holder::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 = poc::as_unique_ptr<int>(hld);
REQUIRE(!hld.has_pointee()); REQUIRE(!hld.has_pointee());
REQUIRE(*new_owner == 19); REQUIRE(*new_owner == 19);
} }
@ -140,12 +139,13 @@ TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr1", "[S]") {
TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr2", "[E]") { TEST_CASE("from_raw_ptr_take_ownership+as_unique_ptr2", "[E]") {
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); auto hld = smart_holder::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>(), "Cannot disown use_count != 1 (as_unique_ptr)."); REQUIRE_THROWS_WITH(poc::as_unique_ptr<int>(hld),
"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]") {
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
REQUIRE_THROWS_WITH((hld.as_unique_ptr<int, helpers::functor_builtin_delete<int>>()), REQUIRE_THROWS_WITH((poc::as_unique_ptr<int, helpers::functor_builtin_delete<int>>(hld)),
"Missing unique_ptr deleter (as_unique_ptr)."); "Missing unique_ptr deleter (as_unique_ptr).");
} }
@ -160,14 +160,14 @@ TEST_CASE("from_raw_ptr_take_ownership+disown+reclaim_disowned", "[S]") {
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
std::unique_ptr<int> new_owner(hld.as_raw_ptr_unowned<int>()); std::unique_ptr<int> new_owner(hld.as_raw_ptr_unowned<int>());
hld.disown(); hld.disown();
REQUIRE(hld.as_lvalue_ref<int>() == 19); REQUIRE(poc::as_lvalue_ref<int>(hld) == 19);
REQUIRE(*new_owner == 19); REQUIRE(*new_owner == 19);
hld.reclaim_disowned(); // Manually veriified: without this, clang++ -fsanitize=address reports hld.reclaim_disowned(); // Manually veriified: without this, clang++ -fsanitize=address reports
// "detected memory leaks". // "detected memory leaks".
// NOLINTNEXTLINE(bugprone-unused-return-value) // NOLINTNEXTLINE(bugprone-unused-return-value)
(void) new_owner.release(); // Manually verified: without this, clang++ -fsanitize=address (void) new_owner.release(); // Manually verified: without this, clang++ -fsanitize=address
// reports "attempting double-free". // reports "attempting double-free".
REQUIRE(hld.as_lvalue_ref<int>() == 19); REQUIRE(poc::as_lvalue_ref<int>(hld) == 19);
REQUIRE(new_owner.get() == nullptr); REQUIRE(new_owner.get() == nullptr);
} }
@ -175,7 +175,7 @@ TEST_CASE("from_raw_ptr_take_ownership+disown+release_disowned", "[S]") {
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
std::unique_ptr<int> new_owner(hld.as_raw_ptr_unowned<int>()); std::unique_ptr<int> new_owner(hld.as_raw_ptr_unowned<int>());
hld.disown(); hld.disown();
REQUIRE(hld.as_lvalue_ref<int>() == 19); REQUIRE(poc::as_lvalue_ref<int>(hld) == 19);
REQUIRE(*new_owner == 19); REQUIRE(*new_owner == 19);
hld.release_disowned(); hld.release_disowned();
REQUIRE(!hld.has_pointee()); REQUIRE(!hld.has_pointee());
@ -195,14 +195,14 @@ TEST_CASE("from_unique_ptr+as_lvalue_ref", "[S]") {
std::unique_ptr<int> orig_owner(new int(19)); std::unique_ptr<int> orig_owner(new int(19));
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
REQUIRE(hld.as_lvalue_ref<int>() == 19); REQUIRE(poc::as_lvalue_ref<int>(hld) == 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));
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
auto new_owner = std::unique_ptr<int>(hld.as_raw_ptr_release_ownership<int>()); auto new_owner = std::unique_ptr<int>(poc::as_raw_ptr_release_ownership<int>(hld));
REQUIRE(!hld.has_pointee()); REQUIRE(!hld.has_pointee());
REQUIRE(*new_owner == 19); REQUIRE(*new_owner == 19);
} }
@ -212,7 +212,7 @@ TEST_CASE("from_unique_ptr+as_raw_ptr_release_ownership2", "[E]") {
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); auto hld = smart_holder::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_raw_ptr_release_ownership<int>(), REQUIRE_THROWS_WITH(poc::as_raw_ptr_release_ownership<int>(hld),
"Cannot disown use_count != 1 (as_raw_ptr_release_ownership)."); "Cannot disown use_count != 1 (as_raw_ptr_release_ownership).");
} }
@ -220,7 +220,7 @@ 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));
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); auto hld = smart_holder::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 = poc::as_unique_ptr<int>(hld);
REQUIRE(!hld.has_pointee()); REQUIRE(!hld.has_pointee());
REQUIRE(*new_owner == 19); REQUIRE(*new_owner == 19);
} }
@ -230,14 +230,15 @@ TEST_CASE("from_unique_ptr+as_unique_ptr2", "[E]") {
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); auto hld = smart_holder::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>(), "Cannot disown use_count != 1 (as_unique_ptr)."); REQUIRE_THROWS_WITH(poc::as_unique_ptr<int>(hld),
"Cannot disown use_count != 1 (as_unique_ptr).");
} }
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));
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
REQUIRE_THROWS_WITH((hld.as_unique_ptr<int, helpers::functor_builtin_delete<int>>()), REQUIRE_THROWS_WITH((poc::as_unique_ptr<int, helpers::functor_builtin_delete<int>>(hld)),
"Incompatible unique_ptr deleter (as_unique_ptr)."); "Incompatible unique_ptr deleter (as_unique_ptr).");
} }
@ -254,7 +255,7 @@ TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base", "[S]") {
std::unique_ptr<helpers::derived> orig_owner(new helpers::derived()); std::unique_ptr<helpers::derived> orig_owner(new helpers::derived());
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
std::unique_ptr<helpers::base> new_owner = hld.as_unique_ptr<helpers::base>(); std::unique_ptr<helpers::base> new_owner = poc::as_unique_ptr<helpers::base>(hld);
REQUIRE(!hld.has_pointee()); REQUIRE(!hld.has_pointee());
REQUIRE(new_owner->get() == 100); REQUIRE(new_owner->get() == 100);
} }
@ -265,7 +266,7 @@ TEST_CASE("from_unique_ptr_derived+as_unique_ptr_base2", "[E]") {
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
REQUIRE_THROWS_WITH( REQUIRE_THROWS_WITH(
(hld.as_unique_ptr<helpers::base, helpers::functor_builtin_delete<helpers::base>>()), (poc::as_unique_ptr<helpers::base, helpers::functor_builtin_delete<helpers::base>>(hld)),
"Incompatible unique_ptr deleter (as_unique_ptr)."); "Incompatible unique_ptr deleter (as_unique_ptr).");
} }
@ -273,7 +274,7 @@ TEST_CASE("from_unique_ptr_with_deleter+as_lvalue_ref", "[S]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(new int(19)); std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(new int(19));
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
REQUIRE(hld.as_lvalue_ref<int>() == 19); REQUIRE(poc::as_lvalue_ref<int>(hld) == 19);
} }
TEST_CASE("from_unique_ptr_with_std_function_deleter+as_lvalue_ref", "[S]") { TEST_CASE("from_unique_ptr_with_std_function_deleter+as_lvalue_ref", "[S]") {
@ -281,14 +282,14 @@ TEST_CASE("from_unique_ptr_with_std_function_deleter+as_lvalue_ref", "[S]") {
new int(19), [](const int *raw_ptr) { delete raw_ptr; }); new int(19), [](const int *raw_ptr) { delete raw_ptr; });
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
REQUIRE(hld.as_lvalue_ref<int>() == 19); REQUIRE(poc::as_lvalue_ref<int>(hld) == 19);
} }
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(new int(19)); std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(new int(19));
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
REQUIRE_THROWS_WITH(hld.as_raw_ptr_release_ownership<int>(), REQUIRE_THROWS_WITH(poc::as_raw_ptr_release_ownership<int>(hld),
"Cannot disown custom deleter (as_raw_ptr_release_ownership)."); "Cannot disown custom deleter (as_raw_ptr_release_ownership).");
} }
@ -296,7 +297,7 @@ TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr", "[E]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(new int(19)); std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(new int(19));
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); auto hld = smart_holder::from_unique_ptr(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(poc::as_unique_ptr<int>(hld),
"Incompatible unique_ptr deleter (as_unique_ptr)."); "Incompatible unique_ptr deleter (as_unique_ptr).");
} }
@ -305,7 +306,7 @@ TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr_with_deleter1", "[S]") {
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); auto hld = smart_holder::from_unique_ptr(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<int, helpers::functor_builtin_delete<int>>(); = poc::as_unique_ptr<int, helpers::functor_builtin_delete<int>>(hld);
REQUIRE(!hld.has_pointee()); REQUIRE(!hld.has_pointee());
REQUIRE(*new_owner == 19); REQUIRE(*new_owner == 19);
} }
@ -314,7 +315,7 @@ TEST_CASE("from_unique_ptr_with_deleter+as_unique_ptr_with_deleter2", "[E]") {
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(new int(19)); std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner(new int(19));
auto hld = smart_holder::from_unique_ptr(std::move(orig_owner)); auto hld = smart_holder::from_unique_ptr(std::move(orig_owner));
REQUIRE(orig_owner.get() == nullptr); REQUIRE(orig_owner.get() == nullptr);
REQUIRE_THROWS_WITH((hld.as_unique_ptr<int, helpers::functor_other_delete<int>>()), REQUIRE_THROWS_WITH((poc::as_unique_ptr<int, helpers::functor_other_delete<int>>(hld)),
"Incompatible unique_ptr deleter (as_unique_ptr)."); "Incompatible unique_ptr deleter (as_unique_ptr).");
} }
@ -330,27 +331,27 @@ TEST_CASE("from_unique_ptr_with_deleter+as_shared_ptr", "[S]") {
TEST_CASE("from_shared_ptr+as_lvalue_ref", "[S]") { TEST_CASE("from_shared_ptr+as_lvalue_ref", "[S]") {
std::shared_ptr<int> orig_owner(new int(19)); std::shared_ptr<int> orig_owner(new int(19));
auto hld = smart_holder::from_shared_ptr(orig_owner); auto hld = smart_holder::from_shared_ptr(orig_owner);
REQUIRE(hld.as_lvalue_ref<int>() == 19); REQUIRE(poc::as_lvalue_ref<int>(hld) == 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));
auto hld = smart_holder::from_shared_ptr(orig_owner); auto hld = smart_holder::from_shared_ptr(orig_owner);
REQUIRE_THROWS_WITH(hld.as_raw_ptr_release_ownership<int>(), REQUIRE_THROWS_WITH(poc::as_raw_ptr_release_ownership<int>(hld),
"Cannot disown external shared_ptr (as_raw_ptr_release_ownership)."); "Cannot disown external shared_ptr (as_raw_ptr_release_ownership).");
} }
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));
auto hld = smart_holder::from_shared_ptr(orig_owner); auto hld = smart_holder::from_shared_ptr(orig_owner);
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(), REQUIRE_THROWS_WITH(poc::as_unique_ptr<int>(hld),
"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));
auto hld = smart_holder::from_shared_ptr(orig_owner); auto hld = smart_holder::from_shared_ptr(orig_owner);
REQUIRE_THROWS_WITH((hld.as_unique_ptr<int, helpers::functor_builtin_delete<int>>()), REQUIRE_THROWS_WITH((poc::as_unique_ptr<int, helpers::functor_builtin_delete<int>>(hld)),
"Missing unique_ptr deleter (as_unique_ptr)."); "Missing unique_ptr deleter (as_unique_ptr).");
} }
@ -362,19 +363,19 @@ TEST_CASE("from_shared_ptr+as_shared_ptr", "[S]") {
TEST_CASE("error_unpopulated_holder", "[E]") { TEST_CASE("error_unpopulated_holder", "[E]") {
smart_holder hld; smart_holder hld;
REQUIRE_THROWS_WITH(hld.as_lvalue_ref<int>(), "Unpopulated holder (as_lvalue_ref)."); REQUIRE_THROWS_WITH(poc::as_lvalue_ref<int>(hld), "Unpopulated holder (as_lvalue_ref).");
} }
TEST_CASE("error_disowned_holder", "[E]") { TEST_CASE("error_disowned_holder", "[E]") {
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
hld.as_unique_ptr<int>(); poc::as_unique_ptr<int>(hld);
REQUIRE_THROWS_WITH(hld.as_lvalue_ref<int>(), "Disowned holder (as_lvalue_ref)."); REQUIRE_THROWS_WITH(poc::as_lvalue_ref<int>(hld), "Disowned holder (as_lvalue_ref).");
} }
TEST_CASE("error_cannot_disown_nullptr", "[E]") { TEST_CASE("error_cannot_disown_nullptr", "[E]") {
auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19)); auto hld = smart_holder::from_raw_ptr_take_ownership(new int(19));
hld.as_unique_ptr<int>(); poc::as_unique_ptr<int>(hld);
REQUIRE_THROWS_WITH(hld.as_unique_ptr<int>(), "Cannot disown nullptr (as_unique_ptr)."); REQUIRE_THROWS_WITH(poc::as_unique_ptr<int>(hld), "Cannot disown nullptr (as_unique_ptr).");
} }
TEST_CASE("indestructible_int-from_raw_ptr_unowned+as_raw_ptr_unowned", "[S]") { TEST_CASE("indestructible_int-from_raw_ptr_unowned+as_raw_ptr_unowned", "[S]") {