mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 05:05:11 +00:00
return best representation of polymorphic types (fixes #105)
This commit is contained in:
parent
d40885a1e6
commit
d7efa4ff7b
@ -121,6 +121,7 @@ set(PYBIND11_EXAMPLES
|
|||||||
example/example13.cpp
|
example/example13.cpp
|
||||||
example/example14.cpp
|
example/example14.cpp
|
||||||
example/example15.cpp
|
example/example15.cpp
|
||||||
|
example/example16.cpp
|
||||||
example/issues.cpp
|
example/issues.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -5,7 +5,8 @@ Changelog
|
|||||||
|
|
||||||
1.5 (not yet released)
|
1.5 (not yet released)
|
||||||
----------------------
|
----------------------
|
||||||
* Pickling support
|
* For polymorphic types, use RTTI to try to return the closest type registered with pybind11.
|
||||||
|
* Pickling support for serializing and unserializing C++ instances to a byte stream in Python
|
||||||
* Added a variadic ``make_tuple()`` function
|
* Added a variadic ``make_tuple()`` function
|
||||||
* Address a rare issue that could confuse the current virtual function dispatcher
|
* Address a rare issue that could confuse the current virtual function dispatcher
|
||||||
* Documentation improvements: import issues, symbol visibility, pickling, limitations
|
* Documentation improvements: import issues, symbol visibility, pickling, limitations
|
||||||
|
@ -24,6 +24,7 @@ void init_ex12(py::module &);
|
|||||||
void init_ex13(py::module &);
|
void init_ex13(py::module &);
|
||||||
void init_ex14(py::module &);
|
void init_ex14(py::module &);
|
||||||
void init_ex15(py::module &);
|
void init_ex15(py::module &);
|
||||||
|
void init_ex16(py::module &);
|
||||||
void init_issues(py::module &);
|
void init_issues(py::module &);
|
||||||
|
|
||||||
PYBIND11_PLUGIN(example) {
|
PYBIND11_PLUGIN(example) {
|
||||||
@ -44,6 +45,7 @@ PYBIND11_PLUGIN(example) {
|
|||||||
init_ex13(m);
|
init_ex13(m);
|
||||||
init_ex14(m);
|
init_ex14(m);
|
||||||
init_ex15(m);
|
init_ex15(m);
|
||||||
|
init_ex16(m);
|
||||||
init_issues(m);
|
init_issues(m);
|
||||||
|
|
||||||
return m.ptr();
|
return m.ptr();
|
||||||
|
24
example/example16.cpp
Normal file
24
example/example16.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
example/example16.cpp -- automatic upcasting for polymorphic types
|
||||||
|
|
||||||
|
Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
|
||||||
|
|
||||||
|
All rights reserved. Use of this source code is governed by a
|
||||||
|
BSD-style license that can be found in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "example.h"
|
||||||
|
|
||||||
|
struct BaseClass { virtual ~BaseClass() {} };
|
||||||
|
struct DerivedClass1 : BaseClass { };
|
||||||
|
struct DerivedClass2 : BaseClass { };
|
||||||
|
|
||||||
|
void init_ex16(py::module &m) {
|
||||||
|
py::class_<BaseClass>(m, "BaseClass").def(py::init<>());
|
||||||
|
py::class_<DerivedClass1>(m, "DerivedClass1").def(py::init<>());
|
||||||
|
py::class_<DerivedClass2>(m, "DerivedClass2").def(py::init<>());
|
||||||
|
|
||||||
|
m.def("return_class_1", []() -> BaseClass* { return new DerivedClass1(); });
|
||||||
|
m.def("return_class_2", []() -> BaseClass* { return new DerivedClass2(); });
|
||||||
|
m.def("return_none", []() -> BaseClass* { return nullptr; });
|
||||||
|
}
|
12
example/example16.py
Normal file
12
example/example16.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
from __future__ import print_function
|
||||||
|
import sys
|
||||||
|
|
||||||
|
sys.path.append('.')
|
||||||
|
|
||||||
|
from example import return_class_1
|
||||||
|
from example import return_class_2
|
||||||
|
from example import return_none
|
||||||
|
|
||||||
|
print(type(return_class_1()))
|
||||||
|
print(type(return_class_2()))
|
||||||
|
print(type(return_none()))
|
3
example/example16.ref
Normal file
3
example/example16.ref
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<class 'example.DerivedClass1'>
|
||||||
|
<class 'example.DerivedClass2'>
|
||||||
|
<type 'NoneType'>
|
@ -137,6 +137,7 @@ public:
|
|||||||
|
|
||||||
PYBIND11_NOINLINE static handle cast(const void *_src, return_value_policy policy, handle parent,
|
PYBIND11_NOINLINE static handle cast(const void *_src, return_value_policy policy, handle parent,
|
||||||
const std::type_info *type_info,
|
const std::type_info *type_info,
|
||||||
|
const std::type_info *type_info_backup,
|
||||||
void *(*copy_constructor)(const void *),
|
void *(*copy_constructor)(const void *),
|
||||||
const void *existing_holder = nullptr) {
|
const void *existing_holder = nullptr) {
|
||||||
void *src = const_cast<void *>(_src);
|
void *src = const_cast<void *>(_src);
|
||||||
@ -153,6 +154,11 @@ public:
|
|||||||
return handle((PyObject *) it_instance->second).inc_ref();
|
return handle((PyObject *) it_instance->second).inc_ref();
|
||||||
|
|
||||||
auto it = internals.registered_types_cpp.find(std::type_index(*type_info));
|
auto it = internals.registered_types_cpp.find(std::type_index(*type_info));
|
||||||
|
if (it == internals.registered_types_cpp.end()) {
|
||||||
|
type_info = type_info_backup;
|
||||||
|
it = internals.registered_types_cpp.find(std::type_index(*type_info));
|
||||||
|
}
|
||||||
|
|
||||||
if (it == internals.registered_types_cpp.end()) {
|
if (it == internals.registered_types_cpp.end()) {
|
||||||
std::string tname = type_info->name();
|
std::string tname = type_info->name();
|
||||||
detail::clean_type_id(tname);
|
detail::clean_type_id(tname);
|
||||||
@ -213,11 +219,11 @@ public:
|
|||||||
static handle cast(const type &src, return_value_policy policy, handle parent) {
|
static handle cast(const type &src, return_value_policy policy, handle parent) {
|
||||||
if (policy == return_value_policy::automatic)
|
if (policy == return_value_policy::automatic)
|
||||||
policy = return_value_policy::copy;
|
policy = return_value_policy::copy;
|
||||||
return type_caster_generic::cast(&src, policy, parent, &typeid(type), ©_constructor);
|
return cast(&src, policy, parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
static handle cast(const type *src, return_value_policy policy, handle parent) {
|
static handle cast(const type *src, return_value_policy policy, handle parent) {
|
||||||
return type_caster_generic::cast(src, policy, parent, &typeid(type), ©_constructor);
|
return type_caster_generic::cast(src, policy, parent, src ? &typeid(*src) : nullptr, &typeid(type), ©_constructor);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> using cast_op_type = pybind11::detail::cast_op_type<T>;
|
template <typename T> using cast_op_type = pybind11::detail::cast_op_type<T>;
|
||||||
@ -664,7 +670,9 @@ public:
|
|||||||
|
|
||||||
static handle cast(const holder_type &src, return_value_policy policy, handle parent) {
|
static handle cast(const holder_type &src, return_value_policy policy, handle parent) {
|
||||||
return type_caster_generic::cast(
|
return type_caster_generic::cast(
|
||||||
src.get(), policy, parent, &typeid(type), ©_constructor, &src);
|
src.get(), policy, parent,
|
||||||
|
src.get() ? &typeid(*src.get()) : nullptr, &typeid(type),
|
||||||
|
©_constructor, &src);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
Loading…
Reference in New Issue
Block a user