Skip to content

Commit c389e05

Browse files
committed
Added return_by_value, enhanced data member support to handle constant members
[SVN r15935]
1 parent 2c7829f commit c389e05

File tree

7 files changed

+241
-20
lines changed

7 files changed

+241
-20
lines changed

doc/v2/reference.html

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
p.c3 {font-style: italic}
1414
h2.c2 {text-align: center}
1515
h1.c1 {text-align: center}
16-
</style>
16+
</style>
1717
</head>
1818

1919
<body>
@@ -352,8 +352,8 @@ <h2><a name="object_wrappers">Object Wrappers</a></h2>
352352
<dd>
353353
<dl class="index">
354354
<dt><a href="object.html#object-spec">object</a></dt>
355-
</dl>
356-
</dd>
355+
</dl>
356+
</dd>
357357
</dl>
358358
</dd>
359359

@@ -716,6 +716,21 @@ <h3>Models of ResultConverterGenerator</h3>
716716
</dd>
717717
</dl>
718718
</dd>
719+
720+
<dt><a href="return_by_value.html">return_by_value.hpp</a></dt>
721+
722+
<dd>
723+
<dl class="index">
724+
<dt><a href="return_by_value.html#classes">Classes</a></dt>
725+
726+
<dd>
727+
<dl class="index">
728+
<dt><a href=
729+
"return_by_value.html#return_by_value-spec">return_by_value</a></dt>
730+
</dl>
731+
</dd>
732+
</dl>
733+
</dd>
719734
</dl>
720735
</dd>
721736
</dl>
@@ -898,7 +913,8 @@ <h2><a name="topics">Topics</a></h2>
898913

899914
<p>Revised
900915
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
901-
08 October, 2002 <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
916+
15 October, 2002
917+
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
902918
</p>
903919

904920
<p class="c3">&copy; Copyright <a href=

doc/v2/return_by_value.html

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2+
3+
<html>
4+
<head>
5+
<meta name="generator" content=
6+
"HTML Tidy for Windows (vers 1st August 2002), see www.w3.org">
7+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
8+
<link rel="stylesheet" type="text/css" href="../boost.css">
9+
10+
<title>Boost.Python - &lt;boost/python/return_by_value.hpp&gt;</title>
11+
</head>
12+
13+
<body>
14+
<table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
15+
"header">
16+
<tr>
17+
<td valign="top" width="300">
18+
<h3><a href="../../../../index.htm"><img height="86" width="277"
19+
alt="C++ Boost" src="../../../../c++boost.gif" border="0"></a></h3>
20+
</td>
21+
22+
<td valign="top">
23+
<h1 align="center">Boost.Python</h1>
24+
25+
<h2 align="center">Header
26+
&lt;boost/python/return_by_value.hpp&gt;</h2>
27+
</td>
28+
</tr>
29+
</table>
30+
<hr>
31+
32+
<h2>Contents</h2>
33+
34+
<dl class="page-index">
35+
<dt><a href="#classes">Classes</a></dt>
36+
37+
<dd>
38+
<dl class="page-index">
39+
<dt><a href="#return_by_value-spec">Class
40+
<code>return_by_value</code></a></dt>
41+
42+
<dd>
43+
<dl class="page-index">
44+
<dt><a href="#return_by_value-spec-synopsis">Class
45+
<code>return_by_value</code> synopsis</a></dt>
46+
47+
<dt><a href="#return_by_value-spec-metafunctions">Class
48+
<code>return_by_value</code> metafunctions</a></dt>
49+
</dl>
50+
</dd>
51+
</dl>
52+
</dd>
53+
54+
<dt><a href="#examples">Example</a></dt>
55+
</dl>
56+
<hr>
57+
58+
<h2><a name="classes"></a>Classes</h2>
59+
60+
<h3><a name="return_by_value-spec"></a>Class
61+
<code>return_by_value</code></h3>
62+
63+
<p><code>return_by_value</code> is a model of <a href=
64+
"ResultConverter.html#ResultConverterGenerator-concept">ResultConverterGenerator</a>
65+
which can be used to wrap C++ functions returning any reference or value
66+
type such that the return value is copied into a new Python object.</p>
67+
68+
<h4><a name="return_by_value-spec-synopsis"></a>Class
69+
<code>return_by_value</code> synopsis</h4>
70+
<pre>
71+
namespace boost { namespace python
72+
{
73+
struct return_by_value
74+
{
75+
template &lt;class T&gt; struct apply;
76+
};
77+
}}
78+
</pre>
79+
80+
<h4><a name="return_by_value-spec-metafunctions"></a>Class
81+
<code>return_by_value</code> metafunctions</h4>
82+
<pre>
83+
template &lt;class T&gt; struct apply
84+
</pre>
85+
86+
<dl class="metafunction-semantics">
87+
<dt><b>Returns:</b> <code>typedef <a href=
88+
"to_python_value.html#to_python_value-spec">to_python_value</a>&lt;T&gt;
89+
type;</code></dt>
90+
</dl>
91+
92+
<h2><a name="examples"></a>Example</h2>
93+
94+
<h3>C++ Module Definition</h3>
95+
<pre>
96+
#include &lt;boost/python/module.hpp&gt;
97+
#include &lt;boost/python/class.hpp&gt;
98+
#include &lt;boost/python/return_by_value.hpp&gt;
99+
#include &lt;boost/python/return_value_policy.hpp&gt;
100+
101+
// classes to wrap
102+
struct Bar { };
103+
104+
Bar global_bar;
105+
106+
// functions to wrap:
107+
Bar b1();
108+
Bar&amp; b2();
109+
Bar const&amp; b3();
110+
111+
// Wrapper code
112+
using namespace boost::python;
113+
template &lt;class R&gt;
114+
void def_void_function(char const* name, R (*f)())
115+
{
116+
def(name, f, return_value_policy&lt;return_by_value&gt;());
117+
}
118+
119+
BOOST_PYTHON_MODULE(my_module)
120+
{
121+
class_&lt;Bar&gt;("Bar");
122+
def_void_function("b1", b1);
123+
def_void_function("b2", b2);
124+
def_void_function("b3", b3);
125+
}
126+
</pre>
127+
128+
<h3>Python Code</h3>
129+
<pre>
130+
&gt;&gt;&gt; from my_module import *
131+
&gt;&gt;&gt; b = b1() # each of these calls
132+
&gt;&gt;&gt; b = b2() # creates a brand
133+
&gt;&gt;&gt; b = b3() # new Bar object
134+
</pre>
135+
136+
<p>Revised
137+
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
138+
15 October, 2002
139+
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
140+
</p>
141+
142+
<p><i>&copy; Copyright <a href=
143+
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002. All Rights
144+
Reserved.</i></p>
145+
</body>
146+
</html>
147+

include/boost/python/data_members.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# include <boost/type_traits/transform_traits.hpp>
1212
# include <boost/type_traits/cv_traits.hpp>
1313
# include <boost/python/return_value_policy.hpp>
14-
# include <boost/python/copy_non_const_reference.hpp>
14+
# include <boost/python/return_by_value.hpp>
1515
# include <boost/python/object/function_object.hpp>
1616
# include <boost/python/arg_from_python.hpp>
1717
# include <boost/bind.hpp>
@@ -65,7 +65,7 @@ namespace detail
6565
template <class C, class D>
6666
object make_getter(D C::*pm)
6767
{
68-
typedef return_value_policy<copy_non_const_reference> default_policy;
68+
typedef return_value_policy<return_by_value> default_policy;
6969

7070
return objects::function_object(
7171
::boost::bind(

include/boost/python/object/iterator.hpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# include <boost/python/class.hpp>
1111
# include <boost/python/object/class_detail.hpp>
1212
# include <boost/python/return_value_policy.hpp>
13-
# include <boost/python/copy_const_reference.hpp>
13+
# include <boost/python/return_by_value.hpp>
1414
# include <boost/python/object/function_object.hpp>
1515
# include <boost/python/handle.hpp>
1616
# include <boost/type.hpp>
@@ -29,18 +29,7 @@ namespace boost { namespace python { namespace objects {
2929
// iterators are copied, so we just replace the result_converter from
3030
// the default_iterator_call_policies with a permissive one which
3131
// always copies the result.
32-
struct default_iterator_call_policies
33-
: default_call_policies
34-
{
35-
struct result_converter
36-
{
37-
template <class R>
38-
struct apply
39-
{
40-
typedef to_python_value<R> type;
41-
};
42-
};
43-
};
32+
typedef return_value_policy<return_by_value> default_iterator_call_policies;
4433

4534
// Instantiations of these are wrapped to produce Python iterators.
4635
template <class NextPolicies, class Iterator>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright David Abrahams 2002. Permission to copy, use,
2+
// modify, sell and distribute this software is granted provided this
3+
// copyright notice appears in all copies. This software is provided
4+
// "as is" without express or implied warranty, and with no claim as
5+
// to its suitability for any purpose.
6+
#ifndef BY_VALUE_DWA20021015_HPP
7+
# define BY_VALUE_DWA20021015_HPP
8+
9+
# include <boost/python/to_python_value.hpp>
10+
# include <boost/type_traits/add_reference.hpp>
11+
# include <boost/type_traits/add_const.hpp>
12+
13+
namespace boost { namespace python {
14+
15+
struct return_by_value
16+
{
17+
template <class R>
18+
struct apply
19+
{
20+
typedef to_python_value<
21+
typename add_reference<
22+
typename add_const<R>::type
23+
>::type
24+
> type;
25+
};
26+
};
27+
28+
}} // namespace boost::python
29+
30+
#endif // BY_VALUE_DWA20021015_HPP

test/data_members.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// to its suitability for any purpose.
66
#include <boost/python/class.hpp>
77
#include <boost/python/module.hpp>
8+
#include <boost/python/return_value_policy.hpp>
9+
#include <boost/python/return_by_value.hpp>
810
#include "test_class.hpp"
911

1012
#if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245
@@ -20,6 +22,16 @@ typedef test_class<1> Y;
2022

2123
double get_fair_value(X const& x) { return x.value(); }
2224

25+
struct Var
26+
{
27+
Var(std::string name_) : name(name_), value(), name2(name.c_str()) {}
28+
std::string const name;
29+
std::string get_name1() const { return name; }
30+
std::string const& get_name2() const { return name; }
31+
float value;
32+
char const* name2;
33+
};
34+
2335
BOOST_PYTHON_MODULE(data_members_ext)
2436
{
2537
class_<X>("X", init<int>())
@@ -34,6 +46,19 @@ BOOST_PYTHON_MODULE(data_members_ext)
3446
.def("set", &Y::set)
3547
.def_readwrite("x", &Y::x)
3648
;
49+
50+
class_<Var>("Var", init<std::string>())
51+
.def_readonly("name", &Var::name)
52+
.def_readonly("name2", &Var::name2)
53+
.def_readwrite("value", &Var::value)
54+
55+
// Test return_by_value for plain values and for
56+
// pointers... return_by_value was implemented as a
57+
// side-effect of implementing data member support, so it made
58+
// sense to add the test here.
59+
.def("get_name1", &Var::get_name1, return_value_policy<return_by_value>())
60+
.def("get_name2", &Var::get_name2, return_value_policy<return_by_value>())
61+
;
3762
}
3863

3964
#include "module_tail.cpp"

test/data_members.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'''
22
>>> from data_members_ext import *
3+
34
>>> x = X(42)
45
>>> x.x
56
42
@@ -9,13 +10,26 @@
910
1011
>>> x.fair_value
1112
42.0
12-
1313
>>> y = Y(69)
1414
>>> y.x
1515
69
1616
>>> y.x = 77
1717
>>> y.x
1818
77
19+
20+
>>> v = Var("pi")
21+
>>> v.value = 3.14
22+
>>> v.name
23+
'pi'
24+
>>> v.name2
25+
'pi'
26+
27+
>>> v.get_name1()
28+
'pi'
29+
30+
>>> v.get_name2()
31+
'pi'
32+
1933
'''
2034

2135
def run(args = None):

0 commit comments

Comments
 (0)