Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1 | /* |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 2 | pybind11/pybind11.h: Main header file of the C++11 python |
| 3 | binding generator library |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 4 | |
| 5 | Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch> |
| 6 | |
| 7 | All rights reserved. Use of this source code is governed by a |
| 8 | BSD-style license that can be found in the LICENSE file. |
| 9 | */ |
| 10 | |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 11 | #pragma once |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 12 | |
| 13 | #if defined(_MSC_VER) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 14 | # pragma warning(push) |
| 15 | # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant |
| 16 | # pragma warning(disable: 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning) |
| 17 | # pragma warning(disable: 4996) // warning C4996: The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name |
| 18 | # pragma warning(disable: 4100) // warning C4100: Unreferenced formal parameter |
| 19 | # pragma warning(disable: 4512) // warning C4512: Assignment operator was implicitly defined as deleted |
Wenzel Jakob | cd5cda7 | 2015-08-03 12:17:54 +0200 | [diff] [blame] | 20 | #elif defined(__GNUG__) and !defined(__clang__) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 21 | # pragma GCC diagnostic push |
| 22 | # pragma GCC diagnostic ignored "-Wunused-but-set-parameter" |
| 23 | # pragma GCC diagnostic ignored "-Wunused-but-set-variable" |
| 24 | # pragma GCC diagnostic ignored "-Wmissing-field-initializers" |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 25 | #endif |
| 26 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 27 | #include "attr.h" |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 28 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 29 | NAMESPACE_BEGIN(pybind11) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 30 | |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 31 | /// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 32 | class cpp_function : public function { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 33 | protected: |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 34 | /// Picks a suitable return value converter from cast.h |
| 35 | template <typename T> using return_value_caster = |
| 36 | detail::type_caster<typename std::conditional< |
Wenzel Jakob | 4177ed4 | 2016-01-17 22:36:38 +0100 | [diff] [blame] | 37 | std::is_void<T>::value, detail::void_type, typename detail::intrinsic_type<T>::type>::type>; |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 38 | |
| 39 | /// Picks a suitable argument value converter from cast.h |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 40 | template <typename... T> using arg_value_caster = |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 41 | detail::type_caster<typename std::tuple<T...>>; |
| 42 | public: |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 43 | cpp_function() { } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 44 | |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 45 | /// Vanilla function pointers |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 46 | template <typename Return, typename... Args, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 47 | cpp_function(Return (*f)(Args...), const Extra&... extra) { |
| 48 | auto rec = new detail::function_record(); |
| 49 | rec->data = (void *) f; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 50 | |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 51 | typedef arg_value_caster<Args...> cast_in; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 52 | typedef return_value_caster<Return> cast_out; |
| 53 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 54 | /* Dispatch code which converts function arguments and performs the actual function call */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 55 | rec->impl = [](detail::function_record *rec, handle pyArgs, handle parent) -> handle { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 56 | cast_in args; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 57 | |
| 58 | /* Try to cast the function arguments into the C++ domain */ |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 59 | if (!args.load(pyArgs, true)) |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 60 | return PYBIND11_TRY_NEXT_OVERLOAD; |
| 61 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 62 | /* Invoke call policy pre-call hook */ |
| 63 | detail::process_attributes<Extra...>::precall(pyArgs); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 64 | |
| 65 | /* Do the call and convert the return value back into the Python domain */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 66 | handle result = cast_out::cast( |
| 67 | args.template call<Return>((Return (*) (Args...)) rec->data), |
| 68 | rec->policy, parent); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 69 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 70 | /* Invoke call policy post-call hook */ |
| 71 | detail::process_attributes<Extra...>::postcall(pyArgs, result); |
| 72 | |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 73 | return result; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 74 | }; |
| 75 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 76 | /* Process any user-provided function attributes */ |
| 77 | detail::process_attributes<Extra...>::init(extra..., rec); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 78 | |
| 79 | /* Generate a readable signature describing the function's arguments and return value types */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 80 | using detail::descr; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 81 | PYBIND11_DESCR signature = cast_in::name() + detail::_(" -> ") + cast_out::name(); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 82 | |
| 83 | /* Register the function with Python from generic (non-templated) code */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 84 | initialize(rec, signature.text(), signature.types(), sizeof...(Args)); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 85 | } |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 86 | |
| 87 | /// Delegating helper constructor to deal with lambda functions |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 88 | template <typename Func, typename... Extra> cpp_function(Func &&f, const Extra&... extra) { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 89 | initialize(std::forward<Func>(f), |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 90 | (typename detail::remove_class<decltype( |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 91 | &std::remove_reference<Func>::type::operator())>::type *) nullptr, extra...); |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 94 | /// Delegating helper constructor to deal with class methods (non-const) |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 95 | template <typename Return, typename Class, typename... Arg, typename... Extra> cpp_function( |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 96 | Return (Class::*f)(Arg...), const Extra&... extra) { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 97 | initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); }, |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 98 | (Return (*) (Class *, Arg...)) nullptr, extra...); |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 101 | /// Delegating helper constructor to deal with class methods (const) |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 102 | template <typename Return, typename Class, typename... Arg, typename... Extra> cpp_function( |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 103 | Return (Class::*f)(Arg...) const, const Extra&... extra) { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 104 | initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); }, |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 105 | (Return (*)(const Class *, Arg ...)) nullptr, extra...); |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 108 | /// Return the function name |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 109 | object name() const { return attr("__name__"); } |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 110 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 111 | protected: |
| 112 | /// Special internal constructor for functors, lambda functions, etc. |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 113 | template <typename Func, typename Return, typename... Args, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 114 | void initialize(Func &&f, Return (*)(Args...), const Extra&... extra) { |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 115 | struct capture { typename std::remove_reference<Func>::type f; }; |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 116 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 117 | /* Store the function including any extra state it might have (e.g. a lambda capture object) */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 118 | auto rec = new detail::function_record(); |
| 119 | rec->data = new capture { std::forward<Func>(f) }; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 120 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 121 | /* Create a cleanup handler, but only if we have to (less generated code) */ |
Wenzel Jakob | 19208fe | 2015-10-13 17:37:25 +0200 | [diff] [blame] | 122 | if (!std::is_trivially_destructible<Func>::value) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 123 | rec->free_data = [](void *ptr) { delete (capture *) ptr; }; |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 124 | else |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 125 | rec->free_data = operator delete; |
Wenzel Jakob | 19208fe | 2015-10-13 17:37:25 +0200 | [diff] [blame] | 126 | |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 127 | typedef arg_value_caster<Args...> cast_in; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 128 | typedef return_value_caster<Return> cast_out; |
| 129 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 130 | /* Dispatch code which converts function arguments and performs the actual function call */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 131 | rec->impl = [](detail::function_record *rec, handle pyArgs, handle parent) -> handle { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 132 | cast_in args; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 133 | |
| 134 | /* Try to cast the function arguments into the C++ domain */ |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 135 | if (!args.load(pyArgs, true)) |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 136 | return PYBIND11_TRY_NEXT_OVERLOAD; |
| 137 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 138 | /* Invoke call policy pre-call hook */ |
| 139 | detail::process_attributes<Extra...>::precall(pyArgs); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 140 | |
| 141 | /* Do the call and convert the return value back into the Python domain */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 142 | handle result = cast_out::cast( |
| 143 | args.template call<Return>(((capture *) rec->data)->f), |
| 144 | rec->policy, parent); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 145 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 146 | /* Invoke call policy post-call hook */ |
| 147 | detail::process_attributes<Extra...>::postcall(pyArgs, result); |
| 148 | |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 149 | return result; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 150 | }; |
| 151 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 152 | /* Process any user-provided function attributes */ |
| 153 | detail::process_attributes<Extra...>::init(extra..., rec); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 154 | |
| 155 | /* Generate a readable signature describing the function's arguments and return value types */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 156 | using detail::descr; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 157 | PYBIND11_DESCR signature = cast_in::name() + detail::_(" -> ") + cast_out::name(); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 158 | |
| 159 | /* Register the function with Python from generic (non-templated) code */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 160 | initialize(rec, signature.text(), signature.types(), sizeof...(Args)); |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 163 | /// Register a function call with Python (generic non-templated code goes here) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 164 | void initialize(detail::function_record *rec, const char *text, |
| 165 | const std::type_info *const *types, int args) { |
| 166 | |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 167 | /* Create copies of all referenced C-style strings */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 168 | rec->name = strdup(rec->name ? rec->name : ""); |
| 169 | if (rec->doc) rec->doc = strdup(rec->doc); |
| 170 | for (auto &a: rec->args) { |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 171 | if (a.name) |
| 172 | a.name = strdup(a.name); |
| 173 | if (a.descr) |
| 174 | a.descr = strdup(a.descr); |
| 175 | else if (a.value) |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 176 | a.descr = strdup(((std::string) ((object) handle(a.value).attr("__repr__")).call().str()).c_str()); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 177 | } |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 178 | auto const ®istered_types = detail::get_internals().registered_types_cpp; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 179 | |
| 180 | /* Generate a proper function signature */ |
| 181 | std::string signature; |
| 182 | size_t type_depth = 0, char_index = 0, type_index = 0, arg_index = 0; |
| 183 | while (true) { |
| 184 | char c = text[char_index++]; |
| 185 | if (c == '\0') |
| 186 | break; |
| 187 | |
| 188 | if (c == '{') { |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 189 | if (type_depth == 1 && arg_index < rec->args.size()) { |
| 190 | signature += rec->args[arg_index].name; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 191 | signature += " : "; |
| 192 | } |
| 193 | ++type_depth; |
| 194 | } else if (c == '}') { |
| 195 | --type_depth; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 196 | if (type_depth == 1 && arg_index < rec->args.size()) { |
| 197 | if (rec->args[arg_index].descr) { |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 198 | signature += " = "; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 199 | signature += rec->args[arg_index].descr; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 200 | } |
| 201 | arg_index++; |
| 202 | } |
| 203 | } else if (c == '%') { |
| 204 | const std::type_info *t = types[type_index++]; |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 205 | if (!t) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 206 | pybind11_fail("Internal error while parsing type signature (1)"); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 207 | auto it = registered_types.find(t); |
| 208 | if (it != registered_types.end()) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 209 | signature += ((const detail::type_info *) it->second)->type->tp_name; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 210 | } else { |
| 211 | std::string tname(t->name()); |
| 212 | detail::clean_type_id(tname); |
| 213 | signature += tname; |
| 214 | } |
| 215 | } else { |
| 216 | signature += c; |
| 217 | } |
| 218 | } |
Wenzel Jakob | 95d1869 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 219 | if (type_depth != 0 || types[type_index] != nullptr) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 220 | pybind11_fail("Internal error while parsing type signature (2)"); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 221 | |
| 222 | #if !defined(PYBIND11_CPP14) |
| 223 | delete[] types; |
| 224 | delete[] text; |
| 225 | #endif |
Wenzel Jakob | f5fae92 | 2015-08-24 15:31:24 +0200 | [diff] [blame] | 226 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 227 | #if PY_MAJOR_VERSION < 3 |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 228 | if (strcmp(rec->name, "__next__") == 0) { |
| 229 | std::free(rec->name); |
| 230 | rec->name = strdup("next"); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 231 | } |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 232 | #endif |
| 233 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 234 | if (!rec->args.empty() && (int) rec->args.size() != args) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 235 | pybind11_fail( |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 236 | "cpp_function(): function \"" + std::string(rec->name) + "\" takes " + |
| 237 | std::to_string(args) + " arguments, but " + std::to_string(rec->args.size()) + |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 238 | " pybind11::arg entries were specified!"); |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 239 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 240 | rec->is_constructor = !strcmp(rec->name, "__init__"); |
| 241 | rec->signature = strdup(signature.c_str()); |
| 242 | rec->args.shrink_to_fit(); |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 243 | |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 244 | #if PY_MAJOR_VERSION < 3 |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 245 | if (rec->sibling && PyMethod_Check(rec->sibling.ptr())) |
| 246 | rec->sibling = PyMethod_GET_FUNCTION(rec->sibling.ptr()); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 247 | #endif |
| 248 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 249 | detail::function_record *chain = nullptr, *chain_start = rec; |
| 250 | if (rec->sibling && PyCFunction_Check(rec->sibling.ptr())) { |
| 251 | capsule rec_capsule(PyCFunction_GetSelf(rec->sibling.ptr()), true); |
| 252 | chain = (detail::function_record *) rec_capsule; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 253 | /* Never append a method to an overload chain of a parent class; |
| 254 | instead, hide the parent's overloads in this case */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 255 | if (chain->class_ != rec->class_) |
| 256 | chain = nullptr; |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 259 | if (!chain) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 260 | /* No existing overload was found, create a new function object */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 261 | rec->def = new PyMethodDef(); |
| 262 | memset(rec->def, 0, sizeof(PyMethodDef)); |
| 263 | rec->def->ml_name = rec->name; |
| 264 | rec->def->ml_meth = reinterpret_cast<PyCFunction>(*dispatcher); |
| 265 | rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS; |
| 266 | capsule rec_capsule(rec, [](PyObject *o) { |
| 267 | destruct((detail::function_record *) PyCapsule_GetPointer(o, nullptr)); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 268 | }); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 269 | m_ptr = PyCFunction_New(rec->def, rec_capsule.ptr()); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 270 | if (!m_ptr) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 271 | pybind11_fail("cpp_function::cpp_function(): Could not allocate function object"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 272 | } else { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 273 | /* Append at the end of the overload chain */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 274 | m_ptr = rec->sibling.ptr(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 275 | inc_ref(); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 276 | chain_start = chain; |
| 277 | while (chain->next) |
| 278 | chain = chain->next; |
| 279 | chain->next = rec; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 280 | } |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 281 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 282 | std::string signatures; |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 283 | int index = 0; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 284 | /* Create a nice pydoc rec including all signatures and |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 285 | docstrings of the functions in the overload chain */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 286 | for (auto it = chain_start; it != nullptr; it = it->next) { |
| 287 | if (chain) |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 288 | signatures += std::to_string(++index) + ". "; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 289 | signatures += "Signature : "; |
| 290 | signatures += it->signature; |
| 291 | signatures += "\n"; |
| 292 | if (it->doc && strlen(it->doc) > 0) { |
| 293 | signatures += "\n"; |
| 294 | signatures += it->doc; |
| 295 | signatures += "\n"; |
| 296 | } |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 297 | if (it->next) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 298 | signatures += "\n"; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 299 | } |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 300 | |
| 301 | /* Install docstring */ |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 302 | PyCFunctionObject *func = (PyCFunctionObject *) m_ptr; |
| 303 | if (func->m_ml->ml_doc) |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 304 | std::free((char *) func->m_ml->ml_doc); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 305 | func->m_ml->ml_doc = strdup(signatures.c_str()); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 306 | |
| 307 | if (rec->class_) { |
| 308 | m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->class_.ptr()); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 309 | if (!m_ptr) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 310 | pybind11_fail("cpp_function::cpp_function(): Could not allocate instance method object"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 311 | Py_DECREF(func); |
| 312 | } |
| 313 | } |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 314 | |
| 315 | /// When a cpp_function is GCed, release any memory allocated by pybind11 |
| 316 | static void destruct(detail::function_record *rec) { |
| 317 | while (rec) { |
| 318 | detail::function_record *next = rec->next; |
| 319 | if (rec->free_data) |
| 320 | rec->free_data(rec->data); |
| 321 | std::free((char *) rec->name); |
| 322 | std::free((char *) rec->doc); |
| 323 | std::free((char *) rec->signature); |
| 324 | for (auto &arg: rec->args) { |
| 325 | std::free((char *) arg.name); |
| 326 | std::free((char *) arg.descr); |
| 327 | arg.value.dec_ref(); |
| 328 | } |
| 329 | if (rec->def) { |
| 330 | std::free((char *) rec->def->ml_doc); |
| 331 | delete rec->def; |
| 332 | } |
| 333 | delete rec; |
| 334 | rec = next; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /// Main dispatch logic for calls to functions bound using pybind11 |
| 339 | static PyObject *dispatcher(PyObject *self, PyObject *args, PyObject *kwargs) { |
| 340 | /* Iterator over the list of potentially admissible overloads */ |
| 341 | detail::function_record *overloads = (detail::function_record *) PyCapsule_GetPointer(self, nullptr), |
| 342 | *it = overloads; |
| 343 | |
| 344 | /* Need to know how many arguments + keyword arguments there are to pick the right overload */ |
| 345 | int nargs = (int) PyTuple_Size(args), |
| 346 | nkwargs = kwargs ? (int) PyDict_Size(kwargs) : 0; |
| 347 | |
| 348 | handle parent = nargs > 0 ? PyTuple_GetItem(args, 0) : nullptr, |
| 349 | result = PYBIND11_TRY_NEXT_OVERLOAD; |
| 350 | try { |
| 351 | for (; it != nullptr; it = it->next) { |
| 352 | tuple args_(args, true); |
| 353 | int kwargs_consumed = 0; |
| 354 | |
| 355 | /* For each overload: |
| 356 | 1. If the required list of arguments is longer than the |
| 357 | actually provided amount, create a copy of the argument |
| 358 | list and fill in any available keyword/default arguments. |
| 359 | 2. Ensure that all keyword arguments were "consumed" |
| 360 | 3. Call the function call dispatcher (function_record::impl) |
| 361 | */ |
| 362 | |
| 363 | if (nargs < (int) it->args.size()) { |
| 364 | args_ = tuple(it->args.size()); |
| 365 | for (int i = 0; i < nargs; ++i) { |
| 366 | handle item = PyTuple_GET_ITEM(args, i); |
| 367 | PyTuple_SET_ITEM(args_.ptr(), i, item.inc_ref().ptr()); |
| 368 | } |
| 369 | |
| 370 | int arg_ctr = 0; |
| 371 | for (auto const &it2 : it->args) { |
| 372 | int index = arg_ctr++; |
| 373 | if (PyTuple_GET_ITEM(args_.ptr(), index)) |
| 374 | continue; |
| 375 | |
| 376 | handle value; |
| 377 | if (kwargs) |
| 378 | value = PyDict_GetItemString(kwargs, it2.name); |
| 379 | |
| 380 | if (value) |
| 381 | kwargs_consumed++; |
| 382 | else if (it2.value) |
| 383 | value = it2.value; |
| 384 | |
| 385 | if (value) { |
| 386 | PyTuple_SET_ITEM(args_.ptr(), index, value.inc_ref().ptr()); |
| 387 | } else { |
| 388 | kwargs_consumed = -1; /* definite failure */ |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if (kwargs_consumed == nkwargs) |
| 395 | result = it->impl(it, args_, parent); |
| 396 | |
| 397 | if (result.ptr() != PYBIND11_TRY_NEXT_OVERLOAD) |
| 398 | break; |
| 399 | } |
| 400 | } catch (const error_already_set &) { return nullptr; |
| 401 | } catch (const index_error &e) { PyErr_SetString(PyExc_IndexError, e.what()); return nullptr; |
| 402 | } catch (const stop_iteration &e) { PyErr_SetString(PyExc_StopIteration, e.what()); return nullptr; |
| 403 | } catch (const std::bad_alloc &e) { PyErr_SetString(PyExc_MemoryError, e.what()); return nullptr; |
| 404 | } catch (const std::domain_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return nullptr; |
| 405 | } catch (const std::invalid_argument &e) { PyErr_SetString(PyExc_ValueError, e.what()); return nullptr; |
| 406 | } catch (const std::length_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return nullptr; |
| 407 | } catch (const std::out_of_range &e) { PyErr_SetString(PyExc_IndexError, e.what()); return nullptr; |
| 408 | } catch (const std::range_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return nullptr; |
| 409 | } catch (const std::exception &e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return nullptr; |
| 410 | } catch (...) { |
| 411 | PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!"); |
| 412 | return nullptr; |
| 413 | } |
| 414 | |
| 415 | if (result.ptr() == PYBIND11_TRY_NEXT_OVERLOAD) { |
| 416 | std::string msg = "Incompatible function arguments. The " |
| 417 | "following argument types are supported:\n"; |
| 418 | int ctr = 0; |
| 419 | for (detail::function_record *it2 = overloads; it2 != nullptr; it2 = it2->next) { |
| 420 | msg += " "+ std::to_string(++ctr) + ". "; |
| 421 | msg += it2->signature; |
| 422 | msg += "\n"; |
| 423 | } |
| 424 | PyErr_SetString(PyExc_TypeError, msg.c_str()); |
| 425 | return nullptr; |
| 426 | } else if (!result) { |
| 427 | std::string msg = "Unable to convert function return value to a " |
| 428 | "Python type! The signature was\n\t"; |
| 429 | msg += it->signature; |
| 430 | PyErr_SetString(PyExc_TypeError, msg.c_str()); |
| 431 | return nullptr; |
| 432 | } else { |
| 433 | if (overloads->is_constructor) { |
| 434 | /* When a construtor ran successfully, the corresponding |
| 435 | holder type (e.g. std::unique_ptr) must still be initialized. */ |
| 436 | PyObject *inst = PyTuple_GetItem(args, 0); |
| 437 | auto tinfo = detail::get_type_info(Py_TYPE(inst)); |
| 438 | tinfo->init_holder(inst, nullptr); |
| 439 | } |
| 440 | return result.ptr(); |
| 441 | } |
| 442 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 443 | }; |
| 444 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 445 | /// Wrapper for Python extension modules |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 446 | class module : public object { |
| 447 | public: |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 448 | PYBIND11_OBJECT_DEFAULT(module, object, PyModule_Check) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 449 | |
| 450 | module(const char *name, const char *doc = nullptr) { |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 451 | #if PY_MAJOR_VERSION >= 3 |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 452 | PyModuleDef *def = new PyModuleDef(); |
| 453 | memset(def, 0, sizeof(PyModuleDef)); |
| 454 | def->m_name = name; |
| 455 | def->m_doc = doc; |
| 456 | def->m_size = -1; |
| 457 | Py_INCREF(def); |
| 458 | m_ptr = PyModule_Create(def); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 459 | #else |
| 460 | m_ptr = Py_InitModule3(name, nullptr, doc); |
| 461 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 462 | if (m_ptr == nullptr) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 463 | pybind11_fail("Internal error in module::module()"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 464 | inc_ref(); |
| 465 | } |
| 466 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 467 | template <typename Func, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 468 | module &def(const char *name_, Func &&f, const Extra& ... extra) { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 469 | cpp_function func(std::forward<Func>(f), name(name_), |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 470 | sibling((handle) attr(name_)), extra...); |
| 471 | /* PyModule_AddObject steals a reference to 'func' */ |
| 472 | PyModule_AddObject(ptr(), name_, func.inc_ref().ptr()); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 473 | return *this; |
| 474 | } |
| 475 | |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 476 | module def_submodule(const char *name, const char *doc = nullptr) { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 477 | std::string full_name = std::string(PyModule_GetName(m_ptr)) |
| 478 | + std::string(".") + std::string(name); |
| 479 | module result(PyImport_AddModule(full_name.c_str()), true); |
Wenzel Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 480 | if (doc) |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 481 | result.attr("__doc__") = pybind11::str(doc); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 482 | attr(name) = result; |
| 483 | return result; |
| 484 | } |
Wenzel Jakob | db028d6 | 2015-10-13 23:44:25 +0200 | [diff] [blame] | 485 | |
| 486 | static module import(const char *name) { |
Wenzel Jakob | dd57a34 | 2015-12-26 14:04:52 +0100 | [diff] [blame] | 487 | PyObject *obj = PyImport_ImportModule(name); |
| 488 | if (!obj) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 489 | pybind11_fail("Module \"" + std::string(name) + "\" not found!"); |
Wenzel Jakob | dd57a34 | 2015-12-26 14:04:52 +0100 | [diff] [blame] | 490 | return module(obj, false); |
Wenzel Jakob | db028d6 | 2015-10-13 23:44:25 +0200 | [diff] [blame] | 491 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 492 | }; |
| 493 | |
| 494 | NAMESPACE_BEGIN(detail) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 495 | /// Generic support for creating new Python heap types |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 496 | class generic_type : public object { |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 497 | template <typename type, typename holder_type> friend class class_; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 498 | public: |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 499 | PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 500 | protected: |
| 501 | void initialize(type_record *rec) { |
| 502 | if (rec->base_type) { |
| 503 | if (rec->base_handle) |
| 504 | pybind11_fail("generic_type: specified base type multiple times!"); |
| 505 | rec->base_handle = detail::get_type_handle(*(rec->base_type)); |
| 506 | if (!rec->base_handle) { |
| 507 | std::string tname(rec->base_type->name()); |
| 508 | detail::clean_type_id(tname); |
| 509 | pybind11_fail("generic_type: type \"" + std::string(rec->name) + |
| 510 | "\" referenced unknown base type \"" + tname + "\""); |
| 511 | } |
| 512 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 513 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 514 | object type_holder(PyType_Type.tp_alloc(&PyType_Type, 0), false); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 515 | object name(PYBIND11_FROM_STRING(rec->name), false); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 516 | auto type = (PyHeapTypeObject*) type_holder.ptr(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 517 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 518 | if (!type_holder || !name) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 519 | pybind11_fail("generic_type: unable to create type object!"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 520 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 521 | /* Register supplemental type information in C++ dict */ |
| 522 | auto &internals = get_internals(); |
| 523 | detail::type_info *tinfo = new detail::type_info(); |
| 524 | tinfo->type = (PyTypeObject *) type; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 525 | tinfo->type_size = rec->type_size; |
| 526 | tinfo->init_holder = rec->init_holder; |
| 527 | internals.registered_types_cpp[rec->type] = tinfo; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 528 | internals.registered_types_py[type] = tinfo; |
| 529 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 530 | auto scope_module = (object) rec->scope.attr("__module__"); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 531 | if (!scope_module) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 532 | scope_module = (object) rec->scope.attr("__name__"); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 533 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 534 | std::string full_name = (scope_module ? ((std::string) scope_module.str() + "." + rec->name) |
| 535 | : std::string(rec->name)); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 536 | /* Basic type attributes */ |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 537 | type->ht_type.tp_name = strdup(full_name.c_str()); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 538 | type->ht_type.tp_basicsize = rec->instance_size; |
| 539 | type->ht_type.tp_base = (PyTypeObject *) rec->base_handle.ptr(); |
| 540 | rec->base_handle.inc_ref(); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 541 | |
| 542 | #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 |
| 543 | /* Qualified names for Python >= 3.3 */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 544 | auto scope_qualname = (object) rec->scope.attr("__qualname__"); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 545 | if (scope_qualname) { |
| 546 | type->ht_qualname = PyUnicode_FromFormat( |
| 547 | "%U.%U", scope_qualname.ptr(), name.ptr()); |
| 548 | } else { |
| 549 | type->ht_qualname = name.ptr(); |
| 550 | name.inc_ref(); |
| 551 | } |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 552 | #endif |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 553 | type->ht_name = name.release().ptr(); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 554 | |
| 555 | /* Supported protocols */ |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 556 | type->ht_type.tp_as_number = &type->as_number; |
| 557 | type->ht_type.tp_as_sequence = &type->as_sequence; |
| 558 | type->ht_type.tp_as_mapping = &type->as_mapping; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 559 | |
| 560 | /* Supported elementary operations */ |
| 561 | type->ht_type.tp_init = (initproc) init; |
| 562 | type->ht_type.tp_new = (newfunc) new_instance; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 563 | type->ht_type.tp_dealloc = rec->dealloc; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 564 | |
| 565 | /* Support weak references (needed for the keep_alive feature) */ |
Wenzel Jakob | 88d1d04 | 2016-01-20 01:26:42 +0100 | [diff] [blame^] | 566 | type->ht_type.tp_weaklistoffset = offsetof(instance_essentials<void>, weakrefs); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 567 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 568 | /* Flags */ |
| 569 | type->ht_type.tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE; |
| 570 | #if PY_MAJOR_VERSION < 3 |
| 571 | type->ht_type.tp_flags |= Py_TPFLAGS_CHECKTYPES; |
| 572 | #endif |
| 573 | type->ht_type.tp_flags &= ~Py_TPFLAGS_HAVE_GC; |
| 574 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 575 | if (rec->doc) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 576 | /* Allocate memory for docstring (using PyObject_MALLOC, since |
| 577 | Python will free this later on) */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 578 | size_t size = strlen(rec->doc) + 1; |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 579 | type->ht_type.tp_doc = (char *) PyObject_MALLOC(size); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 580 | memcpy((void *) type->ht_type.tp_doc, rec->doc, size); |
Wenzel Jakob | 5116b02 | 2015-09-05 02:09:17 +0200 | [diff] [blame] | 581 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 582 | |
| 583 | if (PyType_Ready(&type->ht_type) < 0) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 584 | pybind11_fail("generic_type: PyType_Ready failed!"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 585 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 586 | m_ptr = type_holder.ptr(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 587 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 588 | if (scope_module) // Needed by pydoc |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 589 | attr("__module__") = scope_module; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 590 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 591 | /* Register type with the parent scope */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 592 | rec->scope.attr(handle(type->ht_name)) = *this; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 593 | |
| 594 | type_holder.release(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 597 | /// Allocate a metaclass on demand (for static properties) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 598 | handle metaclass() { |
| 599 | auto &ht_type = ((PyHeapTypeObject *) m_ptr)->ht_type; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 600 | auto &ob_type = PYBIND11_OB_TYPE(ht_type); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 601 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 602 | if (ob_type == &PyType_Type) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 603 | std::string name_ = std::string(ht_type.tp_name) + "__Meta"; |
| 604 | object type_holder(PyType_Type.tp_alloc(&PyType_Type, 0), false); |
| 605 | object name(PYBIND11_FROM_STRING(name_.c_str()), false); |
| 606 | if (!type_holder || !name) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 607 | pybind11_fail("generic_type::metaclass(): unable to create type object!"); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 608 | |
| 609 | auto type = (PyHeapTypeObject*) type_holder.ptr(); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 610 | type->ht_name = name.release().ptr(); |
| 611 | #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3 |
| 612 | /* Qualified names for Python >= 3.3 */ |
| 613 | type->ht_qualname = PyUnicode_FromFormat( |
| 614 | "%U__Meta", ((object) attr("__qualname__")).ptr()); |
| 615 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 616 | type->ht_type.tp_name = strdup(name_.c_str()); |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 617 | type->ht_type.tp_base = ob_type; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 618 | type->ht_type.tp_flags |= (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE) & |
| 619 | ~Py_TPFLAGS_HAVE_GC; |
| 620 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 621 | if (PyType_Ready(&type->ht_type) < 0) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 622 | pybind11_fail("generic_type::metaclass(): PyType_Ready failed!"); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 623 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 624 | ob_type = (PyTypeObject *) type_holder.release().ptr(); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 625 | } |
| 626 | return handle((PyObject *) ob_type); |
| 627 | } |
| 628 | |
| 629 | static int init(void *self, PyObject *, PyObject *) { |
| 630 | std::string msg = std::string(Py_TYPE(self)->tp_name) + ": No constructor defined!"; |
| 631 | PyErr_SetString(PyExc_TypeError, msg.c_str()); |
| 632 | return -1; |
| 633 | } |
| 634 | |
| 635 | static PyObject *new_instance(PyTypeObject *type, PyObject *, PyObject *) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 636 | instance<void> *self = (instance<void> *) PyType_GenericAlloc((PyTypeObject *) type, 0); |
| 637 | auto tinfo = detail::get_type_info(type); |
| 638 | self->value = ::operator new(tinfo->type_size); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 639 | self->owned = true; |
| 640 | self->parent = nullptr; |
| 641 | self->constructed = false; |
| 642 | detail::get_internals().registered_instances[self->value] = (PyObject *) self; |
| 643 | return (PyObject *) self; |
| 644 | } |
| 645 | |
| 646 | static void dealloc(instance<void> *self) { |
| 647 | if (self->value) { |
| 648 | bool dont_cache = self->parent && ((instance<void> *) self->parent)->value == self->value; |
| 649 | if (!dont_cache) { // avoid an issue with internal references matching their parent's address |
| 650 | auto ®istered_instances = detail::get_internals().registered_instances; |
| 651 | auto it = registered_instances.find(self->value); |
| 652 | if (it == registered_instances.end()) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 653 | pybind11_fail("generic_type::dealloc(): Tried to deallocate unregistered instance!"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 654 | registered_instances.erase(it); |
| 655 | } |
| 656 | Py_XDECREF(self->parent); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 657 | if (self->weakrefs) |
| 658 | PyObject_ClearWeakRefs((PyObject *) self); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 659 | } |
| 660 | Py_TYPE(self)->tp_free((PyObject*) self); |
| 661 | } |
| 662 | |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 663 | void install_buffer_funcs( |
| 664 | buffer_info *(*get_buffer)(PyObject *, void *), |
| 665 | void *get_buffer_data) { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 666 | PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr; |
| 667 | type->ht_type.tp_as_buffer = &type->as_buffer; |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 668 | #if PY_MAJOR_VERSION < 3 |
| 669 | type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER; |
| 670 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 671 | type->as_buffer.bf_getbuffer = getbuffer; |
| 672 | type->as_buffer.bf_releasebuffer = releasebuffer; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 673 | auto tinfo = detail::get_type_info(&type->ht_type); |
| 674 | tinfo->get_buffer = get_buffer; |
| 675 | tinfo->get_buffer_data = get_buffer_data; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | static int getbuffer(PyObject *obj, Py_buffer *view, int flags) { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 679 | auto tinfo = detail::get_type_info(Py_TYPE(obj)); |
| 680 | if (view == nullptr || obj == nullptr || !tinfo || !tinfo->get_buffer) { |
| 681 | PyErr_SetString(PyExc_BufferError, "generic_type::getbuffer(): Internal error"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 682 | return -1; |
| 683 | } |
| 684 | memset(view, 0, sizeof(Py_buffer)); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 685 | buffer_info *info = tinfo->get_buffer(obj, tinfo->get_buffer_data); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 686 | view->obj = obj; |
| 687 | view->ndim = 1; |
| 688 | view->internal = info; |
| 689 | view->buf = info->ptr; |
| 690 | view->itemsize = info->itemsize; |
| 691 | view->len = view->itemsize; |
| 692 | for (auto s : info->shape) |
| 693 | view->len *= s; |
| 694 | if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) |
| 695 | view->format = const_cast<char *>(info->format.c_str()); |
| 696 | if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) { |
| 697 | view->ndim = info->ndim; |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 698 | view->strides = (ssize_t *) &info->strides[0]; |
| 699 | view->shape = (ssize_t *) &info->shape[0]; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 700 | } |
| 701 | Py_INCREF(view->obj); |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static void releasebuffer(PyObject *, Py_buffer *view) { delete (buffer_info *) view->internal; } |
| 706 | }; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 707 | NAMESPACE_END(detail) |
| 708 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 709 | template <typename type, typename holder_type = std::unique_ptr<type>> |
| 710 | class class_ : public detail::generic_type { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 711 | public: |
| 712 | typedef detail::instance<type, holder_type> instance_type; |
| 713 | |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 714 | PYBIND11_OBJECT(class_, detail::generic_type, PyType_Check) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 715 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 716 | template <typename... Extra> |
| 717 | class_(handle scope, const char *name, const Extra &... extra) { |
| 718 | detail::type_record record; |
| 719 | record.scope = scope; |
| 720 | record.name = name; |
| 721 | record.type = &typeid(type); |
| 722 | record.type_size = sizeof(type); |
| 723 | record.instance_size = sizeof(instance_type); |
| 724 | record.init_holder = init_holder; |
| 725 | record.dealloc = dealloc; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 726 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 727 | /* Process optional arguments, if any */ |
| 728 | detail::process_attributes<Extra...>::init(extra..., &record); |
| 729 | |
| 730 | detail::generic_type::initialize(&record); |
| 731 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 732 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 733 | template <typename Func, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 734 | class_ &def(const char *name_, Func&& f, const Extra&... extra) { |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 735 | cpp_function cf(std::forward<Func>(f), name(name_), |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 736 | sibling(attr(name_)), is_method(*this), |
| 737 | extra...); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 738 | attr(cf.name()) = cf; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 739 | return *this; |
| 740 | } |
| 741 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 742 | template <typename Func, typename... Extra> class_ & |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 743 | def_static(const char *name_, Func f, const Extra&... extra) { |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 744 | cpp_function cf(std::forward<Func>(f), name(name_), |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 745 | sibling(attr(name_)), extra...); |
Wenzel Jakob | 5708221 | 2015-09-04 23:42:12 +0200 | [diff] [blame] | 746 | attr(cf.name()) = cf; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 747 | return *this; |
| 748 | } |
| 749 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 750 | template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 751 | class_ &def(const detail::op_<id, ot, L, R> &op, const Extra&... extra) { |
| 752 | op.template execute<type>(*this, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 753 | return *this; |
| 754 | } |
| 755 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 756 | template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 757 | class_ & def_cast(const detail::op_<id, ot, L, R> &op, const Extra&... extra) { |
| 758 | op.template execute_cast<type>(*this, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 759 | return *this; |
| 760 | } |
| 761 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 762 | template <typename... Args, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 763 | class_ &def(const detail::init<Args...> &init, const Extra&... extra) { |
| 764 | init.template execute<type>(*this, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 765 | return *this; |
| 766 | } |
| 767 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 768 | template <typename Func> class_& def_buffer(Func &&func) { |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 769 | struct capture { Func func; }; |
| 770 | capture *ptr = new capture { std::forward<Func>(func) }; |
| 771 | install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 772 | detail::type_caster<type> caster; |
| 773 | if (!caster.load(obj, false)) |
| 774 | return nullptr; |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 775 | return new buffer_info(((capture *) ptr)->func(caster)); |
| 776 | }, ptr); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 777 | return *this; |
| 778 | } |
| 779 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 780 | template <typename C, typename D, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 781 | class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 782 | cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, |
| 783 | return_value_policy::reference_internal, |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 784 | is_method(*this), extra...), |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 785 | fset([pm](C &c, const D &value) { c.*pm = value; }, |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 786 | is_method(*this), extra...); |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 787 | def_property(name, fget, fset); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 788 | return *this; |
| 789 | } |
| 790 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 791 | template <typename C, typename D, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 792 | class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) { |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 793 | cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, |
| 794 | return_value_policy::reference_internal, |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 795 | is_method(*this), extra...); |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 796 | def_property_readonly(name, fget); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 797 | return *this; |
| 798 | } |
| 799 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 800 | template <typename D, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 801 | class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) { |
| 802 | cpp_function fget([pm](object) -> const D &{ return *pm; }, |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 803 | return_value_policy::reference_internal, extra...), |
| 804 | fset([pm](object, const D &value) { *pm = value; }, extra...); |
| 805 | def_property_static(name, fget, fset); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 806 | return *this; |
| 807 | } |
| 808 | |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 809 | template <typename D, typename... Extra> |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 810 | class_ &def_readonly_static(const char *name, const D *pm, const Extra& ...extra) { |
| 811 | cpp_function fget([pm](object) -> const D &{ return *pm; }, |
| 812 | return_value_policy::reference_internal, extra...); |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 813 | def_property_readonly_static(name, fget); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 814 | return *this; |
| 815 | } |
| 816 | |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 817 | class_ &def_property_readonly(const char *name, const cpp_function &fget, const char *doc = nullptr) { |
| 818 | def_property(name, fget, cpp_function(), doc); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 819 | return *this; |
| 820 | } |
| 821 | |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 822 | class_ &def_property_readonly_static(const char *name, const cpp_function &fget, const char *doc = nullptr) { |
| 823 | def_property_static(name, fget, cpp_function(), doc); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 824 | return *this; |
| 825 | } |
| 826 | |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 827 | class_ &def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const char *doc = nullptr) { |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 828 | object doc_obj = doc ? pybind11::str(doc) : (object) fget.attr("__doc__"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 829 | object property( |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 830 | PyObject_CallFunctionObjArgs((PyObject *) &PyProperty_Type, fget.ptr() ? fget.ptr() : Py_None, |
| 831 | fset.ptr() ? fset.ptr() : Py_None, Py_None, doc_obj.ptr(), nullptr), false); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 832 | attr(name) = property; |
| 833 | return *this; |
| 834 | } |
| 835 | |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 836 | class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const char *doc = nullptr) { |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 837 | object doc_obj = doc ? pybind11::str(doc) : (object) fget.attr("__doc__"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 838 | object property( |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 839 | PyObject_CallFunctionObjArgs((PyObject *) &PyProperty_Type, fget.ptr() ? fget.ptr() : Py_None, |
| 840 | fset.ptr() ? fset.ptr() : Py_None, Py_None, doc_obj.ptr(), nullptr), false); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 841 | metaclass().attr(name) = property; |
| 842 | return *this; |
| 843 | } |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 844 | |
| 845 | template <typename target> class_ alias() { |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 846 | auto &instances = pybind11::detail::get_internals().registered_types_cpp; |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 847 | instances[&typeid(target)] = instances[&typeid(type)]; |
| 848 | return *this; |
| 849 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 850 | private: |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 851 | /// Initialize holder object, variant 1: object derives from enable_shared_from_this |
| 852 | template <typename T> |
| 853 | static void init_holder_helper(instance_type *inst, const holder_type * /* unused */, const std::enable_shared_from_this<T> * /* dummy */) { |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 854 | try { |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 855 | new (&inst->holder) holder_type(inst->value->shared_from_this()); |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 856 | } catch (const std::bad_weak_ptr &) { |
| 857 | new (&inst->holder) holder_type(inst->value); |
| 858 | } |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /// Initialize holder object, variant 2: try to construct from existing holder object, if possible |
| 862 | template <typename T = holder_type, |
| 863 | typename std::enable_if<std::is_copy_constructible<T>::value, int>::type = 0> |
| 864 | static void init_holder_helper(instance_type *inst, const holder_type *holder_ptr, const void * /* dummy */) { |
| 865 | if (holder_ptr) |
| 866 | new (&inst->holder) holder_type(*holder_ptr); |
| 867 | else |
| 868 | new (&inst->holder) holder_type(inst->value); |
| 869 | } |
| 870 | |
| 871 | /// Initialize holder object, variant 3: holder is not copy constructible (e.g. unique_ptr), always initialize from raw pointer |
| 872 | template <typename T = holder_type, |
| 873 | typename std::enable_if<!std::is_copy_constructible<T>::value, int>::type = 0> |
| 874 | static void init_holder_helper(instance_type *inst, const holder_type * /* unused */, const void * /* dummy */) { |
| 875 | new (&inst->holder) holder_type(inst->value); |
| 876 | } |
| 877 | |
| 878 | /// Initialize holder object of an instance, possibly given a pointer to an existing holder |
| 879 | static void init_holder(PyObject *inst_, const void *holder_ptr) { |
| 880 | auto inst = (instance_type *) inst_; |
| 881 | init_holder_helper(inst, (const holder_type *) holder_ptr, inst->value); |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 882 | inst->constructed = true; |
| 883 | } |
| 884 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 885 | static void dealloc(PyObject *inst_) { |
| 886 | instance_type *inst = (instance_type *) inst_; |
| 887 | if (inst->owned) { |
| 888 | if (inst->constructed) |
| 889 | inst->holder.~holder_type(); |
| 890 | else |
| 891 | ::operator delete(inst->value); |
| 892 | } |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 893 | generic_type::dealloc((detail::instance<void> *) inst); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 894 | } |
| 895 | }; |
| 896 | |
| 897 | /// Binds C++ enumerations and enumeration classes to Python |
| 898 | template <typename Type> class enum_ : public class_<Type> { |
| 899 | public: |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 900 | template <typename... Extra> |
| 901 | enum_(const handle &scope, const char *name, const Extra&... extra) |
| 902 | : class_<Type>(scope, name, extra...), m_parent(scope) { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 903 | auto entries = new std::unordered_map<int, const char *>(); |
Wenzel Jakob | 8456a4d | 2015-10-13 02:42:20 +0200 | [diff] [blame] | 904 | this->def("__repr__", [name, entries](Type value) -> std::string { |
Wenzel Jakob | a750031 | 2015-08-29 02:08:32 +0200 | [diff] [blame] | 905 | auto it = entries->find((int) value); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 906 | return std::string(name) + "." + |
| 907 | ((it == entries->end()) ? std::string("???") |
| 908 | : std::string(it->second)); |
| 909 | }); |
Wenzel Jakob | 6918922 | 2015-10-01 21:32:23 +0200 | [diff] [blame] | 910 | this->def("__int__", [](Type value) { return (int) value; }); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 911 | m_entries = entries; |
| 912 | } |
| 913 | |
| 914 | /// Export enumeration entries into the parent scope |
| 915 | void export_values() { |
| 916 | PyObject *dict = ((PyTypeObject *) this->m_ptr)->tp_dict; |
| 917 | PyObject *key, *value; |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 918 | ssize_t pos = 0; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 919 | while (PyDict_Next(dict, &pos, &key, &value)) |
| 920 | if (PyObject_IsInstance(value, this->m_ptr)) |
| 921 | m_parent.attr(key) = value; |
| 922 | } |
| 923 | |
| 924 | /// Add an enumeration entry |
| 925 | enum_& value(char const* name, Type value) { |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 926 | this->attr(name) = pybind11::cast(value, return_value_policy::copy); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 927 | (*m_entries)[(int) value] = name; |
| 928 | return *this; |
| 929 | } |
| 930 | private: |
| 931 | std::unordered_map<int, const char *> *m_entries; |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 932 | handle m_parent; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 933 | }; |
| 934 | |
| 935 | NAMESPACE_BEGIN(detail) |
Wenzel Jakob | 7186783 | 2015-07-29 17:43:52 +0200 | [diff] [blame] | 936 | template <typename... Args> struct init { |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 937 | template <typename Base, typename Holder, typename... Extra> void execute(pybind11::class_<Base, Holder> &class_, const Extra&... extra) const { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 938 | /// Function which calls a specific C++ in-place constructor |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 939 | class_.def("__init__", [](Base *instance, Args... args) { new (instance) Base(args...); }, extra...); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 940 | } |
| 941 | }; |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 942 | |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 943 | PYBIND11_NOINLINE inline void keep_alive_impl(int Nurse, int Patient, handle args, handle ret) { |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 944 | /* Clever approach based on weak references taken from Boost.Python */ |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 945 | handle nurse (Nurse > 0 ? PyTuple_GetItem(args.ptr(), Nurse - 1) : ret.ptr()); |
| 946 | handle patient(Patient > 0 ? PyTuple_GetItem(args.ptr(), Patient - 1) : ret.ptr()); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 947 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 948 | if (!nurse || !patient) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 949 | pybind11_fail("Could not activate keep_alive!"); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 950 | |
| 951 | cpp_function disable_lifesupport( |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 952 | [patient](handle weakref) { patient.dec_ref(); weakref.dec_ref(); }); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 953 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 954 | weakref wr(nurse, disable_lifesupport); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 955 | |
Wenzel Jakob | b2c2c79 | 2016-01-17 22:36:40 +0100 | [diff] [blame] | 956 | patient.inc_ref(); /* reference patient and leak the weak reference */ |
| 957 | (void) wr.release(); |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 958 | } |
| 959 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 960 | NAMESPACE_END(detail) |
| 961 | |
| 962 | template <typename... Args> detail::init<Args...> init() { return detail::init<Args...>(); }; |
| 963 | |
| 964 | template <typename InputType, typename OutputType> void implicitly_convertible() { |
Wenzel Jakob | 2ac5044 | 2016-01-17 22:36:35 +0100 | [diff] [blame] | 965 | auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * { |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 966 | if (!detail::type_caster<InputType>().load(obj, false)) |
| 967 | return nullptr; |
| 968 | tuple args(1); |
| 969 | args[0] = obj; |
| 970 | PyObject *result = PyObject_Call((PyObject *) type, args.ptr(), nullptr); |
| 971 | if (result == nullptr) |
| 972 | PyErr_Clear(); |
| 973 | return result; |
| 974 | }; |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 975 | auto & registered_types = detail::get_internals().registered_types_cpp; |
Wenzel Jakob | f5fae92 | 2015-08-24 15:31:24 +0200 | [diff] [blame] | 976 | auto it = registered_types.find(&typeid(OutputType)); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 977 | if (it == registered_types.end()) |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 978 | pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>()); |
Wenzel Jakob | d561cb0 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 979 | ((detail::type_info *) it->second)->implicit_conversions.push_back(implicit_caster); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | inline void init_threading() { PyEval_InitThreads(); } |
| 983 | |
| 984 | class gil_scoped_acquire { |
| 985 | PyGILState_STATE state; |
| 986 | public: |
| 987 | inline gil_scoped_acquire() { state = PyGILState_Ensure(); } |
| 988 | inline ~gil_scoped_acquire() { PyGILState_Release(state); } |
| 989 | }; |
| 990 | |
| 991 | class gil_scoped_release { |
| 992 | PyThreadState *state; |
| 993 | public: |
| 994 | inline gil_scoped_release() { state = PyEval_SaveThread(); } |
| 995 | inline ~gil_scoped_release() { PyEval_RestoreThread(state); } |
| 996 | }; |
| 997 | |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 998 | inline function get_overload(const void *this_ptr, const char *name) { |
| 999 | handle py_object = detail::get_object_handle(this_ptr); |
Wenzel Jakob | ac0fde9 | 2015-10-01 18:37:26 +0200 | [diff] [blame] | 1000 | if (!py_object) |
| 1001 | return function(); |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1002 | handle type = py_object.get_type(); |
| 1003 | auto key = std::make_pair(type.ptr(), name); |
| 1004 | |
| 1005 | /* Cache functions that aren't overloaded in python to avoid |
| 1006 | many costly dictionary lookups in Python */ |
| 1007 | auto &cache = detail::get_internals().inactive_overload_cache; |
| 1008 | if (cache.find(key) != cache.end()) |
| 1009 | return function(); |
| 1010 | |
| 1011 | function overload = (function) py_object.attr(name); |
| 1012 | if (overload.is_cpp_function()) { |
| 1013 | cache.insert(key); |
| 1014 | return function(); |
| 1015 | } |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 1016 | |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1017 | PyFrameObject *frame = PyThreadState_Get()->frame; |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 1018 | pybind11::str caller = pybind11::handle(frame->f_code->co_name).str(); |
Wenzel Jakob | 27e8e10 | 2016-01-17 22:36:37 +0100 | [diff] [blame] | 1019 | if ((std::string) caller == name) |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1020 | return function(); |
| 1021 | return overload; |
| 1022 | } |
| 1023 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 1024 | #define PYBIND11_OVERLOAD_INT(ret_type, class_name, name, ...) { \ |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 1025 | pybind11::gil_scoped_acquire gil; \ |
| 1026 | pybind11::function overload = pybind11::get_overload(this, #name); \ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1027 | if (overload) \ |
| 1028 | return overload.call(__VA_ARGS__).cast<ret_type>(); } |
| 1029 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 1030 | #define PYBIND11_OVERLOAD(ret_type, class_name, name, ...) \ |
| 1031 | PYBIND11_OVERLOAD_INT(ret_type, class_name, name, __VA_ARGS__) \ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1032 | return class_name::name(__VA_ARGS__) |
| 1033 | |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 1034 | #define PYBIND11_OVERLOAD_PURE(ret_type, class_name, name, ...) \ |
| 1035 | PYBIND11_OVERLOAD_INT(ret_type, class_name, name, __VA_ARGS__) \ |
Wenzel Jakob | 678d787 | 2016-01-17 22:36:41 +0100 | [diff] [blame] | 1036 | pybind11::pybind11_fail("Tried to call pure virtual function \"" #name "\""); |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1037 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 1038 | NAMESPACE_END(pybind11) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1039 | |
| 1040 | #if defined(_MSC_VER) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 1041 | # pragma warning(pop) |
Wenzel Jakob | cd5cda7 | 2015-08-03 12:17:54 +0200 | [diff] [blame] | 1042 | #elif defined(__GNUG__) and !defined(__clang__) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 1043 | # pragma GCC diagnostic pop |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1044 | #endif |