]>
Commit | Line | Data |
---|---|---|
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 | ||
5 | 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. | |
6 | `` | |
7 | namespace boost { namespace python | |
8 | { | |
9 | struct return_opaque_pointer | |
10 | { | |
11 | template <class R> struct apply; | |
12 | }; | |
13 | }} | |
14 | `` | |
15 | [endsect] | |
16 | [section Class `return_opaque_pointer` metafunctions] | |
17 | ``template <class T> struct apply`` | |
18 | [variablelist | |
19 | [[Returns][`detail::opaque_conversion_holder<R> type;`]] | |
20 | ] | |
21 | [endsect] | |
22 | [section Example] | |
23 | In C++: | |
24 | `` | |
25 | # include <boost/python/return_opaque_pointer.hpp> | |
26 | # include <boost/python/def.hpp> | |
27 | # include <boost/python/module.hpp> | |
28 | # include <boost/python/return_value_policy.hpp> | |
29 | ||
30 | typedef struct opaque_ *opaque; | |
31 | ||
32 | opaque the_op = ((opaque) 0x47110815); | |
33 | ||
34 | opaque get () { return the_op; } | |
35 | void use (opaque op) { | |
36 | if (op != the_op) | |
37 | throw std::runtime_error (std::string ("failed")); | |
38 | } | |
39 | ||
40 | void failuse (opaque op) { | |
41 | if (op == the_op) | |
42 | throw std::runtime_error (std::string ("success")); | |
43 | } | |
44 | ||
45 | BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(opaque_) | |
46 | ||
47 | namespace bpl = boost::python; | |
48 | ||
49 | BOOST_PYTHON_MODULE(opaque_ext) | |
50 | { | |
51 | bpl::def ( | |
52 | "get", &::get, bpl::return_value_policy<bpl::return_opaque_pointer>()); | |
53 | bpl::def ("use", &::use); | |
54 | bpl::def ("failuse", &::failuse); | |
55 | } | |
56 | `` | |
57 | Python code: | |
58 | `` | |
59 | """ | |
60 | >>> from opaque_ext import * | |
61 | >>> # | |
62 | >>> # Check for correct conversion | |
63 | >>> use(get()) | |
64 | >>> failuse(get()) | |
65 | Traceback (most recent call last): | |
66 | ... | |
67 | RuntimeError: success | |
68 | >>> # | |
69 | >>> # Check that there is no conversion from integers ... | |
70 | >>> use(0) | |
71 | Traceback (most recent call last): | |
72 | ... | |
73 | TypeError: bad argument type for built-in operation | |
74 | >>> # | |
75 | >>> # ... and from strings to opaque objects | |
76 | >>> use("") | |
77 | Traceback (most recent call last): | |
78 | ... | |
79 | TypeError: bad argument type for built-in operation | |
80 | """ | |
81 | def run(args = None): | |
82 | import sys | |
83 | import doctest | |
84 | ||
85 | if args is not None: | |
86 | sys.argv = args | |
87 | return doctest.testmod(sys.modules.get(__name__)) | |
88 | ||
89 | if __name__ == '__main__': | |
90 | print "running..." | |
91 | import sys | |
92 | sys.exit(run()[0]) | |
93 | `` | |
94 | [endsect] | |
95 | [endsect] |