added finalizers
[luabind.git] / luabind / detail / object_rep.hpp
blobad397df6ff0eab8d6b4bb5380144095f9e3e8673
1 // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
24 #ifndef LUABIND_OBJECT_REP_HPP_INCLUDED
25 #define LUABIND_OBJECT_REP_HPP_INCLUDED
27 #include <luabind/config.hpp>
28 #include <luabind/detail/ref.hpp>
30 namespace luabind { namespace detail
32 class class_rep;
34 void finalize(lua_State* L, class_rep* crep);
36 // this class is allocated inside lua for each pointer.
37 // it contains the actual c++ object-pointer.
38 // it also tells if it is const or not.
39 class object_rep
41 public:
42 enum { constant = 1, owner = 2, smart_pointer = 4, lua_class = 8, call_super = 16 };
44 // dest is a function that is called to delete the c++ object this struct holds
45 object_rep(void* obj, class_rep* crep, int flags, void(*dest)(void*)):
46 m_object(obj),
47 m_classrep(crep),
48 m_flags(flags),
49 m_lua_table_ref(LUA_NOREF),
50 m_destructor(dest),
51 m_dependency_cnt(1),
52 m_dependency_ref(LUA_NOREF)
54 // if the object is owned by lua, a valid destructor must be given
55 assert((((m_flags & owner) && dest) || !(m_flags & owner)) && "internal error, please report");
57 // an object can't be collected if it's const
58 assert((!((m_flags & constant) && (m_flags & owner))) && "internal error, please report");
61 object_rep(class_rep* crep, int flags, int table_ref)
62 : m_object(0)
63 , m_classrep(crep)
64 , m_flags(flags)
65 , m_lua_table_ref(table_ref)
66 , m_destructor(0)
67 , m_dependency_cnt(1)
68 , m_dependency_ref(LUA_NOREF)
72 ~object_rep()
74 if (m_flags & owner && m_destructor) m_destructor(m_object);
77 void* ptr() const throw() { return m_object; }
79 void* ptr(int pointer_offset) const throw()
81 return reinterpret_cast<char*>(m_object) + pointer_offset;
84 const class_rep* crep() const throw() { return m_classrep; }
85 class_rep* crep() throw() { return m_classrep; }
86 int flags() const throw() { return m_flags; }
87 void set_flags(int flags) { m_flags = flags; }
89 int lua_table_ref() const { return m_lua_table_ref; }
91 void remove_ownership()
93 assert((m_flags & owner) && "cannot remove ownership of object that's not owned");
94 m_flags &= ~owner;
97 void set_destructor(void(*ptr)(void*))
99 m_destructor = ptr;
102 void set_object(void* p) { m_object = p; }
104 void add_dependency(lua_State* L, int index)
106 if (m_dependency_ref == LUA_NOREF)
108 lua_newtable(L);
109 m_dependency_ref = detail::ref(L);
112 detail::getref(L, m_dependency_ref);
113 lua_pushvalue(L, index);
114 lua_rawseti(L, -2, m_dependency_cnt);
115 lua_pop(L, 1);
116 ++m_dependency_cnt;
119 void release_refs(lua_State* L)
121 if (m_dependency_ref != LUA_NOREF) detail::unref(L, m_dependency_ref);
122 if (m_lua_table_ref != LUA_NOREF) detail::unref(L, m_lua_table_ref); // correct?
125 static int garbage_collector(lua_State* L)
127 object_rep* obj = static_cast<object_rep*>(lua_touserdata(L, -1));
129 finalize(L, obj->crep());
131 obj->release_refs(L);
132 obj->~object_rep();
133 return 0;
136 private:
138 void* m_object; // the c++ object pointer / if lua class, this is a pointer the the instance of the
139 // c++ base or 0.
140 class_rep* m_classrep; // the class information about this object's type
141 int m_flags;
142 int m_lua_table_ref; // reference to lua table if this is a lua class
143 void(*m_destructor)(void*); // this could be in class_rep?
144 int m_dependency_cnt; // counts dependencies
145 int m_dependency_ref; // reference to lua table holding dependency references
148 template<class T>
149 void destructor(void* ptr)
151 delete static_cast<T*>(ptr);
155 template<class T>
156 struct destructor_s
158 static void apply(void* ptr)
160 delete static_cast<T*>(ptr);
164 inline object_rep* is_class_object(lua_State* L, int index)
166 object_rep* obj = static_cast<detail::object_rep*>(lua_touserdata(L, index));
167 if (!obj) return 0;
168 if (lua_getmetatable(L, index) == 0) return 0;
170 lua_pushstring(L, "__luabind_class");
171 lua_gettable(L, -2);
172 bool confirmation = lua_toboolean(L, -1) != 0;
173 lua_pop(L, 2);
174 if (!confirmation) return 0;
175 return obj;
181 #endif // LUABIND_OBJECT_REP_HPP_INCLUDED