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 #ifndef PY_FUNCTION_DWA200286_HPP
6 # define PY_FUNCTION_DWA200286_HPP
8 # include <boost/python/detail/signature.hpp>
9 # include <boost/detail/workaround.hpp>
10 # include <boost/mpl/size.hpp>
13 namespace boost { namespace python { namespace objects {
15 // This type is used as a "generalized Python callback", wrapping the
16 // function signature:
18 // PyObject* (PyObject* args, PyObject* keywords)
20 struct BOOST_PYTHON_DECL py_function_impl_base
22 virtual ~py_function_impl_base();
23 virtual PyObject* operator()(PyObject*, PyObject*) = 0;
24 virtual unsigned min_arity() const = 0;
25 virtual unsigned max_arity() const;
26 virtual python::detail::py_func_sig_info signature() const = 0;
29 template <class Caller>
30 struct caller_py_function_impl : py_function_impl_base
32 caller_py_function_impl(Caller const& caller)
36 PyObject* operator()(PyObject* args, PyObject* kw)
38 return m_caller(args, kw);
41 virtual unsigned min_arity() const
43 return m_caller.min_arity();
46 virtual python::detail::py_func_sig_info signature() const
48 return m_caller.signature();
55 template <class Caller, class Sig>
56 struct signature_py_function_impl : py_function_impl_base
58 signature_py_function_impl(Caller const& caller)
62 PyObject* operator()(PyObject* args, PyObject* kw)
64 return m_caller(args, kw);
67 virtual unsigned min_arity() const
69 return mpl::size<Sig>::value - 1;
72 virtual python::detail::py_func_sig_info signature() const
74 python::detail::signature_element const* sig = python::detail::signature<Sig>::elements();
75 python::detail::py_func_sig_info res = {sig, sig};
83 template <class Caller, class Sig>
84 struct full_py_function_impl : py_function_impl_base
86 full_py_function_impl(Caller const& caller, unsigned min_arity, unsigned max_arity)
88 , m_min_arity(min_arity)
89 , m_max_arity(max_arity > min_arity ? max_arity : min_arity)
92 PyObject* operator()(PyObject* args, PyObject* kw)
94 return m_caller(args, kw);
97 virtual unsigned min_arity() const
102 virtual unsigned max_arity() const
107 virtual python::detail::py_func_sig_info signature() const
109 python::detail::signature_element const* sig = python::detail::signature<Sig>::elements();
110 python::detail::py_func_sig_info res = {sig, sig};
116 unsigned m_min_arity;
117 unsigned m_max_arity;
122 template <class Caller>
123 py_function(Caller const& caller)
124 : m_impl(new caller_py_function_impl<Caller>(caller))
127 template <class Caller, class Sig>
128 py_function(Caller const& caller, Sig)
129 : m_impl(new signature_py_function_impl<Caller, Sig>(caller))
132 template <class Caller, class Sig>
133 py_function(Caller const& caller, Sig, int min_arity, int max_arity = 0)
134 : m_impl(new full_py_function_impl<Caller, Sig>(caller, min_arity, max_arity))
137 py_function(py_function const& rhs)
138 #if defined(BOOST_NO_CXX11_SMART_PTR)
141 : m_impl(std::move(rhs.m_impl))
145 PyObject* operator()(PyObject* args, PyObject* kw) const
147 return (*m_impl)(args, kw);
150 unsigned min_arity() const
152 return m_impl->min_arity();
155 unsigned max_arity() const
157 return m_impl->max_arity();
160 python::detail::signature_element const* signature() const
162 return m_impl->signature().signature;
165 python::detail::signature_element const& get_return_type() const
167 return *m_impl->signature().ret;
171 #if defined(BOOST_NO_CXX11_SMART_PTR)
172 mutable std::auto_ptr<py_function_impl_base> m_impl;
174 mutable std::unique_ptr<py_function_impl_base> m_impl;
178 }}} // namespace boost::python::objects
180 #endif // PY_FUNCTION_DWA200286_HPP