Remove a lot of unused code.
[luabind.git] / luabind / detail / class_rep.hpp
blobd032fe8ca2dd056689aaaf371c0b781e20f4e5ca
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_CLASS_REP_HPP_INCLUDED
25 #define LUABIND_CLASS_REP_HPP_INCLUDED
27 #include <boost/limits.hpp>
28 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
30 #include <string>
31 #include <utility>
32 #include <vector>
34 #include <luabind/config.hpp>
35 #include <luabind/detail/object_rep.hpp>
36 #include <luabind/detail/garbage_collector.hpp>
37 #include <luabind/detail/operator_id.hpp>
38 #include <luabind/detail/class_registry.hpp>
39 #include <luabind/error.hpp>
40 #include <luabind/handle.hpp>
41 #include <luabind/detail/primitives.hpp>
43 namespace luabind
46 template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(LUABIND_MAX_BASES, class A, detail::null_type)>
47 struct bases {};
48 typedef bases<detail::null_type> no_bases;
51 namespace luabind { namespace detail
54 LUABIND_API std::string stack_content_by_name(lua_State* L, int start_index);
56 struct class_registration;
58 struct conversion_storage;
60 // This function is used as a tag to identify "properties".
61 LUABIND_API int property_tag(lua_State*);
63 // this is class-specific information, poor man's vtable
64 // this is allocated statically (removed by the compiler)
65 // a pointer to this structure is stored in the lua tables'
66 // metatable with the name __classrep
67 // it is used when matching parameters to function calls
68 // to determine possible implicit casts
69 // it is also used when finding the best match for overloaded
70 // methods
72 class LUABIND_API class_rep
74 friend struct class_registration;
75 friend int super_callback(lua_State*);
76 //TODO: avoid the lua-prefix
77 friend int lua_class_gettable(lua_State*);
78 friend int lua_class_settable(lua_State*);
79 friend int static_class_gettable(lua_State*);
80 public:
82 enum class_type
84 cpp_class = 0,
85 lua_class = 1
88 // destructor is a lua callback function that is hooked as garbage collector event on every instance
89 // of this class (including those that is not owned by lua). It gets an object_rep as argument
90 // on the lua stack. It should delete the object pointed to by object_rep::ptr if object_pre::flags
91 // is object_rep::owner (which means that lua owns the object)
93 // EXPECTS THE TOP VALUE ON THE LUA STACK TO
94 // BE THE USER DATA WHERE THIS CLASS IS BEING
95 // INSTANTIATED!
96 class_rep(LUABIND_TYPE_INFO type
97 , const char* name
98 , lua_State* L
99 , void(*destructor)(void*)
100 , void(*const_holder_destructor)(void*)
101 , LUABIND_TYPE_INFO holder_type
102 , LUABIND_TYPE_INFO const_holder_type
103 , void*(*extractor)(void*)
104 , const void*(*const_extractor)(void*)
105 , void(*const_converter)(void*,void*)
106 , void(*construct_holder)(void*,void*)
107 , void(*construct_const_holder)(void*,void*)
108 , void(*default_construct_holder)(void*)
109 , void(*default_construct_const_holder)(void*)
110 , void(*adopt_fun)(void*)
111 , int holder_size
112 , int holder_alignment);
114 // used when creating a lua class
115 // EXPECTS THE TOP VALUE ON THE LUA STACK TO
116 // BE THE USER DATA WHERE THIS CLASS IS BEING
117 // INSTANTIATED!
118 class_rep(lua_State* L, const char* name);
120 ~class_rep();
122 std::pair<void*,void*> allocate(lua_State* L) const;
124 // called from the metamethod for __index
125 // the object pointer is passed on the lua stack
126 int gettable(lua_State* L);
128 // called from the metamethod for __newindex
129 // the object pointer is passed on the lua stack
130 bool settable(lua_State* L);
132 // this is called as __index metamethod on every instance of this class
133 static int gettable_dispatcher(lua_State* L);
135 // this is called as __newindex metamethod on every instance of this class
136 static int settable_dispatcher(lua_State* L);
137 static int operator_dispatcher(lua_State* L);
139 // this is called as metamethod __call on the class_rep.
140 static int constructor_dispatcher(lua_State* L);
142 struct base_info
144 int pointer_offset; // the offset added to the pointer to obtain a basepointer (due to multiple-inheritance)
145 class_rep* base;
148 void add_base_class(const base_info& binfo);
150 const std::vector<base_info>& bases() const throw() { return m_bases; }
152 void set_type(LUABIND_TYPE_INFO t) { m_type = t; }
153 LUABIND_TYPE_INFO type() const throw() { return m_type; }
154 LUABIND_TYPE_INFO holder_type() const throw() { return m_holder_type; }
155 LUABIND_TYPE_INFO const_holder_type() const throw() { return m_const_holder_type; }
156 bool has_holder() const throw() { return m_construct_holder != 0; }
158 const char* name() const throw() { return m_name; }
160 // the lua reference to the metatable for this class' instances
161 int metatable_ref() const throw() { return m_instance_metatable; }
163 void get_table(lua_State* L) const { m_table.push(L); }
164 void get_default_table(lua_State* L) const { m_default_table.push(L); }
166 void(*construct_holder() const)(void*, void*) { return m_construct_holder; }
167 void(*destructor() const)(void*) { return m_destructor; }
168 void(*const_holder_destructor() const)(void*) { return m_const_holder_destructor; }
169 typedef const void*(*t_const_extractor)(void*);
170 t_const_extractor const_extractor() const { return m_const_extractor; }
171 typedef void*(*t_extractor)(void*);
172 t_extractor extractor() const { return m_extractor; }
174 void(*const_converter() const)(void*,void*) { return m_const_converter; }
176 class_type get_class_type() const { return m_class_type; }
178 void add_static_constant(const char* name, int val);
180 // takes a pointer to the instance object
181 // and if it has a wrapper, the wrapper
182 // will convert its weak_ptr into a strong ptr.
183 void adopt(bool const_obj, void* obj);
185 static int super_callback(lua_State* L);
187 static int lua_settable_dispatcher(lua_State* L);
189 // called from the metamethod for __index
190 // obj is the object pointer
191 static int static_class_gettable(lua_State* L);
193 void* convert_to(
194 LUABIND_TYPE_INFO target_type
195 , const object_rep* obj, conversion_storage&) const;
197 bool has_operator_in_lua(lua_State*, int id);
199 int holder_alignment() const
201 return m_holder_alignment;
204 int holder_size() const
206 return m_holder_size;
210 void set_holder_alignment(int n)
212 m_holder_alignment = n;
215 void set_holder_size(int n)
217 m_holder_size = n;
220 void derived_from(const class_rep* base)
222 m_holder_alignment = base->m_holder_alignment;
223 m_holder_size = base->m_holder_size;
224 m_holder_type = base->m_holder_type;
225 m_const_holder_type = base->m_const_holder_type;
226 m_extractor = base->m_extractor;
227 m_const_extractor = base->m_const_extractor;
228 m_const_converter = base->m_const_converter;
229 m_construct_holder = base->m_construct_holder;
230 m_construct_const_holder = base->m_construct_const_holder;
231 m_default_construct_holder = base->m_default_construct_holder;
232 m_default_construct_const_holder = base->m_default_construct_const_holder;
235 private:
237 void cache_operators(lua_State*);
239 // this is a pointer to the type_info structure for
240 // this type
241 // warning: this may be a problem when using dll:s, since
242 // typeid() may actually return different pointers for the same
243 // type.
244 LUABIND_TYPE_INFO m_type;
245 LUABIND_TYPE_INFO m_holder_type;
246 LUABIND_TYPE_INFO m_const_holder_type;
248 // this function pointer is used if the type is held by
249 // a smart pointer. This function takes the type we are holding
250 // (the held_type, the smart pointer) and extracts the actual
251 // pointer.
252 void*(*m_extractor)(void*);
253 const void*(*m_const_extractor)(void*);
255 void(*m_const_converter)(void*, void*);
257 // this function is used to construct the held_type
258 // (the smart pointer). The arguments are the memory
259 // in which it should be constructed (with placement new)
260 // and the raw pointer that should be wrapped in the
261 // smart pointer
262 typedef void(*construct_held_type_t)(void*,void*);
263 construct_held_type_t m_construct_holder;
264 construct_held_type_t m_construct_const_holder;
266 typedef void(*default_construct_held_type_t)(void*);
267 default_construct_held_type_t m_default_construct_holder;
268 default_construct_held_type_t m_default_construct_const_holder;
270 typedef void(*adopt_t)(void*);
271 adopt_t m_adopt_fun;
273 // this is the size of the userdata chunk
274 // for each object_rep of this class. We
275 // need this since held_types are constructed
276 // in the same memory (to avoid fragmentation)
277 int m_holder_size;
278 int m_holder_alignment;
280 // a list of info for every class this class derives from
281 // the information stored here is sufficient to do
282 // type casts to the base classes
283 std::vector<base_info> m_bases;
285 // the class' name (as given when registered to lua with class_)
286 const char* m_name;
288 // a reference to this structure itself. Since this struct
289 // is kept inside lua (to let lua collect it when lua_close()
290 // is called) we need to lock it to prevent collection.
291 // the actual reference is not currently used.
292 detail::lua_reference m_self_ref;
294 // this should always be used when accessing
295 // members in instances of a class.
296 // this table contains c closures for all
297 // member functions in this class, they
298 // may point to both static and virtual functions
299 handle m_table;
301 // this table contains default implementations of the
302 // virtual functions in m_table.
303 handle m_default_table;
305 // the type of this class.. determines if it's written in c++ or lua
306 class_type m_class_type;
308 // this is a lua reference that points to the lua table
309 // that is to be used as meta table for all instances
310 // of this class.
311 int m_instance_metatable;
313 // ***** the maps below contains all members in this class *****
315 void(*m_destructor)(void*);
316 void(*m_const_holder_destructor)(void*);
318 std::map<const char*, int, ltstr> m_static_constants;
320 // the first time an operator is invoked
321 // we check the associated lua table
322 // and cache the result
323 int m_operator_cache;
326 bool is_class_rep(lua_State* L, int index);
330 //#include <luabind/detail/overload_rep_impl.hpp>
332 #endif // LUABIND_CLASS_REP_HPP_INCLUDED