Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1 | /* |
| 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 Jakob | bd4a529 | 2015-07-11 17:41:48 +0200 | [diff] [blame] | 10 | #pragma once |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 11 | |
| 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 | |
| 27 | #define PYTHON_PLUGIN(name) \ |
| 28 | extern "C" PYTHON_EXPORT PyObject *PyInit_##name() |
| 29 | |
| 30 | #include <vector> |
| 31 | #include <string> |
| 32 | #include <stdexcept> |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 33 | #include <unordered_map> |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 34 | #include <memory> |
| 35 | |
| 36 | /// Include Python header, disable linking to pythonX_d.lib on Windows in debug mode |
| 37 | #if defined(_MSC_VER) |
| 38 | #define HAVE_ROUND |
| 39 | #pragma warning(push) |
| 40 | #pragma warning(disable: 4510 4610 4512) |
| 41 | #if _DEBUG |
| 42 | #define _DEBUG_MARKER |
| 43 | #undef _DEBUG |
| 44 | #endif |
| 45 | #endif |
| 46 | #include <Python.h> |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame^] | 47 | #ifdef isalnum |
| 48 | #undef isalnum |
| 49 | #undef isalpha |
| 50 | #undef islower |
| 51 | #undef isspace |
| 52 | #undef isupper |
| 53 | #undef tolower |
| 54 | #undef toupper |
| 55 | #endif |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 56 | #if defined(_MSC_VER) |
| 57 | #if defined(_DEBUG_MARKER) |
| 58 | #define _DEBUG |
| 59 | #undef _DEBUG_MARKER |
| 60 | #endif |
| 61 | #pragma warning(pop) |
| 62 | #endif |
| 63 | |
| 64 | NAMESPACE_BEGIN(pybind) |
| 65 | |
| 66 | typedef Py_ssize_t ssize_t; |
| 67 | |
| 68 | /// Approach used to cast a previously unknown C++ instance into a Python object |
| 69 | enum class return_value_policy : int { |
| 70 | /** Automatic: copy objects returned as values and take ownership of objects |
| 71 | returned as pointers */ |
| 72 | automatic = 0, |
| 73 | /** Reference the object and take ownership. Python will call the |
| 74 | destructor and delete operator when the reference count reaches zero */ |
| 75 | take_ownership, |
| 76 | /** Reference the object, but do not take ownership (dangerous when C++ code |
| 77 | deletes it and Python still has a nonzero reference count) */ |
| 78 | reference, |
| 79 | /** Reference the object, but do not take ownership. The object is considered |
| 80 | be owned by the C++ instance whose method or property returned it. The |
| 81 | Python object will increase the reference count of this 'parent' by 1 */ |
| 82 | reference_internal, |
| 83 | /// Create a new copy of the returned object, which will be owned by Python |
| 84 | copy |
| 85 | }; |
| 86 | |
| 87 | /// Format strings for basic number types |
| 88 | template <typename type> struct format_descriptor { }; |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame^] | 89 | #define PYBIND_DECL_FMT(t, n) template<> struct format_descriptor<t> { static std::string value() { return n; }; }; |
| 90 | PYBIND_DECL_FMT(int8_t, "b"); PYBIND_DECL_FMT(uint8_t, "B"); PYBIND_DECL_FMT(int16_t, "h"); PYBIND_DECL_FMT(uint16_t, "H"); |
| 91 | PYBIND_DECL_FMT(int32_t, "i"); PYBIND_DECL_FMT(uint32_t, "I"); PYBIND_DECL_FMT(int64_t, "q"); PYBIND_DECL_FMT(uint64_t, "Q"); |
| 92 | PYBIND_DECL_FMT(float, "f"); PYBIND_DECL_FMT(double, "d"); PYBIND_DECL_FMT(bool, "?"); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 93 | |
| 94 | /// Information record describing a Python buffer object |
| 95 | struct buffer_info { |
| 96 | void *ptr; |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 97 | size_t itemsize, count; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 98 | std::string format; // for dense contents, this should be set to format_descriptor<T>::value |
| 99 | int ndim; |
| 100 | std::vector<size_t> shape; |
| 101 | std::vector<size_t> strides; |
| 102 | |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 103 | buffer_info(void *ptr, size_t itemsize, const std::string &format, int ndim, |
| 104 | const std::vector<size_t> &shape, const std::vector<size_t> &strides) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 105 | : ptr(ptr), itemsize(itemsize), format(format), ndim(ndim), |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 106 | shape(shape), strides(strides) { |
| 107 | count = 1; for (int i=0; i<ndim; ++i) count *= shape[i]; |
| 108 | } |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | // C++ bindings of core Python exceptions |
| 112 | struct stop_iteration : public std::runtime_error { public: stop_iteration(const std::string &w="") : std::runtime_error(w) {} }; |
| 113 | struct index_error : public std::runtime_error { public: index_error(const std::string &w="") : std::runtime_error(w) {} }; |
| 114 | struct error_already_set : public std::exception { public: error_already_set() {} }; |
| 115 | /// Thrown when pybind::cast or handle::call fail due to a type casting error |
| 116 | struct cast_error : public std::runtime_error { public: cast_error(const std::string &w = "") : std::runtime_error(w) {} }; |
| 117 | |
| 118 | NAMESPACE_BEGIN(detail) |
| 119 | |
| 120 | /// PyObject wrapper around generic types |
| 121 | template <typename type, typename holder_type = std::unique_ptr<type>> struct instance { |
| 122 | PyObject_HEAD |
| 123 | type *value; |
| 124 | PyObject *parent; |
| 125 | bool owned : 1; |
| 126 | bool constructed : 1; |
| 127 | holder_type holder; |
| 128 | }; |
| 129 | |
| 130 | /// Additional type information which does not fit into the PyTypeObjet |
| 131 | struct type_info { |
| 132 | PyTypeObject *type; |
| 133 | size_t type_size; |
| 134 | void (*init_holder)(PyObject *); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 135 | std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions; |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 136 | buffer_info *(*get_buffer)(PyObject *, void *) = nullptr; |
| 137 | void *get_buffer_data = nullptr; |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 138 | }; |
| 139 | |
Wenzel Jakob | 43398a8 | 2015-07-28 16:12:20 +0200 | [diff] [blame] | 140 | /// Internal data struture used to track registered instances and types |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 141 | struct internals { |
| 142 | std::unordered_map<std::string, type_info> registered_types; |
| 143 | std::unordered_map<void *, PyObject *> registered_instances; |
| 144 | }; |
| 145 | |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 146 | /// Return a reference to the current 'internals' information |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 147 | inline internals &get_internals(); |
| 148 | |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 149 | /// Index sequence for convenient template metaprogramming involving tuples |
| 150 | template<size_t ...> struct index_sequence { }; |
| 151 | template<size_t N, size_t ...S> struct make_index_sequence : make_index_sequence <N - 1, N - 1, S...> { }; |
| 152 | template<size_t ...S> struct make_index_sequence <0, S...> { typedef index_sequence<S...> type; }; |
| 153 | |
| 154 | /// Strip the class from a method type |
| 155 | template <typename T> struct remove_class {}; |
| 156 | template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...)> { typedef R type(A...); }; |
| 157 | template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...) const> { typedef R type(A...); }; |
| 158 | |
| 159 | /// Helper template to strip away type modifiers |
| 160 | template <typename T> struct decay { typedef T type; }; |
| 161 | template <typename T> struct decay<const T> { typedef typename decay<T>::type type; }; |
| 162 | template <typename T> struct decay<T*> { typedef typename decay<T>::type type; }; |
| 163 | template <typename T> struct decay<T&> { typedef typename decay<T>::type type; }; |
| 164 | template <typename T> struct decay<T&&> { typedef typename decay<T>::type type; }; |
| 165 | template <typename T, size_t N> struct decay<const T[N]> { typedef typename decay<T>::type type; }; |
| 166 | template <typename T, size_t N> struct decay<T[N]> { typedef typename decay<T>::type type; }; |
| 167 | |
| 168 | /// Helper type to replace 'void' in some expressions |
| 169 | struct void_type { }; |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame^] | 170 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 171 | NAMESPACE_END(detail) |
| 172 | NAMESPACE_END(pybind) |