Skip to content

Commit 8ef1255

Browse files
committed
Hard-coding smart_holder into classh.
1 parent 604e718 commit 8ef1255

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

include/pybind11/classh.h

+8-17
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
#pragma once
22

3+
#include "smart_holder_poc.h"
34
#include "pybind11.h"
45

56
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
67

78
template <typename type_, typename... options>
89
class classh : public detail::generic_type {
9-
template <typename T> using is_holder = detail::is_holder_type<type_, T>;
1010
template <typename T> using is_subtype = detail::is_strict_base_of<type_, T>;
1111
template <typename T> using is_base = detail::is_strict_base_of<T, type_>;
1212
// struct instead of using here to help MSVC:
1313
template <typename T> struct is_valid_class_option :
14-
detail::any_of<is_holder<T>, is_subtype<T>, is_base<T>> {};
14+
detail::any_of<is_subtype<T>, is_base<T>> {};
1515

1616
public:
1717
using type = type_;
1818
using type_alias = detail::exactly_one_t<is_subtype, void, options...>;
1919
constexpr static bool has_alias = !std::is_void<type_alias>::value;
20-
using holder_type = detail::exactly_one_t<is_holder, std::unique_ptr<type>, options...>;
20+
using holder_type = pybindit::memory::smart_holder;
2121

2222
static_assert(detail::all_of<is_valid_class_option<options>...>::value,
2323
"Unknown/invalid classh template parameters provided");
@@ -48,7 +48,7 @@ class classh : public detail::generic_type {
4848
record.holder_size = sizeof(holder_type);
4949
record.init_instance = init_instance;
5050
record.dealloc = dealloc;
51-
record.default_holder = detail::is_instantiation<std::unique_ptr, holder_type>::value;
51+
record.default_holder = false;
5252

5353
set_operator_new<type>(&record);
5454

@@ -271,7 +271,7 @@ class classh : public detail::generic_type {
271271
static void init_holder(detail::instance *inst, detail::value_and_holder &v_h,
272272
const holder_type * /* unused */, const std::enable_shared_from_this<T> * /* dummy */) {
273273
try {
274-
auto sh = std::dynamic_pointer_cast<typename holder_type::element_type>(
274+
auto sh = std::dynamic_pointer_cast<type>( // Was: typename holder_type::element_type
275275
v_h.value_ptr<type>()->shared_from_this());
276276
if (sh) {
277277
new (std::addressof(v_h.holder<holder_type>())) holder_type(std::move(sh));
@@ -285,24 +285,15 @@ class classh : public detail::generic_type {
285285
}
286286
}
287287

288-
static void init_holder_from_existing(const detail::value_and_holder &v_h,
289-
const holder_type *holder_ptr, std::true_type /*is_copy_constructible*/) {
290-
new (std::addressof(v_h.holder<holder_type>())) holder_type(*reinterpret_cast<const holder_type *>(holder_ptr));
291-
}
292-
293-
static void init_holder_from_existing(const detail::value_and_holder &v_h,
294-
const holder_type *holder_ptr, std::false_type /*is_copy_constructible*/) {
295-
new (std::addressof(v_h.holder<holder_type>())) holder_type(std::move(*const_cast<holder_type *>(holder_ptr)));
296-
}
297-
298288
/// Initialize holder object, variant 2: try to construct from existing holder object, if possible
299289
static void init_holder(detail::instance *inst, detail::value_and_holder &v_h,
300290
const holder_type *holder_ptr, const void * /* dummy -- not enable_shared_from_this<T>) */) {
301291
if (holder_ptr) {
302-
init_holder_from_existing(v_h, holder_ptr, std::is_copy_constructible<holder_type>());
292+
new (std::addressof(v_h.holder<holder_type>())) holder_type(*reinterpret_cast<const holder_type *>(holder_ptr));
303293
v_h.set_holder_constructed();
304294
} else if (inst->owned || detail::always_construct_holder<holder_type>::value) {
305-
new (std::addressof(v_h.holder<holder_type>())) holder_type(v_h.value_ptr<type>());
295+
new (std::addressof(v_h.holder<holder_type>())) holder_type(
296+
std::move(holder_type::from_raw_ptr_take_ownership(v_h.value_ptr<type>())));
306297
v_h.set_holder_constructed();
307298
}
308299
}

0 commit comments

Comments
 (0)