Commit c275ee6b by Ivan Smirnov

Add support for "direct" converters

parent ba08db4d
......@@ -157,7 +157,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, false)), tindex(type_info) { }
PYBIND11_NOINLINE bool load(handle src, bool convert) {
if (!src)
......@@ -208,13 +208,21 @@ public:
}
}
/* Perform an implicit conversion */
/* Perform an implicit or a direct conversion */
if (convert) {
for (auto &converter : typeinfo->implicit_conversions) {
temp = object(converter(src.ptr(), typeinfo->type), false);
if (load(temp, false))
return true;
}
auto& direct = get_internals().direct_conversions;
auto it = direct.find(tindex);
if (it != direct.end()) {
for (auto& converter : it->second) {
if (converter(src.ptr(), value))
return true;
}
}
}
return false;
}
......@@ -294,6 +302,7 @@ public:
protected:
const type_info *typeinfo = nullptr;
std::type_index tindex;
void *value = nullptr;
object temp;
};
......
......@@ -321,6 +321,7 @@ struct internals {
std::unordered_map<const void *, void*> registered_types_py; // PyTypeObject* -> type_info
std::unordered_multimap<const void *, void*> registered_instances; // void * -> PyObject*
std::unordered_set<std::pair<const PyObject *, const char *>, overload_hash> inactive_overload_cache;
std::unordered_map<std::type_index, std::vector<bool (*)(PyObject *, void *&)>> direct_conversions;
std::forward_list<void (*) (std::exception_ptr)> registered_exception_translators;
#if defined(WITH_THREAD)
decltype(PyThread_create_key()) tstate = 0; // Usually an int but a long on Cygwin64 with Python 3.x
......
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