boost/python/manage_new_object.hpp

クラス

manage_new_object クラス

struct manage_new_object

copy_non_const_reference は、new 式で確保したオブジェクトへのポインタを返し、呼び出し側がそのオブジェクトを削除する責任をもつことを想定する C++ 関数をラップするのに使用する ResultConverterGenerator のモデルである。

manage_new_object クラスの概要

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

manage_new_object クラスのメタ関数

template<class T>
struct apply
要件

ある U に対して TU*

typedef to_python_indirect<T> type

C++ 側
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/manage_new_object.hpp>
#include <boost/python/return_value_policy.hpp>


struct Foo {
   Foo(int x) : x(x){}
   int get_x() { return x; }
   int x;
};

Foo* make_foo(int x) { return new Foo(x); }

// ラッパコード
using namespace boost::python;
BOOST_PYTHON_MODULE(my_module)
{
    def("make_foo", make_foo, return_value_policy<manage_new_object>())
    class_<Foo>("Foo")
        .def("get_x", &Foo::get_x)
        ;
}
Python 側
>>> from my_module import *
>>> f = make_foo(3)     # Foo オブジェクトを作成
>>> f.get_x()
3