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