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 - ObjectWrapper Concept</title> |
---|
14 | </head> |
---|
15 | |
---|
16 | <body link="#0000ff" vlink="#800080"> |
---|
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">ObjectWrapper and TypeWrapper Concepts</h2> |
---|
29 | </td> |
---|
30 | </tr> |
---|
31 | </table> |
---|
32 | <hr> |
---|
33 | |
---|
34 | <dl class="page-index"> |
---|
35 | <dt><a href="#introduction">Introduction</a></dt> |
---|
36 | |
---|
37 | <dt><a href="#concept-requirements">Concept Requirements</a></dt> |
---|
38 | |
---|
39 | <dd> |
---|
40 | <dl class="page-index"> |
---|
41 | <dt><a href="#ObjectWrapper-concept">ObjectWrapper Concept</a></dt> |
---|
42 | |
---|
43 | <dt><a href="#TypeWrapper-concept">TypeWrapper Concept</a></dt> |
---|
44 | </dl> |
---|
45 | </dd> |
---|
46 | |
---|
47 | <dt><a href="#caveat">Caveat</a></dt> |
---|
48 | </dl> |
---|
49 | |
---|
50 | <h2><a name="introduction"></a>Introduction</h2> |
---|
51 | |
---|
52 | <p>This page defines two concepts used to describe classes which manage a |
---|
53 | Python objects, and which are intended to support usage with a |
---|
54 | Python-like syntax.</p> |
---|
55 | |
---|
56 | <h2><a name="concept-requirements"></a>Concept Requirements</h2> |
---|
57 | |
---|
58 | <h3><a name="ObjectWrapper-concept"></a>ObjectWrapper Concept</h3> |
---|
59 | Models of the ObjectWrapper concept have <a href= |
---|
60 | "object.html#object-spec">object</a> as a publicly-accessible base class, |
---|
61 | and are used to supply special construction behavior and/or additional |
---|
62 | convenient functionality through (often templated) member functions. |
---|
63 | Except when the return type <code>R</code> is itself an <a href= |
---|
64 | "#TypeWrapper-concept">TypeWrapper</a>, a member function invocation of |
---|
65 | the form |
---|
66 | <pre> |
---|
67 | x.<i>some_function</i>(<i>a<small>1</small>, a<small>2</small>,...a<small>n</small></i>) |
---|
68 | </pre> |
---|
69 | always has semantics equivalent to: |
---|
70 | <pre> |
---|
71 | <a href= |
---|
72 | "extract.html#extract-spec">extract</a><R>(x.attr("<i>some_function</i>")(<a |
---|
73 | href= |
---|
74 | "object.html#object-spec-ctors">object</a>(<i>a<small>1</small></i>), <a |
---|
75 | href= |
---|
76 | "object.html#object-spec-ctors">object</a>(<i>a<small>2</small></i>),...<a |
---|
77 | href="object.html#object-spec-ctors">object</a>(<i>a<small>n</small></i>)))() |
---|
78 | </pre> |
---|
79 | When the <code>R</code> is an <a href= |
---|
80 | "#TypeWrapper-concept">TypeWrapper</a>, the result type may be |
---|
81 | constructed by taking direct posession of: |
---|
82 | <pre> |
---|
83 | x.attr("<i>some_function</i>")(<a href= |
---|
84 | "object.html#object-spec-ctors">object</a>(<i>a<small>1</small></i>), <a |
---|
85 | href= |
---|
86 | "object.html#object-spec-ctors">object</a>(<i>a<small>2</small></i>),...<a |
---|
87 | href= |
---|
88 | "object.html#object-spec-ctors">object</a>(<i>a<small>n</small></i>)).ptr() |
---|
89 | </pre> |
---|
90 | [see <a href="#caveat">caveat</a> below] |
---|
91 | |
---|
92 | <h3><a name="TypeWrapper-concept"></a>TypeWrapper Concept</h3> |
---|
93 | TypeWrapper is a refinement of ObjectWrapper which is associated with a |
---|
94 | particular Python type <code>X</code>. For a given TypeWrapper |
---|
95 | <code>T</code>, a valid constructor expression |
---|
96 | <pre> |
---|
97 | T(<i>a<small>1</small>, a<small>2</small>,...a<small>n</small></i>) |
---|
98 | </pre> |
---|
99 | builds a new <code>T</code> object managing the result of invoking |
---|
100 | <code>X</code> with arguments corresponding to |
---|
101 | <pre> |
---|
102 | <a href= |
---|
103 | "object.html#object-spec-ctors">object</a>(<i>a<small>1</small></i>), <a |
---|
104 | href= |
---|
105 | "object.html#object-spec-ctors">object</a>(<i>a<small>2</small></i>),...<a |
---|
106 | href= |
---|
107 | "object.html#object-spec-ctors">object</a>(<i>a<small>n</small></i>) |
---|
108 | </pre> |
---|
109 | |
---|
110 | When used as arguments to wrapped C++ functions, or as the template |
---|
111 | parameter to <code><a |
---|
112 | href="extract.html#extract-spec">extract</a><></code>, only |
---|
113 | instances of the associated Python type will be considered a match. |
---|
114 | |
---|
115 | <h3><a name="caveat">Caveat</a></h3> |
---|
116 | The upshot of the special member function invocation rules when the |
---|
117 | return type is a TypeWrapper is that it is possible for the returned |
---|
118 | object to manage a Python object of an inappropriate type. This is not |
---|
119 | usually a serious problem; the worst-case result is that errors will be |
---|
120 | detected at runtime a little later than they might otherwise be. For an |
---|
121 | example of how this can occur, note that the <code><a href= |
---|
122 | "dict.html#dict-spec">dict</a></code> member function <code>items</code> |
---|
123 | returns an object of type <code><a href= |
---|
124 | "list.html#list-spec">list</a></code>. Now suppose the user defines this |
---|
125 | <code>dict</code> subclass in Python: |
---|
126 | <pre> |
---|
127 | >>> class mydict(dict): |
---|
128 | ... def items(self): |
---|
129 | ... return tuple(dict.items(self)) # return a tuple |
---|
130 | </pre> |
---|
131 | Since an instance of <code>mydict</code> is also an instance of |
---|
132 | <code>dict</code>, when used as an argument to a wrapped C++ function, |
---|
133 | <code><a href="dict.html#dict-spec">boost::python::dict</a></code> can |
---|
134 | accept objects of Python type <code>mydict</code>. Invoking |
---|
135 | <code>items()</code> on this object can result in an instance of <code><a |
---|
136 | href="list.html#list-spec">boost::python::list</a></code> which actually |
---|
137 | holds a Python tuple. Subsequent attempts to use list methods (e.g. |
---|
138 | <code>append</code>, or any other mutating operation) on this object will |
---|
139 | raise the same exception that would occur if you tried to do it from |
---|
140 | Python. |
---|
141 | <hr> |
---|
142 | |
---|
143 | <p>Revised |
---|
144 | <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
---|
145 | 13 November, 2002 |
---|
146 | <!--webbot bot="Timestamp" endspan i-checksum="39359" --> |
---|
147 | </p> |
---|
148 | |
---|
149 | <p><i>© Copyright <a href= |
---|
150 | "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002.</i></p> |
---|
151 | </body> |
---|
152 | </html> |
---|
153 | |
---|