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 void finalize(lua_State
* L
, class_rep
* crep
);
36 // TODO: move code into separate compilation unit
38 // this class is allocated inside lua for each pointer.
39 // it contains the actual c++ object-pointer.
40 // it also tells if it is const or not.
44 enum { constant
= 1, owner
= 2, smart_pointer
= 4, lua_class
= 8, call_super
= 16 };
46 // dest is a function that is called to delete the c++ object this struct holds
47 object_rep(void* obj
, class_rep
* crep
, int flags
, void(*dest
)(void*))
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");
58 object_rep(class_rep
* crep
, int flags
, detail::lua_reference
const& table_ref
)
62 , m_lua_table_ref(table_ref
)
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 void get_lua_table(lua_State
* L
) const { return m_lua_table_ref
.get(L
); }
89 void remove_ownership()
91 assert((m_flags
& owner
) && "cannot remove ownership of object that's not owned");
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
.is_valid())
107 m_dependency_ref
.set(L
);
110 m_dependency_ref
.get(L
);
111 lua_pushvalue(L
, index
);
112 lua_rawseti(L
, -2, m_dependency_cnt
);
117 void release_refs(lua_State
* L
)
119 m_dependency_ref
.reset();
120 m_lua_table_ref
.reset();
123 // instance_holder* instance() const
124 // { return m_instance; }
126 // void set_instance(instance_holder* x)
127 // { m_instance = x; }
129 static int garbage_collector(lua_State
* L
)
131 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, -1));
133 finalize(L
, obj
->crep());
135 obj
->release_refs(L
);
142 void* m_object
; // pointer to the c++ object or holder / if lua class, this is a pointer the the instance of the
144 class_rep
* m_classrep
; // the class information about this object's type
146 detail::lua_reference m_lua_table_ref
; // reference to lua table if this is a lua class
147 void(*m_destructor
)(void*); // this could be in class_rep? it can't: see intrusive_ptr
148 int m_dependency_cnt
; // counts dependencies
149 detail::lua_reference m_dependency_ref
; // reference to lua table holding dependency references
151 // ======== the new way, separate object_rep from the holder
152 // instance_holder* m_instance;
158 static void apply(void* ptr
)
160 delete static_cast<T
*>(ptr
);
165 struct destruct_only_s
167 static void apply(void* ptr
)
169 static_cast<T
*>(ptr
)->~T();
174 inline object_rep
* is_class_object(lua_State
* L
, int index
)
176 object_rep
* obj
= static_cast<detail::object_rep
*>(lua_touserdata(L
, index
));
178 if (lua_getmetatable(L
, index
) == 0) return 0;
180 lua_pushstring(L
, "__luabind_class");
182 bool confirmation
= lua_toboolean(L
, -1) != 0;
184 if (!confirmation
) return 0;
191 #endif // LUABIND_OBJECT_REP_HPP_INCLUDED