to_python_converter.qbk 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. [section boost/python/to_python_converter.hpp]
  2. [section Introduction]
  3. `to_python_converter` registers a conversion from objects of a given C++ type into a Python object.
  4. [endsect]
  5. [section Class template `to_python_converter`]
  6. `to_python_converter` adds a wrapper around a static member function of its second template parameter, handling low-level details such as insertion into the converter registry.
  7. In the table below, x denotes an object of type T
  8. [table
  9. [[Parameter][Requirements][Description]]
  10. [[T][][The C++ type of the source object in the conversion]]
  11. [[Conversion][`PyObject* p = Conversion::convert(x)`,
  12. `if p == 0`, `PyErr_Occurred() != 0`.][A class type whose static member function convert does the real work of the conversion.]]
  13. [[bool has_get_pytype=false][`PyTypeObject const * p = Conversion::get_pytype()`]
  14. [Optional member - if Conversion has `get_pytype` member supply `true` for this parameters. If present `get_pytype` is used to document the return type of functions using this conversion. The `get_pytype` may be implemented using the classes and functions from pytype_function.hpp NOTE : For backward compatibility this parameter may be passed after checking if BOOST_PYTHON_SUPPORTS_PY_SIGNATURES is defined (see [link function_invocation_and_creation.function_documentation.boost_python_pytype_function_hpp.example here]).]
  15. ]]
  16. ``
  17. namespace boost { namespace python
  18. {
  19. template <class T, class Conversion, bool convertion_has_get_pytype_member=false>
  20. struct to_python_converter
  21. {
  22. to_python_converter();
  23. };
  24. }}
  25. ``
  26. [section Class template `to_python_converter` constructor]
  27. ``to_python_converter();``
  28. [variablelist
  29. [[Effects][Registers a `to_python` converter which uses `Conversion::convert()` to do its work.]]
  30. ]
  31. [endsect]
  32. [endsect]
  33. [section Example]
  34. This example presumes that someone has implemented the standard noddy example module from the Python documentation, and placed the corresponding declarations in "noddy.h". Because noddy_NoddyObject is the ultimate trivial extension type, the example is a bit contrived: it wraps a function for which all information is contained in the type of its return value.
  35. In C++:
  36. ``
  37. #include <boost/python/reference.hpp>
  38. #include <boost/python/module.hpp>
  39. #include "noddy.h"
  40. struct tag {};
  41. tag make_tag() { return tag(); }
  42. using namespace boost::python;
  43. struct tag_to_noddy
  44. {
  45. static PyObject* convert(tag const& x)
  46. {
  47. return PyObject_New(noddy_NoddyObject, &noddy_NoddyType);
  48. }
  49. static PyTypeObject const* get_pytype()
  50. {
  51. return &noddy_NoddyType;
  52. }
  53. };
  54. BOOST_PYTHON_MODULE(to_python_converter)
  55. {
  56. def("make_tag", make_tag);
  57. to_python_converter<tag, tag_to_noddy, true>(); //"true" because tag_to_noddy has member get_pytype
  58. }
  59. ``
  60. In Python:
  61. ``
  62. >>> import to_python_converter
  63. >>> def always_none():
  64. ... return None
  65. ...
  66. >>> def choose_function(x):
  67. ... if (x % 2 != 0):
  68. ... return to_python_converter.make_tag
  69. ... else:
  70. ... return always_none
  71. ...
  72. >>> a = [ choose_function(x) for x in range(5) ]
  73. >>> b = [ f() for f in a ]
  74. >>> type(b[0])
  75. <type 'NoneType'>
  76. >>> type(b[1])
  77. <type 'Noddy'>
  78. >>> type(b[2])
  79. <type 'NoneType'>
  80. >>> type(b[3])
  81. <type 'Noddy'>
  82. ``
  83. [endsect]
  84. [endsect]