blob: bbcda47f5e5e2c18919416406a09bf4ad46ed6d4 [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Wenzel Jakob48548ea2016-01-17 22:36:44 +01002 pybind11/pybind11.h: Main header file of the C++11 python
3 binding generator library
Wenzel Jakob38bd7112015-07-05 20:05:44 +02004
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 Jakobbd4a5292015-07-11 17:41:48 +020011#pragma once
Wenzel Jakob38bd7112015-07-05 20:05:44 +020012
13#if defined(_MSC_VER)
Wenzel Jakob48548ea2016-01-17 22:36:44 +010014# 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 Jakobcd5cda72015-08-03 12:17:54 +020020#elif defined(__GNUG__) and !defined(__clang__)
Wenzel Jakob48548ea2016-01-17 22:36:44 +010021# 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 Jakob38bd7112015-07-05 20:05:44 +020025#endif
26
Wenzel Jakob48548ea2016-01-17 22:36:44 +010027#include "attr.h"
Wenzel Jakob38bd7112015-07-05 20:05:44 +020028
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020029NAMESPACE_BEGIN(pybind11)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020030
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020031/// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object
Wenzel Jakobbd4a5292015-07-11 17:41:48 +020032class cpp_function : public function {
Wenzel Jakobd561cb02016-01-17 22:36:41 +010033protected:
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020034 /// 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 Jakob4177ed42016-01-17 22:36:38 +010037 std::is_void<T>::value, detail::void_type, typename detail::intrinsic_type<T>::type>::type>;
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020038
39 /// Picks a suitable argument value converter from cast.h
Wenzel Jakob71867832015-07-29 17:43:52 +020040 template <typename... T> using arg_value_caster =
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020041 detail::type_caster<typename std::tuple<T...>>;
42public:
Wenzel Jakobbd4a5292015-07-11 17:41:48 +020043 cpp_function() { }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020044
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020045 /// Vanilla function pointers
Wenzel Jakob66c9a402016-01-17 22:36:36 +010046 template <typename Return, typename... Args, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +010047 cpp_function(Return (*f)(Args...), const Extra&... extra) {
48 auto rec = new detail::function_record();
49 rec->data = (void *) f;
Wenzel Jakob71867832015-07-29 17:43:52 +020050
Wenzel Jakob66c9a402016-01-17 22:36:36 +010051 typedef arg_value_caster<Args...> cast_in;
Wenzel Jakob71867832015-07-29 17:43:52 +020052 typedef return_value_caster<Return> cast_out;
53
Wenzel Jakobd561cb02016-01-17 22:36:41 +010054 /* Dispatch code which converts function arguments and performs the actual function call */
Wenzel Jakob48548ea2016-01-17 22:36:44 +010055 rec->impl = [](detail::function_record *rec, handle pyArgs, handle parent) -> handle {
Wenzel Jakob71867832015-07-29 17:43:52 +020056 cast_in args;
Wenzel Jakobd561cb02016-01-17 22:36:41 +010057
58 /* Try to cast the function arguments into the C++ domain */
Wenzel Jakob2ac50442016-01-17 22:36:35 +010059 if (!args.load(pyArgs, true))
Wenzel Jakobd561cb02016-01-17 22:36:41 +010060 return PYBIND11_TRY_NEXT_OVERLOAD;
61
Wenzel Jakob48548ea2016-01-17 22:36:44 +010062 /* Invoke call policy pre-call hook */
63 detail::process_attributes<Extra...>::precall(pyArgs);
Wenzel Jakobd561cb02016-01-17 22:36:41 +010064
65 /* Do the call and convert the return value back into the Python domain */
Wenzel Jakob48548ea2016-01-17 22:36:44 +010066 handle result = cast_out::cast(
67 args.template call<Return>((Return (*) (Args...)) rec->data),
68 rec->policy, parent);
Wenzel Jakobd561cb02016-01-17 22:36:41 +010069
Wenzel Jakob48548ea2016-01-17 22:36:44 +010070 /* Invoke call policy post-call hook */
71 detail::process_attributes<Extra...>::postcall(pyArgs, result);
72
Wenzel Jakob5f218b32016-01-17 22:36:39 +010073 return result;
Wenzel Jakob71867832015-07-29 17:43:52 +020074 };
75
Wenzel Jakob48548ea2016-01-17 22:36:44 +010076 /* Process any user-provided function attributes */
77 detail::process_attributes<Extra...>::init(extra..., rec);
Wenzel Jakobd561cb02016-01-17 22:36:41 +010078
79 /* Generate a readable signature describing the function's arguments and return value types */
Wenzel Jakob48548ea2016-01-17 22:36:44 +010080 using detail::descr;
Wenzel Jakob66c9a402016-01-17 22:36:36 +010081 PYBIND11_DESCR signature = cast_in::name() + detail::_(" -> ") + cast_out::name();
Wenzel Jakobd561cb02016-01-17 22:36:41 +010082
83 /* Register the function with Python from generic (non-templated) code */
Wenzel Jakob48548ea2016-01-17 22:36:44 +010084 initialize(rec, signature.text(), signature.types(), sizeof...(Args));
Wenzel Jakob38bd7112015-07-05 20:05:44 +020085 }
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020086
87 /// Delegating helper constructor to deal with lambda functions
Wenzel Jakob48548ea2016-01-17 22:36:44 +010088 template <typename Func, typename... Extra> cpp_function(Func &&f, const Extra&... extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +020089 initialize(std::forward<Func>(f),
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020090 (typename detail::remove_class<decltype(
Wenzel Jakob48548ea2016-01-17 22:36:44 +010091 &std::remove_reference<Func>::type::operator())>::type *) nullptr, extra...);
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020092 }
93
Wenzel Jakobd561cb02016-01-17 22:36:41 +010094 /// Delegating helper constructor to deal with class methods (non-const)
Wenzel Jakob71867832015-07-29 17:43:52 +020095 template <typename Return, typename Class, typename... Arg, typename... Extra> cpp_function(
Wenzel Jakob48548ea2016-01-17 22:36:44 +010096 Return (Class::*f)(Arg...), const Extra&... extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +020097 initialize([f](Class *c, Arg... args) -> Return { return (c->*f)(args...); },
Wenzel Jakob48548ea2016-01-17 22:36:44 +010098 (Return (*) (Class *, Arg...)) nullptr, extra...);
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020099 }
100
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100101 /// Delegating helper constructor to deal with class methods (const)
Wenzel Jakob71867832015-07-29 17:43:52 +0200102 template <typename Return, typename Class, typename... Arg, typename... Extra> cpp_function(
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100103 Return (Class::*f)(Arg...) const, const Extra&... extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +0200104 initialize([f](const Class *c, Arg... args) -> Return { return (c->*f)(args...); },
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100105 (Return (*)(const Class *, Arg ...)) nullptr, extra...);
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200106 }
107
Wenzel Jakob57082212015-09-04 23:42:12 +0200108 /// Return the function name
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100109 object name() const { return attr("__name__"); }
Wenzel Jakob57082212015-09-04 23:42:12 +0200110
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100111protected:
112 /// Special internal constructor for functors, lambda functions, etc.
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100113 template <typename Func, typename Return, typename... Args, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100114 void initialize(Func &&f, Return (*)(Args...), const Extra&... extra) {
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100115 struct capture { typename std::remove_reference<Func>::type f; };
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200116
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100117 /* Store the function including any extra state it might have (e.g. a lambda capture object) */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100118 auto rec = new detail::function_record();
119 rec->data = new capture { std::forward<Func>(f) };
Wenzel Jakob71867832015-07-29 17:43:52 +0200120
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100121 /* Create a cleanup handler, but only if we have to (less generated code) */
Wenzel Jakob19208fe2015-10-13 17:37:25 +0200122 if (!std::is_trivially_destructible<Func>::value)
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100123 rec->free_data = [](void *ptr) { delete (capture *) ptr; };
Wenzel Jakob2ac50442016-01-17 22:36:35 +0100124 else
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100125 rec->free_data = operator delete;
Wenzel Jakob19208fe2015-10-13 17:37:25 +0200126
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100127 typedef arg_value_caster<Args...> cast_in;
Wenzel Jakob71867832015-07-29 17:43:52 +0200128 typedef return_value_caster<Return> cast_out;
129
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100130 /* Dispatch code which converts function arguments and performs the actual function call */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100131 rec->impl = [](detail::function_record *rec, handle pyArgs, handle parent) -> handle {
Wenzel Jakob71867832015-07-29 17:43:52 +0200132 cast_in args;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100133
134 /* Try to cast the function arguments into the C++ domain */
Wenzel Jakob2ac50442016-01-17 22:36:35 +0100135 if (!args.load(pyArgs, true))
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100136 return PYBIND11_TRY_NEXT_OVERLOAD;
137
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100138 /* Invoke call policy pre-call hook */
139 detail::process_attributes<Extra...>::precall(pyArgs);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100140
141 /* Do the call and convert the return value back into the Python domain */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100142 handle result = cast_out::cast(
143 args.template call<Return>(((capture *) rec->data)->f),
144 rec->policy, parent);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100145
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100146 /* Invoke call policy post-call hook */
147 detail::process_attributes<Extra...>::postcall(pyArgs, result);
148
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100149 return result;
Wenzel Jakob71867832015-07-29 17:43:52 +0200150 };
151
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100152 /* Process any user-provided function attributes */
153 detail::process_attributes<Extra...>::init(extra..., rec);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100154
155 /* Generate a readable signature describing the function's arguments and return value types */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100156 using detail::descr;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100157 PYBIND11_DESCR signature = cast_in::name() + detail::_(" -> ") + cast_out::name();
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100158
159 /* Register the function with Python from generic (non-templated) code */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100160 initialize(rec, signature.text(), signature.types(), sizeof...(Args));
Wenzel Jakob281aa0e2015-07-30 15:29:00 +0200161 }
162
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100163 /// Register a function call with Python (generic non-templated code goes here)
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100164 void initialize(detail::function_record *rec, const char *text,
165 const std::type_info *const *types, int args) {
166
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100167 /* Create copies of all referenced C-style strings */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100168 rec->name = strdup(rec->name ? rec->name : "");
169 if (rec->doc) rec->doc = strdup(rec->doc);
170 for (auto &a: rec->args) {
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100171 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 Jakob27e8e102016-01-17 22:36:37 +0100176 a.descr = strdup(((std::string) ((object) handle(a.value).attr("__repr__")).call().str()).c_str());
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100177 }
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100178 auto const &registered_types = detail::get_internals().registered_types_cpp;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100179
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 Jakob48548ea2016-01-17 22:36:44 +0100189 if (type_depth == 1 && arg_index < rec->args.size()) {
190 signature += rec->args[arg_index].name;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100191 signature += " : ";
192 }
193 ++type_depth;
194 } else if (c == '}') {
195 --type_depth;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100196 if (type_depth == 1 && arg_index < rec->args.size()) {
197 if (rec->args[arg_index].descr) {
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100198 signature += " = ";
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100199 signature += rec->args[arg_index].descr;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100200 }
201 arg_index++;
202 }
203 } else if (c == '%') {
204 const std::type_info *t = types[type_index++];
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100205 if (!t)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100206 pybind11_fail("Internal error while parsing type signature (1)");
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100207 auto it = registered_types.find(t);
208 if (it != registered_types.end()) {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100209 signature += ((const detail::type_info *) it->second)->type->tp_name;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100210 } 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 Jakob95d18692016-01-17 22:36:40 +0100219 if (type_depth != 0 || types[type_index] != nullptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100220 pybind11_fail("Internal error while parsing type signature (2)");
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100221
222 #if !defined(PYBIND11_CPP14)
223 delete[] types;
224 delete[] text;
225 #endif
Wenzel Jakobf5fae922015-08-24 15:31:24 +0200226
Wenzel Jakob57082212015-09-04 23:42:12 +0200227#if PY_MAJOR_VERSION < 3
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100228 if (strcmp(rec->name, "__next__") == 0) {
229 std::free(rec->name);
230 rec->name = strdup("next");
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100231 }
Wenzel Jakob57082212015-09-04 23:42:12 +0200232#endif
233
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100234 if (!rec->args.empty() && (int) rec->args.size() != args)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100235 pybind11_fail(
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100236 "cpp_function(): function \"" + std::string(rec->name) + "\" takes " +
237 std::to_string(args) + " arguments, but " + std::to_string(rec->args.size()) +
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200238 " pybind11::arg entries were specified!");
Wenzel Jakobbd4a5292015-07-11 17:41:48 +0200239
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100240 rec->is_constructor = !strcmp(rec->name, "__init__");
241 rec->signature = strdup(signature.c_str());
242 rec->args.shrink_to_fit();
Wenzel Jakobbd4a5292015-07-11 17:41:48 +0200243
Wenzel Jakob57082212015-09-04 23:42:12 +0200244#if PY_MAJOR_VERSION < 3
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100245 if (rec->sibling && PyMethod_Check(rec->sibling.ptr()))
246 rec->sibling = PyMethod_GET_FUNCTION(rec->sibling.ptr());
Wenzel Jakob57082212015-09-04 23:42:12 +0200247#endif
248
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100249 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 Jakobd561cb02016-01-17 22:36:41 +0100253 /* Never append a method to an overload chain of a parent class;
254 instead, hide the parent's overloads in this case */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100255 if (chain->class_ != rec->class_)
256 chain = nullptr;
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200257 }
258
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100259 if (!chain) {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100260 /* No existing overload was found, create a new function object */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100261 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 Jakobd561cb02016-01-17 22:36:41 +0100268 });
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100269 m_ptr = PyCFunction_New(rec->def, rec_capsule.ptr());
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200270 if (!m_ptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100271 pybind11_fail("cpp_function::cpp_function(): Could not allocate function object");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200272 } else {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100273 /* Append at the end of the overload chain */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100274 m_ptr = rec->sibling.ptr();
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200275 inc_ref();
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100276 chain_start = chain;
277 while (chain->next)
278 chain = chain->next;
279 chain->next = rec;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200280 }
Wenzel Jakob281aa0e2015-07-30 15:29:00 +0200281
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200282 std::string signatures;
Wenzel Jakob71867832015-07-29 17:43:52 +0200283 int index = 0;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100284 /* Create a nice pydoc rec including all signatures and
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100285 docstrings of the functions in the overload chain */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100286 for (auto it = chain_start; it != nullptr; it = it->next) {
287 if (chain)
Wenzel Jakob71867832015-07-29 17:43:52 +0200288 signatures += std::to_string(++index) + ". ";
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100289 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 Jakob71867832015-07-29 17:43:52 +0200297 if (it->next)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200298 signatures += "\n";
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200299 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100300
301 /* Install docstring */
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200302 PyCFunctionObject *func = (PyCFunctionObject *) m_ptr;
303 if (func->m_ml->ml_doc)
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100304 std::free((char *) func->m_ml->ml_doc);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200305 func->m_ml->ml_doc = strdup(signatures.c_str());
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100306
307 if (rec->class_) {
308 m_ptr = PYBIND11_INSTANCE_METHOD_NEW(m_ptr, rec->class_.ptr());
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200309 if (!m_ptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100310 pybind11_fail("cpp_function::cpp_function(): Could not allocate instance method object");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200311 Py_DECREF(func);
312 }
313 }
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100314
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 Jakob38bd7112015-07-05 20:05:44 +0200443};
444
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100445/// Wrapper for Python extension modules
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200446class module : public object {
447public:
Wenzel Jakobb1b71402015-10-18 16:48:30 +0200448 PYBIND11_OBJECT_DEFAULT(module, object, PyModule_Check)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200449
450 module(const char *name, const char *doc = nullptr) {
Wenzel Jakob57082212015-09-04 23:42:12 +0200451#if PY_MAJOR_VERSION >= 3
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200452 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 Jakob57082212015-09-04 23:42:12 +0200459#else
460 m_ptr = Py_InitModule3(name, nullptr, doc);
461#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200462 if (m_ptr == nullptr)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100463 pybind11_fail("Internal error in module::module()");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200464 inc_ref();
465 }
466
Wenzel Jakob71867832015-07-29 17:43:52 +0200467 template <typename Func, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100468 module &def(const char *name_, Func &&f, const Extra& ... extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +0200469 cpp_function func(std::forward<Func>(f), name(name_),
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100470 sibling((handle) attr(name_)), extra...);
471 /* PyModule_AddObject steals a reference to 'func' */
472 PyModule_AddObject(ptr(), name_, func.inc_ref().ptr());
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200473 return *this;
474 }
475
Wenzel Jakobbd4a5292015-07-11 17:41:48 +0200476 module def_submodule(const char *name, const char *doc = nullptr) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200477 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 Jakobbd4a5292015-07-11 17:41:48 +0200480 if (doc)
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200481 result.attr("__doc__") = pybind11::str(doc);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200482 attr(name) = result;
483 return result;
484 }
Wenzel Jakobdb028d62015-10-13 23:44:25 +0200485
486 static module import(const char *name) {
Wenzel Jakobdd57a342015-12-26 14:04:52 +0100487 PyObject *obj = PyImport_ImportModule(name);
488 if (!obj)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100489 pybind11_fail("Module \"" + std::string(name) + "\" not found!");
Wenzel Jakobdd57a342015-12-26 14:04:52 +0100490 return module(obj, false);
Wenzel Jakobdb028d62015-10-13 23:44:25 +0200491 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200492};
493
494NAMESPACE_BEGIN(detail)
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100495/// Generic support for creating new Python heap types
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100496class generic_type : public object {
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100497 template <typename type, typename holder_type> friend class class_;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200498public:
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100499 PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check)
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100500protected:
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 Jakob38bd7112015-07-05 20:05:44 +0200513
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100514 object type_holder(PyType_Type.tp_alloc(&PyType_Type, 0), false);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100515 object name(PYBIND11_FROM_STRING(rec->name), false);
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100516 auto type = (PyHeapTypeObject*) type_holder.ptr();
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200517
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100518 if (!type_holder || !name)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100519 pybind11_fail("generic_type: unable to create type object!");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200520
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100521 /* 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 Jakob48548ea2016-01-17 22:36:44 +0100525 tinfo->type_size = rec->type_size;
526 tinfo->init_holder = rec->init_holder;
527 internals.registered_types_cpp[rec->type] = tinfo;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100528 internals.registered_types_py[type] = tinfo;
529
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100530 auto scope_module = (object) rec->scope.attr("__module__");
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100531 if (!scope_module)
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100532 scope_module = (object) rec->scope.attr("__name__");
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100533
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100534 std::string full_name = (scope_module ? ((std::string) scope_module.str() + "." + rec->name)
535 : std::string(rec->name));
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100536 /* Basic type attributes */
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200537 type->ht_type.tp_name = strdup(full_name.c_str());
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100538 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 Jakobd561cb02016-01-17 22:36:41 +0100541
542#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
543 /* Qualified names for Python >= 3.3 */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100544 auto scope_qualname = (object) rec->scope.attr("__qualname__");
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100545 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 Jakob57082212015-09-04 23:42:12 +0200552#endif
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100553 type->ht_name = name.release().ptr();
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100554
555 /* Supported protocols */
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200556 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 Jakobd561cb02016-01-17 22:36:41 +0100559
560 /* Supported elementary operations */
561 type->ht_type.tp_init = (initproc) init;
562 type->ht_type.tp_new = (newfunc) new_instance;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100563 type->ht_type.tp_dealloc = rec->dealloc;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100564
565 /* Support weak references (needed for the keep_alive feature) */
Wenzel Jakob88d1d042016-01-20 01:26:42 +0100566 type->ht_type.tp_weaklistoffset = offsetof(instance_essentials<void>, weakrefs);
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100567
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100568 /* 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 Jakob48548ea2016-01-17 22:36:44 +0100575 if (rec->doc) {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100576 /* Allocate memory for docstring (using PyObject_MALLOC, since
577 Python will free this later on) */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100578 size_t size = strlen(rec->doc) + 1;
Wenzel Jakob66c9a402016-01-17 22:36:36 +0100579 type->ht_type.tp_doc = (char *) PyObject_MALLOC(size);
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100580 memcpy((void *) type->ht_type.tp_doc, rec->doc, size);
Wenzel Jakob5116b022015-09-05 02:09:17 +0200581 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200582
583 if (PyType_Ready(&type->ht_type) < 0)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100584 pybind11_fail("generic_type: PyType_Ready failed!");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200585
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100586 m_ptr = type_holder.ptr();
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200587
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100588 if (scope_module) // Needed by pydoc
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100589 attr("__module__") = scope_module;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200590
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100591 /* Register type with the parent scope */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100592 rec->scope.attr(handle(type->ht_name)) = *this;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100593
594 type_holder.release();
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200595 }
596
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100597 /// Allocate a metaclass on demand (for static properties)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200598 handle metaclass() {
599 auto &ht_type = ((PyHeapTypeObject *) m_ptr)->ht_type;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100600 auto &ob_type = PYBIND11_OB_TYPE(ht_type);
Wenzel Jakob57082212015-09-04 23:42:12 +0200601
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200602 if (ob_type == &PyType_Type) {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100603 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 Jakob678d7872016-01-17 22:36:41 +0100607 pybind11_fail("generic_type::metaclass(): unable to create type object!");
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100608
609 auto type = (PyHeapTypeObject*) type_holder.ptr();
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100610 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 Jakob38bd7112015-07-05 20:05:44 +0200616 type->ht_type.tp_name = strdup(name_.c_str());
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100617 type->ht_type.tp_base = ob_type;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100618 type->ht_type.tp_flags |= (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE) &
619 ~Py_TPFLAGS_HAVE_GC;
620
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200621 if (PyType_Ready(&type->ht_type) < 0)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100622 pybind11_fail("generic_type::metaclass(): PyType_Ready failed!");
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100623
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100624 ob_type = (PyTypeObject *) type_holder.release().ptr();
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200625 }
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 Jakobd561cb02016-01-17 22:36:41 +0100636 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 Jakob38bd7112015-07-05 20:05:44 +0200639 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 &registered_instances = detail::get_internals().registered_instances;
651 auto it = registered_instances.find(self->value);
652 if (it == registered_instances.end())
Wenzel Jakob678d7872016-01-17 22:36:41 +0100653 pybind11_fail("generic_type::dealloc(): Tried to deallocate unregistered instance!");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200654 registered_instances.erase(it);
655 }
656 Py_XDECREF(self->parent);
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100657 if (self->weakrefs)
658 PyObject_ClearWeakRefs((PyObject *) self);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200659 }
660 Py_TYPE(self)->tp_free((PyObject*) self);
661 }
662
Wenzel Jakob43398a82015-07-28 16:12:20 +0200663 void install_buffer_funcs(
664 buffer_info *(*get_buffer)(PyObject *, void *),
665 void *get_buffer_data) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200666 PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr;
667 type->ht_type.tp_as_buffer = &type->as_buffer;
Wenzel Jakob57082212015-09-04 23:42:12 +0200668#if PY_MAJOR_VERSION < 3
669 type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
670#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200671 type->as_buffer.bf_getbuffer = getbuffer;
672 type->as_buffer.bf_releasebuffer = releasebuffer;
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100673 auto tinfo = detail::get_type_info(&type->ht_type);
674 tinfo->get_buffer = get_buffer;
675 tinfo->get_buffer_data = get_buffer_data;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200676 }
677
678 static int getbuffer(PyObject *obj, Py_buffer *view, int flags) {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100679 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 Jakob38bd7112015-07-05 20:05:44 +0200682 return -1;
683 }
684 memset(view, 0, sizeof(Py_buffer));
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100685 buffer_info *info = tinfo->get_buffer(obj, tinfo->get_buffer_data);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200686 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 Jakob27e8e102016-01-17 22:36:37 +0100698 view->strides = (ssize_t *) &info->strides[0];
699 view->shape = (ssize_t *) &info->shape[0];
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200700 }
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 Jakob38bd7112015-07-05 20:05:44 +0200707NAMESPACE_END(detail)
708
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100709template <typename type, typename holder_type = std::unique_ptr<type>>
710class class_ : public detail::generic_type {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200711public:
712 typedef detail::instance<type, holder_type> instance_type;
713
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100714 PYBIND11_OBJECT(class_, detail::generic_type, PyType_Check)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200715
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100716 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 Jakob38bd7112015-07-05 20:05:44 +0200726
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100727 /* Process optional arguments, if any */
728 detail::process_attributes<Extra...>::init(extra..., &record);
729
730 detail::generic_type::initialize(&record);
731 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200732
Wenzel Jakob71867832015-07-29 17:43:52 +0200733 template <typename Func, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100734 class_ &def(const char *name_, Func&& f, const Extra&... extra) {
Wenzel Jakob57082212015-09-04 23:42:12 +0200735 cpp_function cf(std::forward<Func>(f), name(name_),
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100736 sibling(attr(name_)), is_method(*this),
737 extra...);
Wenzel Jakob57082212015-09-04 23:42:12 +0200738 attr(cf.name()) = cf;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200739 return *this;
740 }
741
Wenzel Jakob71867832015-07-29 17:43:52 +0200742 template <typename Func, typename... Extra> class_ &
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100743 def_static(const char *name_, Func f, const Extra&... extra) {
Wenzel Jakob57082212015-09-04 23:42:12 +0200744 cpp_function cf(std::forward<Func>(f), name(name_),
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100745 sibling(attr(name_)), extra...);
Wenzel Jakob57082212015-09-04 23:42:12 +0200746 attr(cf.name()) = cf;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200747 return *this;
748 }
749
Wenzel Jakob71867832015-07-29 17:43:52 +0200750 template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100751 class_ &def(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
752 op.template execute<type>(*this, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200753 return *this;
754 }
755
Wenzel Jakob71867832015-07-29 17:43:52 +0200756 template <detail::op_id id, detail::op_type ot, typename L, typename R, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100757 class_ & def_cast(const detail::op_<id, ot, L, R> &op, const Extra&... extra) {
758 op.template execute_cast<type>(*this, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200759 return *this;
760 }
761
Wenzel Jakob71867832015-07-29 17:43:52 +0200762 template <typename... Args, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100763 class_ &def(const detail::init<Args...> &init, const Extra&... extra) {
764 init.template execute<type>(*this, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200765 return *this;
766 }
767
Wenzel Jakob71867832015-07-29 17:43:52 +0200768 template <typename Func> class_& def_buffer(Func &&func) {
Wenzel Jakob43398a82015-07-28 16:12:20 +0200769 struct capture { Func func; };
770 capture *ptr = new capture { std::forward<Func>(func) };
771 install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200772 detail::type_caster<type> caster;
773 if (!caster.load(obj, false))
774 return nullptr;
Wenzel Jakob43398a82015-07-28 16:12:20 +0200775 return new buffer_info(((capture *) ptr)->func(caster));
776 }, ptr);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200777 return *this;
778 }
779
Wenzel Jakob71867832015-07-29 17:43:52 +0200780 template <typename C, typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100781 class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +0200782 cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; },
783 return_value_policy::reference_internal,
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100784 is_method(*this), extra...),
Wenzel Jakob71867832015-07-29 17:43:52 +0200785 fset([pm](C &c, const D &value) { c.*pm = value; },
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100786 is_method(*this), extra...);
Wenzel Jakob71867832015-07-29 17:43:52 +0200787 def_property(name, fget, fset);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200788 return *this;
789 }
790
Wenzel Jakob71867832015-07-29 17:43:52 +0200791 template <typename C, typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100792 class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) {
Wenzel Jakob71867832015-07-29 17:43:52 +0200793 cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; },
794 return_value_policy::reference_internal,
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100795 is_method(*this), extra...);
Wenzel Jakob71867832015-07-29 17:43:52 +0200796 def_property_readonly(name, fget);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200797 return *this;
798 }
799
Wenzel Jakob71867832015-07-29 17:43:52 +0200800 template <typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100801 class_ &def_readwrite_static(const char *name, D *pm, const Extra& ...extra) {
802 cpp_function fget([pm](object) -> const D &{ return *pm; },
Wenzel Jakob71867832015-07-29 17:43:52 +0200803 return_value_policy::reference_internal, extra...),
804 fset([pm](object, const D &value) { *pm = value; }, extra...);
805 def_property_static(name, fget, fset);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200806 return *this;
807 }
808
Wenzel Jakob71867832015-07-29 17:43:52 +0200809 template <typename D, typename... Extra>
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100810 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 Jakob71867832015-07-29 17:43:52 +0200813 def_property_readonly_static(name, fget);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200814 return *this;
815 }
816
Wenzel Jakoba576e6a2015-07-29 17:51:54 +0200817 class_ &def_property_readonly(const char *name, const cpp_function &fget, const char *doc = nullptr) {
818 def_property(name, fget, cpp_function(), doc);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200819 return *this;
820 }
821
Wenzel Jakoba576e6a2015-07-29 17:51:54 +0200822 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 Jakob38bd7112015-07-05 20:05:44 +0200824 return *this;
825 }
826
Wenzel Jakoba576e6a2015-07-29 17:51:54 +0200827 class_ &def_property(const char *name, const cpp_function &fget, const cpp_function &fset, const char *doc = nullptr) {
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100828 object doc_obj = doc ? pybind11::str(doc) : (object) fget.attr("__doc__");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200829 object property(
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100830 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 Jakob38bd7112015-07-05 20:05:44 +0200832 attr(name) = property;
833 return *this;
834 }
835
Wenzel Jakoba576e6a2015-07-29 17:51:54 +0200836 class_ &def_property_static(const char *name, const cpp_function &fget, const cpp_function &fset, const char *doc = nullptr) {
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100837 object doc_obj = doc ? pybind11::str(doc) : (object) fget.attr("__doc__");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200838 object property(
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100839 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 Jakob38bd7112015-07-05 20:05:44 +0200841 metaclass().attr(name) = property;
842 return *this;
843 }
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200844
845 template <typename target> class_ alias() {
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100846 auto &instances = pybind11::detail::get_internals().registered_types_cpp;
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200847 instances[&typeid(target)] = instances[&typeid(type)];
848 return *this;
849 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200850private:
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100851 /// 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 Jakob6e213c92015-11-24 23:05:58 +0100854 try {
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100855 new (&inst->holder) holder_type(inst->value->shared_from_this());
Wenzel Jakob6e213c92015-11-24 23:05:58 +0100856 } catch (const std::bad_weak_ptr &) {
857 new (&inst->holder) holder_type(inst->value);
858 }
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100859 }
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 Jakob6e213c92015-11-24 23:05:58 +0100882 inst->constructed = true;
883 }
884
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200885 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 Jakobd561cb02016-01-17 22:36:41 +0100893 generic_type::dealloc((detail::instance<void> *) inst);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200894 }
895};
896
897/// Binds C++ enumerations and enumeration classes to Python
898template <typename Type> class enum_ : public class_<Type> {
899public:
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100900 template <typename... Extra>
901 enum_(const handle &scope, const char *name, const Extra&... extra)
902 : class_<Type>(scope, name, extra...), m_parent(scope) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200903 auto entries = new std::unordered_map<int, const char *>();
Wenzel Jakob8456a4d2015-10-13 02:42:20 +0200904 this->def("__repr__", [name, entries](Type value) -> std::string {
Wenzel Jakoba7500312015-08-29 02:08:32 +0200905 auto it = entries->find((int) value);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200906 return std::string(name) + "." +
907 ((it == entries->end()) ? std::string("???")
908 : std::string(it->second));
909 });
Wenzel Jakob69189222015-10-01 21:32:23 +0200910 this->def("__int__", [](Type value) { return (int) value; });
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200911 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 Jakob27e8e102016-01-17 22:36:37 +0100918 ssize_t pos = 0;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200919 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 Jakob8f4eb002015-10-15 18:13:33 +0200926 this->attr(name) = pybind11::cast(value, return_value_policy::copy);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200927 (*m_entries)[(int) value] = name;
928 return *this;
929 }
930private:
931 std::unordered_map<int, const char *> *m_entries;
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100932 handle m_parent;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200933};
934
935NAMESPACE_BEGIN(detail)
Wenzel Jakob71867832015-07-29 17:43:52 +0200936template <typename... Args> struct init {
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100937 template <typename Base, typename Holder, typename... Extra> void execute(pybind11::class_<Base, Holder> &class_, const Extra&... extra) const {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200938 /// Function which calls a specific C++ in-place constructor
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100939 class_.def("__init__", [](Base *instance, Args... args) { new (instance) Base(args...); }, extra...);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200940 }
941};
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100942
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100943PYBIND11_NOINLINE inline void keep_alive_impl(int Nurse, int Patient, handle args, handle ret) {
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100944 /* Clever approach based on weak references taken from Boost.Python */
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100945 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 Jakob5f218b32016-01-17 22:36:39 +0100947
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100948 if (!nurse || !patient)
Wenzel Jakob678d7872016-01-17 22:36:41 +0100949 pybind11_fail("Could not activate keep_alive!");
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100950
951 cpp_function disable_lifesupport(
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100952 [patient](handle weakref) { patient.dec_ref(); weakref.dec_ref(); });
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100953
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100954 weakref wr(nurse, disable_lifesupport);
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100955
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100956 patient.inc_ref(); /* reference patient and leak the weak reference */
957 (void) wr.release();
Wenzel Jakob5f218b32016-01-17 22:36:39 +0100958}
959
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200960NAMESPACE_END(detail)
961
962template <typename... Args> detail::init<Args...> init() { return detail::init<Args...>(); };
963
964template <typename InputType, typename OutputType> void implicitly_convertible() {
Wenzel Jakob2ac50442016-01-17 22:36:35 +0100965 auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200966 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 Jakobd561cb02016-01-17 22:36:41 +0100975 auto & registered_types = detail::get_internals().registered_types_cpp;
Wenzel Jakobf5fae922015-08-24 15:31:24 +0200976 auto it = registered_types.find(&typeid(OutputType));
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200977 if (it == registered_types.end())
Wenzel Jakob678d7872016-01-17 22:36:41 +0100978 pybind11_fail("implicitly_convertible: Unable to find type " + type_id<OutputType>());
Wenzel Jakobd561cb02016-01-17 22:36:41 +0100979 ((detail::type_info *) it->second)->implicit_conversions.push_back(implicit_caster);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200980}
981
982inline void init_threading() { PyEval_InitThreads(); }
983
984class gil_scoped_acquire {
985 PyGILState_STATE state;
986public:
987 inline gil_scoped_acquire() { state = PyGILState_Ensure(); }
988 inline ~gil_scoped_acquire() { PyGILState_Release(state); }
989};
990
991class gil_scoped_release {
992 PyThreadState *state;
993public:
994 inline gil_scoped_release() { state = PyEval_SaveThread(); }
995 inline ~gil_scoped_release() { PyEval_RestoreThread(state); }
996};
997
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200998inline function get_overload(const void *this_ptr, const char *name) {
999 handle py_object = detail::get_object_handle(this_ptr);
Wenzel Jakobac0fde92015-10-01 18:37:26 +02001000 if (!py_object)
1001 return function();
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001002 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 Jakob27e8e102016-01-17 22:36:37 +01001016
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001017 PyFrameObject *frame = PyThreadState_Get()->frame;
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02001018 pybind11::str caller = pybind11::handle(frame->f_code->co_name).str();
Wenzel Jakob27e8e102016-01-17 22:36:37 +01001019 if ((std::string) caller == name)
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001020 return function();
1021 return overload;
1022}
1023
Wenzel Jakobb1b71402015-10-18 16:48:30 +02001024#define PYBIND11_OVERLOAD_INT(ret_type, class_name, name, ...) { \
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02001025 pybind11::gil_scoped_acquire gil; \
1026 pybind11::function overload = pybind11::get_overload(this, #name); \
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001027 if (overload) \
1028 return overload.call(__VA_ARGS__).cast<ret_type>(); }
1029
Wenzel Jakobb1b71402015-10-18 16:48:30 +02001030#define PYBIND11_OVERLOAD(ret_type, class_name, name, ...) \
1031 PYBIND11_OVERLOAD_INT(ret_type, class_name, name, __VA_ARGS__) \
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001032 return class_name::name(__VA_ARGS__)
1033
Wenzel Jakobb1b71402015-10-18 16:48:30 +02001034#define PYBIND11_OVERLOAD_PURE(ret_type, class_name, name, ...) \
1035 PYBIND11_OVERLOAD_INT(ret_type, class_name, name, __VA_ARGS__) \
Wenzel Jakob678d7872016-01-17 22:36:41 +01001036 pybind11::pybind11_fail("Tried to call pure virtual function \"" #name "\"");
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001037
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02001038NAMESPACE_END(pybind11)
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001039
1040#if defined(_MSC_VER)
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001041# pragma warning(pop)
Wenzel Jakobcd5cda72015-08-03 12:17:54 +02001042#elif defined(__GNUG__) and !defined(__clang__)
Wenzel Jakob48548ea2016-01-17 22:36:44 +01001043# pragma GCC diagnostic pop
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001044#endif