pybind11/include/pybind11
oremanj fd9bc8f54d Add basic support for tag-based static polymorphism (#1326)
* Add basic support for tag-based static polymorphism

Sometimes it is possible to look at a C++ object and know what its dynamic type is,
even if it doesn't use C++ polymorphism, because instances of the object and its
subclasses conform to some other mechanism for being self-describing; for example,
perhaps there's an enumerated "tag" or "kind" member in the base class that's always
set to an indication of the correct type. This might be done for performance reasons,
or to permit most-derived types to be trivially copyable. One of the most widely-known
examples is in LLVM: https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html

This PR permits pybind11 to be informed of such conventions via a new specializable
detail::polymorphic_type_hook<> template, which generalizes the previous logic for
determining the runtime type of an object based on C++ RTTI. Implementors provide
a way to map from a base class object to a const std::type_info* for the dynamic
type; pybind11 then uses this to ensure that casting a Base* to Python creates a
Python object that knows it's wrapping the appropriate sort of Derived.

There are a number of restrictions with this tag-based static polymorphism support
compared to pybind11's existing support for built-in C++ polymorphism:

- there is no support for this-pointer adjustment, so only single inheritance is permitted
- there is no way to make C++ code call new Python-provided subclasses
- when binding C++ classes that redefine a method in a subclass, the .def() must be
  repeated in the binding for Python to know about the update

But these are not much of an issue in practice in many cases, the impact on the
complexity of pybind11's innards is minimal and localized, and the support for
automatic downcasting improves usability a great deal.
2018-04-14 02:13:10 +02:00
..
detail Reimplement version check and combine init macros 2018-03-11 11:10:06 -03:00
attr.h Reduce binary size overhead of new-style constructors 2017-08-28 16:08:53 +02:00
buffer_info.h Move internal headers into detail subdirectory 2017-08-17 04:06:35 +02:00
cast.h Add basic support for tag-based static polymorphism (#1326) 2018-04-14 02:13:10 +02:00
chrono.h Force hidden visibility on pybind code 2017-08-14 11:40:38 -04:00
common.h Add a dummy common.h header with a deprecation warning 2017-09-06 15:22:26 +02:00
complex.h Add -Wdeprecated to test suite and fix associated warnings (#1191) 2017-11-22 17:37:41 -04:00
eigen.h Silence new MSVC C++17 deprecation warnings 2017-12-23 09:00:45 -04:00
embed.h Fix segfault when reloading interpreter with external modules (#1092) 2018-01-11 19:46:10 -04:00
eval.h Force hidden visibility on pybind code 2017-08-14 11:40:38 -04:00
functional.h Simplify function signature annotation and parsing 2017-09-16 12:02:49 +02:00
iostream.h Utility for redirecting C++ streams to Python (#1009) 2017-08-25 02:12:43 +02:00
numpy.h MSVC workaround for broken using detail::_ warning 2018-01-12 12:37:54 -04:00
operators.h Access C++ hash functions from Python and vice versa (#1034) 2017-08-30 14:22:00 +02:00
options.h Move internal headers into detail subdirectory 2017-08-17 04:06:35 +02:00
pybind11.h Implement an enum_ property "name" 2018-04-07 19:11:35 -03:00
pytypes.h Add -Wdeprecated to test suite and fix associated warnings (#1191) 2017-11-22 17:37:41 -04:00
stl_bind.h fix return from std::map bindings to __delitem__ (#1229) 2018-01-11 19:43:37 -04:00
stl.h Fix pybind11 interoperability with Clang trunk (#1269) 2018-02-06 15:40:50 +01:00