make_function.qbk 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. [section boost/python/make_function.hpp]
  2. [section Introduction]
  3. make_function() and make_constructor() are the functions used internally by def() and class_<>::def() to produce Python callable objects which wrap C++ functions and member functions.
  4. [endsect]
  5. [section Functions]
  6. ``
  7. template <class F>
  8. object make_function(F f)
  9. template <class F, class Policies>
  10. object make_function(F f, Policies const& policies)
  11. template <class F, class Policies, class KeywordsOrSignature>
  12. object make_function(F f, Policies const& policies, KeywordsOrSignature const& ks)
  13. template <class F, class Policies, class Keywords, class Signature>
  14. object make_function(F f, Policies const& policies, Keywords const& kw, Signature const& sig)
  15. ``
  16. [variablelist
  17. [[Requires][F is a function pointer or member function pointer type. If policies are supplied, it must be a model of CallPolicies. If kewords are supplied, it must be the result of a keyword-expression specifying no more arguments than the arity of f.]]
  18. [[Effects][Creates a Python callable object which, when called from Python, converts its arguments to C++ and calls f. If F is a pointer-to-member-function type, the target object of the function call (*this) will be taken from the first Python argument, and subsequent Python arguments will be used as the arguments to f.
  19. * If policies are supplied, it will be applied to the function as described here.
  20. * If keywords are supplied, the keywords will be applied in order to the final arguments of the resulting function.
  21. * If Signature is supplied, it should be an instance of an MPL front-extensible sequence representing the function's return type followed by its argument types. Pass a Signature when wrapping function object types whose signatures can't be deduced, or when you wish to override the types which will be passed to the wrapped function. ]]
  22. [[Returns][An instance of object which holds the new Python callable object.]]
  23. [[Caveats][An argument of pointer type may be 0 if None is passed from Python. An argument type which is a constant reference may refer to a temporary which was created from the Python object for just the duration of the call to the wrapped function, for example a std::vector conjured up by the conversion process from a Python list. Use a non-const reference argument when a persistent lvalue is required. ]]
  24. ]
  25. ``
  26. template <class F>
  27. object make_constructor(F f)
  28. template <class F, class Policies>
  29. object make_constructor(F f, Policies const& policies)
  30. template <class F, class Policies, class KeywordsOrSignature>
  31. object make_constructor(F f, Policies const& policies, KeywordsOrSignature const& ks)
  32. template <class F, class Policies, class Keywords, class Signature>
  33. object make_constructor(F f, Policies const& policies, Keywords const& kw, Signature const& sig)
  34. ``
  35. [variablelist
  36. [[Requires][F is a function pointer type. If policies are supplied, it must be a model of CallPolicies. If kewords are supplied, it must be the result of a keyword-expression specifying no more arguments than the arity of f.]]
  37. [[Effects][Creates a Python callable object which, when called from Python, converts its arguments to C++ and calls f.]]
  38. [[Returns][An instance of object which holds the new Python callable object.]]
  39. ]
  40. [endsect]
  41. [section Example]
  42. C++ function exposed below returns a callable object wrapping one of two functions.
  43. ``
  44. #include <boost/python/make_function.hpp>
  45. #include <boost/python/module.hpp>
  46. char const* foo() { return "foo"; }
  47. char const* bar() { return "bar"; }
  48. using namespace boost::python;
  49. object choose_function(bool selector)
  50. {
  51. if (selector)
  52. return boost::python::make_function(foo);
  53. else
  54. return boost::python::make_function(bar);
  55. }
  56. BOOST_PYTHON_MODULE(make_function_test)
  57. {
  58. def("choose_function", choose_function);
  59. }
  60. ``
  61. It can be used this way in Python:
  62. ``
  63. >>> from make_function_test import *
  64. >>> f = choose_function(1)
  65. >>> g = choose_function(0)
  66. >>> f()
  67. 'foo'
  68. >>> g()
  69. 'bar'
  70. ``
  71. [endsect]
  72. [endsect]