Teach adopt() to hold the adopted pointer in custom pointer type.
[luabind.git] / test / test_smart_ptr_attributes.cpp
blob0392fb43f648308be92641c00f8fac6c6a627867
1 // Copyright Daniel Wallin 2009. Use, modification and distribution is
2 // subject to the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #include "test.hpp"
6 #include <luabind/luabind.hpp>
7 #include <boost/shared_ptr.hpp>
8 #include <luabind/shared_ptr_converter.hpp>
9 #include <luabind/class_info.hpp>
11 struct Foo
13 Foo() : m_baz(0) {}
14 int m_baz;
17 struct Bar
19 boost::shared_ptr<Foo> getFoo() const { return m_foo; }
20 void setFoo( boost::shared_ptr<Foo> foo ) { m_foo = foo; }
22 boost::shared_ptr<Foo> m_foo;
25 void test_main(lua_State* L)
27 using namespace luabind;
29 bind_class_info(L);
31 module( L )
33 class_<Foo, boost::shared_ptr<Foo> >( "Foo" )
34 .def( constructor<>() )
35 .def_readwrite("baz", &Foo::m_baz),
36 class_<Bar, boost::shared_ptr<Bar> >( "Bar" )
37 .def( constructor<>() )
38 .property("fooz", &Bar::getFoo, &Bar::setFoo)
39 .def_readwrite("foo", &Bar::m_foo)
41 dostring( L, "foo = Foo();");
42 dostring( L, "foo.baz = 42;");
43 dostring( L, "x = Bar();");
44 dostring( L, "x.fooz = foo;");
45 dostring( L, "print(type(x), class_info(x).name);");
46 dostring( L, "print(type(x.fooz), class_info(x.fooz).name);");
47 dostring( L, "print(type(x.foo), class_info(x.foo).name);"); // crashes here);