*** empty log message ***
[luabind.git] / luabind / detail / object_rep.hpp
blobcb079ff0b78bc8d259de2565e2b92057ee690785
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 object_rep(class_rep* crep, int flags, detail::lua_reference const& table_ref);
47 ~object_rep();
49 void* ptr() const { return m_object; }
51 void* ptr(int pointer_offset) const
53 return reinterpret_cast<char*>(m_object) + pointer_offset;
56 const class_rep* crep() const { return m_classrep; }
57 class_rep* crep() { return m_classrep; }
58 int flags() const { return m_flags; }
59 void set_flags(int flags) { m_flags = flags; }
61 void get_lua_table(lua_State* L) const { m_lua_table_ref.get(L); }
63 void remove_ownership();
64 void set_destructor(void(*ptr)(void*));
66 void set_object(void* p) { m_object = p; }
68 void add_dependency(lua_State* L, int index);
69 void release_refs(lua_State* L);
71 static int garbage_collector(lua_State* L);
73 private:
75 void* m_object; // pointer to the c++ object or holder / if lua class, this is a pointer the the instance of the
76 // c++ base or 0.
77 class_rep* m_classrep; // the class information about this object's type
78 int m_flags;
79 detail::lua_reference m_lua_table_ref; // reference to lua table if this is a lua class
80 void(*m_destructor)(void*); // this could be in class_rep? it can't: see intrusive_ptr
81 int m_dependency_cnt; // counts dependencies
82 detail::lua_reference m_dependency_ref; // reference to lua table holding dependency references
84 // ======== the new way, separate object_rep from the holder
85 // instance_holder* m_instance;
88 template<class T>
89 struct delete_s
91 static void apply(void* ptr)
93 delete static_cast<T*>(ptr);
97 template<class T>
98 struct destruct_only_s
100 static void apply(void* ptr)
102 #ifndef NDEBUG
103 int completeness_check[sizeof(T)];
104 (void)completeness_check;
105 #endif
106 static_cast<T*>(ptr)->~T();
111 inline object_rep* is_class_object(lua_State* L, int index)
113 object_rep* obj = static_cast<detail::object_rep*>(lua_touserdata(L, index));
114 if (!obj) return 0;
115 if (lua_getmetatable(L, index) == 0) return 0;
117 lua_pushstring(L, "__luabind_class");
118 lua_gettable(L, -2);
119 bool confirmation = lua_toboolean(L, -1) != 0;
120 lua_pop(L, 2);
121 if (!confirmation) return 0;
122 return obj;
128 #endif // LUABIND_OBJECT_REP_HPP_INCLUDED