blob: bebcd0725fea767988fae4d3732341ba30a7f9e6 [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Wenzel Jakob8f4eb002015-10-15 18:13:33 +02002 pybind11/common.h -- Basic macros
Wenzel Jakob38bd7112015-07-05 20:05:44 +02003
4 Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
Wenzel Jakobbd4a5292015-07-11 17:41:48 +020010#pragma once
Wenzel Jakob38bd7112015-07-05 20:05:44 +020011
12#if !defined(NAMESPACE_BEGIN)
13#define NAMESPACE_BEGIN(name) namespace name {
14#endif
15#if !defined(NAMESPACE_END)
16#define NAMESPACE_END(name) }
17#endif
18
Wenzel Jakobb1b71402015-10-18 16:48:30 +020019#if !defined(PYBIND11_EXPORT)
Wenzel Jakob6c698fe2015-10-01 16:47:21 +020020#if defined(WIN32) || defined(_WIN32)
Wenzel Jakobb1b71402015-10-18 16:48:30 +020021#define PYBIND11_EXPORT __declspec(dllexport)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020022#else
Wenzel Jakobb1b71402015-10-18 16:48:30 +020023#define PYBIND11_EXPORT __attribute__ ((visibility("default")))
Wenzel Jakob38bd7112015-07-05 20:05:44 +020024#endif
25#endif
Wenzel Jakob0fb85282015-10-19 23:50:51 +020026#if defined(_MSC_VER)
27#define PYBIND11_NOINLINE __declspec(noinline)
28#else
29#define PYBIND11_NOINLINE __attribute__ ((noinline))
30#endif
31
Wenzel Jakob38bd7112015-07-05 20:05:44 +020032
Wenzel Jakob38bd7112015-07-05 20:05:44 +020033#include <vector>
34#include <string>
35#include <stdexcept>
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020036#include <unordered_set>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020037#include <unordered_map>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020038#include <memory>
39
40/// Include Python header, disable linking to pythonX_d.lib on Windows in debug mode
41#if defined(_MSC_VER)
42#define HAVE_ROUND
43#pragma warning(push)
Wenzel Jakob57082212015-09-04 23:42:12 +020044#pragma warning(disable: 4510 4610 4512 4005)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020045#if _DEBUG
46#define _DEBUG_MARKER
47#undef _DEBUG
48#endif
49#endif
50#include <Python.h>
Wenzel Jakob96c10532015-10-01 16:42:15 +020051#include <frameobject.h>
Wenzel Jakob281aa0e2015-07-30 15:29:00 +020052#ifdef isalnum
53#undef isalnum
54#undef isalpha
55#undef islower
56#undef isspace
57#undef isupper
58#undef tolower
59#undef toupper
60#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +020061#if defined(_MSC_VER)
62#if defined(_DEBUG_MARKER)
63#define _DEBUG
64#undef _DEBUG_MARKER
65#endif
66#pragma warning(pop)
67#endif
68
Wenzel Jakob57082212015-09-04 23:42:12 +020069#if PY_MAJOR_VERSION >= 3
Wenzel Jakobdd57a342015-12-26 14:04:52 +010070#define PYBIND11_PLUGIN_IMPL(name) \
Wenzel Jakobb1b71402015-10-18 16:48:30 +020071 extern "C" PYBIND11_EXPORT PyObject *PyInit_##name()
Wenzel Jakob57082212015-09-04 23:42:12 +020072#else
Wenzel Jakobdd57a342015-12-26 14:04:52 +010073#define PYBIND11_PLUGIN_IMPL(name) \
Wenzel Jakobb1b71402015-10-18 16:48:30 +020074 extern "C" PYBIND11_EXPORT PyObject *init##name()
Wenzel Jakob57082212015-09-04 23:42:12 +020075#endif
Wenzel Jakobdd57a342015-12-26 14:04:52 +010076#define PYBIND11_PLUGIN(name) \
77 static PyObject *pybind11_init(); \
78 PYBIND11_PLUGIN_IMPL(name) { \
79 try { \
80 return pybind11_init(); \
81 } catch (const std::exception &e) { \
82 PyErr_SetString(PyExc_ImportError, e.what()); \
83 return nullptr; \
84 } \
85 } \
86 PyObject *pybind11_init()
87
Wenzel Jakob57082212015-09-04 23:42:12 +020088
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020089NAMESPACE_BEGIN(pybind11)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020090
91typedef Py_ssize_t ssize_t;
92
93/// Approach used to cast a previously unknown C++ instance into a Python object
94enum class return_value_policy : int {
95 /** Automatic: copy objects returned as values and take ownership of objects
96 returned as pointers */
97 automatic = 0,
98 /** Reference the object and take ownership. Python will call the
99 destructor and delete operator when the reference count reaches zero */
100 take_ownership,
101 /** Reference the object, but do not take ownership (dangerous when C++ code
102 deletes it and Python still has a nonzero reference count) */
103 reference,
104 /** Reference the object, but do not take ownership. The object is considered
105 be owned by the C++ instance whose method or property returned it. The
106 Python object will increase the reference count of this 'parent' by 1 */
107 reference_internal,
108 /// Create a new copy of the returned object, which will be owned by Python
109 copy
110};
111
112/// Format strings for basic number types
113template <typename type> struct format_descriptor { };
Wenzel Jakobb1b71402015-10-18 16:48:30 +0200114#define PYBIND11_DECL_FMT(t, n) template<> struct format_descriptor<t> { static std::string value() { return n; }; };
115PYBIND11_DECL_FMT(int8_t, "b"); PYBIND11_DECL_FMT(uint8_t, "B"); PYBIND11_DECL_FMT(int16_t, "h"); PYBIND11_DECL_FMT(uint16_t, "H");
116PYBIND11_DECL_FMT(int32_t, "i"); PYBIND11_DECL_FMT(uint32_t, "I"); PYBIND11_DECL_FMT(int64_t, "q"); PYBIND11_DECL_FMT(uint64_t, "Q");
117PYBIND11_DECL_FMT(float, "f"); PYBIND11_DECL_FMT(double, "d"); PYBIND11_DECL_FMT(bool, "?");
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200118
119/// Information record describing a Python buffer object
120struct buffer_info {
121 void *ptr;
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200122 size_t itemsize, count;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200123 std::string format; // for dense contents, this should be set to format_descriptor<T>::value
124 int ndim;
125 std::vector<size_t> shape;
126 std::vector<size_t> strides;
127
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200128 buffer_info(void *ptr, size_t itemsize, const std::string &format, int ndim,
129 const std::vector<size_t> &shape, const std::vector<size_t> &strides)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200130 : ptr(ptr), itemsize(itemsize), format(format), ndim(ndim),
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200131 shape(shape), strides(strides) {
132 count = 1; for (int i=0; i<ndim; ++i) count *= shape[i];
133 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200134};
135
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200136NAMESPACE_BEGIN(detail)
137
Wenzel Jakob96c10532015-10-01 16:42:15 +0200138inline std::string error_string();
139
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200140/// PyObject wrapper around generic types
141template <typename type, typename holder_type = std::unique_ptr<type>> struct instance {
142 PyObject_HEAD
143 type *value;
144 PyObject *parent;
145 bool owned : 1;
146 bool constructed : 1;
147 holder_type holder;
148};
149
Wenzel Jakobfa1bfb22015-10-22 16:03:44 +0200150/// Additional type information which does not fit into the PyTypeObject
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200151struct type_info {
152 PyTypeObject *type;
153 size_t type_size;
154 void (*init_holder)(PyObject *);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200155 std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions;
Wenzel Jakob43398a82015-07-28 16:12:20 +0200156 buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
157 void *get_buffer_data = nullptr;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200158};
159
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200160struct overload_hash {
161 inline std::size_t operator()(const std::pair<const PyObject *, const char *>& v) const {
162 size_t value = std::hash<const void *>()(v.first);
163 value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value<<6) + (value>>2);
164 return value;
165 }
166};
167
Wenzel Jakob43398a82015-07-28 16:12:20 +0200168/// Internal data struture used to track registered instances and types
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200169struct internals {
Wenzel Jakobf5fae922015-08-24 15:31:24 +0200170 std::unordered_map<const std::type_info *, type_info> registered_types;
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200171 std::unordered_map<const void *, PyObject *> registered_instances;
172 std::unordered_set<std::pair<const PyObject *, const char *>, overload_hash> inactive_overload_cache;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200173};
174
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200175/// Return a reference to the current 'internals' information
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200176inline internals &get_internals();
177
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200178/// Index sequence for convenient template metaprogramming involving tuples
179template<size_t ...> struct index_sequence { };
180template<size_t N, size_t ...S> struct make_index_sequence : make_index_sequence <N - 1, N - 1, S...> { };
181template<size_t ...S> struct make_index_sequence <0, S...> { typedef index_sequence<S...> type; };
182
183/// Strip the class from a method type
184template <typename T> struct remove_class {};
185template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...)> { typedef R type(A...); };
186template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...) const> { typedef R type(A...); };
187
188/// Helper template to strip away type modifiers
189template <typename T> struct decay { typedef T type; };
190template <typename T> struct decay<const T> { typedef typename decay<T>::type type; };
191template <typename T> struct decay<T*> { typedef typename decay<T>::type type; };
192template <typename T> struct decay<T&> { typedef typename decay<T>::type type; };
193template <typename T> struct decay<T&&> { typedef typename decay<T>::type type; };
194template <typename T, size_t N> struct decay<const T[N]> { typedef typename decay<T>::type type; };
195template <typename T, size_t N> struct decay<T[N]> { typedef typename decay<T>::type type; };
196
197/// Helper type to replace 'void' in some expressions
198struct void_type { };
Wenzel Jakob281aa0e2015-07-30 15:29:00 +0200199
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200200/// to_string variant which also accepts strings
201template <typename T> inline typename std::enable_if<!std::is_enum<T>::value, std::string>::type
202to_string(const T &value) { return std::to_string(value); }
203template <> inline std::string to_string(const std::string &value) { return value; }
204template <typename T> inline typename std::enable_if<std::is_enum<T>::value, std::string>::type
205to_string(T value) { return std::to_string((int) value); }
206
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200207NAMESPACE_END(detail)
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200208
209// C++ bindings of core Python exceptions
210struct stop_iteration : public std::runtime_error { public: stop_iteration(const std::string &w="") : std::runtime_error(w) {} };
211struct index_error : public std::runtime_error { public: index_error(const std::string &w="") : std::runtime_error(w) {} };
212struct error_already_set : public std::runtime_error { public: error_already_set() : std::runtime_error(detail::error_string()) {} };
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200213/// Thrown when pybind11::cast or handle::call fail due to a type casting error
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200214struct cast_error : public std::runtime_error { public: cast_error(const std::string &w = "") : std::runtime_error(w) {} };
215
Wenzel Jakob8f4eb002015-10-15 18:13:33 +0200216NAMESPACE_END(pybind11)