正しい方法でやりたいです。ここで boost::serialization::singleton を公開しているのを見てきました Boost python export singletonですが、それを使用したくありません。代わりに単純なマイヤーズ シングルトンを使用したい。
以下のコードは機能し ますが、 http://www.boost.org/doc/libs/1_43_0/libs/python/doc/v2/reference_existing_object.html#reference_existing_object-spec/の使用 は危険であるとドキュメントに記載されています。
コード:
class Singleton
{
private:
Singleton(){};
public:
static Singleton & getInstance()
{
static Singleton instance;
return instance;
}
int getNumber() { return 5; }
};
そしてモジュールで:
class_<Singleton>("singleton", no_init)
.def("getInstance", &Singleton::getInstance, return_value_policy<reference_existing_object>()).staticmethod("getInstance")
.def("getNumber", &Singleton::getNumber)
;
それを行う良い方法は何ですか?を使用するreturn_internal_reference<>()
と、Python コードの実行中にエラーが発生しました。