]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/python/doc/reference/extract.qbk
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / python / doc / reference / extract.qbk
CommitLineData
7c673cae
FG
1[section boost/python/extract.hpp]
2[section Introduction]
3Exposes a mechanism for extracting C++ object values from generalized Python objects. Note that `extract<...>` can also be used to "downcast" an [link object_wrappers.boost_python_object_hpp.class_object `object`] to some specific [link concepts.objectwrapper ObjectWrapper]. Because invoking a mutable python type with an argument of the same type (e.g. `list([1,2]`) typically makes a copy of the argument object, this may be the only way to access the [link concepts.objectwrapper ObjectWrapper]\ 's interface on the original object.
4[endsect]
5[section Class template `extract`]
6`extract<T>` can be used to extract a value of an arbitrary C++ type from an instance of [link object_wrappers.boost_python_object_hpp.class_object object]. Two usages are supported:
7
8# `extract<T>(o)` is a temporary object which is implicitly convertible to `T` (explicit conversion is also available through the object's function-call operator). However, if no conversion is available which can convert o to an object of type `T`, a Python TypeError exception will be raised.
9# `extract<T> x(o);` constructs an extractor whose `check()` member function can be used to ask whether a conversion is available without causing an exception to be thrown.
10
11``
12namespace boost { namespace python
13{
14 template <class T>
15 struct extract
16 {
17 typedef unspecified result_type;
18
19 extract(PyObject*);
20 extract(object const&);
21
22 result_type operator()() const;
23 operator result_type() const;
24
25 bool check() const;
26 };
27}}
28``
29[endsect]
30[section Class template `extract` constructors and destructor]
31``
32extract(PyObject* p);
33extract(object const&);
34``
35[variablelist
36[[Requires][The first form requires that p is non-null.]]
37[[Effects][Stores a pointer to the Python object managed by its constructor argument. In particular, the reference count of the object is not incremented. The onus is on the user to be sure it is not destroyed before the extractor's conversion function is called.]]
38]
39[endsect]
40[section Class template `extract` observer functions]
41``
42result_type operator()() const;
43operator result_type() const;
44``
45[variablelist
46[[Effects][Converts the stored pointer to result_type, which is either T or T const&. ]]
47[[Returns][An object of result_type corresponding to the one referenced by the stored pointer.]]
48[[Throws][[link high_level_components.boost_python_errors_hpp.class_error_already_set `error_already_set`] and sets a `TypeError` if no such conversion is available. May also emit other unspecified exceptions thrown by the converter which is actually used.]]
49]
50`` bool check() const;``
51[variablelist
52[[Postconditions][None. In particular, note that a return value of true does not preclude an exception being thrown from operator result_type() or operator()().]]
53[[Returns][false only if no conversion from the stored pointer to T is available.]]
54]
55[endsect]
56[section Example]
57``
58#include <cstdio>
59using namespace boost::python;
60int Print(str s)
61{
62 // extract a C string from the Python string object
63 char const* c_str = extract<char const*>(s);
64
65 // Print it using printf
66 std::printf("%s\n", c_str);
67
68 // Get the Python string's length and convert it to an int
69 return extract<int>(s.attr("__len__")())
70}
71``
72 The following example shows how extract can be used along with [link high_level_components.boost_python_class_hpp.class_template_class_t_bases_hel `class_<...>`] to create and access an instance of a wrapped C++ class.
73``
74struct X
75{
76 X(int x) : v(x) {}
77 int value() { return v; }
78 private:
79 int v;
80};
81
82BOOST_PYTHON_MODULE(extract_ext)
83{
84 object x_class(
85 class_<X>("X", init<int>())
86 .def("value", &X::value))
87 ;
88
89 // Instantiate an X object through the Python interface.
90 // Its lifetime is now managed by x_obj.
91 object x_obj = x_class(3);
92
93 // Get a reference to the C++ object out of the Python object
94 X& x = extract<X&>(x_obj);
95 assert(x.value() == 3);
96}
97``
98[endsect]
99[endsect]