blob: d1a6b47e741593bcad62b64da2a47914ec5ecde4 [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
27#define PYTHON_PLUGIN(name) \
28 extern "C" PYTHON_EXPORT PyObject *PyInit_##name()
29
30#include <vector>
31#include <string>
32#include <stdexcept>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020033#include <unordered_map>
34#include <iostream>
35#include <memory>
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020036#include <functional>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020037
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
57NAMESPACE_BEGIN(pybind)
58
59typedef Py_ssize_t ssize_t;
60
61/// Approach used to cast a previously unknown C++ instance into a Python object
62enum 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
81template <typename type> struct format_descriptor { };
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020082#define DECL_FMT(t, n) template<> struct format_descriptor<t> { static std::string value() { return n; }; };
83DECL_FMT(int8_t, "b"); DECL_FMT(uint8_t, "B"); DECL_FMT(int16_t, "h"); DECL_FMT(uint16_t, "H");
84DECL_FMT(int32_t, "i"); DECL_FMT(uint32_t, "I"); DECL_FMT(int64_t, "q"); DECL_FMT(uint64_t, "Q");
85DECL_FMT(float , "f"); DECL_FMT(double, "d");
86#undef DECL_FMT
Wenzel Jakob38bd7112015-07-05 20:05:44 +020087
88/// Information record describing a Python buffer object
89struct buffer_info {
90 void *ptr;
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020091 size_t itemsize, count;
Wenzel Jakob38bd7112015-07-05 20:05:44 +020092 std::string format; // for dense contents, this should be set to format_descriptor<T>::value
93 int ndim;
94 std::vector<size_t> shape;
95 std::vector<size_t> strides;
96
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020097 buffer_info(void *ptr, size_t itemsize, const std::string &format, int ndim,
98 const std::vector<size_t> &shape, const std::vector<size_t> &strides)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020099 : ptr(ptr), itemsize(itemsize), format(format), ndim(ndim),
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200100 shape(shape), strides(strides) {
101 count = 1; for (int i=0; i<ndim; ++i) count *= shape[i];
102 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200103};
104
105// C++ bindings of core Python exceptions
106struct stop_iteration : public std::runtime_error { public: stop_iteration(const std::string &w="") : std::runtime_error(w) {} };
107struct index_error : public std::runtime_error { public: index_error(const std::string &w="") : std::runtime_error(w) {} };
108struct error_already_set : public std::exception { public: error_already_set() {} };
109/// Thrown when pybind::cast or handle::call fail due to a type casting error
110struct cast_error : public std::runtime_error { public: cast_error(const std::string &w = "") : std::runtime_error(w) {} };
111
112NAMESPACE_BEGIN(detail)
113
114/// PyObject wrapper around generic types
115template <typename type, typename holder_type = std::unique_ptr<type>> struct instance {
116 PyObject_HEAD
117 type *value;
118 PyObject *parent;
119 bool owned : 1;
120 bool constructed : 1;
121 holder_type holder;
122};
123
124/// Additional type information which does not fit into the PyTypeObjet
125struct type_info {
126 PyTypeObject *type;
127 size_t type_size;
128 void (*init_holder)(PyObject *);
129 std::function<buffer_info *(PyObject *)> get_buffer;
130 std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions;
131};
132
133/// Internal data struture used to track registered instances and types
134struct internals {
135 std::unordered_map<std::string, type_info> registered_types;
136 std::unordered_map<void *, PyObject *> registered_instances;
137};
138
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200139/// Return a reference to the current 'internals' information
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200140inline internals &get_internals();
141
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200142/// Index sequence for convenient template metaprogramming involving tuples
143template<size_t ...> struct index_sequence { };
144template<size_t N, size_t ...S> struct make_index_sequence : make_index_sequence <N - 1, N - 1, S...> { };
145template<size_t ...S> struct make_index_sequence <0, S...> { typedef index_sequence<S...> type; };
146
147/// Strip the class from a method type
148template <typename T> struct remove_class {};
149template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...)> { typedef R type(A...); };
150template <typename C, typename R, typename... A> struct remove_class<R (C::*)(A...) const> { typedef R type(A...); };
151
152/// Helper template to strip away type modifiers
153template <typename T> struct decay { typedef T type; };
154template <typename T> struct decay<const T> { typedef typename decay<T>::type type; };
155template <typename T> struct decay<T*> { typedef typename decay<T>::type type; };
156template <typename T> struct decay<T&> { typedef typename decay<T>::type type; };
157template <typename T> struct decay<T&&> { typedef typename decay<T>::type type; };
158template <typename T, size_t N> struct decay<const T[N]> { typedef typename decay<T>::type type; };
159template <typename T, size_t N> struct decay<T[N]> { typedef typename decay<T>::type type; };
160
161/// Helper type to replace 'void' in some expressions
162struct void_type { };
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200163NAMESPACE_END(detail)
164NAMESPACE_END(pybind)