forked from boostorg/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbacks.cpp
More file actions
128 lines (106 loc) · 3.19 KB
/
callbacks.cpp
File metadata and controls
128 lines (106 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright David Abrahams 2002. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#include <boost/python/module.hpp>
//#include <boost/python/returning.hpp>
#include <boost/python/class.hpp>
#include <boost/ref.hpp>
#include <boost/python/ptr.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/reference_existing_object.hpp>
#include <boost/python/call.hpp>
using namespace boost::python;
int apply_int_int(PyObject* f, int x)
{
return call<int>(f, x);
}
void apply_void_int(PyObject* f, int x)
{
call<void>(f, x);
}
struct X
{
explicit X(int x) : x(x), magic(7654321) { ++counter; }
X(X const& rhs) : x(rhs.x), magic(7654321) { ++counter; }
~X() { assert(magic == 7654321); magic = 6666666; x = 9999; --counter; }
void set(int x) { assert(magic == 7654321); this->x = x; }
int value() const { assert(magic == 7654321); return x; }
static int count() { return counter; }
private:
void operator=(X const&);
private:
int x;
long magic;
static int counter;
};
X apply_X_X(PyObject* f, X x)
{
return call<X>(f, x);
}
void apply_void_X_ref(PyObject* f, X& x)
{
call<void>(f, boost::ref(x));
}
X& apply_X_ref_pyobject(PyObject* f, PyObject* obj)
{
return call<X&>(f, obj);
}
X* apply_X_ptr_pyobject(PyObject* f, PyObject* obj)
{
return call<X*>(f, obj);
}
void apply_void_X_cref(PyObject* f, X const& x)
{
call<void>(f, boost::cref(x));
}
void apply_void_X_ptr(PyObject* f, X* x)
{
call<void>(f, ptr(x));
}
void apply_void_X_deep_ptr(PyObject* f, X* x)
{
call<void>(f, x);
}
char const* apply_cstring_cstring(PyObject* f, char const* s)
{
return call<char const*>(f, s);
}
char const* apply_cstring_pyobject(PyObject* f, PyObject* s)
{
return call<char const*>(f, s);
}
char apply_char_char(PyObject* f, char c)
{
return call<char>(f, c);
}
int X::counter;
BOOST_PYTHON_MODULE_INIT(callbacks_ext)
{
boost::python::module("callbacks_ext")
.def("apply_int_int", apply_int_int)
.def("apply_void_int", apply_void_int)
.def("apply_X_X", apply_X_X)
.def("apply_void_X_ref", apply_void_X_ref)
.def("apply_void_X_cref", apply_void_X_cref)
.def("apply_void_X_ptr", apply_void_X_ptr)
.def("apply_void_X_deep_ptr", apply_void_X_deep_ptr)
.def("apply_X_ptr_pyobject", apply_X_ptr_pyobject
, return_value_policy<reference_existing_object>())
.def("apply_X_ref_pyobject", apply_X_ref_pyobject
, return_value_policy<reference_existing_object>())
.def("apply_cstring_cstring", apply_cstring_cstring)
.def("apply_cstring_pyobject", apply_cstring_pyobject)
.def("apply_char_char", apply_char_char)
.add(
class_<X>("X")
.def_init(args<int>())
.def_init(args<X const&>())
.def("value", &X::value)
.def("set", &X::set)
)
.def("x_count", &X::count)
;
}
#include "module_tail.cpp"