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>
32 #include <luabind/config.hpp>
33 #include <luabind/detail/object_rep.hpp>
34 #include <luabind/detail/construct_rep.hpp>
35 #include <luabind/detail/garbage_collector.hpp>
36 #include <luabind/detail/operator_id.hpp>
37 #include <luabind/detail/signature_match.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/detail/error.hpp>
42 #include <luabind/detail/method_rep.hpp>
47 template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(LUABIND_MAX_BASES
, class A
, detail::null_type
)>
49 typedef bases
<detail::null_type
> no_bases
;
52 namespace luabind
{ namespace detail
56 LUABIND_API
std::string
stack_content_by_name(lua_State
* L
, int start_index
);
57 int construct_lua_class_callback(lua_State
* L
);
59 struct class_registration
;
61 // this is class-specific information, poor man's vtable
62 // this is allocated statically (removed by the compiler)
63 // a pointer to this structure is stored in the lua tables'
64 // metatable with the name __classrep
65 // it is used when matching parameters to function calls
66 // to determine possible implicit casts
67 // it is also used when finding the best match for overloaded
70 class LUABIND_API class_rep
72 friend struct class_registration
;
73 friend int super_callback(lua_State
*);
74 //TODO: avoid the lua-prefix
75 friend int lua_class_gettable(lua_State
*);
76 friend int lua_class_settable(lua_State
*);
77 friend int static_class_gettable(lua_State
*);
86 // destructor is a lua callback function that is hooked as garbage collector event on every instance
87 // of this class (including those that is not owned by lua). It gets an object_rep as argument
88 // on the lua stack. It should delete the object pointed to by object_rep::ptr if object_pre::flags
89 // is object_rep::owner (which means that lua owns the object)
91 // EXPECTS THE TOP VALUE ON THE LUA STACK TO
92 // BE THE USER DATA WHERE THIS CLASS IS BEING
94 class_rep(LUABIND_TYPE_INFO type
97 , void(*destructor
)(void*)
98 , void(*const_holder_destructor
)(void*)
99 , LUABIND_TYPE_INFO holder_type
100 , LUABIND_TYPE_INFO const_holder_type
101 , void*(*extractor
)(void*)
102 , const void*(*const_extractor
)(void*)
103 , void(*const_converter
)(void*,void*)
104 , void(*construct_holder
)(void*,void*)
105 , void(*construct_const_holder
)(void*,void*)
107 , int holder_alignment
);
109 // used when creating a lua class
110 // EXPECTS THE TOP VALUE ON THE LUA STACK TO
111 // BE THE USER DATA WHERE THIS CLASS IS BEING
113 class_rep(lua_State
* L
, const char* name
);
117 std::pair
<void*,void*> allocate(lua_State
* L
) const;
119 // called from the metamethod for __index
120 // the object pointer is passed on the lua stack
121 int gettable(lua_State
* L
);
123 // called from the metamethod for __newindex
124 // the object pointer is passed on the lua stack
125 bool settable(lua_State
* L
);
127 // this is called as __index metamethod on every instance of this class
128 static int gettable_dispatcher(lua_State
* L
);
130 // this is called as __newindex metamethod on every instance of this class
131 static int settable_dispatcher(lua_State
* L
);
132 static int operator_dispatcher(lua_State
* L
);
134 // this is called as metamethod __call on the class_rep.
135 static int constructor_dispatcher(lua_State
* L
);
137 // static int implicit_cast(const class_rep* from, const class_rep* to, int& pointer_offset);
139 // the functions dispatcher assumes the following:
140 // there is one upvalue that points to the method_rep that this dispatcher is to call
141 // the first parameter on the lua stack is an object_rep that points to the object the
142 // call is being made on
143 static int function_dispatcher(lua_State
* L
);
147 int pointer_offset
; // the offset added to the pointer to obtain a basepointer (due to multiple-inheritance)
152 void add_base_class(const base_info
& binfo
);
154 inline const std::vector
<base_info
>& bases() const throw() { return m_bases
; }
156 inline void set_type(LUABIND_TYPE_INFO t
) { m_type
= t
; }
157 inline LUABIND_TYPE_INFO
type() const throw() { return m_type
; }
158 inline LUABIND_TYPE_INFO
holder_type() const throw() { return m_holder_type
; }
159 inline LUABIND_TYPE_INFO
const_holder_type() const throw() { return m_const_holder_type
; }
160 inline bool has_holder() const throw() { return m_construct_holder
!= 0; }
162 inline const char* name() const throw() { return m_name
; }
164 // the lua reference to this class_rep
165 inline int self_ref() const throw() { return m_self_ref
; }
166 // the lua reference to the metatable for this class' instances
167 inline int metatable_ref() const throw() { return m_instance_metatable
; }
168 inline int table_ref() const { return m_table_ref
; }
170 inline void(*destructor() const)(void*) { return m_destructor
; }
171 inline void(*const_holder_destructor() const)(void*) { return m_const_holder_destructor
; }
172 typedef const void*(*t_const_extractor
)(void*);
173 inline t_const_extractor
const_extractor() const { return m_const_extractor
; }
174 typedef void*(*t_extractor
)(void*);
175 inline t_extractor
extractor() const { return m_extractor
; }
177 inline void(*const_converter() const)(void*,void*) { return m_const_converter
; }
179 inline class_type
get_class_type() const { return m_class_type
; }
181 void add_static_constant(const char* name
, int val
);
182 void add_method(lua_State
* L
, const char* name
, detail::method_rep
& m
);
184 static int super_callback(lua_State
* L
);
186 static int lua_settable_dispatcher(lua_State
* L
);
187 static int construct_lua_class_callback(lua_State
* L
);
189 // called from the metamethod for __index
190 // obj is the object pointer
191 static int lua_class_gettable(lua_State
* L
);
193 // called from the metamethod for __newindex
194 // obj is the object pointer
195 static int lua_class_settable(lua_State
* L
);
197 static int static_class_gettable(lua_State
* L
);
199 void* convert_to(LUABIND_TYPE_INFO target_type
, const object_rep
* obj
, void*) const;
201 bool has_operator_in_lua(lua_State
*, int id
);
205 boost::function2
<int, lua_State
*, int> func
;
209 const std::map
<const char*, callback
, ltstr
>& properties() const;
210 typedef std::map
<const char*, callback
, ltstr
> property_map
;
212 int holder_alignment() const
214 return m_holder_alignment
;
217 int holder_size() const
219 return m_holder_size
;
223 void set_holder_alignment(int n
)
225 m_holder_alignment
= n
;
228 void set_holder_size(int n
)
233 void derived_from(const class_rep
* base
)
235 m_holder_alignment
= base
->m_holder_alignment
;
236 m_holder_size
= base
->m_holder_size
;
237 m_holder_type
= base
->m_holder_type
;
238 m_const_holder_type
= base
->m_const_holder_type
;
239 m_extractor
= base
->m_extractor
;
240 m_const_extractor
= base
->m_const_extractor
;
241 m_const_converter
= base
->m_const_converter
;
242 m_construct_holder
= base
->m_construct_holder
;
243 m_construct_const_holder
= base
->m_construct_const_holder
;
246 struct operator_callback
: public overload_rep_base
248 inline void set_fun(int (*f
)(lua_State
*)) { func
= f
; }
249 inline int call(lua_State
* L
) { return func(L
); }
250 inline void set_arity(int arity
) { m_arity
= arity
; }
253 int(*func
)(lua_State
*);
258 void cache_operators(lua_State
*);
260 // this is a pointer to the type_info structure for
262 // warning: this may be a problem when using dll:s, since
263 // typeid() may actually return different pointers for the same
265 LUABIND_TYPE_INFO m_type
;
266 LUABIND_TYPE_INFO m_holder_type
;
267 LUABIND_TYPE_INFO m_const_holder_type
;
269 // this function pointer is used if the type is held by
270 // a smart pointer. This function takes the type we are holding
271 // (the held_type, the smart pointer) and extracts the actual
273 void*(*m_extractor
)(void*);
274 const void*(*m_const_extractor
)(void*);
276 void(*m_const_converter
)(void*, void*);
278 // this function is used to construct the held_type
279 // (the smart pointer). The arguments are the memory
280 // in which it should be constructed (with placement new)
281 // and the raw pointer that should be wrapped in the
283 typedef void(*construct_held_type_t
)(void*,void*);
284 construct_held_type_t m_construct_holder
;
285 construct_held_type_t m_construct_const_holder
;
287 // this is the size of the userdata chunk
288 // for each object_rep of this class. We
289 // need this since held_types are constructed
290 // in the same memory (to avoid fragmentation)
292 int m_holder_alignment
;
294 // a list of info for every class this class derives from
295 // the information stored here is sufficient to do
296 // type casts to the base classes
297 std::vector
<base_info
> m_bases
;
299 // the class' name (as given when registered to lua with class_)
302 // contains signatures and construction functions
303 // for all constructors
304 construct_rep m_constructor
;
306 // a reference to this structure itself. Since this struct
307 // is kept inside lua (to let lua collect it when lua_close()
308 // is called) we need to lock it to prevent collection.
309 // the actual reference is not currently used.
312 // a reference to the lua table that represents this class
313 // (only used if it is a lua class)
315 // this should always be used, when accessing static
316 // members in the class, and even when accessing
317 // members in instances of a class.
320 // the type of this class.. determines if it's written in c++ or lua
321 class_type m_class_type
;
323 // this is a lua reference that points to the lua table
324 // that is to be used as meta table for all instances
326 int m_instance_metatable
;
328 // ***** the maps below contains all members in this class *****
330 // maps method names to a structure with more
331 // information about that method.
332 // that struct contains the function-signatures
333 // for every overload
334 std::map
<const char*, method_rep
, ltstr
> m_methods
;
336 #ifndef LUABIND_DONT_COPY_STRINGS
337 // this is where the strings that the maps contains
338 // pointer to are kept. To make sure they are destructed.
339 std::vector
<char*> m_strings
;
342 // datamembers, some members may be readonly, and
343 // only have a getter function
344 std::map
<const char*, callback
, ltstr
> m_getters
;
345 std::map
<const char*, callback
, ltstr
> m_setters
;
347 std::vector
<operator_callback
> m_operators
[number_of_operators
]; // the operators in lua
349 void(*m_destructor
)(void*);
350 void(*m_const_holder_destructor
)(void*);
352 std::map
<const char*, int, ltstr
> m_static_constants
;
354 // the first time an operator is invoked
355 // we check the associated lua table
356 // and cache the result
357 int m_operator_cache
;
360 bool is_class_rep(lua_State
* L
, int index
);
364 #include <luabind/detail/overload_rep_impl.hpp>
366 #endif // LUABIND_CLASS_REP_HPP_INCLUDED