return_opaque_pointer.qbk 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. [section boost/python/return_opaque_pointer.hpp]
  2. [section Class `return_opaqe_pointer`]
  3. return_opaque_pointer is a model of [link concepts.resultconverter.resultconvertergenerator_concept ResultConverterGenerator] which can be used to wrap C++ functions returning pointers to undefined types such that the return value is copied into a new Python object.
  4. In addition to specifying the `return_opaque_pointer` policy the [link to_from_python_type_conversion.boost_python_opaque_pointer_conv.macro_boost_python_opaque_specia `BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID`] macro must be used to define specializations for the [link utility_and_infrastructure.boost_python_type_id_hpp.functions `type_id`] function on the type pointed to by returned pointer.
  5. ``
  6. namespace boost { namespace python
  7. {
  8. struct return_opaque_pointer
  9. {
  10. template <class R> struct apply;
  11. };
  12. }}
  13. ``
  14. [endsect]
  15. [section Class `return_opaque_pointer` metafunctions]
  16. ``template <class T> struct apply``
  17. [variablelist
  18. [[Returns][`detail::opaque_conversion_holder<R> type;`]]
  19. ]
  20. [endsect]
  21. [section Example]
  22. In C++:
  23. ``
  24. # include <boost/python/return_opaque_pointer.hpp>
  25. # include <boost/python/def.hpp>
  26. # include <boost/python/module.hpp>
  27. # include <boost/python/return_value_policy.hpp>
  28. typedef struct opaque_ *opaque;
  29. opaque the_op = ((opaque) 0x47110815);
  30. opaque get () { return the_op; }
  31. void use (opaque op) {
  32. if (op != the_op)
  33. throw std::runtime_error (std::string ("failed"));
  34. }
  35. void failuse (opaque op) {
  36. if (op == the_op)
  37. throw std::runtime_error (std::string ("success"));
  38. }
  39. BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(opaque_)
  40. namespace bpl = boost::python;
  41. BOOST_PYTHON_MODULE(opaque_ext)
  42. {
  43. bpl::def (
  44. "get", &::get, bpl::return_value_policy<bpl::return_opaque_pointer>());
  45. bpl::def ("use", &::use);
  46. bpl::def ("failuse", &::failuse);
  47. }
  48. ``
  49. Python code:
  50. ``
  51. """
  52. >>> from opaque_ext import *
  53. >>> #
  54. >>> # Check for correct conversion
  55. >>> use(get())
  56. >>> failuse(get())
  57. Traceback (most recent call last):
  58. ...
  59. RuntimeError: success
  60. >>> #
  61. >>> # Check that there is no conversion from integers ...
  62. >>> use(0)
  63. Traceback (most recent call last):
  64. ...
  65. TypeError: bad argument type for built-in operation
  66. >>> #
  67. >>> # ... and from strings to opaque objects
  68. >>> use("")
  69. Traceback (most recent call last):
  70. ...
  71. TypeError: bad argument type for built-in operation
  72. """
  73. def run(args = None):
  74. import sys
  75. import doctest
  76. if args is not None:
  77. sys.argv = args
  78. return doctest.testmod(sys.modules.get(__name__))
  79. if __name__ == '__main__':
  80. print "running..."
  81. import sys
  82. sys.exit(run()[0])
  83. ``
  84. [endsect]
  85. [endsect]