Ticket #9687: make_method.hpp

File make_method.hpp, 1.7 KB (added by phatina@…, 9 years ago)

Raw method API

Line 
1// Copyright 2014, Peter Hatina <phatina@redhat.com>
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#ifndef MAKE_METHOD_HPP
6#define MAKE_METHOD_HPP
7
8#include <boost/python/extract.hpp>
9#include <boost/python/dict.hpp>
10#include <boost/python/object/py_function.hpp>
11#include <boost/python/raw_function.hpp>
12#include <boost/python/tuple.hpp>
13#include <boost/mpl/vector/vector10.hpp>
14#include <boost/limits.hpp>
15
16namespace boost { namespace python {
17
18namespace detail {
19
20template <typename C, typename M>
21class raw_method_dispatcher
22{
23public:
24 raw_method_dispatcher(M method): m_method(method) {}
25
26 PyObject* operator()(PyObject* args, PyObject* kwds)
27 {
28 detail::borrowed_reference_t *rargs = detail::borrowed_reference(args);
29 object args_obj(rargs);
30
31 // Extract a C++ reference to object, which is about to call raw method
32 C &obj = extract<C&>(object(args_obj[0]));
33
34 // Return boost::python object containing the raw method call
35 return incref(
36 object(
37 (obj.*m_method)(
38 tuple(args_obj.slice(1, len(args_obj))),
39 kwds ? dict(detail::borrowed_reference(kwds)) : dict())
40 ).ptr()
41 );
42 }
43
44private:
45 M m_method;
46};
47
48}
49
50template <typename C, typename M>
51object raw_method(M method, std::size_t min_args = 0)
52{
53 return detail::make_raw_function(
54 objects::py_function(
55 detail::raw_method_dispatcher<C, M>(method),
56 boost::mpl::vector1<PyObject*>(),
57 min_args,
58 (std::numeric_limits<unsigned>::max)())
59 );
60}
61
62}}
63
64#endif // MAKE_METHOD_HPP