File: ptr.qbk

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (118 lines) | stat: -rw-r--r-- 4,129 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
[section boost/python/ptr.hpp]
[section Introduction]
<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.

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. 
[endsect]
[section Functions]
``
template <class T>
pointer_wrapper<T> ptr(T x);
``
[variablelist
[[Requires][T is a pointer type.]]
[[Returns][pointer_wrapper<T>(x)]]
[[Throws][nothing.]]
]
[endsect]
[section Class template `pointer_wrapper`]
A "type envelope" which is returned by `ptr()`, used to indicate reference semantics for pointers passed to Python callbacks.
``
namespace boost { namespace python
{
    template<class Ptr> class pointer_wrapper
    { 
     public:
        typedef Ptr type;

        explicit pointer_wrapper(Ptr x);
        operator Ptr() const;
        Ptr get() const;
    };
}}
``
[endsect]
[section Class template `pointer_wrapper` types]
``
typedef Ptr type;
``
The type of the pointer being wrapped. 
[endsect]
[section Class template `pointer_wrapper` constructors and destructor]
``
explicit pointer_wrapper(Ptr x);
``
[variablelist
[[Requires][`Ptr` is a pointer type]]
[[Effects][Stores `x` in a the `pointer_wrapper<>`. ]]
[[Throws][nothing.]]
]
[endsect]
[section Class template `pointer_wrapper` observer functions]
``
operator Ptr() const;
Ptr get() const;
``
[variablelist
[[Returns][a copy of the stored pointer. ]]
[[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. ]]
]
[endsect]
[section Metafunctions]
[section Class template `is_pointer_wrapper`]
A unary metafunction whose value is true iff its argument is a pointer_wrapper<>. 
``
namespace boost { namespace python
{
    template<class T> class is_pointer_wrapper
    { 
        static unspecified value = ...;
    };
}}
``
[variablelist
[[Returns][`true` iff `T` is a specialization of `pointer_wrapper<>`.
value is an integral constant convertible to bool of unspecified type ]]
]
[endsect]
[section Class template `unwrap_pointer`]
A unary metafunction which extracts the wrapped pointer type from a specialization of pointer_wrapper<>.
``
namespace boost { namespace python
{
    template<class T> class unwrap_pointer
    { 
        typedef unspecified type;
    };
}}
``
[variablelist
[[Returns][`T::type` if `T` is a specialization of `pointer_wrapper<>`, `T` otherwise ]]
]
[endsect]
[endsect]
[section Example]
This example illustrates the use of ptr() to prevent an object from being copied:
``
#include <boost/python/call.hpp>
#include <boost/python/ptr.hpp>

class expensive_to_copy
{
   ...
};

void pass_as_arg(expensive_to_copy* x, PyObject* f)
{
   // call the Python function f, passing a Python object built around
   // which refers to *x by-pointer.
   //
   // *** Note: ensuring that *x outlives the argument to f() is    ***
   // *** up to the user! Failure to do so could result in a crash! ***

   boost::python::call<void>(f, ptr(x));
}
...
``
[endsect]
[endsect]