raw_function.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright David Abrahams 2003.
  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 RAW_FUNCTION_DWA200336_HPP
  6. # define RAW_FUNCTION_DWA200336_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/tuple.hpp>
  9. # include <boost/python/dict.hpp>
  10. # include <boost/python/object/py_function.hpp>
  11. # include <boost/mpl/vector/vector10.hpp>
  12. # include <boost/limits.hpp>
  13. # include <cstddef>
  14. namespace boost { namespace python {
  15. namespace detail
  16. {
  17. template <class F>
  18. struct raw_dispatcher
  19. {
  20. raw_dispatcher(F f) : f(f) {}
  21. PyObject* operator()(PyObject* args, PyObject* keywords)
  22. {
  23. return incref(
  24. object(
  25. f(
  26. tuple(borrowed_reference(args))
  27. , keywords ? dict(borrowed_reference(keywords)) : dict()
  28. )
  29. ).ptr()
  30. );
  31. }
  32. private:
  33. F f;
  34. };
  35. object BOOST_PYTHON_DECL make_raw_function(objects::py_function);
  36. }
  37. template <class F>
  38. object raw_function(F f, std::size_t min_args = 0)
  39. {
  40. return detail::make_raw_function(
  41. objects::py_function(
  42. detail::raw_dispatcher<F>(f)
  43. , mpl::vector1<PyObject*>()
  44. , min_args
  45. , (std::numeric_limits<unsigned>::max)()
  46. )
  47. );
  48. }
  49. }} // namespace boost::python
  50. #endif // RAW_FUNCTION_DWA200336_HPP