10 bool dostring(lua_State
* L
, const char* str
)
12 if (luaL_loadbuffer(L
, str
, std::strlen(str
), str
) || lua_pcall(L
, 0, 0, 0))
14 const char* a
= lua_tostring(L
, -1);
15 std::cout
<< a
<< "\n";
22 #include <luabind/luabind.hpp>
23 #include <boost/intrusive_ptr.hpp>
29 // tell luabind that there is a converter for boost::intrusive_ptr<T>
31 yes_t
is_user_defined(by_value
<boost::intrusive_ptr
<T
> >);
34 yes_t
is_user_defined(by_const_reference
<boost::intrusive_ptr
<T
> >);
36 // function used to destruct the object in lua
38 struct decrement_ref_count
40 static void apply(void* ptr
)
42 T
* p
= static_cast<T
*>(ptr
);
43 intrusive_ptr_release(p
);
48 void convert_cpp_to_lua(lua_State
* L
, const boost::intrusive_ptr
<T
>& ptr
)
56 T
* raw_ptr
= ptr
.get();
57 // add reference to object, this will be released when the object is collected
58 intrusive_ptr_add_ref(raw_ptr
);
60 detail::class_registry
* registry
= luabind::detail::class_registry::get_registry(L
);
61 detail::class_rep
* crep
= registry
->find_class(LUABIND_TYPEID(T
));
63 assert(crep
!= 0 && "You are trying to convert an unregistered type");
65 // create the struct to hold the object
66 void* obj
= lua_newuserdata(L
, sizeof(detail::object_rep
));
67 // we send 0 as destructor since we know it will never be called
68 new(obj
) luabind::detail::object_rep(raw_ptr
, crep
, detail::object_rep::owner
, decrement_ref_count
<T
>::apply
);
71 detail::getref(L
, crep
->metatable_ref());
72 lua_setmetatable(L
, -2);
76 boost::intrusive_ptr
<T
>
77 convert_lua_to_cpp(lua_State
* L
, by_const_reference
<boost::intrusive_ptr
<T
> >, int index
)
79 typename
detail::default_policy::template generate_converter
<T
*, detail::lua_to_cpp
>::type converter
;
80 T
* ptr
= converter
.apply(L
, LUABIND_DECORATE_TYPE(T
*), index
);
81 return boost::intrusive_ptr
<T
>(ptr
);
85 boost::intrusive_ptr
<T
> convert_lua_to_cpp(lua_State
* L
, by_value
<boost::intrusive_ptr
<T
> >, int index
)
87 return convert_lua_to_cpp(L
, by_const_reference
<boost::intrusive_ptr
<T
> >(), index
);
91 int match_lua_to_cpp(lua_State
* L
, by_value
<boost::intrusive_ptr
<T
> >, int index
)
93 typedef typename
detail::default_policy::template generate_converter
<T
*, detail::lua_to_cpp
>::type converter_t
;
94 converter_t::match(L
, LUABIND_DECORATE_TYPE(T
*), index
);
98 int match_lua_to_cpp(lua_State
* L
, by_const_reference
<boost::intrusive_ptr
<T
> >, int index
)
100 typedef typename
detail::default_policy::template generate_converter
<T
*, detail::lua_to_cpp
>::type converter_t
;
101 converter_t::match(L
, LUABIND_DECORATE_TYPE(T
*), index
);
113 ~A() { std::cout
<< "free memory\n"; }
118 void intrusive_ptr_add_ref(A
* ptr
)
121 std::cout
<< "add ref\n";
124 void intrusive_ptr_release(A
* ptr
)
127 std::cout
<< "release\n";
128 if (ptr
->cnt
== 0) delete ptr
;
131 void f(boost::intrusive_ptr
<A
> ptr
)
133 std::cout
<< "count: " << ptr
->cnt
<< "\n";
136 boost::intrusive_ptr
<A
> factory()
138 return boost::intrusive_ptr
<A
>(new A());
143 lua_State
* L
= lua_open();
148 using namespace luabind
;
151 .def_readonly("cnt", &A::cnt
);
153 luabind::function(L
, "factory", &factory
);
154 luabind::function(L
, "f", &f
);
156 dostring(L
, "a = factory()");
157 dostring(L
, "print('lua count: ' .. a.cnt)");