diff --git a/doc/v2/reference.html b/doc/v2/reference.html index be866666..8ec51d9b 100644 --- a/doc/v2/reference.html +++ b/doc/v2/reference.html @@ -13,7 +13,7 @@ p.c3 {font-style: italic} h2.c2 {text-align: center} h1.c1 {text-align: center} - + @@ -352,8 +352,8 @@
object
-
-
+ + @@ -716,6 +716,21 @@ + +
return_by_value.hpp
+ +
+
+
Classes
+ +
+
+
return_by_value
+
+
+
+
@@ -898,7 +913,8 @@

Revised - 08 October, 2002 + 15 October, 2002 +

© Copyright + + + + + + + + Boost.Python - <boost/python/return_by_value.hpp> + + + + + + + + + +
+

C++ Boost

+
+

Boost.Python

+ +

Header + <boost/python/return_by_value.hpp>

+
+


+ +

Contents

+ +
+
Classes
+ +
+
+
Class + return_by_value
+ +
+
+
Class + return_by_value synopsis
+ +
Class + return_by_value metafunctions
+
+
+
+
+ +
Example
+
+
+ +

Classes

+ +

Class + return_by_value

+ +

return_by_value is a model of ResultConverterGenerator + 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.

+ +

Class + return_by_value synopsis

+
+namespace boost { namespace python
+{
+    struct return_by_value
+    {
+        template <class T> struct apply;
+    };
+}}
+
+ +

Class + return_by_value metafunctions

+
+template <class T> struct apply
+
+ +
+
Returns: typedef to_python_value<T> + type;
+
+ +

Example

+ +

C++ Module Definition

+
+#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);
+}
+
+ +

Python Code

+
+>>> from my_module import *
+>>> b = b1() # each of these calls
+>>> b = b2() # creates a brand
+>>> b = b3() # new Bar object
+
+ +

Revised + + 15 October, 2002 + +

+ +

© Copyright Dave Abrahams 2002. All Rights + Reserved.

+ + + diff --git a/include/boost/python/data_members.hpp b/include/boost/python/data_members.hpp index 01190811..62a38570 100644 --- a/include/boost/python/data_members.hpp +++ b/include/boost/python/data_members.hpp @@ -11,7 +11,7 @@ # include # include # include -# include +# include # include # include # include @@ -65,7 +65,7 @@ namespace detail template object make_getter(D C::*pm) { - typedef return_value_policy default_policy; + typedef return_value_policy default_policy; return objects::function_object( ::boost::bind( diff --git a/include/boost/python/object/iterator.hpp b/include/boost/python/object/iterator.hpp index 01bef3f1..65c206d7 100644 --- a/include/boost/python/object/iterator.hpp +++ b/include/boost/python/object/iterator.hpp @@ -10,7 +10,7 @@ # include # include # include -# include +# include # include # include # include @@ -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 - struct apply - { - typedef to_python_value type; - }; - }; -}; +typedef return_value_policy default_iterator_call_policies; // Instantiations of these are wrapped to produce Python iterators. template diff --git a/include/boost/python/return_by_value.hpp b/include/boost/python/return_by_value.hpp new file mode 100644 index 00000000..97f9f245 --- /dev/null +++ b/include/boost/python/return_by_value.hpp @@ -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 +# include +# include + +namespace boost { namespace python { + +struct return_by_value +{ + template + struct apply + { + typedef to_python_value< + typename add_reference< + typename add_const::type + >::type + > type; + }; +}; + +}} // namespace boost::python + +#endif // BY_VALUE_DWA20021015_HPP diff --git a/test/data_members.cpp b/test/data_members.cpp index 2345634a..5a87eeae 100644 --- a/test/data_members.cpp +++ b/test/data_members.cpp @@ -5,6 +5,8 @@ // to its suitability for any purpose. #include #include +#include +#include #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", init()) @@ -34,6 +46,19 @@ BOOST_PYTHON_MODULE(data_members_ext) .def("set", &Y::set) .def_readwrite("x", &Y::x) ; + + class_("Var", init()) + .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()) + .def("get_name2", &Var::get_name2, return_value_policy()) + ; } #include "module_tail.cpp" diff --git a/test/data_members.py b/test/data_members.py index ab79a90a..c908f515 100644 --- a/test/data_members.py +++ b/test/data_members.py @@ -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):