From 8f3e045deb6d504e202d9e57318b6c5d8fd691e6 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Sun, 23 Oct 2016 15:43:03 +0100 Subject: [PATCH] Use detail::get_type_info() wherever sensible This reduces direct access to internals.registered_types_cpp to just a few places. --- include/pybind11/cast.h | 5 +++-- include/pybind11/pybind11.h | 25 +++++++++---------------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 1b82d44f4..86a2f4ad1 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -91,7 +91,8 @@ PYBIND11_NOINLINE inline detail::type_info* get_type_info(PyTypeObject *type) { } 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 it = types.find(std::type_index(tp)); @@ -158,7 +159,7 @@ inline void keep_alive_impl(handle nurse, handle patient); class type_caster_generic { public: 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) { if (!src) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index ffa25801b..68287e05a 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -180,8 +180,6 @@ protected: a.descr = strdup(a.value.attr("__repr__")().cast().c_str()); } - auto const ®istered_types = detail::get_internals().registered_types_cpp; - /* Generate a proper function signature */ std::string signature; 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++]; if (!t) pybind11_fail("Internal error while parsing type signature (1)"); - auto it = registered_types.find(std::type_index(*t)); - if (it != registered_types.end()) { - signature += ((const detail::type_info *) it->second)->type->tp_name; + if (auto tinfo = detail::get_type_info(*t)) { + signature += tinfo->type->tp_name; } else { std::string tname(t->name()); detail::clean_type_id(tname); @@ -610,8 +607,7 @@ protected: auto &internals = get_internals(); auto tindex = std::type_index(*(rec->type)); - if (internals.registered_types_cpp.find(tindex) != - internals.registered_types_cpp.end()) + if (get_type_info(*(rec->type))) pybind11_fail("generic_type: type \"" + std::string(rec->name) + "\" is already registered!"); @@ -1334,11 +1330,11 @@ template void implicitly_convertible() PyErr_Clear(); return result; }; - auto ®istered_types = detail::get_internals().registered_types_cpp; - auto it = registered_types.find(std::type_index(typeid(OutputType))); - if (it == registered_types.end()) + + if (auto tinfo = detail::get_type_info(typeid(OutputType))) + tinfo->implicit_conversions.push_back(implicit_caster); + else pybind11_fail("implicitly_convertible: Unable to find type " + type_id()); - ((detail::type_info *) it->second)->implicit_conversions.push_back(implicit_caster); } template @@ -1590,11 +1586,8 @@ inline function get_type_overload(const void *this_ptr, const detail::type_info } template function get_overload(const T *this_ptr, const char *name) { - auto &cpp_types = detail::get_internals().registered_types_cpp; - auto it = cpp_types.find(typeid(T)); - if (it == cpp_types.end()) - return function(); - return get_type_overload(this_ptr, (const detail::type_info *) it->second, name); + auto tinfo = detail::get_type_info(typeid(T)); + return tinfo ? get_type_overload(this_ptr, tinfo, name) : function(); } #define PYBIND11_OVERLOAD_INT(ret_type, cname, name, ...) { \