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
34 // this class is allocated inside lua for each pointer.
35 // it contains the actual c++ object-pointer.
36 // it also tells if it is const or not.
40 enum { constant
= 1, owner
= 2, smart_pointer
= 4, lua_class
= 8, call_super
= 16 };
42 // dest is a function that is called to delete the c++ object this struct holds
43 object_rep(void* obj
, class_rep
* crep
, int flags
, void(*dest
)(void*)):
47 m_lua_table_ref(LUA_NOREF
),
50 m_dependency_ref(LUA_NOREF
)
52 // if the object is owned by lua, a valid destructor must be given
53 assert(((m_flags
& owner
) && dest
) || !(m_flags
& owner
));
55 // an object can't be collected if it's const
56 assert(!((m_flags
& constant
) && (m_flags
& owner
)));
59 object_rep(class_rep
* crep
, int flags
, int table_ref
)
63 , m_lua_table_ref(table_ref
)
66 , m_dependency_ref(LUA_NOREF
)
72 if (m_flags
& owner
&& m_destructor
) m_destructor(m_object
);
75 void* ptr() const throw() { return m_object
; }
77 void* ptr(int pointer_offset
) const throw()
79 return reinterpret_cast<char*>(m_object
) + pointer_offset
;
82 const class_rep
* crep() const throw() { return m_classrep
; }
83 class_rep
* crep() throw() { return m_classrep
; }
84 int flags() const throw() { return m_flags
; }
85 void set_flags(int flags
) { m_flags
= flags
; }
87 int lua_table_ref() const { return m_lua_table_ref
; }
89 void remove_ownership()
91 assert(m_flags
& owner
);
95 void set_destructor(void(*ptr
)(void*))
100 void set_object(void* p
) { m_object
= p
; }
102 void add_dependency(lua_State
* L
, int index
)
104 if (m_dependency_ref
== LUA_NOREF
)
107 m_dependency_ref
= detail::ref(L
);
110 detail::getref(L
, m_dependency_ref
);
111 lua_pushvalue(L
, index
);
112 lua_rawseti(L
, -2, m_dependency_cnt
);
117 void release_refs(lua_State
* L
)
119 if (m_dependency_ref
!= LUA_NOREF
) detail::unref(L
, m_dependency_ref
);
120 if (m_lua_table_ref
!= LUA_NOREF
) detail::unref(L
, m_lua_table_ref
); // correct?
123 static int garbage_collector(lua_State
* L
)
125 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, -1));
126 obj
->release_refs(L
);
133 void* m_object
; // the c++ object pointer / if lua class, this is a pointer the the instance of the
135 class_rep
* m_classrep
; // the class information about this object's type
137 int m_lua_table_ref
; // reference to lua table if this is a lua class
138 void(*m_destructor
)(void*); // this could be in class_rep?
139 int m_dependency_cnt
; // counts dependencies
140 int m_dependency_ref
; // reference to lua table holding dependency references
144 void destructor(void* ptr
)
146 delete static_cast<T
*>(ptr
);
153 static void apply(void* ptr
)
155 delete static_cast<T
*>(ptr
);
159 inline object_rep
* is_class_object(lua_State
* L
, int index
)
161 object_rep
* obj
= static_cast<detail::object_rep
*>(lua_touserdata(L
, index
));
163 if (lua_getmetatable(L
, index
) == 0) return 0;
165 lua_pushstring(L
, "__luabind_class");
167 bool confirmation
= lua_toboolean(L
, -1) != 0;
169 if (!confirmation
) return 0;
176 #endif // LUABIND_OBJECT_REP_HPP_INCLUDED