mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-11 08:03:55 +00:00
Use detail::get_type_info() wherever sensible
This reduces direct access to internals.registered_types_cpp to just a few places.
This commit is contained in:
parent
a6e6a8b108
commit
8f3e045deb
@ -91,7 +91,8 @@ PYBIND11_NOINLINE inline detail::type_info* get_type_info(PyTypeObject *type) {
|
|||||||
} while (true);
|
} while (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
PYBIND11_NOINLINE inline detail::type_info *get_type_info(const std::type_info &tp, bool throw_if_missing) {
|
PYBIND11_NOINLINE inline detail::type_info *get_type_info(const std::type_info &tp,
|
||||||
|
bool throw_if_missing = false) {
|
||||||
auto &types = get_internals().registered_types_cpp;
|
auto &types = get_internals().registered_types_cpp;
|
||||||
|
|
||||||
auto it = types.find(std::type_index(tp));
|
auto it = types.find(std::type_index(tp));
|
||||||
@ -158,7 +159,7 @@ inline void keep_alive_impl(handle nurse, handle patient);
|
|||||||
class type_caster_generic {
|
class type_caster_generic {
|
||||||
public:
|
public:
|
||||||
PYBIND11_NOINLINE type_caster_generic(const std::type_info &type_info)
|
PYBIND11_NOINLINE type_caster_generic(const std::type_info &type_info)
|
||||||
: typeinfo(get_type_info(type_info, false)) { }
|
: typeinfo(get_type_info(type_info)) { }
|
||||||
|
|
||||||
PYBIND11_NOINLINE bool load(handle src, bool convert) {
|
PYBIND11_NOINLINE bool load(handle src, bool convert) {
|
||||||
if (!src)
|
if (!src)
|
||||||
|
@ -180,8 +180,6 @@ protected:
|
|||||||
a.descr = strdup(a.value.attr("__repr__")().cast<std::string>().c_str());
|
a.descr = strdup(a.value.attr("__repr__")().cast<std::string>().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto const ®istered_types = detail::get_internals().registered_types_cpp;
|
|
||||||
|
|
||||||
/* Generate a proper function signature */
|
/* Generate a proper function signature */
|
||||||
std::string signature;
|
std::string signature;
|
||||||
size_t type_depth = 0, char_index = 0, type_index = 0, arg_index = 0;
|
size_t type_depth = 0, char_index = 0, type_index = 0, arg_index = 0;
|
||||||
@ -216,9 +214,8 @@ protected:
|
|||||||
const std::type_info *t = types[type_index++];
|
const std::type_info *t = types[type_index++];
|
||||||
if (!t)
|
if (!t)
|
||||||
pybind11_fail("Internal error while parsing type signature (1)");
|
pybind11_fail("Internal error while parsing type signature (1)");
|
||||||
auto it = registered_types.find(std::type_index(*t));
|
if (auto tinfo = detail::get_type_info(*t)) {
|
||||||
if (it != registered_types.end()) {
|
signature += tinfo->type->tp_name;
|
||||||
signature += ((const detail::type_info *) it->second)->type->tp_name;
|
|
||||||
} else {
|
} else {
|
||||||
std::string tname(t->name());
|
std::string tname(t->name());
|
||||||
detail::clean_type_id(tname);
|
detail::clean_type_id(tname);
|
||||||
@ -610,8 +607,7 @@ protected:
|
|||||||
auto &internals = get_internals();
|
auto &internals = get_internals();
|
||||||
auto tindex = std::type_index(*(rec->type));
|
auto tindex = std::type_index(*(rec->type));
|
||||||
|
|
||||||
if (internals.registered_types_cpp.find(tindex) !=
|
if (get_type_info(*(rec->type)))
|
||||||
internals.registered_types_cpp.end())
|
|
||||||
pybind11_fail("generic_type: type \"" + std::string(rec->name) +
|
pybind11_fail("generic_type: type \"" + std::string(rec->name) +
|
||||||
"\" is already registered!");
|
"\" is already registered!");
|
||||||
|
|
||||||
@ -1334,11 +1330,11 @@ template <typename InputType, typename OutputType> void implicitly_convertible()
|
|||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
auto ®istered_types = detail::get_internals().registered_types_cpp;
|
|
||||||
auto it = registered_types.find(std::type_index(typeid(OutputType)));
|
if (auto tinfo = detail::get_type_info(typeid(OutputType)))
|
||||||
if (it == registered_types.end())
|
tinfo->implicit_conversions.push_back(implicit_caster);
|
||||||
|
else
|
||||||
pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>());
|
pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>());
|
||||||
((detail::type_info *) it->second)->implicit_conversions.push_back(implicit_caster);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ExceptionTranslator>
|
template <typename ExceptionTranslator>
|
||||||
@ -1590,11 +1586,8 @@ inline function get_type_overload(const void *this_ptr, const detail::type_info
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class T> function get_overload(const T *this_ptr, const char *name) {
|
template <class T> function get_overload(const T *this_ptr, const char *name) {
|
||||||
auto &cpp_types = detail::get_internals().registered_types_cpp;
|
auto tinfo = detail::get_type_info(typeid(T));
|
||||||
auto it = cpp_types.find(typeid(T));
|
return tinfo ? get_type_overload(this_ptr, tinfo, name) : function();
|
||||||
if (it == cpp_types.end())
|
|
||||||
return function();
|
|
||||||
return get_type_overload(this_ptr, (const detail::type_info *) it->second, name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) { \
|
#define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) { \
|
||||||
|
Loading…
Reference in New Issue
Block a user