Boost Python 包装C++函数供Python使用时返回值为引用类型的解决方案

               作者:华亮        地址:http://blog.csdn.net/cedricporter


我们有一段C++代码

A aaa;

A& DoSomethingWithA( int a )
{
	aaa.Set( 12 );
	return aaa;
	//return &aaa;
}
//
BOOST_PYTHON_MODULE( Haha )
{
	using namespace boost::python;
	class_< A >( "A", "Lala" )
		.def( "Set", &A::Set, arg("c") )
		.def_readwrite( "b", &A::b );
	def( "DoSomethingWithA", DoSomethingWithA, arg("a"), return_value_policy<reference_existing_object>() );
}

一段Python

a = DoSomethingWithA( 5 )

def do():
    return a.b;

我们在C++中写道

	boost::python::object main_module = boost::python::import("a");  
	object dofoo = main_module.attr("do");  
	std::cout << extract<int>( dofoo() );

此时会输出12,正如我们所想的那样。


关键问题所在是


def( "DoSomethingWithA", DoSomethingWithA, arg("a"), return_value_policy<reference_existing_object>() );



下面这段文字从http://wiki.python.org/moin/boost.python/CallPolicy#return_value_policy.3CT.3E拷贝过来的。有空再翻译了。

return_value_policy<T>

with T one of:


reference_existing_object

naïve (dangerous) approach

boost.python/ResultConverterGenerator which can be used to wrap C++ functions returning a reference or pointer to a C++ object.

When the wrapped function is called, the value referenced by its return value is not copied. 
A new Python object is created which contains an unowned U* pointer to the referent of the wrapped function's return value, and no attempt is made to ensure that the lifetime of the referent is at least as long as that of the corresponding Python object.

This class is used in the implementation of return_internal_reference. Also NULL pointer returning as None.


copy_non_const_reference


copy_const_reference



BoostPython v1 approach

manage_new_object

BoostPython/ResultConverterGenerator which can be used to wrap C++ functions returning a pointer to an object allocated with a new-expression and expecting the caller to take responsibility for deleting that C++ object from heap. boost.python will do it as part of Python object destruction.

Use case:

T* factory() { return new T(); }

class_<T>("T");

def("Tfactory", factory, return_value_policy<manage_new_object>() );

return_by_value

boost.python/ResultConverterGenerator which can be used to wrap C++ functions returning any reference or value type.
The return value is copied into a new Python object.


你可能感兴趣的:(C++,object,python,Module,reference,returning)