boost/python/to_python_indirect.hpp
はじめに
<boost/python/to_python_indirect.hpp>
は、ラップした C++ クラスインスタンスをポインタかスマートポインタで保持する新しい Python オブジェクトを構築する手段を提供する。
クラス
to_python_indirect
クラステンプレート
-
template<class T, class MakeHolder>
struct to_python_indirect to_python_indirect
クラステンプレートは第 1 引数型のオブジェクトを拡張クラスのインスタンスとして Python に変換する。第 2 引数で与えた所有権ポリシーを使用する。以下において
x
は型T
のオブジェクト、h
はboost::python::objects::instance_holder*
型のオブジェクト、p
は型U*
のオブジェクトである。- テンプレートパラメータ
T --
class_
クラステンプレートで Python へエクスポートする C++ クラスを参照剥がしする型。- 要件
U cv&
(cv
は省略可能な CV 指定子)か、*x がU const&
に変換可能な Dereferenceable 型のいずれか(U
はクラス型)。
MakeHolder --
静的関数
execute
がinstance_holder
を作成するクラス。- 要件
h = MakeHolder::execute(p)
to_python_indirect
のインスタンスは ResultConverter のモデルである。
to_python_indirect
クラステンプレートの概要
namespace boost { namespace python
{
template <class T, class MakeHolder>
struct to_python_indirect
{
static bool convertible();
PyObject* operator()(T ptr_or_reference) const;
private:
static PyTypeObject* type();
};
}}
to_python_indirect
クラステンプレートのオブザーバ関数
-
PyObject *operator()(T x) const
- 要件
x
はオブジェクトへの参照(ポインタ型の場合、非 null)。かつ convertible() == true。- 効果
適切に型付けされた Boost.Python 拡張クラスのインスタンスを作成し、
MakeHolder
を使用してx
からinstance_holder
を作成する。次に新しい拡張クラスインスタンス内にinstance_holder
をインストールし、最後にそれへのポインタを返す。
to_python_indirect
クラステンプレートの静的関数
-
bool convertible()
- 効果
いずれかのモジュールが
U
に対応する Python 型を登録していればtrue
。
例
reference_existing_object
の機能をコンパイル時のエラーチェックを省いて模造した例。
struct make_reference_holder
{
typedef boost::python::objects::instance_holder* result_type;
template <class T>
static result_type execute(T* p)
{
return new boost::python::objects::pointer_holder<T*, T>(p);
}
};
struct reference_existing_object
{
// ResultConverter を返すメタ関数
template <class T>
struct apply
{
typedef boost::python::to_python_indirect<T,make_reference_holder> type;
};
};