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
;
55 namespace luabind
{ namespace detail
59 LUABIND_API
std::string
stack_content_by_name(lua_State
* L
, int start_index
);
60 int construct_lua_class_callback(lua_State
* L
);
62 // this is class-specific information, poor man's vtable
63 // this is allocated statically (removed by the compiler)
64 // a pointer to this structure is stored in the lua tables'
65 // metatable with the name __classrep
66 // it is used when matching parameters to function calls
67 // to determine possible implicit casts
68 // it is also used when finding the best match for overloaded
71 class LUABIND_API class_rep
73 friend struct luabind::class_base
;
74 friend int super_callback(lua_State
*);
75 //TODO: avoid the lua-prefix
76 friend int lua_class_gettable(lua_State
*);
77 friend int lua_class_settable(lua_State
*);
78 friend int static_class_gettable(lua_State
*);
87 // destructor is a lua callback function that is hooked as garbage collector event on every instance
88 // of this class (including those that is not owned by lua). It gets an object_rep as argument
89 // on the lua stack. It should delete the object pointed to by object_rep::ptr if object_pre::flags
90 // is object_rep::owner (which means that lua owns the object)
92 // EXPECTS THE TOP VALUE ON THE LUA STACK TO
93 // BE THE USER DATA WHERE THIS CLASS IS BEING
95 class_rep(LUABIND_TYPE_INFO type
98 , void(*destructor
)(void*)
99 , void(*const_holder_destructor
)(void*)
100 , LUABIND_TYPE_INFO holder_type
101 , LUABIND_TYPE_INFO const_holder_type
102 , void*(*extractor
)(void*)
103 , const void*(*const_extractor
)(void*)
104 , void(*const_converter
)(void*,void*)
105 , void(*construct_holder
)(void*,void*)
106 , void(*construct_const_holder
)(void*,void*)
108 , int holder_alignment
);
110 // used when creating a lua class
111 // EXPECTS THE TOP VALUE ON THE LUA STACK TO
112 // BE THE USER DATA WHERE THIS CLASS IS BEING
114 class_rep(lua_State
* L
, const char* name
);
118 std::pair
<void*,void*> allocate(lua_State
* L
) const;
120 // called from the metamethod for __index
121 // the object pointer is passed on the lua stack
122 int gettable(lua_State
* L
);
124 // called from the metamethod for __newindex
125 // the object pointer is passed on the lua stack
126 bool settable(lua_State
* L
);
128 // this is called as __index metamethod on every instance of this class
129 static int gettable_dispatcher(lua_State
* L
);
131 // this is called as __newindex metamethod on every instance of this class
132 static int settable_dispatcher(lua_State
* L
);
133 static int operator_dispatcher(lua_State
* L
);
135 // this is called as metamethod __call on the class_rep.
136 static int constructor_dispatcher(lua_State
* L
);
138 // static int implicit_cast(const class_rep* from, const class_rep* to, int& pointer_offset);
140 // the functions dispatcher assumes the following:
141 // there is one upvalue that points to the method_rep that this dispatcher is to call
142 // the first parameter on the lua stack is an object_rep that points to the object the
143 // call is being made on
144 static int function_dispatcher(lua_State
* L
);
148 int pointer_offset
; // the offset added to the pointer to obtain a basepointer (due to multiple-inheritance)
153 void add_base_class(const base_info
& binfo
);
155 inline const std::vector
<base_info
>& bases() const throw() { return m_bases
; }
157 inline void set_type(LUABIND_TYPE_INFO t
) { m_type
= t
; }
158 inline LUABIND_TYPE_INFO
type() const throw() { return m_type
; }
159 inline LUABIND_TYPE_INFO
holder_type() const throw() { return m_holder_type
; }
160 inline LUABIND_TYPE_INFO
const_holder_type() const throw() { return m_const_holder_type
; }
161 inline bool has_holder() const throw() { return m_construct_holder
!= 0; }
163 inline const char* name() const throw() { return m_name
; }
165 // the lua reference to this class_rep
166 inline int self_ref() const throw() { return m_self_ref
; }
167 // the lua reference to the metatable for this class' instances
168 inline int metatable_ref() const throw() { return m_instance_metatable
; }
169 inline int table_ref() const { return m_table_ref
; }
171 inline void(*destructor() const)(void*) { return m_destructor
; }
172 inline void(*const_holder_destructor() const)(void*) { return m_const_holder_destructor
; }
173 inline void*(*extractor() const)(void*) { return m_extractor
; }
174 inline const void*(*const_extractor() const)(void*) { return m_const_extractor
; }
176 inline void(*const_converter() const)(void*,void*) { return m_const_converter
; }
178 inline class_type
get_class_type() const { return m_class_type
; }
180 void add_static_constant(const char* name
, int val
);
181 void add_method(lua_State
* L
, const char* name
, const detail::method_rep
& m
);
183 static int super_callback(lua_State
* L
);
185 static int lua_settable_dispatcher(lua_State
* L
);
186 static int construct_lua_class_callback(lua_State
* L
);
188 // called from the metamethod for __index
189 // obj is the object pointer
190 static int lua_class_gettable(lua_State
* L
);
192 // called from the metamethod for __newindex
193 // obj is the object pointer
194 static int lua_class_settable(lua_State
* L
);
196 static int static_class_gettable(lua_State
* L
);
198 void* convert_to(LUABIND_TYPE_INFO target_type
, const object_rep
* obj
, void*) const;
200 bool has_operator_in_lua(lua_State
*, int id
);
204 boost::function2
<int, lua_State
*, int> func
;
208 const std::map
<const char*, callback
, ltstr
>& properties() const;
209 typedef std::map
<const char*, callback
, ltstr
> property_map
;
213 void cache_operators(lua_State
*);
215 // this is a pointer to the type_info structure for
217 // warning: this may be a problem when using dll:s, since
218 // typeid() may actually return different pointers for the same
220 LUABIND_TYPE_INFO m_type
;
221 LUABIND_TYPE_INFO m_holder_type
;
222 LUABIND_TYPE_INFO m_const_holder_type
;
224 // this function pointer is used if the type is held by
225 // a smart pointer. This function takes the type we are holding
226 // (the held_type, the smart pointer) and extracts the actual
228 void*(*m_extractor
)(void*);
229 const void*(*m_const_extractor
)(void*);
231 void(*m_const_converter
)(void*, void*);
233 // this function is used to construct the held_type
234 // (the smart pointer). The arguments are the memory
235 // in which it should be constructed (with placement new)
236 // and the raw pointer that should be wrapped in the
238 typedef void(*construct_held_type_t
)(void*,void*);
239 construct_held_type_t m_construct_holder
;
240 construct_held_type_t m_construct_const_holder
;
242 // this is the size of the userdata chunk
243 // for each object_rep of this class. We
244 // need this since held_types are constructed
245 // in the same memory (to avoid fragmentation)
247 int m_holder_alignment
;
249 // a list of info for every class this class derives from
250 // the information stored here is sufficient to do
251 // type casts to the base classes
252 std::vector
<base_info
> m_bases
;
254 // the class' name (as given when registered to lua with class_)
257 // contains signatures and construction functions
258 // for all constructors
259 construct_rep m_constructor
;
261 // a reference to this structure itself. Since this struct
262 // is kept inside lua (to let lua collect it when lua_close()
263 // is called) we need to lock it to prevent collection.
264 // the actual reference is not currently used.
267 // a reference to the lua table that represents this class
268 // (only used if it is a lua class)
270 // this should always be used, when accessing static
271 // members in the class, and even when accessing
272 // members in instances of a class.
275 // the type of this class.. determines if it's written in c++ or lua
276 class_type m_class_type
;
278 // this is a lua reference that points to the lua table
279 // that is to be used as meta table for all instances
281 int m_instance_metatable
;
283 // ***** the maps below contains all members in this class *****
285 // maps method names to a structure with more
286 // information about that method.
287 // that struct contains the function-signatures
288 // for every overload
289 std::map
<const char*, method_rep
, ltstr
> m_methods
;
291 #ifndef LUABIND_DONT_COPY_STRINGS
292 // this is where the strings that the maps contains
293 // pointer to are kept. To make sure they are destructed.
294 std::vector
<char*> m_strings
;
297 // datamembers, some members may be readonly, and
298 // only have a getter function
299 std::map
<const char*, callback
, ltstr
> m_getters
;
300 std::map
<const char*, callback
, ltstr
> m_setters
;
302 struct operator_callback
: public overload_rep_base
304 inline void set_fun(int (*f
)(lua_State
*)) { func
= f
; }
305 inline int call(lua_State
* L
) { return func(L
); }
306 inline void set_arity(int arity
) { m_arity
= arity
; }
310 int(*func
)(lua_State
*);
313 std::vector
<operator_callback
> m_operators
[number_of_operators
]; // the operators in lua
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