Commit dc10e8a9 by Ralf W. Grosse-Kunstleve

Merge branch 'test_unique_ptr_member' into pr2672_use_smart_holder_as_default

parents 5cf93a08 a569281f
...@@ -1237,7 +1237,7 @@ struct smart_holder_type_caster_load { ...@@ -1237,7 +1237,7 @@ struct smart_holder_type_caster_load {
} }
T *loaded_as_raw_ptr_unowned() const { T *loaded_as_raw_ptr_unowned() const {
void *void_ptr = load_impl.unowned_void_ptr_from_direct_conversion; // UNTESTED. void *void_ptr = load_impl.unowned_void_ptr_from_direct_conversion;
if (void_ptr == nullptr) { if (void_ptr == nullptr) {
if (have_holder()) { if (have_holder()) {
throw_if_uninitialized_or_disowned_holder(); throw_if_uninitialized_or_disowned_holder();
...@@ -1259,7 +1259,7 @@ struct smart_holder_type_caster_load { ...@@ -1259,7 +1259,7 @@ struct smart_holder_type_caster_load {
std::shared_ptr<T> loaded_as_shared_ptr() const { std::shared_ptr<T> loaded_as_shared_ptr() const {
if (load_impl.unowned_void_ptr_from_direct_conversion != nullptr) if (load_impl.unowned_void_ptr_from_direct_conversion != nullptr)
throw cast_error("Unowned pointer from direct conversion cannot be converted to a" throw cast_error("Unowned pointer from direct conversion cannot be converted to a"
" std::shared_ptr."); // UNTESTED. " std::shared_ptr.");
if (!have_holder()) return nullptr; if (!have_holder()) return nullptr;
throw_if_uninitialized_or_disowned_holder(); throw_if_uninitialized_or_disowned_holder();
std::shared_ptr<void> void_ptr = holder().template as_shared_ptr<void>(); std::shared_ptr<void> void_ptr = holder().template as_shared_ptr<void>();
...@@ -1270,13 +1270,13 @@ struct smart_holder_type_caster_load { ...@@ -1270,13 +1270,13 @@ struct smart_holder_type_caster_load {
std::unique_ptr<T, D> loaded_as_unique_ptr(const char *context = "loaded_as_unique_ptr") { std::unique_ptr<T, D> loaded_as_unique_ptr(const char *context = "loaded_as_unique_ptr") {
if (load_impl.unowned_void_ptr_from_direct_conversion != nullptr) if (load_impl.unowned_void_ptr_from_direct_conversion != nullptr)
throw cast_error("Unowned pointer from direct conversion cannot be converted to a" throw cast_error("Unowned pointer from direct conversion cannot be converted to a"
" std::unique_ptr."); // UNTESTED. " std::unique_ptr.");
if (!have_holder()) return nullptr; if (!have_holder()) return nullptr;
throw_if_uninitialized_or_disowned_holder(); throw_if_uninitialized_or_disowned_holder();
holder().template ensure_compatible_rtti_uqp_del<T, D>(context); holder().template ensure_compatible_rtti_uqp_del<T, D>(context);
holder().ensure_use_count_1(context); holder().ensure_use_count_1(context);
auto raw_void_ptr = holder().template as_raw_ptr_unowned<void>(); auto raw_void_ptr = holder().template as_raw_ptr_unowned<void>();
// MISSING: Safety checks for type conversions // SMART_HOLDER_WIP: MISSING: Safety checks for type conversions
// (T must be polymorphic or meet certain other conditions). // (T must be polymorphic or meet certain other conditions).
T *raw_type_ptr = convert_type(raw_void_ptr); T *raw_type_ptr = convert_type(raw_void_ptr);
...@@ -1325,7 +1325,7 @@ private: ...@@ -1325,7 +1325,7 @@ private:
} }
}; };
// IMPROVABLE: Formally factor out of type_caster_base. // SMART_HOLDER_WIP: IMPROVABLE: Formally factor out of type_caster_base.
struct make_constructor : private type_caster_base<int> { // Any type, nothing special about int. struct make_constructor : private type_caster_base<int> { // Any type, nothing special about int.
using type_caster_base<int>::Constructor; using type_caster_base<int>::Constructor;
using type_caster_base<int>::make_copy_constructor; using type_caster_base<int>::make_copy_constructor;
...@@ -1501,7 +1501,7 @@ struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_l ...@@ -1501,7 +1501,7 @@ struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_l
static handle cast(const std::shared_ptr<T> &src, return_value_policy policy, handle parent) { static handle cast(const std::shared_ptr<T> &src, return_value_policy policy, handle parent) {
if (policy != return_value_policy::automatic if (policy != return_value_policy::automatic
&& policy != return_value_policy::reference_internal) { && policy != return_value_policy::reference_internal) {
// IMPROVABLE: Error message. // SMART_HOLDER_WIP: IMPROVABLE: Error message.
throw cast_error("Invalid return_value_policy for shared_ptr."); throw cast_error("Invalid return_value_policy for shared_ptr.");
} }
...@@ -1513,8 +1513,8 @@ struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_l ...@@ -1513,8 +1513,8 @@ struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_l
void *src_raw_void_ptr = static_cast<void *>(src_raw_ptr); void *src_raw_void_ptr = static_cast<void *>(src_raw_ptr);
const detail::type_info *tinfo = st.second; const detail::type_info *tinfo = st.second;
if (handle existing_inst = find_registered_python_instance(src_raw_void_ptr, tinfo)) if (handle existing_inst = find_registered_python_instance(src_raw_void_ptr, tinfo))
// MISSING: Enforcement of consistency with existing smart_holder. // SMART_HOLDER_WIP: MISSING: Enforcement of consistency with existing smart_holder.
// MISSING: keep_alive. // SMART_HOLDER_WIP: MISSING: keep_alive.
return existing_inst; return existing_inst;
auto inst = reinterpret_steal<object>(make_new_instance(tinfo->type)); auto inst = reinterpret_steal<object>(make_new_instance(tinfo->type));
...@@ -1566,7 +1566,7 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste ...@@ -1566,7 +1566,7 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
if (policy != return_value_policy::automatic if (policy != return_value_policy::automatic
&& policy != return_value_policy::reference_internal && policy != return_value_policy::reference_internal
&& policy != return_value_policy::move) { && policy != return_value_policy::move) {
// IMPROVABLE: Error message. // SMART_HOLDER_WIP: IMPROVABLE: Error message.
throw cast_error("Invalid return_value_policy for unique_ptr."); throw cast_error("Invalid return_value_policy for unique_ptr.");
} }
......
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
# pragma warning(disable: 4324) // warning C4324: structure was padded due to alignment specifier # pragma warning(disable: 4324) // warning C4324: structure was padded due to alignment specifier
#endif #endif
namespace {
// test_brace_initialization // test_brace_initialization
struct NoBraceInitialization { struct NoBraceInitialization {
NoBraceInitialization(std::vector<int> v) : vec{std::move(v)} {} NoBraceInitialization(std::vector<int> v) : vec{std::move(v)} {}
...@@ -32,14 +34,17 @@ struct NoBraceInitialization { ...@@ -32,14 +34,17 @@ struct NoBraceInitialization {
std::vector<int> vec; std::vector<int> vec;
}; };
// test_mismatched_holder
struct MismatchBase1 { }; struct MismatchBase1 { };
struct MismatchDerived1 : MismatchBase1 { }; struct MismatchDerived1 : MismatchBase1 { };
struct MismatchBase2 { }; struct MismatchBase2 { };
struct MismatchDerived2 : MismatchBase2 { }; struct MismatchDerived2 : MismatchBase2 { };
// test_multiple_instances_with_same_pointer
struct SamePointer {}; struct SamePointer {};
} // namespace
PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(MismatchBase1, std::shared_ptr<MismatchBase1>) PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(MismatchBase1, std::shared_ptr<MismatchBase1>)
PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(MismatchDerived1, std::unique_ptr<MismatchDerived1>) PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(MismatchDerived1, std::unique_ptr<MismatchDerived1>)
PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(MismatchBase2, std::unique_ptr<MismatchBase2>) PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(MismatchBase2, std::unique_ptr<MismatchBase2>)
......
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
#include "pybind11_tests.h" #include "pybind11_tests.h"
#include "constructor_stats.h" #include "constructor_stats.h"
namespace {
// Many bases for testing that multiple inheritance from many classes (i.e. requiring extra // Many bases for testing that multiple inheritance from many classes (i.e. requiring extra
// space for holder constructed flags) works. // space for holder constructed flags) works.
template <int N> struct BaseN { template <int N> struct BaseN {
...@@ -43,26 +45,27 @@ int WithStatic2::static_value2 = 2; ...@@ -43,26 +45,27 @@ int WithStatic2::static_value2 = 2;
int VanillaStaticMix1::static_value = 12; int VanillaStaticMix1::static_value = 12;
int VanillaStaticMix2::static_value = 12; int VanillaStaticMix2::static_value = 12;
namespace { // test_multiple_inheritance_virtbase
struct Base1a {
struct Base1a {
Base1a(int i) : i(i) { } Base1a(int i) : i(i) { }
int foo() { return i; } int foo() { return i; }
int i; int i;
}; };
struct Base2a { struct Base2a {
Base2a(int i) : i(i) { } Base2a(int i) : i(i) { }
int bar() { return i; } int bar() { return i; }
int i; int i;
}; };
struct Base12a : Base1a, Base2a { struct Base12a : Base1a, Base2a {
Base12a(int i, int j) : Base1a(i), Base2a(j) { } Base12a(int i, int j) : Base1a(i), Base2a(j) { }
}; };
struct I801B1 { int a = 1; I801B1() = default; I801B1(const I801B1 &) = default; virtual ~I801B1() = default; }; // test_mi_unaligned_base
struct I801B2 { int b = 2; I801B2() = default; I801B2(const I801B2 &) = default; virtual ~I801B2() = default; }; // test_mi_base_return
struct I801C : I801B1, I801B2 {}; struct I801B1 { int a = 1; I801B1() = default; I801B1(const I801B1 &) = default; virtual ~I801B1() = default; };
struct I801D : I801C {}; // Indirect MI struct I801B2 { int b = 2; I801B2() = default; I801B2(const I801B2 &) = default; virtual ~I801B2() = default; };
struct I801C : I801B1, I801B2 {};
struct I801D : I801C {}; // Indirect MI
} // namespace } // namespace
......
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