test_smart_ptr_attributes.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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)
  4. #include "test.hpp"
  5. #include <luabind/luabind.hpp>
  6. #include <boost/shared_ptr.hpp>
  7. #include <luabind/shared_ptr_converter.hpp>
  8. #include <luabind/class_info.hpp>
  9. struct Foo
  10. {
  11. Foo() : m_baz(0) {}
  12. int m_baz;
  13. };
  14. struct Bar
  15. {
  16. boost::shared_ptr<Foo> getFoo() const { return m_foo; }
  17. void setFoo( boost::shared_ptr<Foo> foo ) { m_foo = foo; }
  18. boost::shared_ptr<Foo> m_foo;
  19. };
  20. void test_main(lua_State* L)
  21. {
  22. using namespace luabind;
  23. bind_class_info(L);
  24. module( L )
  25. [
  26. class_<Foo, boost::shared_ptr<Foo> >( "Foo" )
  27. .def( constructor<>() )
  28. .def_readwrite("baz", &Foo::m_baz),
  29. class_<Bar, boost::shared_ptr<Bar> >( "Bar" )
  30. .def( constructor<>() )
  31. .property("fooz", &Bar::getFoo, &Bar::setFoo)
  32. .def_readwrite("foo", &Bar::m_foo)
  33. ];
  34. dostring( L, "foo = Foo();");
  35. dostring( L, "foo.baz = 42;");
  36. dostring( L, "x = Bar();");
  37. dostring( L, "x.fooz = foo;");
  38. dostring( L, "print(type(x), class_info(x).name);");
  39. dostring( L, "print(type(x.fooz), class_info(x.fooz).name);");
  40. dostring( L, "print(type(x.foo), class_info(x.foo).name);"); // crashes here);
  41. }