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 | |
| 10 | #if !defined(__PYBIND_COMMON_H) |
| 11 | #define __PYBIND_COMMON_H |
| 12 | |
| 13 | #if !defined(NAMESPACE_BEGIN) |
| 14 | #define NAMESPACE_BEGIN(name) namespace name { |
| 15 | #endif |
| 16 | #if !defined(NAMESPACE_END) |
| 17 | #define NAMESPACE_END(name) } |
| 18 | #endif |
| 19 | |
| 20 | #if !defined(PYTHON_EXPORT) |
| 21 | #if defined(WIN32) |
| 22 | #define PYTHON_EXPORT __declspec(dllexport) |
| 23 | #else |
| 24 | #define PYTHON_EXPORT __attribute__ ((visibility("default"))) |
| 25 | #endif |
| 26 | #endif |
| 27 | |
| 28 | #define PYTHON_PLUGIN(name) \ |
| 29 | extern "C" PYTHON_EXPORT PyObject *PyInit_##name() |
| 30 | |
| 31 | #include <vector> |
| 32 | #include <string> |
| 33 | #include <stdexcept> |
| 34 | #include <functional> |
| 35 | #include <unordered_map> |
| 36 | #include <iostream> |
| 37 | #include <memory> |
| 38 | |
| 39 | /// Include Python header, disable linking to pythonX_d.lib on Windows in debug mode |
| 40 | #if defined(_MSC_VER) |
| 41 | #define HAVE_ROUND |
| 42 | #pragma warning(push) |
| 43 | #pragma warning(disable: 4510 4610 4512) |
| 44 | #if _DEBUG |
| 45 | #define _DEBUG_MARKER |
| 46 | #undef _DEBUG |
| 47 | #endif |
| 48 | #endif |
| 49 | #include <Python.h> |
| 50 | #if defined(_MSC_VER) |
| 51 | #if defined(_DEBUG_MARKER) |
| 52 | #define _DEBUG |
| 53 | #undef _DEBUG_MARKER |
| 54 | #endif |
| 55 | #pragma warning(pop) |
| 56 | #endif |
| 57 | |
| 58 | NAMESPACE_BEGIN(pybind) |
| 59 | |
| 60 | typedef Py_ssize_t ssize_t; |
| 61 | |
| 62 | /// Approach used to cast a previously unknown C++ instance into a Python object |
| 63 | enum class return_value_policy : int { |
| 64 | /** Automatic: copy objects returned as values and take ownership of objects |
| 65 | returned as pointers */ |
| 66 | automatic = 0, |
| 67 | /** Reference the object and take ownership. Python will call the |
| 68 | destructor and delete operator when the reference count reaches zero */ |
| 69 | take_ownership, |
| 70 | /** Reference the object, but do not take ownership (dangerous when C++ code |
| 71 | deletes it and Python still has a nonzero reference count) */ |
| 72 | reference, |
| 73 | /** Reference the object, but do not take ownership. The object is considered |
| 74 | be owned by the C++ instance whose method or property returned it. The |
| 75 | Python object will increase the reference count of this 'parent' by 1 */ |
| 76 | reference_internal, |
| 77 | /// Create a new copy of the returned object, which will be owned by Python |
| 78 | copy |
| 79 | }; |
| 80 | |
| 81 | /// Format strings for basic number types |
| 82 | template <typename type> struct format_descriptor { }; |
| 83 | template<> struct format_descriptor<int8_t> { static std::string value() { return "b"; }; }; |
| 84 | template<> struct format_descriptor<uint8_t> { static std::string value() { return "B"; }; }; |
| 85 | template<> struct format_descriptor<int16_t> { static std::string value() { return "h"; }; }; |
| 86 | template<> struct format_descriptor<uint16_t> { static std::string value() { return "H"; }; }; |
| 87 | template<> struct format_descriptor<int32_t> { static std::string value() { return "i"; }; }; |
| 88 | template<> struct format_descriptor<uint32_t> { static std::string value() { return "I"; }; }; |
| 89 | template<> struct format_descriptor<int64_t> { static std::string value() { return "q"; }; }; |
| 90 | template<> struct format_descriptor<uint64_t> { static std::string value() { return "Q"; }; }; |
| 91 | template<> struct format_descriptor<float> { static std::string value() { return "f"; }; }; |
| 92 | template<> struct format_descriptor<double> { static std::string value() { return "d"; }; }; |
| 93 | |
| 94 | /// Information record describing a Python buffer object |
| 95 | struct buffer_info { |
| 96 | void *ptr; |
| 97 | size_t itemsize; |
| 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 | |
| 103 | buffer_info(void *ptr, size_t itemsize, const std::string &format, |
| 104 | int ndim, const std::vector<size_t> &shape, |
| 105 | const std::vector<size_t> &strides) |
| 106 | : ptr(ptr), itemsize(itemsize), format(format), ndim(ndim), |
| 107 | shape(shape), strides(strides) {} |
| 108 | }; |
| 109 | |
| 110 | // C++ bindings of core Python exceptions |
| 111 | struct stop_iteration : public std::runtime_error { public: stop_iteration(const std::string &w="") : std::runtime_error(w) {} }; |
| 112 | struct index_error : public std::runtime_error { public: index_error(const std::string &w="") : std::runtime_error(w) {} }; |
| 113 | struct error_already_set : public std::exception { public: error_already_set() {} }; |
| 114 | /// Thrown when pybind::cast or handle::call fail due to a type casting error |
| 115 | struct cast_error : public std::runtime_error { public: cast_error(const std::string &w = "") : std::runtime_error(w) {} }; |
| 116 | |
| 117 | NAMESPACE_BEGIN(detail) |
| 118 | |
| 119 | /// PyObject wrapper around generic types |
| 120 | template <typename type, typename holder_type = std::unique_ptr<type>> struct instance { |
| 121 | PyObject_HEAD |
| 122 | type *value; |
| 123 | PyObject *parent; |
| 124 | bool owned : 1; |
| 125 | bool constructed : 1; |
| 126 | holder_type holder; |
| 127 | }; |
| 128 | |
| 129 | /// Additional type information which does not fit into the PyTypeObjet |
| 130 | struct type_info { |
| 131 | PyTypeObject *type; |
| 132 | size_t type_size; |
| 133 | void (*init_holder)(PyObject *); |
| 134 | std::function<buffer_info *(PyObject *)> get_buffer; |
| 135 | std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions; |
| 136 | }; |
| 137 | |
| 138 | /// Internal data struture used to track registered instances and types |
| 139 | struct internals { |
| 140 | std::unordered_map<std::string, type_info> registered_types; |
| 141 | std::unordered_map<void *, PyObject *> registered_instances; |
| 142 | }; |
| 143 | |
| 144 | inline internals &get_internals(); |
| 145 | |
| 146 | NAMESPACE_END(detail) |
| 147 | NAMESPACE_END(pybind) |
| 148 | |
| 149 | #endif /* __PYBIND_COMMON_H */ |