luabindでstd::shared_ptrを使いたいのですが

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
Suikaba
記事: 194
登録日時: 13年前

luabindでstd::shared_ptrを使いたいのですが

#1

投稿記事 by Suikaba » 12年前

毎度お世話になります。とりあえずコードを見ていただけますでしょうか。

コード:

class Hoge
{
public:
	void show()
	{
		std::cout << "Hello World" << std::endl;
	}
};

int main()
{
	lua_State* l = lua_open();
	luaL_openlibs( l );
	luaopen_base( l );
	luabind::open( l );

	luabind::module( l )
	[
		luabind::class_<Hoge, boost::shared_ptr<Hoge>>( "Hoge" )
		.def( "show", &Hoge::show )
	];
	boost::shared_ptr<Hoge> p( new Hoge() );
	luaL_dofile( l, "test.lua" );
	luabind::call_function<void>( l, "exec", p );
	lua_close( l );
}

コード:

-- test.lua
exec = function( hoge_ptr )
	hoge_ptr:show();
end
ここではboost::shared_ptrを使っていますが、今までTR1のshared_ptrを使っていましたので、
それを使いたいと思うのですが、単純に

コード:

class Hoge
{
public:
	void show()
	{
		std::cout << "Hello World" << std::endl;
	}
};

int main()
{
	lua_State* l = lua_open();
	luaL_openlibs( l );
	luaopen_base( l );
	luabind::open( l );

	luabind::module( l )
	[
		luabind::class_<Hoge, std::shared_ptr<Hoge>>( "Hoge" )
		.def( "show", &Hoge::show )
	];
	std::shared_ptr<Hoge> p( new Hoge() );
	luaL_dofile( l, "test.lua" );
	luabind::call_function<void>( l, "exec", p );
	lua_close( l );
}
としても(当たり前ですが)落ちてしまいます。

コード:

namespace luabind
{
	template<class T>
	T* get_pointer(std::shared_ptr<T> const& p) { return p.get(); }
}
を見かけたので、追加したりしましたが、ダメでした。
どなたかご存じの方がおられましたら、教えていただければと思います。
一応、boost::shared_ptrの方は動作を確認しました。

アバター
GRAM
記事: 164
登録日時: 13年前
住所: 大阪

Re: luabindでstd::shared_ptrを使いたいのですが

#2

投稿記事 by GRAM » 12年前

8.7 Smart pointers 「スマートポインタ」
get_pointer() overloads are provided for the smart pointers in Boost, and std::auto_ptr<>. Should you need to provide your own overload, note that it is called unqualified and is expected to be found by argument dependent lookup. Thus it should be defined in the same namespace as the pointer type it operates on.
とあります。
luabind空間ではだめですね。std空間に定義すると未定義になりますが、テンプレート引数の中身までADLは侵入してくるはずなので、Tの中身がわかってるならその名前空間で定義すればよいのではないでしょうか?

※もしくはライブラリーのコードを変更してしまってluabindの名前空間のものを呼ぶようにするか・・・(どうせメタプログラミングのライブラリなので、hppの中身丸見えですし)

Suikaba
記事: 194
登録日時: 13年前

Re: luabindでstd::shared_ptrを使いたいのですが

#3

投稿記事 by Suikaba » 12年前

>>GRAMさん
なるほど。詳しい回答ありがとうございます。早速試してみます。

回答有り難うございました。解決とさせて頂きます。

【追記】
試したところ正常に動作しました。ほんとうに有り難うございます。

閉鎖

“C言語何でも質問掲示板” へ戻る