*** empty log message ***
[luabind.git] / luabind / detail / object_rep.hpp
blob0dff97ca4a6deb43316f238cc4c8aedeaff6a945
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 // 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.
41 class object_rep
43 public:
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*))
48 : m_object(obj)
49 , m_classrep(crep)
50 , m_flags(flags)
51 , m_destructor(dest)
52 , m_dependency_cnt(1)
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)
59 : m_object(0)
60 , m_classrep(crep)
61 , m_flags(flags)
62 , m_lua_table_ref(table_ref)
63 , m_destructor(0)
64 , m_dependency_cnt(1)
68 ~object_rep()
70 // delete m_instance;
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");
92 m_flags &= ~owner;
95 void set_destructor(void(*ptr)(void*))
97 m_destructor = ptr;
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())
106 lua_newtable(L);
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);
113 lua_pop(L, 1);
114 ++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);
136 obj->~object_rep();
137 return 0;
140 private:
142 void* m_object; // pointer to the c++ object or holder / if lua class, this is a pointer the the instance of the
143 // c++ base or 0.
144 class_rep* m_classrep; // the class information about this object's type
145 int m_flags;
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;
155 template<class T>
156 struct delete_s
158 static void apply(void* ptr)
160 delete static_cast<T*>(ptr);
164 template<class T>
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));
177 if (!obj) return 0;
178 if (lua_getmetatable(L, index) == 0) return 0;
180 lua_pushstring(L, "__luabind_class");
181 lua_gettable(L, -2);
182 bool confirmation = lua_toboolean(L, -1) != 0;
183 lua_pop(L, 2);
184 if (!confirmation) return 0;
185 return obj;
191 #endif // LUABIND_OBJECT_REP_HPP_INCLUDED