1
1
#pragma once
2
2
3
+ #include " smart_holder_poc.h"
3
4
#include " pybind11.h"
4
5
5
6
PYBIND11_NAMESPACE_BEGIN (PYBIND11_NAMESPACE)
6
7
7
8
template <typename type_, typename... options>
8
9
class classh : public detail::generic_type {
9
- template <typename T> using is_holder = detail::is_holder_type<type_, T>;
10
10
template <typename T> using is_subtype = detail::is_strict_base_of<type_, T>;
11
11
template <typename T> using is_base = detail::is_strict_base_of<T, type_>;
12
12
// struct instead of using here to help MSVC:
13
13
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>> {};
15
15
16
16
public:
17
17
using type = type_;
18
18
using type_alias = detail::exactly_one_t <is_subtype, void , options...>;
19
19
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 ;
21
21
22
22
static_assert (detail::all_of<is_valid_class_option<options>...>::value,
23
23
" Unknown/invalid classh template parameters provided" );
@@ -48,7 +48,7 @@ class classh : public detail::generic_type {
48
48
record.holder_size = sizeof (holder_type);
49
49
record.init_instance = init_instance;
50
50
record.dealloc = dealloc;
51
- record.default_holder = detail::is_instantiation<std::unique_ptr, holder_type>::value ;
51
+ record.default_holder = false ;
52
52
53
53
set_operator_new<type>(&record);
54
54
@@ -271,7 +271,7 @@ class classh : public detail::generic_type {
271
271
static void init_holder (detail::instance *inst, detail::value_and_holder &v_h,
272
272
const holder_type * /* unused */ , const std::enable_shared_from_this<T> * /* dummy */ ) {
273
273
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
275
275
v_h.value_ptr <type>()->shared_from_this ());
276
276
if (sh) {
277
277
new (std::addressof (v_h.holder <holder_type>())) holder_type (std::move (sh));
@@ -285,24 +285,15 @@ class classh : public detail::generic_type {
285
285
}
286
286
}
287
287
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
-
298
288
// / Initialize holder object, variant 2: try to construct from existing holder object, if possible
299
289
static void init_holder (detail::instance *inst, detail::value_and_holder &v_h,
300
290
const holder_type *holder_ptr, const void * /* dummy -- not enable_shared_from_this<T>) */ ) {
301
291
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 ));
303
293
v_h.set_holder_constructed ();
304
294
} 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>())));
306
297
v_h.set_holder_constructed ();
307
298
}
308
299
}
0 commit comments