ptr.qbk 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. [section boost/python/ptr.hpp]
  2. [section Introduction]
  3. <boost/python/ptr.hpp> defines the ptr() function template, which allows users to specify how to convert C++ pointer values to python in the context of implementing overridable virtual functions, invoking Python callable objects, or explicitly converting C++ objects to Python. Normally, when passing pointers to Python callbacks, the pointee is copied to ensure that the Python object never holds a dangling reference. To specify that the new Python object should merely contain a copy of a pointer p, the user can pass ptr(p) instead of passing p directly. This interface is meant to mirror the use of boost::ref(), which can be similarly used to prevent copying of referents.
  4. ptr(p) returns an instance of [link function_invocation_and_creation.boost_python_ptr_hpp.class_template_pointer_wrapper `pointer_wrapper<>`], which can be detected using the [link function_invocation_and_creation.boost_python_ptr_hpp.metafunctions.class_template_is_pointer_wrappe `is_pointer_wrapper<>`] metafunction; [link function_invocation_and_creation.boost_python_ptr_hpp.metafunctions.class_template_unwrap_pointer `unwrap_pointer<>`] is a metafunction which extracts the original pointer type from a `pointer_wrapper<>`. These classes can be thought of as implementation details.
  5. [endsect]
  6. [section Functions]
  7. ``
  8. template <class T>
  9. pointer_wrapper<T> ptr(T x);
  10. ``
  11. [variablelist
  12. [[Requires][T is a pointer type.]]
  13. [[Returns][pointer_wrapper<T>(x)]]
  14. [[Throws][nothing.]]
  15. ]
  16. [endsect]
  17. [section Class template `pointer_wrapper`]
  18. A "type envelope" which is returned by `ptr()`, used to indicate reference semantics for pointers passed to Python callbacks.
  19. ``
  20. namespace boost { namespace python
  21. {
  22. template<class Ptr> class pointer_wrapper
  23. {
  24. public:
  25. typedef Ptr type;
  26. explicit pointer_wrapper(Ptr x);
  27. operator Ptr() const;
  28. Ptr get() const;
  29. };
  30. }}
  31. ``
  32. [endsect]
  33. [section Class template `pointer_wrapper` types]
  34. ``
  35. typedef Ptr type;
  36. ``
  37. The type of the pointer being wrapped.
  38. [endsect]
  39. [section Class template `pointer_wrapper` constructors and destructor]
  40. ``
  41. explicit pointer_wrapper(Ptr x);
  42. ``
  43. [variablelist
  44. [[Requires][`Ptr` is a pointer type]]
  45. [[Effects][Stores `x` in a the `pointer_wrapper<>`. ]]
  46. [[Throws][nothing.]]
  47. ]
  48. [endsect]
  49. [section Class template `pointer_wrapper` observer functions]
  50. ``
  51. operator Ptr() const;
  52. Ptr get() const;
  53. ``
  54. [variablelist
  55. [[Returns][a copy of the stored pointer. ]]
  56. [[Rationale][pointer_wrapper is intended to be a stand-in for the actual pointer type, but sometimes it's better to have an explicit way to retrieve the pointer. ]]
  57. ]
  58. [endsect]
  59. [section Metafunctions]
  60. [section Class template `is_pointer_wrapper`]
  61. A unary metafunction whose value is true iff its argument is a pointer_wrapper<>.
  62. ``
  63. namespace boost { namespace python
  64. {
  65. template<class T> class is_pointer_wrapper
  66. {
  67. static unspecified value = ...;
  68. };
  69. }}
  70. ``
  71. [variablelist
  72. [[Returns][`true` iff `T` is a specialization of `pointer_wrapper<>`.
  73. value is an integral constant convertible to bool of unspecified type ]]
  74. ]
  75. [endsect]
  76. [section Class template `unwrap_pointer`]
  77. A unary metafunction which extracts the wrapped pointer type from a specialization of pointer_wrapper<>.
  78. ``
  79. namespace boost { namespace python
  80. {
  81. template<class T> class unwrap_pointer
  82. {
  83. typedef unspecified type;
  84. };
  85. }}
  86. ``
  87. [variablelist
  88. [[Returns][`T::type` if `T` is a specialization of `pointer_wrapper<>`, `T` otherwise ]]
  89. ]
  90. [endsect]
  91. [endsect]
  92. [section Example]
  93. This example illustrates the use of ptr() to prevent an object from being copied:
  94. ``
  95. #include <boost/python/call.hpp>
  96. #include <boost/python/ptr.hpp>
  97. class expensive_to_copy
  98. {
  99. ...
  100. };
  101. void pass_as_arg(expensive_to_copy* x, PyObject* f)
  102. {
  103. // call the Python function f, passing a Python object built around
  104. // which refers to *x by-pointer.
  105. //
  106. // *** Note: ensuring that *x outlives the argument to f() is ***
  107. // *** up to the user! Failure to do so could result in a crash! ***
  108. boost::python::call<void>(f, ptr(x));
  109. }
  110. ...
  111. ``
  112. [endsect]
  113. [endsect]