mirror of
https://github.com/boostorg/python.git
synced 2026-01-26 06:42:27 +00:00
Initial checkin
[SVN r13137]
This commit is contained in:
39
include/boost/python/converter/rvalue_from_python_chain.hpp
Normal file
39
include/boost/python/converter/rvalue_from_python_chain.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
// 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.
|
||||
#ifndef RVALUE_FROM_PYTHON_CHAIN_DWA200237_HPP
|
||||
# define RVALUE_FROM_PYTHON_CHAIN_DWA200237_HPP
|
||||
# include <boost/python/converter/registry.hpp>
|
||||
# include <boost/type_traits/transform_traits.hpp>
|
||||
# include <boost/type_traits/cv_traits.hpp>
|
||||
|
||||
namespace boost { namespace python { namespace converter {
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <class T>
|
||||
struct rvalue_from_python_chain_impl
|
||||
{
|
||||
static rvalue_from_python_registration*const& value;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
rvalue_from_python_registration*const& rvalue_from_python_chain_impl<T>::value
|
||||
= registry::rvalue_converters(undecorated_type_id<T>());
|
||||
}
|
||||
|
||||
template <class T>
|
||||
struct rvalue_from_python_chain
|
||||
: detail::rvalue_from_python_chain_impl<
|
||||
typename add_reference<
|
||||
typename add_cv<T>::type
|
||||
>::type
|
||||
>
|
||||
{
|
||||
};
|
||||
|
||||
}}} // namespace boost::python::converter
|
||||
|
||||
#endif // RVALUE_FROM_PYTHON_CHAIN_DWA200237_HPP
|
||||
71
test/callbacks.cpp
Normal file
71
test/callbacks.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
// 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>
|
||||
|
||||
using namespace boost::python;
|
||||
|
||||
int apply_int_int(PyObject* f, int x)
|
||||
{
|
||||
return returning<int>::call(f, x);
|
||||
}
|
||||
|
||||
void apply_void_int(PyObject* f, int x)
|
||||
{
|
||||
returning<void>::call(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 returning<X>::call(f, x);
|
||||
}
|
||||
|
||||
void apply_void_X_ref(PyObject* f, X x)
|
||||
{
|
||||
returning<X>::call(f, boost::ref(x));
|
||||
}
|
||||
|
||||
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)
|
||||
.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)
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
|
||||
43
test/callbacks.py
Normal file
43
test/callbacks.py
Normal file
@@ -0,0 +1,43 @@
|
||||
'''
|
||||
>>> from callbacks_ext import *
|
||||
|
||||
>>> def double(x):
|
||||
... return x + x
|
||||
...
|
||||
>>> apply_int_int(double, 42)
|
||||
84
|
||||
>>> apply_void_int(double, 42)
|
||||
|
||||
>>> def identity(x):
|
||||
... return x
|
||||
|
||||
>>> x = apply_X_X(identity, X(42))
|
||||
>>> x.value()
|
||||
42
|
||||
>>> x_count()
|
||||
1
|
||||
>>> del x
|
||||
>>> x_count()
|
||||
0
|
||||
|
||||
>>> def increment(x):
|
||||
... x.set(x.value() + 1)
|
||||
...
|
||||
>>> x = X(42)
|
||||
>>> apply_void_X_ref(increment, x)
|
||||
>>> x.value()
|
||||
43
|
||||
'''
|
||||
|
||||
def run(args = None):
|
||||
import sys
|
||||
import doctest
|
||||
|
||||
if args is not None:
|
||||
sys.argv = args
|
||||
return doctest.testmod(sys.modules.get(__name__))
|
||||
|
||||
if __name__ == '__main__':
|
||||
print "running..."
|
||||
import sys
|
||||
sys.exit(run()[0])
|
||||
Reference in New Issue
Block a user