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:
Ivan Smirnov 2016-10-23 15:43:03 +01:00
parent a6e6a8b108
commit 8f3e045deb
2 changed files with 12 additions and 18 deletions

View File

@ -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)

View File

@ -180,8 +180,6 @@ protected:
a.descr = strdup(a.value.attr("__repr__")().cast<std::string>().c_str());
}
auto const &registered_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 <typename InputType, typename OutputType> void implicitly_convertible()
PyErr_Clear();
return result;
};
auto &registered_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<OutputType>());
((detail::type_info *) it->second)->implicit_conversions.push_back(implicit_caster);
}
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) {
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, ...) { \