Boost Python 包装C++函数供Python使用时返回值为引用类型的解决方案
2011-09-28 00:41
555 查看
作者:华亮 地址:http://blog.csdn.net/cedricporter
我们有一段C++代码
一段Python
我们在C++中写道
此时会输出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拷贝过来的。有空再翻译了。
with T one of:
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.
BoostPython v1
approach
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:
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++代码
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) approachboost.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 v1approach
manage_new_object
BoostPython/ResultConverterGenerator whichcan 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 whichcan be used to wrap C++ functions returning any reference or value type.
The return value is copied into a new Python object.
相关文章推荐
- C++函数的返回值——返回引用类型&非引用类型
- 关于Column '*' not found 解决方案 Hibernate使用SQL查询返回实体类型,即返回某个类,或实体类
- C++函数的返回值——返回引用类型&非引用类型
- C++函数的返回值——返回引用类型&非引用类型
- C++函数的返回值——返回引用类型&非引用类型
- C++函数的返回值——返回引用类型&非引用类型
- 对引用和指针使用以及函数返回引用和指针类型的理解
- C++函数的返回值问题(1)——返回引用类型&非引用类型
- 对引用和指针使用以及函数返回引用和指针类型的理解
- C++函数的返回值——返回引用类型&非引用类型
- 6.4内部类:类作为方法的返回值类型,内部类中使用外部类的引用(或称外部类对象)
- Python引用类型和值类型的区别与使用解析
- 在使用EFCodeFirst中出现类型“System.Data.Objects.ObjectContext”在未被引用的程序集中定义的解决方案
- C++函数的返回值——返回引用类型&非引用类型
- C++函数的返回值——返回引用类型&非引用类型
- 在使用EFCodeFirst中出现类型“System.Data.Objects.ObjectContext”在未被引用的程序集中定义的解决方案
- 对引用和指针使用以及函数返回引用和指针类型的理解
- 对引用和指针使用以及函数返回引用和指针类型的理解
- C++函数的返回值——返回引用类型&非引用类型
- C++函数的返回值——返回引用类型&非引用类型