Skip to content

Commit 38f60b6

Browse files
committed
Revert "Experiment: effectively undoing all shared_from_this modifications."
This reverts commit d72d54e.
1 parent 57ec31b commit 38f60b6

File tree

1 file changed

+9
-22
lines changed

1 file changed

+9
-22
lines changed

include/pybind11/detail/smart_holder_type_casters.h

+9-22
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
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-
3429
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
3530

3631
using pybindit::memory::smart_holder;
@@ -278,7 +273,6 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag
278273
return false;
279274
}
280275

281-
#ifdef JUNK
282276
template <typename WrappedType, typename AnyBaseOfWrappedType>
283277
static bool try_initialization_using_shared_from_this(
284278
holder_type *uninitialized_location,
@@ -292,7 +286,6 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag
292286
new (uninitialized_location) holder_type(holder_type::from_shared_ptr(shd_ptr));
293287
return true;
294288
}
295-
#endif
296289

297290
template <typename WrappedType, typename AliasType>
298291
static void init_instance_for_type(detail::instance *inst, const void *holder_const_void_ptr) {
@@ -305,26 +298,26 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag
305298
register_instance(inst, v_h.value_ptr(), v_h.type);
306299
v_h.set_instance_registered();
307300
}
301+
auto uninitialized_location = std::addressof(v_h.holder<holder_type>());
302+
auto value_ptr_w_t = v_h.value_ptr<WrappedType>();
308303
if (holder_void_ptr) {
309304
// Note: inst->owned ignored.
310305
auto holder_ptr = static_cast<holder_type *>(holder_void_ptr);
311-
new (std::addressof(v_h.holder<holder_type>())) holder_type(std::move(*holder_ptr));
306+
new (uninitialized_location) holder_type(std::move(*holder_ptr));
312307
} else {
313308
if (!try_initialization_using_shared_from_this(
314-
std::addressof(v_h.holder<holder_type>()),
315-
v_h.value_ptr<WrappedType>(),
316-
v_h.value_ptr<WrappedType>())) {
309+
uninitialized_location, value_ptr_w_t, value_ptr_w_t)) {
317310
if (inst->owned) {
318-
new (std::addressof(v_h.holder<holder_type>())) holder_type(
319-
holder_type::from_raw_ptr_take_ownership(v_h.value_ptr<WrappedType>()));
311+
new (uninitialized_location)
312+
holder_type(holder_type::from_raw_ptr_take_ownership(value_ptr_w_t));
320313
} else {
321-
new (std::addressof(v_h.holder<holder_type>())) holder_type(
322-
holder_type::from_raw_ptr_unowned(v_h.value_ptr<WrappedType>()));
314+
new (uninitialized_location)
315+
holder_type(holder_type::from_raw_ptr_unowned(value_ptr_w_t));
323316
}
324317
}
325318
}
326319
v_h.holder<holder_type>().pointee_depends_on_holder_owner
327-
= dynamic_raw_ptr_cast_if_possible<AliasType>(v_h.value_ptr<WrappedType>()) != nullptr;
320+
= dynamic_raw_ptr_cast_if_possible<AliasType>(value_ptr_w_t) != nullptr;
328321
v_h.set_holder_constructed();
329322
}
330323

@@ -394,20 +387,14 @@ struct smart_holder_type_caster_load {
394387
auto type_raw_ptr = convert_type(void_raw_ptr);
395388
if (holder().pointee_depends_on_holder_owner) {
396389
// 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
401390
return std::shared_ptr<T>(
402391
type_raw_ptr,
403392
shared_ptr_dec_ref_deleter{
404393
handle((PyObject *) load_impl.loaded_v_h.inst).inc_ref()});
405394
}
406-
#ifdef JUNK
407395
if (holder().vptr_is_using_noop_deleter) {
408396
throw std::runtime_error("Non-owning holder (loaded_as_shared_ptr).");
409397
}
410-
#endif
411398
std::shared_ptr<void> void_shd_ptr = holder().template as_shared_ptr<void>();
412399
return std::shared_ptr<T>(void_shd_ptr, type_raw_ptr);
413400
}

0 commit comments

Comments
 (0)