blob: d327ab6af580b5c777494b9daf2a588d5ef39aeb [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
2 pybind/common.h -- Basic macros
3
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
19#if !defined(PYTHON_EXPORT)
20#if defined(WIN32)
21#define PYTHON_EXPORT __declspec(dllexport)
22#else
23#define PYTHON_EXPORT __attribute__ ((visibility("default")))
24#endif
25#endif
26
Wenzel Jakob38bd7112015-07-05 20:05:44 +020027#include <vector>
28#include <string>
29#include <stdexcept>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020030#include <unordered_map>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020031#include <memory>
32
33/// Include Python header, disable linking to pythonX_d.lib on Windows in debug mode
34#if defined(_MSC_VER)
35#define HAVE_ROUND
36#pragma warning(push)
Wenzel Jakob57082212015-09-04 23:42:12 +020037#pragma warning(disable: 4510 4610 4512 4005)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020038#if _DEBUG
39#define _DEBUG_MARKER
40#undef _DEBUG
41#endif
42#endif
43#include <Python.h>
Wenzel Jakob281aa0e2015-07-30 15:29:00 +020044#ifdef isalnum
45#undef isalnum
46#undef isalpha
47#undef islower
48#undef isspace
49#undef isupper
50#undef tolower
51#undef toupper
52#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +020053#if defined(_MSC_VER)
54#if defined(_DEBUG_MARKER)
55#define _DEBUG
56#undef _DEBUG_MARKER
57#endif
58#pragma warning(pop)
59#endif
60
Wenzel Jakob57082212015-09-04 23:42:12 +020061#if PY_MAJOR_VERSION >= 3
62#define PYTHON_PLUGIN(name) \
63 extern "C" PYTHON_EXPORT PyObject *PyInit_##name()
64#else
65#define PYTHON_PLUGIN(name) \
66 extern "C" PYTHON_EXPORT PyObject *init##name()
67#endif
68
Wenzel Jakob38bd7112015-07-05 20:05:44 +020069NAMESPACE_BEGIN(pybind)
70
71typedef Py_ssize_t ssize_t;
72
73/// Approach used to cast a previously unknown C++ instance into a Python object
74enum class return_value_policy : int {
75 /** Automatic: copy objects returned as values and take ownership of objects
76 returned as pointers */
77 automatic = 0,
78 /** Reference the object and take ownership. Python will call the
79 destructor and delete operator when the reference count reaches zero */
80 take_ownership,
81 /** Reference the object, but do not take ownership (dangerous when C++ code
82 deletes it and Python still has a nonzero reference count) */
83 reference,
84 /** Reference the object, but do not take ownership. The object is considered
85 be owned by the C++ instance whose method or property returned it. The
86 Python object will increase the reference count of this 'parent' by 1 */
87 reference_internal,
88 /// Create a new copy of the returned object, which will be owned by Python
89 copy
90};
91
92/// Format strings for basic number types
93template <typename type> struct format_descriptor { };
Wenzel Jakob281aa0e2015-07-30 15:29:00 +020094#define PYBIND_DECL_FMT(t, n) template<> struct format_descriptor<t> { static std::string value() { return n; }; };
95PYBIND_DECL_FMT(int8_t, "b"); PYBIND_DECL_FMT(uint8_t, "B"); PYBIND_DECL_FMT(int16_t, "h"); PYBIND_DECL_FMT(uint16_t, "H");
96PYBIND_DECL_FMT(int32_t, "i"); PYBIND_DECL_FMT(uint32_t, "I"); PYBIND_DECL_FMT(int64_t, "q"); PYBIND_DECL_FMT(uint64_t, "Q");
97PYBIND_DECL_FMT(float, "f"); PYBIND_DECL_FMT(double, "d"); PYBIND_DECL_FMT(bool, "?");
Wenzel Jakob38bd7112015-07-05 20:05:44 +020098
99/// Information record describing a Python buffer object
100struct buffer_info {
101 void *ptr;
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200102 size_t itemsize, count;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200103 std::string format; // for dense contents, this should be set to format_descriptor<T>::value
104 int ndim;
105 std::vector<size_t> shape;
106 std::vector<size_t> strides;
107
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200108 buffer_info(void *ptr, size_t itemsize, const std::string &format, int ndim,
109 const std::vector<size_t> &shape, const std::vector<size_t> &strides)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200110 : ptr(ptr), itemsize(itemsize), format(format), ndim(ndim),
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200111 shape(shape), strides(strides) {
112 count = 1; for (int i=0; i<ndim; ++i) count *= shape[i];
113 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200114};
115
116// C++ bindings of core Python exceptions
117struct stop_iteration : public std::runtime_error { public: stop_iteration(const std::string &w="") : std::runtime_error(w) {} };
118struct index_error : public std::runtime_error { public: index_error(const std::string &w="") : std::runtime_error(w) {} };
119struct error_already_set : public std::exception { public: error_already_set() {} };
120/// Thrown when pybind::cast or handle::call fail due to a type casting error
121struct cast_error : public std::runtime_error { public: cast_error(const std::string &w = "") : std::runtime_error(w) {} };
122
123NAMESPACE_BEGIN(detail)
124
125/// PyObject wrapper around generic types
126template <typename type, typename holder_type = std::unique_ptr<type>> struct instance {
127 PyObject_HEAD
128 type *value;
129 PyObject *parent;
130 bool owned : 1;
131 bool constructed : 1;
132 holder_type holder;
133};
134
135/// Additional type information which does not fit into the PyTypeObjet
136struct type_info {
137 PyTypeObject *type;
138 size_t type_size;
139 void (*init_holder)(PyObject *);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200140 std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions;
Wenzel Jakob43398a82015-07-28 16:12:20 +0200141 buffer_info *(*get_buffer)(PyObject *, void *) = nullptr;
142 void *get_buffer_data = nullptr;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200143};
144
Wenzel Jakob43398a82015-07-28 16:12:20 +0200145/// Internal data struture used to track registered instances and types
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200146struct internals {
Wenzel Jakobf5fae922015-08-24 15:31:24 +0200147 std::unordered_map<const std::type_info *, type_info> registered_types;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200148 std::unordered_map<void *, PyObject *> registered_instances;
149};
150
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200151/// Return a reference to the current 'internals' information
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200152inline internals &get_internals();
153
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200154/// Index sequence for convenient template metaprogramming involving tuples
155template<size_t ...> struct index_sequence { };
156template<size_t N, size_t ...S> struct make_index_sequence : make_index_sequence <N - 1, N - 1, S...> { };
157template<size_t ...S> struct make_index_sequence <0, S...> { typedef index_sequence<S...> type; };
158
159/// Strip the class from a method type
160template <typename T> struct remove_class {};
161template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...)> { typedef R type(A...); };
162template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...) const> { typedef R type(A...); };
163
164/// Helper template to strip away type modifiers
165template <typename T> struct decay { typedef T type; };
166template <typename T> struct decay<const T> { typedef typename decay<T>::type type; };
167template <typename T> struct decay<T*> { typedef typename decay<T>::type type; };
168template <typename T> struct decay<T&> { typedef typename decay<T>::type type; };
169template <typename T> struct decay<T&&> { typedef typename decay<T>::type type; };
170template <typename T, size_t N> struct decay<const T[N]> { typedef typename decay<T>::type type; };
171template <typename T, size_t N> struct decay<T[N]> { typedef typename decay<T>::type type; };
172
173/// Helper type to replace 'void' in some expressions
174struct void_type { };
Wenzel Jakob281aa0e2015-07-30 15:29:00 +0200175
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200176NAMESPACE_END(detail)
177NAMESPACE_END(pybind)