boost/python/copy_non_const_reference.hpp
クラス
copy_non_const_reference
クラス
-
struct copy_non_const_reference
copy_non_const_reference
は、参照先の値を新しい Python オブジェクトにコピーする型への非const
参照を返す C++ 関数をラップするのに使用する ResultConverterGenerator のモデルである。
copy_non_const_reference
クラスの概要
namespace boost { namespace python
{
struct copy_non_const_reference
{
template <class T> struct apply;
};
}}
copy_non_const_reference
クラスのメタ関数
-
template<class T>
struct apply - 要件
非 const な
U
に対してT
がU&
。
-
typedef to_python_value<T> type
例
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/copy_non_const_reference.hpp>
#include <boost/python/return_value_policy.hpp>
// ラップするクラス群
struct Bar { int x; }
struct Foo {
Foo(int x) : { b.x = x; }
Bar& get_bar() { return b; }
private:
Bar b;
};
// ラッパコード
using namespace boost::python;
BOOST_PYTHON_MODULE(my_module)
{
class_<Bar>("Bar");
class_<Foo>("Foo", init<int>())
.def("get_bar", &Foo::get_bar
, return_value_policy<copy_non_const_reference>())
;
}
>>> from my_module import *
>>> f = Foo(3) # Foo オブジェクトを作成
>>> b = f.get_bar() # 内部的な Bar オブジェクトのコピーを作成