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/method_rep.hpp>
36 #include <luabind/detail/garbage_collector.hpp>
37 #include <luabind/detail/operator_id.hpp>
38 #include <luabind/detail/signature_match.hpp>
39 #include <luabind/detail/class_registry.hpp>
40 #include <luabind/detail/find_best_match.hpp>
41 #include <luabind/detail/get_overload_signature.hpp>
42 #include <luabind/detail/error.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
57 std::string
stack_content_by_name(lua_State
* L
, int start_index
);
58 int construct_lua_class_callback(lua_State
* L
);
60 // this is class-specific information, poor man's vtable
61 // this is allocated statically (removed by the compiler)
62 // a pointer to this structure is stored in the lua tables'
63 // metatable with the name __classrep
64 // it is used when matching parameters to function calls
65 // to determine possible implicit casts
66 // it is also used when finding the best match for overloaded
71 friend struct luabind::class_base
;
72 friend int super_callback(lua_State
*);
73 //TODO: avoid the lua-prefix
74 friend int lua_class_gettable(lua_State
*);
75 friend int lua_class_settable(lua_State
*);
76 friend int static_class_gettable(lua_State
*);
85 // destructor is a lua callback function that is hooked as garbage collector event on every instance
86 // of this class (including those that is not owned by lua). It gets an object_rep as argument
87 // on the lua stack. It should delete the object pointed to by object_rep::ptr if object_pre::flags
88 // is object_rep::owner (which means that lua owns the object)
90 // EXPECTS THE TOP VALUE ON THE LUA STACK TO
91 // BE THE USER DATA WHERE THIS CLASS IS BEING
93 class_rep(LUABIND_TYPE_INFO t
96 , void(*destructor
)(void*)
97 , LUABIND_TYPE_INFO held_t
98 , LUABIND_TYPE_INFO held_const_t
99 , void*(*extractor
)(void*)
100 , void(*construct_held_type
)(void*,void*)
102 , int held_type_alignment
);
104 // used when creating a lua class
105 // EXPECTS THE TOP VALUE ON THE LUA STACK TO
106 // BE THE USER DATA WHERE THIS CLASS IS BEING
108 class_rep(lua_State
* L
, const char* name
);
112 std::pair
<void*,void*> allocate(lua_State
* L
) const;
114 // called from the metamethod for __index
115 // the object pointer is passed on the lua stack
116 int gettable(lua_State
* L
);
118 // called from the metamethod for __newindex
119 // the object pointer is passed on the lua stack
120 bool settable(lua_State
* L
);
122 // this is called as __index metamethod on every instance of this class
123 static int gettable_dispatcher(lua_State
* L
);
125 // this is called as __newindex metamethod on every instance of this class
126 static int settable_dispatcher(lua_State
* L
);
127 static int operator_dispatcher(lua_State
* L
);
129 // this is called as metamethod __call on the class_rep.
130 static int constructor_dispatcher(lua_State
* L
);
132 // static int implicit_cast(const class_rep* from, const class_rep* to, int& pointer_offset);
134 // the functions dispatcher assumes the following:
135 // there is one upvalue that points to the method_rep that this dispatcher is to call
136 // the first parameter on the lua stack is an object_rep that points to the object the
137 // call is being made on
138 static int function_dispatcher(lua_State
* L
);
142 int pointer_offset
; // the offset added to the pointer to obtain a basepointer (due to multiple-inheritance)
147 void add_base_class(const base_info
& binfo
);
149 inline const std::vector
<base_info
>& bases() const throw() { return m_bases
; }
151 inline void set_type(LUABIND_TYPE_INFO t
) { m_type
= t
; }
152 inline LUABIND_TYPE_INFO
type() const throw() { return m_type
; }
153 inline LUABIND_TYPE_INFO
holder_type() const throw() { return m_held_type
; }
154 inline LUABIND_TYPE_INFO
const_holder_type() const throw() { return m_const_holder_type
; }
156 inline const char* name() const throw() { return m_name
; }
158 // the lua reference to this class_rep
159 inline int self_ref() const throw() { return m_self_ref
; }
160 // the lua reference to the metatable for this class' instances
161 inline int metatable_ref() const throw() { return m_instance_metatable
; }
162 inline int table_ref() const { return m_table_ref
; }
164 inline void(*destructor() const)(void*) { return m_destructor
; }
165 inline void*(*extract_ptr() const)(void*) { return m_extract_underlying_fun
; }
167 inline class_type
get_class_type() const { return m_class_type
; }
169 void add_static_constant(const char* name
, int val
);
171 static int super_callback(lua_State
* L
);
173 static int lua_settable_dispatcher(lua_State
* L
);
174 static int construct_lua_class_callback(lua_State
* L
);
176 // called from the metamethod for __index
177 // obj is the object pointer
178 static int lua_class_gettable(lua_State
* L
);
180 // called from the metamethod for __newindex
181 // obj is the object pointer
182 static int lua_class_settable(lua_State
* L
);
184 static int static_class_gettable(lua_State
* L
);
186 void* convert_to(LUABIND_TYPE_INFO target_type
, const object_rep
* obj
) const;
190 // this is a pointer to the type_info structure for
192 // warning: this may be a problem when using dll:s, since
193 // typeid() may actually return different pointers for the same
195 LUABIND_TYPE_INFO m_type
;
196 LUABIND_TYPE_INFO m_held_type
;
197 LUABIND_TYPE_INFO m_const_holder_type
;
199 // this function pointer is used if the type is held by
200 // a smart pointer. This function takes the type we are holding
201 // (the held_type, the smart pointer) and extracts the actual
203 typedef void*(*extract_ptr_t
)(void*);
204 extract_ptr_t m_extract_underlying_fun
;
206 // this function is used to construct the held_type
207 // (the smart pointer). The arguments are the memory
208 // in which it should be constructed (with placement new)
209 // and the raw pointer that should be wrapped in the
211 typedef void(*construct_held_type_t
)(void*,void*);
212 construct_held_type_t m_held_type_constructor
;
214 // this is the size of the userdata chunk
215 // for each object_rep of this class. We
216 // need this since held_types are constructed
217 // in the same memory (to avoid fragmentation)
219 int m_userdata_alignment
;
221 // a list of info for every class this class derives from
222 // the information stored here is sufficient to do
223 // type casts to the base classes
224 std::vector
<base_info
> m_bases
;
226 // the class' name (as given when registered to lua with class_)
229 // contains signatures and construction functions
230 // for all constructors
231 construct_rep m_constructor
;
233 // a reference to this structure itself. Since this struct
234 // is kept inside lua (to let lua collect it when lua_close()
235 // is called) we need to lock it to prevent collection.
236 // the actual reference is not currently used.
239 // a reference to the lua table that represents this class
240 // (only used if it is a lua class)
243 // the type of this class.. determines if it's written in c++ or lua
244 class_type m_class_type
;
246 // this is a lua reference that points to the lua table
247 // that is to be used as meta table for all instances
249 int m_instance_metatable
;
251 // ***** the maps below contains all members in this class *****
253 // maps method names to a structure with more
254 // information about that method.
255 // that struct contains the function-signatures
256 // for every overload
257 std::map
<const char*, method_rep
, ltstr
> m_methods
;
259 #ifndef LUABIND_DONT_COPY_STRINGS
260 // this is where the strings that the maps contains
261 // pointer to are kept. To make sure they are destructed.
262 std::vector
<char*> m_strings
;
267 boost::function2
<int, lua_State
*, int> func
;
271 // datamembers, some members may be readonly, and
272 // only have a getter function
273 std::map
<const char*, callback
, ltstr
> m_getters
;
274 std::map
<const char*, callback
, ltstr
> m_setters
;
276 struct operator_callback
: public overload_rep_base
278 inline void set_fun(int (*f
)(lua_State
*)) { func
= f
; }
279 inline int call(lua_State
* L
) { return func(L
); }
280 inline void set_arity(int arity
) { m_arity
= arity
; }
284 int(*func
)(lua_State
*);
287 std::vector
<operator_callback
> m_operators
[number_of_operators
]; // the operators in lua
289 void(*m_destructor
)(void*);
291 std::map
<const char*, int, ltstr
> m_static_constants
;
294 bool is_class_rep(lua_State
* L
, int index
);
298 #endif // LUABIND_CLASS_REP_HPP_INCLUDED