Fix bug when passing type with holder by value to Lua.
[luabind.git] / luabind / detail / class_rep.hpp
blobefc2f46ccf30da3af38b8b4903d3fdb8391e37f2
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 <utility>
31 #include <list>
33 #include <luabind/config.hpp>
34 #include <luabind/detail/object_rep.hpp>
35 #include <luabind/detail/construct_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/detail/find_best_match.hpp>
40 #include <luabind/detail/get_overload_signature.hpp>
41 #include <luabind/error.hpp>
42 #include <luabind/handle.hpp>
43 #include <luabind/detail/primitives.hpp>
45 #ifdef BOOST_MSVC
46 // msvc doesn't have two-phase, but requires
47 // method_rep (and overload_rep) to be complete
48 // because of its std::list implementation.
49 // gcc on the other hand has two-phase but doesn't
50 // require method_rep to be complete.
51 #include <luabind/detail/method_rep.hpp>
52 #endif
54 namespace luabind
57 template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(LUABIND_MAX_BASES, class A, detail::null_type)>
58 struct bases {};
59 typedef bases<detail::null_type> no_bases;
62 namespace luabind { namespace detail
65 struct method_rep;
66 LUABIND_API std::string stack_content_by_name(lua_State* L, int start_index);
67 int construct_lua_class_callback(lua_State* L);
69 struct class_registration;
71 // this is class-specific information, poor man's vtable
72 // this is allocated statically (removed by the compiler)
73 // a pointer to this structure is stored in the lua tables'
74 // metatable with the name __classrep
75 // it is used when matching parameters to function calls
76 // to determine possible implicit casts
77 // it is also used when finding the best match for overloaded
78 // methods
80 class LUABIND_API class_rep
82 friend struct class_registration;
83 friend int super_callback(lua_State*);
84 //TODO: avoid the lua-prefix
85 friend int lua_class_gettable(lua_State*);
86 friend int lua_class_settable(lua_State*);
87 friend int static_class_gettable(lua_State*);
88 public:
90 enum class_type
92 cpp_class = 0,
93 lua_class = 1
96 #ifndef NDEBUG
97 std::string class_info_string(lua_State*) const;
98 #endif
100 // destructor is a lua callback function that is hooked as garbage collector event on every instance
101 // of this class (including those that is not owned by lua). It gets an object_rep as argument
102 // on the lua stack. It should delete the object pointed to by object_rep::ptr if object_pre::flags
103 // is object_rep::owner (which means that lua owns the object)
105 // EXPECTS THE TOP VALUE ON THE LUA STACK TO
106 // BE THE USER DATA WHERE THIS CLASS IS BEING
107 // INSTANTIATED!
108 class_rep(LUABIND_TYPE_INFO type
109 , const char* name
110 , lua_State* L
111 , void(*destructor)(void*)
112 , void(*const_holder_destructor)(void*)
113 , LUABIND_TYPE_INFO holder_type
114 , LUABIND_TYPE_INFO const_holder_type
115 , void*(*extractor)(void*)
116 , const void*(*const_extractor)(void*)
117 , void(*const_converter)(void*,void*)
118 , void(*construct_holder)(void*,void*)
119 , void(*construct_const_holder)(void*,void*)
120 , void(*default_construct_holder)(void*)
121 , void(*default_construct_const_holder)(void*)
122 , void(*adopt_fun)(void*)
123 , int holder_size
124 , int holder_alignment);
126 // used when creating a lua class
127 // EXPECTS THE TOP VALUE ON THE LUA STACK TO
128 // BE THE USER DATA WHERE THIS CLASS IS BEING
129 // INSTANTIATED!
130 class_rep(lua_State* L, const char* name);
132 ~class_rep();
134 std::pair<void*,void*> allocate(lua_State* L) const;
136 // called from the metamethod for __index
137 // the object pointer is passed on the lua stack
138 int gettable(lua_State* L);
140 // called from the metamethod for __newindex
141 // the object pointer is passed on the lua stack
142 bool settable(lua_State* L);
144 // this is called as __index metamethod on every instance of this class
145 static int gettable_dispatcher(lua_State* L);
147 // this is called as __newindex metamethod on every instance of this class
148 static int settable_dispatcher(lua_State* L);
149 static int operator_dispatcher(lua_State* L);
151 // this is called as metamethod __call on the class_rep.
152 static int constructor_dispatcher(lua_State* L);
154 static int function_dispatcher(lua_State* L);
156 struct base_info
158 int pointer_offset; // the offset added to the pointer to obtain a basepointer (due to multiple-inheritance)
159 class_rep* base;
162 void add_base_class(const base_info& binfo);
164 const std::vector<base_info>& bases() const throw() { return m_bases; }
166 void set_type(LUABIND_TYPE_INFO t) { m_type = t; }
167 LUABIND_TYPE_INFO type() const throw() { return m_type; }
168 LUABIND_TYPE_INFO holder_type() const throw() { return m_holder_type; }
169 LUABIND_TYPE_INFO const_holder_type() const throw() { return m_const_holder_type; }
170 bool has_holder() const throw() { return m_construct_holder != 0; }
172 const char* name() const throw() { return m_name; }
174 // the lua reference to this class_rep
175 // TODO: remove
176 // int self_ref() const throw() { return m_self_ref; }
177 // the lua reference to the metatable for this class' instances
178 int metatable_ref() const throw() { return m_instance_metatable; }
180 void get_table(lua_State* L) const { m_table.push(L); }
181 void get_default_table(lua_State* L) const { m_default_table.push(L); }
183 void(*construct_holder() const)(void*, void*) { return m_construct_holder; }
184 void(*destructor() const)(void*) { return m_destructor; }
185 void(*const_holder_destructor() const)(void*) { return m_const_holder_destructor; }
186 typedef const void*(*t_const_extractor)(void*);
187 t_const_extractor const_extractor() const { return m_const_extractor; }
188 typedef void*(*t_extractor)(void*);
189 t_extractor extractor() const { return m_extractor; }
191 void(*const_converter() const)(void*,void*) { return m_const_converter; }
193 class_type get_class_type() const { return m_class_type; }
195 void add_static_constant(const char* name, int val);
196 void add_method(detail::method_rep const& m);
197 void register_methods(lua_State* L);
199 // takes a pointer to the instance object
200 // and if it has a wrapper, the wrapper
201 // will convert its weak_ptr into a strong ptr.
202 void adopt(bool const_obj, void* obj);
204 static int super_callback(lua_State* L);
206 static int lua_settable_dispatcher(lua_State* L);
207 static int construct_lua_class_callback(lua_State* L);
209 // called from the metamethod for __index
210 // obj is the object pointer
211 static int lua_class_gettable(lua_State* L);
213 // called from the metamethod for __newindex
214 // obj is the object pointer
215 static int lua_class_settable(lua_State* L);
217 // called from the metamethod for __index
218 // obj is the object pointer
219 static int static_class_gettable(lua_State* L);
221 void* convert_to(LUABIND_TYPE_INFO target_type, const object_rep* obj, void*) const;
223 bool has_operator_in_lua(lua_State*, int id);
225 // this is used to describe setters and getters
226 struct callback
228 boost::function2<int, lua_State*, int> func;
229 #ifndef LUABIND_NO_ERROR_CHECKING
230 int (*match)(lua_State*, int);
232 typedef void(*get_sig_ptr)(lua_State*, std::string&);
233 get_sig_ptr sig;
234 #endif
235 int pointer_offset;
238 const std::map<const char*, callback, ltstr>& properties() const;
239 typedef std::map<const char*, callback, ltstr> property_map;
241 int holder_alignment() const
243 return m_holder_alignment;
246 int holder_size() const
248 return m_holder_size;
252 void set_holder_alignment(int n)
254 m_holder_alignment = n;
257 void set_holder_size(int n)
259 m_holder_size = n;
262 void derived_from(const class_rep* base)
264 m_holder_alignment = base->m_holder_alignment;
265 m_holder_size = base->m_holder_size;
266 m_holder_type = base->m_holder_type;
267 m_const_holder_type = base->m_const_holder_type;
268 m_extractor = base->m_extractor;
269 m_const_extractor = base->m_const_extractor;
270 m_const_converter = base->m_const_converter;
271 m_construct_holder = base->m_construct_holder;
272 m_construct_const_holder = base->m_construct_const_holder;
273 m_default_construct_holder = base->m_default_construct_holder;
274 m_default_construct_const_holder = base->m_default_construct_const_holder;
277 struct operator_callback: public overload_rep_base
279 inline void set_fun(int (*f)(lua_State*)) { func = f; }
280 inline int call(lua_State* L) { return func(L); }
281 inline void set_arity(int arity) { m_arity = arity; }
283 private:
284 int(*func)(lua_State*);
287 private:
289 void cache_operators(lua_State*);
291 // this is a pointer to the type_info structure for
292 // this type
293 // warning: this may be a problem when using dll:s, since
294 // typeid() may actually return different pointers for the same
295 // type.
296 LUABIND_TYPE_INFO m_type;
297 LUABIND_TYPE_INFO m_holder_type;
298 LUABIND_TYPE_INFO m_const_holder_type;
300 // this function pointer is used if the type is held by
301 // a smart pointer. This function takes the type we are holding
302 // (the held_type, the smart pointer) and extracts the actual
303 // pointer.
304 void*(*m_extractor)(void*);
305 const void*(*m_const_extractor)(void*);
307 void(*m_const_converter)(void*, void*);
309 // this function is used to construct the held_type
310 // (the smart pointer). The arguments are the memory
311 // in which it should be constructed (with placement new)
312 // and the raw pointer that should be wrapped in the
313 // smart pointer
314 typedef void(*construct_held_type_t)(void*,void*);
315 construct_held_type_t m_construct_holder;
316 construct_held_type_t m_construct_const_holder;
318 typedef void(*default_construct_held_type_t)(void*);
319 default_construct_held_type_t m_default_construct_holder;
320 default_construct_held_type_t m_default_construct_const_holder;
322 typedef void(*adopt_t)(void*);
323 adopt_t m_adopt_fun;
325 // this is the size of the userdata chunk
326 // for each object_rep of this class. We
327 // need this since held_types are constructed
328 // in the same memory (to avoid fragmentation)
329 int m_holder_size;
330 int m_holder_alignment;
332 // a list of info for every class this class derives from
333 // the information stored here is sufficient to do
334 // type casts to the base classes
335 std::vector<base_info> m_bases;
337 // the class' name (as given when registered to lua with class_)
338 const char* m_name;
340 // contains signatures and construction functions
341 // for all constructors
342 construct_rep m_constructor;
344 // a reference to this structure itself. Since this struct
345 // is kept inside lua (to let lua collect it when lua_close()
346 // is called) we need to lock it to prevent collection.
347 // the actual reference is not currently used.
348 detail::lua_reference m_self_ref;
350 // this should always be used when accessing
351 // members in instances of a class.
352 // this table contains c closures for all
353 // member functions in this class, they
354 // may point to both static and virtual functions
355 handle m_table;
357 // this table contains default implementations of the
358 // virtual functions in m_table.
359 handle m_default_table;
361 // the type of this class.. determines if it's written in c++ or lua
362 class_type m_class_type;
364 // this is a lua reference that points to the lua table
365 // that is to be used as meta table for all instances
366 // of this class.
367 int m_instance_metatable;
369 // ***** the maps below contains all members in this class *****
371 // list of methods. pointers into this list is put in the m_table and
372 // m_default_table for access. The struct contains the function-
373 // signatures for every overload
374 std::list<method_rep> m_methods;
376 // datamembers, some members may be readonly, and
377 // only have a getter function
378 std::map<const char*, callback, ltstr> m_getters;
379 std::map<const char*, callback, ltstr> m_setters;
381 std::vector<operator_callback> m_operators[number_of_operators]; // the operators in lua
383 void(*m_destructor)(void*);
384 void(*m_const_holder_destructor)(void*);
386 std::map<const char*, int, ltstr> m_static_constants;
388 // the first time an operator is invoked
389 // we check the associated lua table
390 // and cache the result
391 int m_operator_cache;
394 bool is_class_rep(lua_State* L, int index);
398 //#include <luabind/detail/overload_rep_impl.hpp>
400 #endif // LUABIND_CLASS_REP_HPP_INCLUDED