Skip to content

Commit a25021d

Browse files
committed
Initial checkin
[SVN r13137]
1 parent 532833f commit a25021d

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright David Abrahams 2002. Permission to copy, use,
2+
// modify, sell and distribute this software is granted provided this
3+
// copyright notice appears in all copies. This software is provided
4+
// "as is" without express or implied warranty, and with no claim as
5+
// to its suitability for any purpose.
6+
#ifndef RVALUE_FROM_PYTHON_CHAIN_DWA200237_HPP
7+
# define RVALUE_FROM_PYTHON_CHAIN_DWA200237_HPP
8+
# include <boost/python/converter/registry.hpp>
9+
# include <boost/type_traits/transform_traits.hpp>
10+
# include <boost/type_traits/cv_traits.hpp>
11+
12+
namespace boost { namespace python { namespace converter {
13+
14+
namespace detail
15+
{
16+
template <class T>
17+
struct rvalue_from_python_chain_impl
18+
{
19+
static rvalue_from_python_registration*const& value;
20+
};
21+
22+
template <class T>
23+
rvalue_from_python_registration*const& rvalue_from_python_chain_impl<T>::value
24+
= registry::rvalue_converters(undecorated_type_id<T>());
25+
}
26+
27+
template <class T>
28+
struct rvalue_from_python_chain
29+
: detail::rvalue_from_python_chain_impl<
30+
typename add_reference<
31+
typename add_cv<T>::type
32+
>::type
33+
>
34+
{
35+
};
36+
37+
}}} // namespace boost::python::converter
38+
39+
#endif // RVALUE_FROM_PYTHON_CHAIN_DWA200237_HPP

test/callbacks.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright David Abrahams 2002. Permission to copy, use,
2+
// modify, sell and distribute this software is granted provided this
3+
// copyright notice appears in all copies. This software is provided
4+
// "as is" without express or implied warranty, and with no claim as
5+
// to its suitability for any purpose.
6+
#include <boost/python/module.hpp>
7+
#include <boost/python/returning.hpp>
8+
#include <boost/python/class.hpp>
9+
#include <boost/ref.hpp>
10+
11+
using namespace boost::python;
12+
13+
int apply_int_int(PyObject* f, int x)
14+
{
15+
return returning<int>::call(f, x);
16+
}
17+
18+
void apply_void_int(PyObject* f, int x)
19+
{
20+
returning<void>::call(f, x);
21+
}
22+
23+
struct X
24+
{
25+
explicit X(int x) : x(x), magic(7654321) { ++counter; }
26+
X(X const& rhs) : x(rhs.x), magic(7654321) { ++counter; }
27+
~X() { assert(magic == 7654321); magic = 6666666; x = 9999; --counter; }
28+
29+
void set(int x) { assert(magic == 7654321); this->x = x; }
30+
int value() const { assert(magic == 7654321); return x; }
31+
static int count() { return counter; }
32+
private:
33+
void operator=(X const&);
34+
private:
35+
int x;
36+
long magic;
37+
static int counter;
38+
};
39+
40+
X apply_X_X(PyObject* f, X x)
41+
{
42+
return returning<X>::call(f, x);
43+
}
44+
45+
void apply_void_X_ref(PyObject* f, X x)
46+
{
47+
returning<X>::call(f, boost::ref(x));
48+
}
49+
50+
int X::counter;
51+
52+
BOOST_PYTHON_MODULE_INIT(callbacks_ext)
53+
{
54+
boost::python::module("callbacks_ext")
55+
.def("apply_int_int", apply_int_int)
56+
.def("apply_void_int", apply_void_int)
57+
.def("apply_X_X", apply_X_X)
58+
.def("apply_void_X_ref", apply_void_X_ref)
59+
.add(
60+
class_<X>("X")
61+
.def_init(args<int>())
62+
.def_init(args<X const&>())
63+
.def("value", &X::value)
64+
.def("set", &X::set)
65+
)
66+
.def("x_count", &X::count)
67+
;
68+
}
69+
70+
71+

test/callbacks.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
>>> from callbacks_ext import *
3+
4+
>>> def double(x):
5+
... return x + x
6+
...
7+
>>> apply_int_int(double, 42)
8+
84
9+
>>> apply_void_int(double, 42)
10+
11+
>>> def identity(x):
12+
... return x
13+
14+
>>> x = apply_X_X(identity, X(42))
15+
>>> x.value()
16+
42
17+
>>> x_count()
18+
1
19+
>>> del x
20+
>>> x_count()
21+
0
22+
23+
>>> def increment(x):
24+
... x.set(x.value() + 1)
25+
...
26+
>>> x = X(42)
27+
>>> apply_void_X_ref(increment, x)
28+
>>> x.value()
29+
43
30+
'''
31+
32+
def run(args = None):
33+
import sys
34+
import doctest
35+
36+
if args is not None:
37+
sys.argv = args
38+
return doctest.testmod(sys.modules.get(__name__))
39+
40+
if __name__ == '__main__':
41+
print "running..."
42+
import sys
43+
sys.exit(run()[0])

0 commit comments

Comments
 (0)