snapshot of first attempt to replace `void *` with `any` (messy state)

parent 940d7935
...@@ -245,7 +245,7 @@ struct type_record { ...@@ -245,7 +245,7 @@ struct type_record {
void *(*operator_new)(size_t) = nullptr; void *(*operator_new)(size_t) = nullptr;
/// Function pointer to class_<..>::init_instance /// Function pointer to class_<..>::init_instance
void (*init_instance)(instance *, const void *) = nullptr; void (*init_instance)(instance *, std::any) = nullptr;
/// Function pointer to class_<..>::dealloc /// Function pointer to class_<..>::dealloc
void (*dealloc)(detail::value_and_holder &) = nullptr; void (*dealloc)(detail::value_and_holder &) = nullptr;
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "detail/typeid.h" #include "detail/typeid.h"
#include "detail/descr.h" #include "detail/descr.h"
#include "detail/internals.h" #include "detail/internals.h"
#include <any>
#include <array> #include <array>
#include <limits> #include <limits>
#include <tuple> #include <tuple>
...@@ -232,6 +233,13 @@ struct value_and_holder { ...@@ -232,6 +233,13 @@ struct value_and_holder {
explicit operator bool() const { return xxx_value_ptr<void>(); } explicit operator bool() const { return xxx_value_ptr<void>(); }
template <typename H> H &xxx_holder() const { template <typename H> H &xxx_holder() const {
#ifdef JUNK
to_cout(std::string("xxx_holder<") + typeid(H).name() + ">");
if (std::string(typeid(H).name()) == "St10shared_ptrIN14pybind11_tests17holder_shared_ptr7pointeeEE") {
long *BAD = nullptr;
std::cout << *BAD;
}
#endif
return reinterpret_cast<H &>(zzz_vh[1]); return reinterpret_cast<H &>(zzz_vh[1]);
} }
bool holder_constructed() const { bool holder_constructed() const {
...@@ -500,7 +508,7 @@ public: ...@@ -500,7 +508,7 @@ public:
const detail::type_info *tinfo, const detail::type_info *tinfo,
void *(*copy_constructor)(const void *), void *(*copy_constructor)(const void *),
void *(*move_constructor)(const void *), void *(*move_constructor)(const void *),
const void *existing_holder = nullptr) { std::any existing_holder = std::any{}) {
if (!tinfo) // no type info: error will be set already if (!tinfo) // no type info: error will be set already
return handle(); return handle();
...@@ -586,7 +594,7 @@ public: ...@@ -586,7 +594,7 @@ public:
throw cast_error("unhandled return_value_policy: should not happen!"); throw cast_error("unhandled return_value_policy: should not happen!");
} }
tinfo->init_instance(wrapper, existing_holder); tinfo->init_instance(wrapper, existing_holder); // HOLDER_SHARED_MAKE_UNIQUE STACK #4
return inst.release(); return inst.release();
} }
...@@ -913,10 +921,10 @@ public: ...@@ -913,10 +921,10 @@ public:
make_copy_constructor(src), make_move_constructor(src)); make_copy_constructor(src), make_move_constructor(src));
} }
static handle cast_holder(const itype *src, const void *holder) { static handle cast_holder(const itype *src, std::any holder) {
auto st = src_and_type(src); auto st = src_and_type(src);
return type_caster_generic::cast( return type_caster_generic::cast( // HOLDER_SHARED_MAKE_UNIQUE STACK #5
st.first, return_value_policy::take_ownership, {}, st.second, st.first, return_value_policy::take_ownership, {}, st.second, // tinfo=st.second TODODODO return_value_policy::existing_holder
nullptr, nullptr, holder); nullptr, nullptr, holder);
} }
...@@ -1582,7 +1590,7 @@ struct move_only_holder_caster { ...@@ -1582,7 +1590,7 @@ struct move_only_holder_caster {
static handle cast(holder_type &&src, return_value_policy, handle) { static handle cast(holder_type &&src, return_value_policy, handle) {
auto *ptr = holder_helper<holder_type>::yyy_get(src); // move_only_holder_caster::cast auto *ptr = holder_helper<holder_type>::yyy_get(src); // move_only_holder_caster::cast
return type_caster_base<type>::cast_holder(ptr, std::addressof(src)); return type_caster_base<type>::cast_holder(ptr, std::addressof(src)); // HOLDER_SHARED_MAKE_UNIQUE STACK #6
} }
static constexpr auto name = type_caster_base<type>::name; static constexpr auto name = type_caster_base<type>::name;
}; };
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#pragma once #pragma once
#include "../pytypes.h" #include "../pytypes.h"
#include <any>
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
PYBIND11_NAMESPACE_BEGIN(detail) PYBIND11_NAMESPACE_BEGIN(detail)
...@@ -130,7 +131,7 @@ struct type_info { ...@@ -130,7 +131,7 @@ struct type_info {
const std::type_info *cpptype; const std::type_info *cpptype;
size_t type_size, type_align, holder_size_in_ptrs; size_t type_size, type_align, holder_size_in_ptrs;
void *(*operator_new)(size_t); void *(*operator_new)(size_t);
void (*init_instance)(instance *, const void *); void (*init_instance)(instance *, std::any);
void (*dealloc)(value_and_holder &v_h); void (*dealloc)(value_and_holder &v_h);
std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions; std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions;
std::vector<std::pair<const std::type_info *, void *(*)(void *)>> implicit_casts; std::vector<std::pair<const std::type_info *, void *(*)(void *)>> implicit_casts;
......
...@@ -158,7 +158,7 @@ protected: ...@@ -158,7 +158,7 @@ protected:
"The number of argument annotations does not match the number of function arguments"); "The number of argument annotations does not match the number of function arguments");
/* Dispatch code which converts function arguments and performs the actual function call */ /* Dispatch code which converts function arguments and performs the actual function call */
rec->impl = [](function_call &call) -> handle { rec->impl = [](function_call &call) -> handle { // HOLDER_SHARED_MAKE_UNIQUE STACK #8
cast_in args_converter; cast_in args_converter;
/* Try to cast the function arguments into the C++ domain */ /* Try to cast the function arguments into the C++ domain */
...@@ -180,7 +180,7 @@ protected: ...@@ -180,7 +180,7 @@ protected:
using Guard = extract_guard_t<Extra...>; using Guard = extract_guard_t<Extra...>;
/* Perform the function call */ /* Perform the function call */
handle result = cast_out::cast( handle result = cast_out::cast( // HOLDER_SHARED_MAKE_UNIQUE STACK #7
std::move(args_converter).template call<Return, Guard>(cap->f), policy, call.parent); std::move(args_converter).template call<Return, Guard>(cap->f), policy, call.parent);
/* Invoke call policy post-call hook */ /* Invoke call policy post-call hook */
...@@ -713,7 +713,7 @@ protected: ...@@ -713,7 +713,7 @@ protected:
// 6. Call the function. // 6. Call the function.
try { try {
loader_life_support guard{}; loader_life_support guard{}; // HOLDER_SHARED_MAKE_UNIQUE STACK #9
result = func.impl(call); result = func.impl(call);
} catch (reference_cast_error &) { } catch (reference_cast_error &) {
result = PYBIND11_TRY_NEXT_OVERLOAD; result = PYBIND11_TRY_NEXT_OVERLOAD;
...@@ -1472,19 +1472,19 @@ private: ...@@ -1472,19 +1472,19 @@ private:
static void init_holder_from_existing(const detail::value_and_holder &v_h, static void init_holder_from_existing(const detail::value_and_holder &v_h,
const holder_type *holder_ptr, std::true_type /*is_copy_constructible*/) { const holder_type *holder_ptr, std::true_type /*is_copy_constructible*/) {
new (std::addressof(v_h.xxx_holder<holder_type>())) holder_type(*reinterpret_cast<const holder_type *>(holder_ptr)); new (std::addressof(v_h.xxx_holder<holder_type>())) holder_type(*reinterpret_cast<const holder_type *>(holder_ptr)); // HOLDER_SHARED_MAKE_UNIQUE STACK #1
} }
static void init_holder_from_existing(const detail::value_and_holder &v_h, static void init_holder_from_existing(const detail::value_and_holder &v_h,
const holder_type *holder_ptr, std::false_type /*is_copy_constructible*/) { const holder_type *holder_ptr, std::false_type /*is_copy_constructible*/) {
new (std::addressof(v_h.xxx_holder<holder_type>())) holder_type(std::move(*const_cast<holder_type *>(holder_ptr))); new (std::addressof(v_h.xxx_holder<holder_type>())) holder_type(std::move(*const_cast<holder_type *>(holder_ptr))); // DIRTY CONST_CAST
} }
/// Initialize holder object, variant 2: try to construct from existing holder object, if possible /// Initialize holder object, variant 2: try to construct from existing holder object, if possible
static void init_holder(detail::instance *inst, detail::value_and_holder &v_h, static void init_holder(detail::instance *inst, detail::value_and_holder &v_h,
const holder_type *holder_ptr, const void * /* dummy -- not enable_shared_from_this<T>) */) { const holder_type *holder_ptr, const void * /* dummy -- not enable_shared_from_this<T>) */) {
if (holder_ptr) { if (holder_ptr) {
init_holder_from_existing(v_h, holder_ptr, std::is_copy_constructible<holder_type>()); init_holder_from_existing(v_h, holder_ptr, std::is_copy_constructible<holder_type>()); // HOLDER_SHARED_MAKE_UNIQUE STACK #2
v_h.set_holder_constructed(); v_h.set_holder_constructed();
} else if (inst->aaa_owned || detail::always_construct_holder<holder_type>::value) { } else if (inst->aaa_owned || detail::always_construct_holder<holder_type>::value) {
new (std::addressof(v_h.xxx_holder<holder_type>())) holder_type(v_h.xxx_value_ptr<type>()); // init_holder new (std::addressof(v_h.xxx_holder<holder_type>())) holder_type(v_h.xxx_value_ptr<type>()); // init_holder
...@@ -1496,13 +1496,33 @@ private: ...@@ -1496,13 +1496,33 @@ private:
/// instance. Should be called as soon as the `type` value_ptr is set for an instance. Takes an /// instance. Should be called as soon as the `type` value_ptr is set for an instance. Takes an
/// optional pointer to an existing holder to use; if not specified and the instance is /// optional pointer to an existing holder to use; if not specified and the instance is
/// `.aaa_owned`, a new holder will be constructed to manage the value pointer. /// `.aaa_owned`, a new holder will be constructed to manage the value pointer.
static void init_instance(detail::instance *inst, const void *holder_ptr) { static void init_instance(detail::instance *inst, std::any holder_ptr) { // TODODODO std::any
auto v_h = inst->get_value_and_holder(detail::get_type_info(typeid(type))); auto v_h = inst->get_value_and_holder(detail::get_type_info(typeid(type)));
if (!v_h.instance_registered()) { if (!v_h.instance_registered()) {
register_instance(inst, v_h.xxx_value_ptr<void>(), v_h.type); // before init_holder register_instance(inst, v_h.xxx_value_ptr<void>(), v_h.type); // before init_holder
v_h.set_instance_registered(); v_h.set_instance_registered();
} }
init_holder(inst, v_h, (const holder_type *) holder_ptr, v_h.xxx_value_ptr<type>()); // calling init_holder const holder_type *holder_ptr_raw = nullptr;
if (holder_ptr.has_value() && std::any_cast<std::nullptr_t>(&holder_ptr) == nullptr) {
detail::to_cout("before any_cast");
std::string htn(holder_ptr.type().name());
detail::clean_type_id(htn);
detail::to_cout(std::string("holder_ptr.type().name() ") + htn);
detail::to_cout(std::string(" typeid(holder_type *) ") + type_id<holder_type *>());
auto cast_ptr_mutbl = std::any_cast<holder_type *>(&holder_ptr);
if (cast_ptr_mutbl != nullptr) {
holder_ptr_raw = *cast_ptr_mutbl;
} else {
auto cast_ptr_const = std::any_cast<const holder_type *>(&holder_ptr);
if (cast_ptr_const == nullptr) {
detail::to_cout("any_cast mutbl + const failure");
throw std::runtime_error("Incompatible holder types.");
}
holder_ptr_raw = *cast_ptr_const;
}
detail::to_cout("after any_cast");
}
init_holder(inst, v_h, holder_ptr_raw, v_h.xxx_value_ptr<type>()); // calling init_holder // HOLDER_SHARED_MAKE_UNIQUE STACK #3
} }
/// Deallocates an instance; via holder, if constructed; otherwise via operator delete. /// Deallocates an instance; via holder, if constructed; otherwise via operator delete.
......
...@@ -80,6 +80,7 @@ std::vector<std::unique_ptr<Animal>> create_zoo() ...@@ -80,6 +80,7 @@ std::vector<std::unique_ptr<Animal>> create_zoo()
ret.emplace_back(new Chihuahua("Hertzl")); ret.emplace_back(new Chihuahua("Hertzl"));
ret.emplace_back(new Cat("Tiger", Cat::Kind::Cat)); ret.emplace_back(new Cat("Tiger", Cat::Kind::Cat));
ret.emplace_back(new Panther("Leo")); ret.emplace_back(new Panther("Leo"));
py::detail::to_cout("before ret");
return ret; return ret;
} }
......
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