mirror of
https://github.com/boostorg/python.git
synced 2026-01-24 18:12:43 +00:00
Added return_by_value, enhanced data member support to handle constant members
[SVN r15935]
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
p.c3 {font-style: italic}
|
||||
h2.c2 {text-align: center}
|
||||
h1.c1 {text-align: center}
|
||||
</style>
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
@@ -352,8 +352,8 @@
|
||||
<dd>
|
||||
<dl class="index">
|
||||
<dt><a href="object.html#object-spec">object</a></dt>
|
||||
</dl>
|
||||
</dd>
|
||||
</dl>
|
||||
</dd>
|
||||
</dl>
|
||||
</dd>
|
||||
|
||||
@@ -716,6 +716,21 @@
|
||||
</dd>
|
||||
</dl>
|
||||
</dd>
|
||||
|
||||
<dt><a href="return_by_value.html">return_by_value.hpp</a></dt>
|
||||
|
||||
<dd>
|
||||
<dl class="index">
|
||||
<dt><a href="return_by_value.html#classes">Classes</a></dt>
|
||||
|
||||
<dd>
|
||||
<dl class="index">
|
||||
<dt><a href=
|
||||
"return_by_value.html#return_by_value-spec">return_by_value</a></dt>
|
||||
</dl>
|
||||
</dd>
|
||||
</dl>
|
||||
</dd>
|
||||
</dl>
|
||||
</dd>
|
||||
</dl>
|
||||
@@ -898,7 +913,8 @@
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
|
||||
08 October, 2002 <!--webbot bot="Timestamp" endspan i-checksum="39359" -->
|
||||
15 October, 2002
|
||||
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
|
||||
</p>
|
||||
|
||||
<p class="c3">© Copyright <a href=
|
||||
|
||||
147
doc/v2/return_by_value.html
Normal file
147
doc/v2/return_by_value.html
Normal file
@@ -0,0 +1,147 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<meta name="generator" content=
|
||||
"HTML Tidy for Windows (vers 1st August 2002), 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 - <boost/python/return_by_value.hpp></title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<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="../../../../c++boost.gif" border="0"></a></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Python</h1>
|
||||
|
||||
<h2 align="center">Header
|
||||
<boost/python/return_by_value.hpp></h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<h2>Contents</h2>
|
||||
|
||||
<dl class="page-index">
|
||||
<dt><a href="#classes">Classes</a></dt>
|
||||
|
||||
<dd>
|
||||
<dl class="page-index">
|
||||
<dt><a href="#return_by_value-spec">Class
|
||||
<code>return_by_value</code></a></dt>
|
||||
|
||||
<dd>
|
||||
<dl class="page-index">
|
||||
<dt><a href="#return_by_value-spec-synopsis">Class
|
||||
<code>return_by_value</code> synopsis</a></dt>
|
||||
|
||||
<dt><a href="#return_by_value-spec-metafunctions">Class
|
||||
<code>return_by_value</code> metafunctions</a></dt>
|
||||
</dl>
|
||||
</dd>
|
||||
</dl>
|
||||
</dd>
|
||||
|
||||
<dt><a href="#examples">Example</a></dt>
|
||||
</dl>
|
||||
<hr>
|
||||
|
||||
<h2><a name="classes"></a>Classes</h2>
|
||||
|
||||
<h3><a name="return_by_value-spec"></a>Class
|
||||
<code>return_by_value</code></h3>
|
||||
|
||||
<p><code>return_by_value</code> is a model of <a href=
|
||||
"ResultConverter.html#ResultConverterGenerator-concept">ResultConverterGenerator</a>
|
||||
which can be used to wrap C++ functions returning any reference or value
|
||||
type such that the return value is copied into a new Python object.</p>
|
||||
|
||||
<h4><a name="return_by_value-spec-synopsis"></a>Class
|
||||
<code>return_by_value</code> synopsis</h4>
|
||||
<pre>
|
||||
namespace boost { namespace python
|
||||
{
|
||||
struct return_by_value
|
||||
{
|
||||
template <class T> struct apply;
|
||||
};
|
||||
}}
|
||||
</pre>
|
||||
|
||||
<h4><a name="return_by_value-spec-metafunctions"></a>Class
|
||||
<code>return_by_value</code> metafunctions</h4>
|
||||
<pre>
|
||||
template <class T> struct apply
|
||||
</pre>
|
||||
|
||||
<dl class="metafunction-semantics">
|
||||
<dt><b>Returns:</b> <code>typedef <a href=
|
||||
"to_python_value.html#to_python_value-spec">to_python_value</a><T>
|
||||
type;</code></dt>
|
||||
</dl>
|
||||
|
||||
<h2><a name="examples"></a>Example</h2>
|
||||
|
||||
<h3>C++ Module Definition</h3>
|
||||
<pre>
|
||||
#include <boost/python/module.hpp>
|
||||
#include <boost/python/class.hpp>
|
||||
#include <boost/python/return_by_value.hpp>
|
||||
#include <boost/python/return_value_policy.hpp>
|
||||
|
||||
// classes to wrap
|
||||
struct Bar { };
|
||||
|
||||
Bar global_bar;
|
||||
|
||||
// functions to wrap:
|
||||
Bar b1();
|
||||
Bar& b2();
|
||||
Bar const& b3();
|
||||
|
||||
// Wrapper code
|
||||
using namespace boost::python;
|
||||
template <class R>
|
||||
void def_void_function(char const* name, R (*f)())
|
||||
{
|
||||
def(name, f, return_value_policy<return_by_value>());
|
||||
}
|
||||
|
||||
BOOST_PYTHON_MODULE(my_module)
|
||||
{
|
||||
class_<Bar>("Bar");
|
||||
def_void_function("b1", b1);
|
||||
def_void_function("b2", b2);
|
||||
def_void_function("b3", b3);
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h3>Python Code</h3>
|
||||
<pre>
|
||||
>>> from my_module import *
|
||||
>>> b = b1() # each of these calls
|
||||
>>> b = b2() # creates a brand
|
||||
>>> b = b3() # new Bar object
|
||||
</pre>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
|
||||
15 October, 2002
|
||||
<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
|
||||
</p>
|
||||
|
||||
<p><i>© Copyright <a href=
|
||||
"../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002. All Rights
|
||||
Reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
# include <boost/type_traits/transform_traits.hpp>
|
||||
# include <boost/type_traits/cv_traits.hpp>
|
||||
# include <boost/python/return_value_policy.hpp>
|
||||
# include <boost/python/copy_non_const_reference.hpp>
|
||||
# include <boost/python/return_by_value.hpp>
|
||||
# include <boost/python/object/function_object.hpp>
|
||||
# include <boost/python/arg_from_python.hpp>
|
||||
# include <boost/bind.hpp>
|
||||
@@ -65,7 +65,7 @@ namespace detail
|
||||
template <class C, class D>
|
||||
object make_getter(D C::*pm)
|
||||
{
|
||||
typedef return_value_policy<copy_non_const_reference> default_policy;
|
||||
typedef return_value_policy<return_by_value> default_policy;
|
||||
|
||||
return objects::function_object(
|
||||
::boost::bind(
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
# include <boost/python/class.hpp>
|
||||
# include <boost/python/object/class_detail.hpp>
|
||||
# include <boost/python/return_value_policy.hpp>
|
||||
# include <boost/python/copy_const_reference.hpp>
|
||||
# include <boost/python/return_by_value.hpp>
|
||||
# include <boost/python/object/function_object.hpp>
|
||||
# include <boost/python/handle.hpp>
|
||||
# include <boost/type.hpp>
|
||||
@@ -29,18 +29,7 @@ namespace boost { namespace python { namespace objects {
|
||||
// iterators are copied, so we just replace the result_converter from
|
||||
// the default_iterator_call_policies with a permissive one which
|
||||
// always copies the result.
|
||||
struct default_iterator_call_policies
|
||||
: default_call_policies
|
||||
{
|
||||
struct result_converter
|
||||
{
|
||||
template <class R>
|
||||
struct apply
|
||||
{
|
||||
typedef to_python_value<R> type;
|
||||
};
|
||||
};
|
||||
};
|
||||
typedef return_value_policy<return_by_value> default_iterator_call_policies;
|
||||
|
||||
// Instantiations of these are wrapped to produce Python iterators.
|
||||
template <class NextPolicies, class Iterator>
|
||||
|
||||
30
include/boost/python/return_by_value.hpp
Normal file
30
include/boost/python/return_by_value.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright David Abrahams 2002. Permission to copy, use,
|
||||
// modify, sell and distribute this software is granted provided this
|
||||
// copyright notice appears in all copies. This software is provided
|
||||
// "as is" without express or implied warranty, and with no claim as
|
||||
// to its suitability for any purpose.
|
||||
#ifndef BY_VALUE_DWA20021015_HPP
|
||||
# define BY_VALUE_DWA20021015_HPP
|
||||
|
||||
# include <boost/python/to_python_value.hpp>
|
||||
# include <boost/type_traits/add_reference.hpp>
|
||||
# include <boost/type_traits/add_const.hpp>
|
||||
|
||||
namespace boost { namespace python {
|
||||
|
||||
struct return_by_value
|
||||
{
|
||||
template <class R>
|
||||
struct apply
|
||||
{
|
||||
typedef to_python_value<
|
||||
typename add_reference<
|
||||
typename add_const<R>::type
|
||||
>::type
|
||||
> type;
|
||||
};
|
||||
};
|
||||
|
||||
}} // namespace boost::python
|
||||
|
||||
#endif // BY_VALUE_DWA20021015_HPP
|
||||
@@ -5,6 +5,8 @@
|
||||
// to its suitability for any purpose.
|
||||
#include <boost/python/class.hpp>
|
||||
#include <boost/python/module.hpp>
|
||||
#include <boost/python/return_value_policy.hpp>
|
||||
#include <boost/python/return_by_value.hpp>
|
||||
#include "test_class.hpp"
|
||||
|
||||
#if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245
|
||||
@@ -20,6 +22,16 @@ typedef test_class<1> Y;
|
||||
|
||||
double get_fair_value(X const& x) { return x.value(); }
|
||||
|
||||
struct Var
|
||||
{
|
||||
Var(std::string name_) : name(name_), value(), name2(name.c_str()) {}
|
||||
std::string const name;
|
||||
std::string get_name1() const { return name; }
|
||||
std::string const& get_name2() const { return name; }
|
||||
float value;
|
||||
char const* name2;
|
||||
};
|
||||
|
||||
BOOST_PYTHON_MODULE(data_members_ext)
|
||||
{
|
||||
class_<X>("X", init<int>())
|
||||
@@ -34,6 +46,19 @@ BOOST_PYTHON_MODULE(data_members_ext)
|
||||
.def("set", &Y::set)
|
||||
.def_readwrite("x", &Y::x)
|
||||
;
|
||||
|
||||
class_<Var>("Var", init<std::string>())
|
||||
.def_readonly("name", &Var::name)
|
||||
.def_readonly("name2", &Var::name2)
|
||||
.def_readwrite("value", &Var::value)
|
||||
|
||||
// Test return_by_value for plain values and for
|
||||
// pointers... return_by_value was implemented as a
|
||||
// side-effect of implementing data member support, so it made
|
||||
// sense to add the test here.
|
||||
.def("get_name1", &Var::get_name1, return_value_policy<return_by_value>())
|
||||
.def("get_name2", &Var::get_name2, return_value_policy<return_by_value>())
|
||||
;
|
||||
}
|
||||
|
||||
#include "module_tail.cpp"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
'''
|
||||
>>> from data_members_ext import *
|
||||
|
||||
>>> x = X(42)
|
||||
>>> x.x
|
||||
42
|
||||
@@ -9,13 +10,26 @@
|
||||
|
||||
>>> x.fair_value
|
||||
42.0
|
||||
|
||||
>>> y = Y(69)
|
||||
>>> y.x
|
||||
69
|
||||
>>> y.x = 77
|
||||
>>> y.x
|
||||
77
|
||||
|
||||
>>> v = Var("pi")
|
||||
>>> v.value = 3.14
|
||||
>>> v.name
|
||||
'pi'
|
||||
>>> v.name2
|
||||
'pi'
|
||||
|
||||
>>> v.get_name1()
|
||||
'pi'
|
||||
|
||||
>>> v.get_name2()
|
||||
'pi'
|
||||
|
||||
'''
|
||||
|
||||
def run(args = None):
|
||||
|
||||
Reference in New Issue
Block a user