1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
---|
2 | |
---|
3 | <!-- Copyright David Abrahams 2006. Distributed under the Boost --> |
---|
4 | <!-- Software License, Version 1.0. (See accompanying --> |
---|
5 | <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> |
---|
6 | <html> |
---|
7 | <head> |
---|
8 | <meta name="generator" content= |
---|
9 | "HTML Tidy for Windows (vers 1st August 2002), see www.w3.org"> |
---|
10 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
---|
11 | <link rel="stylesheet" type="text/css" href="../boost.css"> |
---|
12 | |
---|
13 | <title>Boost.Python - <boost/python/implicit.hpp></title> |
---|
14 | </head> |
---|
15 | |
---|
16 | <body> |
---|
17 | <table border="0" cellpadding="7" cellspacing="0" width="100%" summary= |
---|
18 | "header"> |
---|
19 | <tr> |
---|
20 | <td valign="top" width="300"> |
---|
21 | <h3><a href="../../../../index.htm"><img height="86" width="277" |
---|
22 | alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3> |
---|
23 | </td> |
---|
24 | |
---|
25 | <td valign="top"> |
---|
26 | <h1 align="center"><a href="../index.html">Boost.Python</a></h1> |
---|
27 | |
---|
28 | <h2 align="center">Header <boost/python/implicit.hpp></h2> |
---|
29 | </td> |
---|
30 | </tr> |
---|
31 | </table> |
---|
32 | <hr> |
---|
33 | |
---|
34 | <h2>Contents</h2> |
---|
35 | |
---|
36 | <dl class="page-index"> |
---|
37 | <dt><a href="#introduction">Introduction</a></dt> |
---|
38 | |
---|
39 | <dt><a href="#functions">Functions</a></dt> |
---|
40 | |
---|
41 | <dd> |
---|
42 | <dl class="page-index"> |
---|
43 | <dt><a href="#implicitly_convertible-spec">Function Template |
---|
44 | <code>implicitly_convertible</code></a></dt> |
---|
45 | </dl> |
---|
46 | </dd> |
---|
47 | |
---|
48 | <dt><a href="#examples">Example</a></dt> |
---|
49 | </dl> |
---|
50 | <hr> |
---|
51 | |
---|
52 | <h2><a name="introduction"></a>Introduction</h2> |
---|
53 | <code>implicitly_convertible</code> allows Boost.Python to implicitly |
---|
54 | take advantage of a C++ implicit or explicit conversion when matching |
---|
55 | Python objects to C++ argument types. |
---|
56 | |
---|
57 | <h2><a name="functions"></a>Functions</h2> |
---|
58 | |
---|
59 | <h3><a name="implicitly_convertible-spec"></a>Function template |
---|
60 | <code>implicitly_convertible</code></h3> |
---|
61 | <pre> |
---|
62 | template <class Source, class Target> |
---|
63 | void implicitly_convertible(); |
---|
64 | </pre> |
---|
65 | |
---|
66 | <table border="1" summary="implicitly_convertible template parameters"> |
---|
67 | <caption> |
---|
68 | <b><code>implicitly_convertible</code> template parameters</b><br> |
---|
69 | </caption> |
---|
70 | |
---|
71 | <tr> |
---|
72 | <th>Parameter</th> |
---|
73 | |
---|
74 | <th>Description</th> |
---|
75 | </tr> |
---|
76 | |
---|
77 | <tr> |
---|
78 | <td><code>Source</code></td> |
---|
79 | |
---|
80 | <td>The source type of the implicit conversion</td> |
---|
81 | </tr> |
---|
82 | |
---|
83 | <tr> |
---|
84 | <td><code>Target</code></td> |
---|
85 | |
---|
86 | <td>The target type of the implicit conversion</td> |
---|
87 | </tr> |
---|
88 | </table> |
---|
89 | |
---|
90 | <dl class="function-semantics"> |
---|
91 | <dt><b>Requires:</b> The declaration <code>Target t(s);</code>, where |
---|
92 | <code>s</code> is of type <code>Source</code>, is valid.</dt> |
---|
93 | |
---|
94 | <dt><b>Effects:</b> registers an rvalue <code>from_python</code> |
---|
95 | converter to <code>Target</code> which can succeed for any |
---|
96 | <code>PyObject* p</code> iff there exists any registered converter |
---|
97 | which can produce <code>Source</code> rvalues</dt> |
---|
98 | |
---|
99 | <dt><b>Rationale:</b> C++ users expect to be able to take advantage of |
---|
100 | the same sort of interoperability in Python as they do in C++.</dt> |
---|
101 | </dl> |
---|
102 | |
---|
103 | <h2><a name="examples"></a>Example</h2> |
---|
104 | |
---|
105 | <h3>C++ module definition</h3> |
---|
106 | <pre> |
---|
107 | #include <boost/python/class.hpp> |
---|
108 | #include <boost/python/implicit.hpp> |
---|
109 | #include <boost/python/module.hpp> |
---|
110 | |
---|
111 | using namespace boost::python; |
---|
112 | |
---|
113 | struct X |
---|
114 | { |
---|
115 | X(int x) : v(x) {} |
---|
116 | operator int() const { return v; } |
---|
117 | int v; |
---|
118 | }; |
---|
119 | |
---|
120 | int x_value(X const& x) |
---|
121 | { |
---|
122 | return x.v; |
---|
123 | } |
---|
124 | |
---|
125 | X make_x(int n) { return X(n); } |
---|
126 | |
---|
127 | BOOST_PYTHON_MODULE(implicit_ext) |
---|
128 | { |
---|
129 | def("x_value", x_value); |
---|
130 | def("make_x", make_x); |
---|
131 | |
---|
132 | class_<X>("X", |
---|
133 | init<int>()) |
---|
134 | ; |
---|
135 | |
---|
136 | implicitly_convertible<X,int>(); |
---|
137 | implicitly_convertible<int,X>(); |
---|
138 | } |
---|
139 | </pre> |
---|
140 | |
---|
141 | <h3>Python code</h3> |
---|
142 | <pre> |
---|
143 | >>> from implicit_ext import * |
---|
144 | >>> x_value(X(42)) |
---|
145 | 42 |
---|
146 | >>> x_value(42) |
---|
147 | 42 |
---|
148 | >>> x = make_x(X(42)) |
---|
149 | >>> x_value(x) |
---|
150 | 42 |
---|
151 | </pre> |
---|
152 | |
---|
153 | <p>Revised |
---|
154 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
---|
155 | 13 November, 2002 |
---|
156 | <!--webbot bot="Timestamp" endspan i-checksum="39359" --> |
---|
157 | </p> |
---|
158 | |
---|
159 | <p><i>© Copyright <a href= |
---|
160 | "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> |
---|
161 | </body> |
---|
162 | </html> |
---|
163 | |
---|