|
12 | 12 |
|
13 | 13 | #include "cast.h"
|
14 | 14 |
|
| 15 | +#include <functional> |
| 16 | + |
15 | 17 | PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
16 | 18 |
|
17 | 19 | /// \addtogroup annotations
|
@@ -79,6 +81,23 @@ struct metaclass {
|
79 | 81 | explicit metaclass(handle value) : value(value) { }
|
80 | 82 | };
|
81 | 83 |
|
| 84 | +/// Specifies a custom callback with signature `void (PyHeapTypeObject*)` that |
| 85 | +/// may be used to customize the Python type. |
| 86 | +/// |
| 87 | +/// The callback is invoked immediately before `PyType_Ready`. |
| 88 | +/// |
| 89 | +/// Note: This is an advanced interface, and uses of it may require changes to |
| 90 | +/// work with later versions of pybind11. You may wish to consult the |
| 91 | +/// implementation of `make_new_python_type` in `detail/classes.h` to understand |
| 92 | +/// the context in which the callback will be run. |
| 93 | +struct custom_type_setup { |
| 94 | + using callback = std::function<void(PyHeapTypeObject *heap_type)>; |
| 95 | + |
| 96 | + explicit custom_type_setup(callback value) : value(std::move(value)) {} |
| 97 | + |
| 98 | + callback value; |
| 99 | +}; |
| 100 | + |
82 | 101 | /// Annotation that marks a class as local to the module:
|
83 | 102 | struct module_local { const bool value;
|
84 | 103 | constexpr explicit module_local(bool v = true) : value(v) {}
|
@@ -272,6 +291,9 @@ struct type_record {
|
272 | 291 | /// Custom metaclass (optional)
|
273 | 292 | handle metaclass;
|
274 | 293 |
|
| 294 | + /// Custom type setup. |
| 295 | + custom_type_setup::callback custom_type_setup_callback; |
| 296 | + |
275 | 297 | /// Multiple inheritance marker
|
276 | 298 | bool multiple_inheritance : 1;
|
277 | 299 |
|
@@ -476,6 +498,13 @@ struct process_attribute<dynamic_attr> : process_attribute_default<dynamic_attr>
|
476 | 498 | static void init(const dynamic_attr &, type_record *r) { r->dynamic_attr = true; }
|
477 | 499 | };
|
478 | 500 |
|
| 501 | +template <> |
| 502 | +struct process_attribute<custom_type_setup> { |
| 503 | + static void init(const custom_type_setup &value, type_record *r) { |
| 504 | + r->custom_type_setup_callback = value.value; |
| 505 | + } |
| 506 | +}; |
| 507 | + |
479 | 508 | template <>
|
480 | 509 | struct process_attribute<is_final> : process_attribute_default<is_final> {
|
481 | 510 | static void init(const is_final &, type_record *r) { r->is_final = true; }
|
|
0 commit comments