Skip to content

Commit d72d54e

Browse files
committed
Experiment: effectively undoing all shared_from_this modifications.
1 parent febf9ce commit d72d54e

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

include/pybind11/detail/smart_holder_type_casters.h

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
#include <typeinfo>
2727
#include <utility>
2828

29+
#ifdef JUNK
30+
# include <iostream>
31+
inline void to_cout(std::string msg) { std::cout << msg << std::endl; }
32+
#endif
33+
2934
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
3035

3136
using pybindit::memory::smart_holder;
@@ -273,6 +278,7 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag
273278
return false;
274279
}
275280

281+
#ifdef JUNK
276282
template <typename WrappedType, typename AnyBaseOfWrappedType>
277283
static bool try_initialization_using_shared_from_this(
278284
holder_type *uninitialized_location,
@@ -286,6 +292,7 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag
286292
new (uninitialized_location) holder_type(holder_type::from_shared_ptr(shd_ptr));
287293
return true;
288294
}
295+
#endif
289296

290297
template <typename WrappedType, typename AliasType>
291298
static void init_instance_for_type(detail::instance *inst, const void *holder_const_void_ptr) {
@@ -298,26 +305,26 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag
298305
register_instance(inst, v_h.value_ptr(), v_h.type);
299306
v_h.set_instance_registered();
300307
}
301-
auto uninitialized_location = std::addressof(v_h.holder<holder_type>());
302-
auto value_ptr_w_t = v_h.value_ptr<WrappedType>();
303308
if (holder_void_ptr) {
304309
// Note: inst->owned ignored.
305310
auto holder_ptr = static_cast<holder_type *>(holder_void_ptr);
306-
new (uninitialized_location) holder_type(std::move(*holder_ptr));
311+
new (std::addressof(v_h.holder<holder_type>())) holder_type(std::move(*holder_ptr));
307312
} else {
308313
if (!try_initialization_using_shared_from_this(
309-
uninitialized_location, value_ptr_w_t, value_ptr_w_t)) {
314+
std::addressof(v_h.holder<holder_type>()),
315+
v_h.value_ptr<WrappedType>(),
316+
v_h.value_ptr<WrappedType>())) {
310317
if (inst->owned) {
311-
new (uninitialized_location)
312-
holder_type(holder_type::from_raw_ptr_take_ownership(value_ptr_w_t));
318+
new (std::addressof(v_h.holder<holder_type>())) holder_type(
319+
holder_type::from_raw_ptr_take_ownership(v_h.value_ptr<WrappedType>()));
313320
} else {
314-
new (uninitialized_location)
315-
holder_type(holder_type::from_raw_ptr_unowned(value_ptr_w_t));
321+
new (std::addressof(v_h.holder<holder_type>())) holder_type(
322+
holder_type::from_raw_ptr_unowned(v_h.value_ptr<WrappedType>()));
316323
}
317324
}
318325
}
319326
v_h.holder<holder_type>().pointee_depends_on_holder_owner
320-
= dynamic_raw_ptr_cast_if_possible<AliasType>(value_ptr_w_t) != nullptr;
327+
= dynamic_raw_ptr_cast_if_possible<AliasType>(v_h.value_ptr<WrappedType>()) != nullptr;
321328
v_h.set_holder_constructed();
322329
}
323330

@@ -387,14 +394,20 @@ struct smart_holder_type_caster_load {
387394
auto type_raw_ptr = convert_type(void_raw_ptr);
388395
if (holder().pointee_depends_on_holder_owner) {
389396
// Tie lifetime of trampoline Python part to C++ part (PR #2839).
397+
#ifdef JUNK
398+
to_cout("");
399+
to_cout("LOOOK " + std::to_string(__LINE__) + " " + __FILE__);
400+
#endif
390401
return std::shared_ptr<T>(
391402
type_raw_ptr,
392403
shared_ptr_dec_ref_deleter{
393404
handle((PyObject *) load_impl.loaded_v_h.inst).inc_ref()});
394405
}
406+
#ifdef JUNK
395407
if (holder().vptr_is_using_noop_deleter) {
396408
throw std::runtime_error("Non-owning holder (loaded_as_shared_ptr).");
397409
}
410+
#endif
398411
std::shared_ptr<void> void_shd_ptr = holder().template as_shared_ptr<void>();
399412
return std::shared_ptr<T>(void_shd_ptr, type_raw_ptr);
400413
}

0 commit comments

Comments
 (0)