dynamic linking seems to work. Warnings still there.
[luabind.git] / luabind / detail / class_rep.hpp
blob4d7a56b9dc9944c5a353741a72e2fa1d24c92997
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>
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>
44 namespace luabind
47 template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(LUABIND_MAX_BASES, class A, detail::null_type)>
48 struct bases {};
49 typedef bases<detail::null_type> no_bases;
51 struct class_base;
55 namespace luabind { namespace detail
58 struct method_rep;
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
69 // methods
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*);
79 public:
81 enum class_type
83 cpp_class = 0,
84 lua_class = 1
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
94 // INSTANTIATED!
95 class_rep(LUABIND_TYPE_INFO type
96 , const char* name
97 , lua_State* L
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*)
107 , int holder_size
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
113 // INSTANTIATED!
114 class_rep(lua_State* L, const char* name);
116 ~class_rep();
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);
146 struct base_info
148 int pointer_offset; // the offset added to the pointer to obtain a basepointer (due to multiple-inheritance)
149 class_rep* base;
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);
182 static int super_callback(lua_State* L);
184 static int lua_settable_dispatcher(lua_State* L);
185 static int construct_lua_class_callback(lua_State* L);
187 // called from the metamethod for __index
188 // obj is the object pointer
189 static int lua_class_gettable(lua_State* L);
191 // called from the metamethod for __newindex
192 // obj is the object pointer
193 static int lua_class_settable(lua_State* L);
195 static int static_class_gettable(lua_State* L);
197 void* convert_to(LUABIND_TYPE_INFO target_type, const object_rep* obj, void*) const;
199 bool has_operator_in_lua(lua_State*, int id);
201 private:
203 void cache_operators(lua_State*);
205 // this is a pointer to the type_info structure for
206 // this type
207 // warning: this may be a problem when using dll:s, since
208 // typeid() may actually return different pointers for the same
209 // type.
210 LUABIND_TYPE_INFO m_type;
211 LUABIND_TYPE_INFO m_holder_type;
212 LUABIND_TYPE_INFO m_const_holder_type;
214 // this function pointer is used if the type is held by
215 // a smart pointer. This function takes the type we are holding
216 // (the held_type, the smart pointer) and extracts the actual
217 // pointer.
218 void*(*m_extractor)(void*);
219 const void*(*m_const_extractor)(void*);
221 void(*m_const_converter)(void*, void*);
223 // this function is used to construct the held_type
224 // (the smart pointer). The arguments are the memory
225 // in which it should be constructed (with placement new)
226 // and the raw pointer that should be wrapped in the
227 // smart pointer
228 typedef void(*construct_held_type_t)(void*,void*);
229 construct_held_type_t m_construct_holder;
230 construct_held_type_t m_construct_const_holder;
232 // this is the size of the userdata chunk
233 // for each object_rep of this class. We
234 // need this since held_types are constructed
235 // in the same memory (to avoid fragmentation)
236 int m_holder_size;
237 int m_holder_alignment;
239 // a list of info for every class this class derives from
240 // the information stored here is sufficient to do
241 // type casts to the base classes
242 std::vector<base_info> m_bases;
244 // the class' name (as given when registered to lua with class_)
245 const char* m_name;
247 // contains signatures and construction functions
248 // for all constructors
249 construct_rep m_constructor;
251 // a reference to this structure itself. Since this struct
252 // is kept inside lua (to let lua collect it when lua_close()
253 // is called) we need to lock it to prevent collection.
254 // the actual reference is not currently used.
255 int m_self_ref;
257 // a reference to the lua table that represents this class
258 // (only used if it is a lua class)
259 int m_table_ref;
261 // the type of this class.. determines if it's written in c++ or lua
262 class_type m_class_type;
264 // this is a lua reference that points to the lua table
265 // that is to be used as meta table for all instances
266 // of this class.
267 int m_instance_metatable;
269 // ***** the maps below contains all members in this class *****
271 // maps method names to a structure with more
272 // information about that method.
273 // that struct contains the function-signatures
274 // for every overload
275 std::map<const char*, method_rep, ltstr> m_methods;
277 #ifndef LUABIND_DONT_COPY_STRINGS
278 // this is where the strings that the maps contains
279 // pointer to are kept. To make sure they are destructed.
280 std::vector<char*> m_strings;
281 #endif
283 struct callback
285 boost::function2<int, lua_State*, int> func;
286 int pointer_offset;
289 // datamembers, some members may be readonly, and
290 // only have a getter function
291 std::map<const char*, callback, ltstr> m_getters;
292 std::map<const char*, callback, ltstr> m_setters;
294 struct operator_callback: public overload_rep_base
296 inline void set_fun(int (*f)(lua_State*)) { func = f; }
297 inline int call(lua_State* L) { return func(L); }
298 inline void set_arity(int arity) { m_arity = arity; }
300 private:
302 int(*func)(lua_State*);
305 std::vector<operator_callback> m_operators[number_of_operators]; // the operators in lua
307 void(*m_destructor)(void*);
308 void(*m_const_holder_destructor)(void*);
310 std::map<const char*, int, ltstr> m_static_constants;
312 // the first time an operator is invoked
313 // we check the associated lua table
314 // and cache the result
315 int m_operator_cache;
318 bool is_class_rep(lua_State* L, int index);
322 #include <luabind/detail/overload_rep_impl.hpp>
324 #endif // LUABIND_CLASS_REP_HPP_INCLUDED