私は見つけられたすべての関連する質問を見てきましたが、この特定の状況に答えることができませんでした。boost :: python静的なfactoryコンストラクタとstd :: unique_ptrを持つ純粋な仮想基底クラス
私はPythonに公開したいC++の純粋仮想基本クラスインタフェースを持っています。実装は、基地から継承し、露出していない:
struct Base : private boost::noncopyable
{
static std::unique_ptr<Base> create();
virtual ~Base();
virtual int get_int() = 0;
virtual void set_int(const int i) = 0;
};
私だけcreate()
ファクトリ関数を通じて新しいベースインスタンスを構築することができるように、ベースをサブクラス化するのPythonを必要としません。
boost :: pythonで2つの方法でラップしました。
namespace bp = boost::python;
bp::class_<Base, boost::noncopyable>("Base", bp::no_init)
.def("__init__",
bp::make_function([](Base& self) { return Master::create().release(); },
bp::return_value_policy<bp::manage_new_object>(),
boost::mpl::vector<Base*, Base&>(), "Create a new Base")
.staticmethod("__init__")
これは、コンパイルして、Pythonでの負荷が、__init__
は実際に静的ではありません。まず、Boost.Python: How to expose std::unique_ptrに似て私はunique_ptrをを解放__init__
関数を作成してみました!
create(...)
create((Base)arg1) -> Base :
Create a new Base
C++ signature :
Base* create(Base {lvalue})
私が試した第二のアプローチは、次のようにmake_constructor
を使用することです:
namespace bp = boost::python;
bp::class_<Base, boost::noncopyable, std::unique_ptr<Base>>("Base", bp::no_init)
.def("__init__", bp::make_constructor(&Base::create));
bp::register_ptr_to_python<std::unique_ptr<Master>>();
しかし、これはコンパイルされません: /usr/include/boost/python/make_constructor.hpp:40:20: fatal error: call to deleted constructor of std::unique_ptr<Base, std::default_delete<Base>> dispatch(x, is_pointer<T>());