Suppress a bunch of "unreferenced parameter" warnings.
[luabind.git] / examples / intrusive_ptr / intrusive_ptr.cpp
blobd477948a7dc059d3b4fe9a117cbaf439bfc00ab6
1 #include <iostream>
3 extern "C"
5 #include "lua.h"
6 #include "lauxlib.h"
7 #include "lualib.h"
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";
16 lua_pop(L, 1);
17 return true;
19 return false;
22 #include <luabind/luabind.hpp>
23 #include <boost/intrusive_ptr.hpp>
25 namespace luabind
27 namespace converters
29 // tell luabind that there is a converter for boost::intrusive_ptr<T>
30 template<class T>
31 yes_t is_user_defined(by_value<boost::intrusive_ptr<T> >);
33 template<class T>
34 yes_t is_user_defined(by_const_reference<boost::intrusive_ptr<T> >);
36 // function used to destruct the object in lua
37 template<class T>
38 struct decrement_ref_count
40 static void apply(void* ptr)
42 T* p = static_cast<T*>(ptr);
43 intrusive_ptr_release(p);
47 template<class T>
48 void convert_cpp_to_lua(lua_State* L, const boost::intrusive_ptr<T>& ptr)
50 if (!ptr)
52 lua_pushnil(L);
53 return;
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);
70 // set the meta table
71 detail::getref(L, crep->metatable_ref());
72 lua_setmetatable(L, -2);
75 template<class T>
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);
84 template<class T>
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);
90 template<class T>
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 return converter_t::match(L, LUABIND_DECORATE_TYPE(T*), index);
97 template<class T>
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 return converter_t::match(L, LUABIND_DECORATE_TYPE(T*), index);
107 struct A
110 : cnt(0)
113 ~A() { std::cout << "free memory\n"; }
115 int cnt;
118 void intrusive_ptr_add_ref(A* ptr)
120 ++ptr->cnt;
121 std::cout << "add ref\n";
124 void intrusive_ptr_release(A* ptr)
126 --ptr->cnt;
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());
141 int main()
143 lua_State* L = lua_open();
144 lua_baselibopen(L);
146 luabind::open(L);
148 using namespace luabind;
150 module(L)
152 class_<A>("A")
153 .def_readonly("cnt", &A::cnt),
155 def("factory", &factory),
156 def("f", &f)
159 dostring(L, "a = factory()");
160 dostring(L, "print('lua count: ' .. a.cnt)");
161 dostring(L, "f(a)");
163 lua_close(L);
164 return 0;