boost/python/init.hpp
はじめに
<boost/python/init.hpp> は、C++ コンストラクタを Python へ拡張クラスの __init__ 関数としてエクスポートするインターフェイスを定義する。
init-expression
init-expression ::= `see-other-document`
init-expression は、拡張クラスについて生成される __init__ メソッド群を記述するのに使用する。結果は以下のプロパティを持つ。
- docstring
モジュールの
__doc__属性に束縛する値を持つ ntbs。- keywords
生成される
__init__関数の引数(の残りの部分列)に名前をつけるのに使用するkeyword-expression。- call policies
CallPolicies モデルのインスタンス。
- argument types
ラップした C++ オブジェクトを構築するのに使用する C++ 引数型の MPL 列。
init-expressionは、引数型の接頭辞列が与える合法な接頭辞を 1 つ以上持つ。
クラス
init<T1 = unspecified, T2 = unspecified, ...Tn = unspecified> クラステンプレート
-
template<class ...Args>
struct init 1 つ以上の
__init__関数群を指定するのに使用する MPL 列。末尾の Ti のみoptional<>のインスタンスであってもよい。
init クラステンプレートの概要
namespace boost { namespace python
{
template <T1 = unspecified,...Tn = unspecified>
struct init
{
init(char const* doc = 0);
template <class Keywords> init(Keywords const& kw, char const* doc = 0);
template <class Keywords> init(char const* doc, Keywords const& kw);
template <class CallPolicies>
unspecified operator[](CallPolicies const& policies) const
};
}}
init クラステンプレートのコンストラクタ
-
init(char const *doc = 0)
-
template<class Keywords>
init(Keywords const &kw, char const *doc = 0) -
template<class Keywords>
init(char const *doc, Keywords const &kw) - 要件
docは与えられた場合 ntbs。kwは与えられた場合keyword-expressionの結果でなければならない。- 効果
結果は、docstring が
doc、keywords がkwへの参照であるinit-expressionである。第 1 形式を使用した場合、結果の式の keywords は空。式の call policies はdefault_call_policiesのインスタンス。Tnがoptional<U1, U2, ...Um>である場合、式の合法な接頭辞群は次のとおり与えられる。(T1, T2,...Tn-1), (T1, T2,...T_n-1 , U1), (T1, T2,...Tn-1 , U1, U2), ...(T1, T2,...Tn-1 , U1, U2,...Um)
それ以外の場合、式の合法な接頭辞はユーザが指定したテンプレート引数で与えたものとなる。
init クラスのオブザーバ関数
-
template<class Policies>
unspecified operator[](Policies const &policies) const - 要件
Policiesは CallPolicies のモデル。- 効果
initオブジェクトとすべて同じプロパティを持ち、call policies のみpoliciesへの参照である新しいinit-expressionを返す。
optional<T1 = unspecified, T2 = unspecified, ...Tn = unspecified> クラステンプレート
optioanal クラステンプレートの概要
namespace boost { namespace python
{
template <T1 = unspecified,...Tn = unspecified>
struct optional {};
}}
例
次の C++ 宣言があるとすると、
class Y;
class X
{
public:
X(int x, Y* y) : m_y(y) {}
X(double);
private:
Y* m_y;
};
以下のように対応する Boost.Python 拡張クラスを作成できる。
using namespace boost::python;
class_<X>("X", "X のドキュメンテーション文字列。",
init<int,char const*>(args("x","y"), "X.__init__ のドキュメンテーション文字列")[
with_custodian_and_ward<1,3>()]
)
.def(init<double>())
;