Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/python/test/callbacks.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 3.6 KB
RevLine 
[12]1// Copyright David Abrahams 2002.
2// Distributed under the Boost Software License, Version 1.0. (See
3// accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5#include <boost/python/module.hpp>
6#include <boost/python/def.hpp>
7#include <boost/python/class.hpp>
8#include <boost/ref.hpp>
9#include <boost/python/ptr.hpp>
10#include <boost/python/return_value_policy.hpp>
11#include <boost/python/reference_existing_object.hpp>
12#include <boost/python/call.hpp>
13#include <boost/python/object.hpp>
14
15using namespace boost::python;
16BOOST_STATIC_ASSERT(converter::is_object_manager<handle<> >::value);
17
18int apply_int_int(PyObject* f, int x)
19{
20    return call<int>(f, x);
21}
22
23void apply_void_int(PyObject* f, int x)
24{
25    call<void>(f, x);
26}
27
28struct X
29{
30    explicit X(int x) : x(x), magic(7654321) { ++counter; }
31    X(X const& rhs) : x(rhs.x), magic(7654321) { ++counter; }
32    ~X() { assert(magic == 7654321); magic = 6666666; x = 9999; --counter; }
33
34    void set(int x) { assert(magic == 7654321); this->x = x; }
35    int value() const { assert(magic == 7654321); return x; }
36    static int count() { return counter; }
37 private:
38    void operator=(X const&);
39 private:
40    int x;
41    long magic;
42    static int counter;
43};
44
45X apply_X_X(PyObject* f, X x)
46{
47    return call<X>(f, x);
48}
49
50void apply_void_X_ref(PyObject* f, X& x)
51{
52    call<void>(f, boost::ref(x));
53}
54
55X& apply_X_ref_handle(PyObject* f, handle<> obj)
56{
57    return call<X&>(f, obj);
58}
59
60X* apply_X_ptr_handle_cref(PyObject* f, handle<> const& obj)
61{
62    return call<X*>(f, obj);
63}
64
65void apply_void_X_cref(PyObject* f, X const& x)
66{
67    call<void>(f, boost::cref(x));
68}
69
70void apply_void_X_ptr(PyObject* f, X* x)
71{
72    call<void>(f, ptr(x));
73}
74
75void apply_void_X_deep_ptr(PyObject* f, X* x)
76{
77    call<void>(f, x);
78}
79
80char const* apply_cstring_cstring(PyObject* f, char const* s)
81{
82    return call<char const*>(f, s);
83}
84
85char const* apply_cstring_pyobject(PyObject* f, PyObject* s)
86{
87    return call<char const*>(f, borrowed(s));
88}
89
90char apply_char_char(PyObject* f, char c)
91{
92    return call<char>(f, c);
93}
94
95char const* apply_to_string_literal(PyObject* f)
96{
97    return call<char const*>(f, "hello, world");
98}
99
100handle<> apply_to_own_type(handle<> x)
101{
102    // Tests that we can return handle<> from a callback and that we
103    // can pass arbitrary handle<T>.
104    return call<handle<> >(x.get(), type_handle(borrowed(x->ob_type)));
105}
106
107object apply_object_object(PyObject* f, object x)
108{
109    return call<object>(f, x);
110}
111
112int X::counter;
113
114BOOST_PYTHON_MODULE(callbacks_ext)
115{
116    def("apply_object_object", apply_object_object);
117    def("apply_to_own_type", apply_to_own_type);
118    def("apply_int_int", apply_int_int);
119    def("apply_void_int", apply_void_int);
120    def("apply_X_X", apply_X_X);
121    def("apply_void_X_ref", apply_void_X_ref);
122    def("apply_void_X_cref", apply_void_X_cref);
123    def("apply_void_X_ptr", apply_void_X_ptr);
124    def("apply_void_X_deep_ptr", apply_void_X_deep_ptr);
125       
126    def("apply_X_ptr_handle_cref", apply_X_ptr_handle_cref
127        , return_value_policy<reference_existing_object>());
128       
129    def("apply_X_ref_handle", apply_X_ref_handle
130        , return_value_policy<reference_existing_object>());
131       
132    def("apply_cstring_cstring", apply_cstring_cstring);
133    def("apply_cstring_pyobject", apply_cstring_pyobject);
134    def("apply_char_char", apply_char_char);
135    def("apply_to_string_literal", apply_to_string_literal);
136       
137   
138    class_<X>("X", init<int>())
139        .def(init<X const&>())
140        .def("value", &X::value)
141        .def("set", &X::set)
142        ;
143
144    def("x_count", &X::count);
145}
146
147#include "module_tail.cpp"
Note: See TracBrowser for help on using the repository browser.