Remove instance_holder dependency on class_rep.
[luabind.git] / luabind / detail / object_rep.hpp
blob3f688107fc10b6b2c9d780f2cadba8670c4ff55b
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 <boost/aligned_storage.hpp>
28 #include <luabind/config.hpp>
29 #include <luabind/detail/class_rep.hpp>
30 #include <luabind/detail/instance_holder.hpp>
31 #include <luabind/detail/ref.hpp>
33 namespace luabind { namespace detail
35 void finalize(lua_State* L, class_rep* crep);
37 // this class is allocated inside lua for each pointer.
38 // it contains the actual c++ object-pointer.
39 // it also tells if it is const or not.
40 class LUABIND_API object_rep
42 public:
43 object_rep(instance_holder* instance, class_rep* crep);
44 ~object_rep();
46 const class_rep* crep() const { return m_classrep; }
47 class_rep* crep() { return m_classrep; }
49 void set_instance(instance_holder* instance) { m_instance = instance; }
51 void add_dependency(lua_State* L, int index);
52 void release_dependency_refs(lua_State* L);
54 std::pair<void*, int> get_instance(class_id target) const
56 if (m_instance == 0)
57 return std::pair<void*, int>(0, -1);
58 return m_instance->get(m_classrep->casts(), target);
61 bool is_const() const
63 return m_instance && m_instance->pointee_const();
66 void release()
68 if (m_instance)
69 m_instance->release();
72 void* allocate(std::size_t size)
74 if (size <= 32)
75 return &m_instance_buffer;
76 return std::malloc(size);
79 void deallocate(void* storage)
81 if (storage == &m_instance_buffer)
82 return;
83 std::free(storage);
86 private:
88 object_rep(object_rep const&)
91 void operator=(object_rep const&)
94 instance_holder* m_instance;
95 boost::aligned_storage<32> m_instance_buffer;
96 class_rep* m_classrep; // the class information about this object's type
97 std::size_t m_dependency_cnt; // counts dependencies
100 template<class T>
101 struct delete_s
103 static void apply(void* ptr)
105 delete static_cast<T*>(ptr);
109 template<class T>
110 struct destruct_only_s
112 static void apply(void* ptr)
114 // Removes unreferenced formal parameter warning on VC7.
115 (void)ptr;
116 #ifndef NDEBUG
117 int completeness_check[sizeof(T)];
118 (void)completeness_check;
119 #endif
120 static_cast<T*>(ptr)->~T();
125 inline object_rep* is_class_object(lua_State* L, int index)
127 object_rep* obj = static_cast<detail::object_rep*>(lua_touserdata(L, index));
128 if (!obj) return 0;
129 if (lua_getmetatable(L, index) == 0) return 0;
131 lua_pushstring(L, "__luabind_class");
132 lua_gettable(L, -2);
133 bool confirmation = lua_toboolean(L, -1) != 0;
134 lua_pop(L, 2);
135 if (!confirmation) return 0;
136 return obj;
140 LUABIND_API object_rep* get_instance(lua_State* L, int index);
141 LUABIND_API void push_instance_metatable(lua_State* L);
142 LUABIND_API object_rep* push_new_instance(lua_State* L, class_rep* cls);
146 #endif // LUABIND_OBJECT_REP_HPP_INCLUDED