summaryrefslogtreecommitdiff
path: root/libs/python/doc/v2/to_python_indirect.html
blob: 17e0a826c4012b85256455774a026c4eb775f29a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <meta name="generator" content="HTML Tidy, see www.w3.org">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link rel="stylesheet" type="text/css" href="../boost.css">

    <title>Boost.Python - &lt;boost/python/to_python_indirect.hpp&gt;</title>

    <table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
    "header">
      <tr>
        <td valign="top" width="300">
          <h3><a href="../../../../index.htm"><img height="86" width="277" alt=
          "C++ Boost" src="../../../../boost.png" border="0"></a></h3>

        <td valign="top">
          <h1 align="center"><a href="../index.html">Boost.Python</a></h1>

          <h2 align="center">Header &lt;boost/python/to_python_indirect.hpp&gt;</h2>
    </table>
    <hr>

    <h2>Contents</h2>

    <dl class="page-index">
      <dt><a href="#introduction">Introduction</a>


      <dt><a href="#classes">Classes</a>

      <dd>
        <dl class="page-index">
          <dt><a href="#to_python_indirect-spec">Class Template <code>to_python_indirect</code></a>

          <dd>
            <dl class="page-index">

              <dt><a href="#to_python_indirect-spec-synopsis">Class Template
              <code>to_python_indirect</code> synopsis</a>

              <dt><a href="#to_python_indirect-spec-observers">Class Template
              <code>to_python_indirect</code> observer functions</a>

              <dt><a href="#to_python_indirect-spec-statics">Class Template
              <code>to_python_indirect</code> static functions</a>
            </dl>
        </dl>

      <dt><a href="#examples">Example</a>
    </dl>
    <hr>

    <h2><a name="introduction"></a>Introduction</h2>

    <code>&lt;boost/python/to_python_indirect.hpp&gt;</code> supplies
    a way to construct new Python objects that hold wrapped C++ class
    instances via a pointer or smart pointer.

    <h2><a name="classes"></a>Classes</h2>

    <h3><a name="to_python_indirect-spec"></a>Class template <code>to_python_indirect</code></h3>
    <p>Class template <code>to_python_indirect</code> converts objects
of its first argument type to python as extension class instances, using the ownership policy provided by its 2nd argument.

    <p>


    <table border="1" summary="to_python_indirect template parameters">
      <caption>
        <b><code>to_python_indirect</code> Requirements</b><br>

        In the table below, <b><code>x</code></b> denotes an object of
        type <code>T</code>, <b><code>h</code></b> denotes an
        object of type
        <code>boost::python::objects::instance_holder*</code>, and
        <b><code>p</code></b> denotes an object of type
        <code>U*</code>.

      </caption>
      <tr>
        <th>Parameter
        
        <th>Requirements

        <th>Description

      <tr>
        <td><code>T</code> 

        <td>Either <code>U</code>&nbsp;<i>cv</i><code>&amp;</code>
        (where <i>cv</i> is any optional cv-qualification) or a <a
        href="Dereferenceable.html">Dereferenceable</a> type such that
        <code>*x</code> is convertible to <code>U const&amp;</code>, where
        <code>U</code> is a class type.

        <td>A type deferencing a C++ class exposed to Python using
        class template <code><a
        href="class.html#class_-spec">class_</a></code>.

      <tr>
        <td><code>MakeHolder</code> 

        <td>h = MakeHolder::execute(p);

        <td>A class whose static <code>execute()</code> creates an
        <code>instance_holder</code>.

    </table>

    Instantiations of <code>to_python_indirect</code> are models of <a
    href="ResultConverter.html#ResultConverter-concept">ResultConverter</a>.


    <h4><a name="to_python_indirect-spec-synopsis"></a>Class template <code>to_python_indirect</code> synopsis</h4>
<pre>
namespace boost { namespace python
{
  template &lt;class T, class MakeHolder&gt;
  struct to_python_indirect
  {
     static bool convertible();
     PyObject* operator()(T ptr_or_reference) const;
   private:
     static PyTypeObject* type();
  };
}}
</pre>

    <h4><a name="to_python_indirect-spec-observers"></a>Class template <code>to_python_indirect</code> observers</h4>
<pre>
PyObject* operator()(T x) const;
</pre>

    <dl class="function-semantics">

      <dt><b>Requires:</b> <code>x</code> refers to an object (if it
      is a pointer type, it is non-null). <code>convertible() ==
      true</code>.

      <dt><b>Effects:</b> Creates an appropriately-typed Boost.Python
      extension class instance, uses <code>MakeHolder</code> to create
      an <code>instance_holder</code> from <code>x</code>, installs
      the <code>instance_holder</code> in the new extension class
      instance, and returns a pointer to it.

    </dl>


    <h4><a name="to_python_indirect-spec-statics"></a>Class template <code>to_python_indirect</code> statics</h4>
<pre>
bool convertible();
</pre>

      <dt><b>Effects:</b> Returns <code>true</code> iff any module has
      registered a Python type corresponding to <code>U</code>.

    <h2><a name="examples"></a>Example</h2>

This example replicates the functionality of <a
href="reference_existing_object.html#reference_existing_object-spec">reference_existing_object</a>,
but without some of the compile-time error checking.


<pre>

struct make_reference_holder
{
   typedef boost::python::objects::instance_holder* result_type;
   template &lt;class T&gt;
   static result_type execute(T* p)
   {
      return new boost::python::objects::pointer_holder&lt;T*, T&gt;(p);
   }
};

struct reference_existing_object
{
   // metafunction returning the <a href="ResultConverter.html#ResultConverter-concept">ResultConverter</a>
   template &lt;class T&gt;
   struct apply
   {
      typedef boost::python::to_python_indirect&lt;T,make_reference_holder&gt; type;
   };
};
</pre>

    <p>Revised 
    <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
  13 November, 2002
  <!--webbot bot="Timestamp" endspan i-checksum="39359" -->


    <p><i>&copy; Copyright <a href="http://www.boost.org/people/dave_abrahams.htm">Dave
    Abrahams</a> 2002. </i> Distributed
  under the Boost Software License, Version 1.0. (See accompanying file
  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)</p>