luabindでboost::shared_ptrを使う

少し考えさせられたのでメモ。

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

int main()
{
	std::shared_ptr<lua_State> l( lua_open(), []( lua_State* p ) { lua_close( p ); } );
	luaL_openlibs( l.get() );
	luaopen_base( l.get() );
	luabind::open( l.get() );

	luabind::module( l.get() )
	[
		luabind::class_<Hoge, boost::shared_ptr<Hoge>>( "Hoge" )
		.def( "show", &Hoge::show )
	];
	boost::shared_ptr<Hoge> p( new Hoge() );
	luaL_dofile( l.get(), "test.lua" ); // エラー出た時の対処とかは略してます
	luabind::call_function<void>( l.get(), "exec", p );
}
-- test.lua
exec = function( hoge_ptr )
	hoge_ptr:show();
end

実行結果

Hello World

これで動いたことは動いたんですが、TR1のほうのshared_ptrだとどう書くかわからなかったので、分かり次第追記します。

【追記】std::shared_ptrの場合。

template<class T>
T* get_pointer(std::shared_ptr<T> const& p) { return p.get(); }

を追加すれば、動作しました。とある方に教えていただきました。
一応、資料をここに示しておきます。

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のソースを自分で編集すればできるそうですが、怖くてできないのでやめましたorz