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.
23 #include <luabind/detail/object_rep.hpp>
24 #include <luabind/detail/class_rep.hpp>
26 namespace luabind
{ namespace detail
29 // dest is a function that is called to delete the c++ object this struct holds
30 object_rep::object_rep(void* obj
, class_rep
* crep
, int flags
, void(*dest
)(void*))
37 // if the object is owned by lua, a valid destructor must be given
38 assert((((m_flags
& owner
) && dest
) || !(m_flags
& owner
)) && "internal error, please report");
41 object_rep::object_rep(class_rep
* crep
, int flags
, detail::lua_reference
const& table_ref
)
45 , m_lua_table_ref(table_ref
)
51 object_rep::~object_rep()
53 if (m_flags
& owner
&& m_destructor
) m_destructor(m_object
);
56 void object_rep::remove_ownership()
58 assert((m_flags
& owner
) && "cannot remove ownership of object that's not owned");
59 assert((m_classrep
->get_class_type() == class_rep::cpp_class
60 || m_classrep
->bases().size() == 1) && "can only adopt c++ types or lua classes that derives from a c++ class");
62 // daniel040727 Bogus assert above? C++ types can be adopted just fine
63 // without a hierarchy?
66 // if this is a type with a wrapper we also have to
67 // transform the wrappers weak_ref into a strong
68 // reference, to make sure the lua part
69 // stays alive as long as the c++ part stays
71 /* if (m_classrep->get_class_type() == class_rep::cpp_class)
72 m_classrep->adopt(m_flags & constant, m_object);
75 class_rep
* cpp_base
= m_classrep
;
76 while (cpp_base
->get_class_type() == class_rep::lua_class
)
77 cpp_base
= cpp_base
->bases().front().base
;
78 cpp_base
->adopt(m_flags
& constant
, m_object
);
81 void object_rep::set_destructor(void(*ptr
)(void*))
86 void object_rep::add_dependency(lua_State
* L
, int index
)
88 if (!m_dependency_ref
.is_valid())
91 m_dependency_ref
.set(L
);
94 m_dependency_ref
.get(L
);
95 lua_pushvalue(L
, index
);
96 lua_rawseti(L
, -2, m_dependency_cnt
);
101 int object_rep::garbage_collector(lua_State
* L
)
103 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, -1));
105 finalize(L
, obj
->crep());