• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>ObjectWrapper</title>
5<link rel="stylesheet" href="../../boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
8<link rel="up" href="../concepts.html" title="Chapter 1. Concepts">
9<link rel="prev" href="resultconverter.html" title="ResultConverter">
10<link rel="next" href="../high_level_components.html" title="Chapter 2. High Level Components">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
14<hr>
15<div class="spirit-nav">
16<a accesskey="p" href="resultconverter.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../concepts.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../high_level_components.html"><img src="../../images/next.png" alt="Next"></a>
17</div>
18<div class="section">
19<div class="titlepage"><div><div><h2 class="title" style="clear: both">
20<a name="concepts.objectwrapper"></a><a class="link" href="objectwrapper.html" title="ObjectWrapper">ObjectWrapper</a>
21</h2></div></div></div>
22<div class="toc"><dl class="toc">
23<dt><span class="section"><a href="objectwrapper.html#concepts.objectwrapper.introduction">Introduction</a></span></dt>
24<dt><span class="section"><a href="objectwrapper.html#concepts.objectwrapper.objectwrapper_concept_requiremen">ObjectWrapper
25        Concept Requirements</a></span></dt>
26<dt><span class="section"><a href="objectwrapper.html#concepts.objectwrapper.typewrapper_concept_requirements">TypeWrapper
27        Concept Requirements</a></span></dt>
28<dt><span class="section"><a href="objectwrapper.html#concepts.objectwrapper.caveat">Caveat</a></span></dt>
29</dl></div>
30<div class="section">
31<div class="titlepage"><div><div><h3 class="title">
32<a name="concepts.objectwrapper.introduction"></a><a class="link" href="objectwrapper.html#concepts.objectwrapper.introduction" title="Introduction">Introduction</a>
33</h3></div></div></div>
34<p>
35          This page defines two concepts used to describe classes which manage a
36          Python objects, and which are intended to support usage with a Python-like
37          syntax.
38        </p>
39</div>
40<div class="section">
41<div class="titlepage"><div><div><h3 class="title">
42<a name="concepts.objectwrapper.objectwrapper_concept_requiremen"></a><a class="link" href="objectwrapper.html#concepts.objectwrapper.objectwrapper_concept_requiremen" title="ObjectWrapper Concept Requirements">ObjectWrapper
43        Concept Requirements</a>
44</h3></div></div></div>
45<p>
46          Models of the ObjectWrapper concept have <a class="link" href="../object_wrappers/boost_python_object_hpp.html#object_wrappers.boost_python_object_hpp.class_object" title="Class object">object</a>
47          as a publicly-accessible base class, and are used to supply special construction
48          behavior and/or additional convenient functionality through (often templated)
49          member functions. Except when the return type R is itself an <a class="link" href="objectwrapper.html#concepts.objectwrapper.typewrapper_concept_requirements" title="TypeWrapper Concept Requirements">TypeWrapper</a>,
50          a member function invocation of the form
51        </p>
52<pre class="programlisting"><span class="identifier">x</span><span class="special">.</span><span class="identifier">some_function</span><span class="special">(</span><span class="identifier">a1</span><span class="special">,</span> <span class="identifier">a2</span><span class="special">,...</span><span class="identifier">an</span><span class="special">)</span></pre>
53<p>
54          always has semantics equivalent to:
55        </p>
56<pre class="programlisting"><span class="identifier">extract</span><span class="special">&lt;</span><span class="identifier">R</span><span class="special">&gt;(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">attr</span><span class="special">(</span><span class="string">"some_function"</span><span class="special">)(</span><span class="identifier">object</span><span class="special">(</span><span class="identifier">a1</span><span class="special">),</span> <span class="identifier">object</span><span class="special">(</span><span class="identifier">a2</span><span class="special">),...</span><span class="identifier">object</span><span class="special">(</span><span class="identifier">an</span><span class="special">)))()</span></pre>
57<p>
58          (see <a class="link" href="objectwrapper.html#concepts.objectwrapper.caveat" title="Caveat">caveat</a> below).
59        </p>
60</div>
61<div class="section">
62<div class="titlepage"><div><div><h3 class="title">
63<a name="concepts.objectwrapper.typewrapper_concept_requirements"></a><a class="link" href="objectwrapper.html#concepts.objectwrapper.typewrapper_concept_requirements" title="TypeWrapper Concept Requirements">TypeWrapper
64        Concept Requirements</a>
65</h3></div></div></div>
66<p>
67          TypeWrapper is a refinement of <a class="link" href="objectwrapper.html#concepts.objectwrapper.objectwrapper_concept_requiremen" title="ObjectWrapper Concept Requirements">ObjectWrapper</a>
68          which is associated with a particular Python type <code class="computeroutput"><span class="identifier">X</span></code>.
69          For a given TypeWrapper <code class="computeroutput"><span class="identifier">T</span></code>,
70          a valid constructor expression
71        </p>
72<pre class="programlisting"><span class="identifier">T</span><span class="special">(</span><span class="identifier">a1</span><span class="special">,</span> <span class="identifier">a2</span><span class="special">,...</span><span class="identifier">an</span><span class="special">)</span></pre>
73<p>
74          builds a new T object managing the result of invoking X with arguments
75          corresponding to
76        </p>
77<pre class="programlisting"><span class="identifier">object</span><span class="special">(</span><span class="identifier">a1</span><span class="special">),</span> <span class="identifier">object</span><span class="special">(</span><span class="identifier">a2</span><span class="special">),...</span><span class="identifier">object</span><span class="special">(</span><span class="identifier">an</span><span class="special">)</span></pre>
78<p>
79          . When used as arguments to wrapped C++ functions, or as the template parameter
80          to <a class="link" href="../to_from_python_type_conversion.html#to_from_python_type_conversion.boost_python_extract_hpp.class_template_extract" title="Class template extract">extract&lt;&gt;</a>,
81          only instances of the associated Python type will be considered a match.
82        </p>
83</div>
84<div class="section">
85<div class="titlepage"><div><div><h3 class="title">
86<a name="concepts.objectwrapper.caveat"></a><a class="link" href="objectwrapper.html#concepts.objectwrapper.caveat" title="Caveat">Caveat</a>
87</h3></div></div></div>
88<p>
89          The upshot of the special member function invocation rules when the return
90          type is a TypeWrapper is that it is possible for the returned object to
91          manage a Python object of an inappropriate type. This is not usually a
92          serious problem; the worst-case result is that errors will be detected
93          at runtime a little later than they might otherwise be. For an example
94          of how this can occur, note that the <a class="link" href="../object_wrappers.html#object_wrappers.boost_python_dict_hpp.class_dict" title="Class dict">dict</a>
95          member function <code class="computeroutput"><span class="identifier">items</span></code> returns
96          an object of type <a class="link" href="../object_wrappers/boost_python_list_hpp.html#object_wrappers.boost_python_list_hpp.class_list" title="Class list">list</a>.
97          Now suppose the user defines this <code class="computeroutput"><span class="identifier">dict</span></code>
98          subclass in Python:
99        </p>
100<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="keyword">class</span> <span class="identifier">mydict</span><span class="special">(</span><span class="identifier">dict</span><span class="special">):</span>
101<span class="special">...</span>     <span class="identifier">def</span> <span class="identifier">items</span><span class="special">(</span><span class="identifier">self</span><span class="special">):</span>
102<span class="special">...</span>         <span class="keyword">return</span> <span class="identifier">tuple</span><span class="special">(</span><span class="identifier">dict</span><span class="special">.</span><span class="identifier">items</span><span class="special">(</span><span class="identifier">self</span><span class="special">))</span> <span class="special">#</span> <span class="keyword">return</span> <span class="identifier">a</span> <span class="identifier">tuple</span>
103</pre>
104<p>
105          Since an instance of <code class="computeroutput"><span class="identifier">mydict</span></code>
106          is also an instance of <code class="computeroutput"><span class="identifier">dict</span></code>,
107          when used as an argument to a wrapped C++ function, <a class="link" href="../object_wrappers.html#object_wrappers.boost_python_dict_hpp.class_dict" title="Class dict">boost::python::dict</a>
108          can accept objects of Python type <code class="computeroutput"><span class="identifier">mydict</span></code>.
109          Invoking <code class="computeroutput"><span class="identifier">items</span><span class="special">()</span></code>
110          on this object can result in an instance of <a class="link" href="../object_wrappers/boost_python_list_hpp.html#object_wrappers.boost_python_list_hpp.class_list" title="Class list">boost::python::list</a>
111          which actually holds a Python <code class="computeroutput"><span class="identifier">tuple</span></code>.
112          Subsequent attempts to use <code class="computeroutput"><span class="identifier">list</span></code>
113          methods (e.g. <code class="computeroutput"><span class="identifier">append</span></code>, or
114          any other mutating operation) on this object will raise the same exception
115          that would occur if you tried to do it from Python.
116        </p>
117</div>
118</div>
119<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
120<td align="left"></td>
121<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
122        Distributed under the Boost Software License, Version 1.0. (See accompanying
123        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
124      </p>
125</div></td>
126</tr></table>
127<hr>
128<div class="spirit-nav">
129<a accesskey="p" href="resultconverter.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../concepts.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../high_level_components.html"><img src="../../images/next.png" alt="Next"></a>
130</div>
131</body>
132</html>
133