*** empty log message ***
[luabind.git] / src / class.cpp
blob5956d0ea39c934de63de36d95b5209b809709f96
1 // Copyright (c) 2004 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.
23 #include <luabind/lua_include.hpp>
24 #include <luabind/config.hpp>
25 #include <luabind/class.hpp>
27 namespace luabind { namespace detail {
29 struct class_registration : registration
31 class_registration(char const* name);
33 void register_(lua_State* L) const;
35 const char* m_name;
37 mutable std::map<const char*, detail::method_rep, detail::ltstr> m_methods;
39 // datamembers, some members may be readonly, and
40 // only have a getter function
41 mutable std::map<const char*, detail::class_rep::callback, detail::ltstr> m_getters;
42 mutable std::map<const char*, detail::class_rep::callback, detail::ltstr> m_setters;
44 // the operators in lua
45 mutable std::vector<detail::class_rep::operator_callback> m_operators[detail::number_of_operators];
46 mutable std::map<const char*, int, detail::ltstr> m_static_constants;
48 mutable std::vector<class_base::base_desc> m_bases;
49 mutable detail::construct_rep m_constructor;
51 void(*m_destructor)(void*);
52 void(*m_const_holder_destructor)(void*);
54 void*(*m_extractor)(void*);
55 const void*(*m_const_extractor)(void*);
57 void(*m_const_converter)(void*,void*);
59 void(*m_construct_holder)(void*, void*);
60 void(*m_construct_const_holder)(void*, void*);
62 int m_holder_size;
63 int m_holder_alignment;
65 LUABIND_TYPE_INFO m_type;
66 LUABIND_TYPE_INFO m_holder_type;
67 LUABIND_TYPE_INFO m_const_holder_type;
69 #ifndef LUABIND_DONT_COPY_STRINGS
70 // the maps that contains char pointers points into
71 // this vector of strings.
72 mutable std::vector<char*> m_strings;
73 #endif
76 class_registration::class_registration(char const* name)
78 #ifndef LUABIND_DONT_COPY_STRINGS
79 m_strings.push_back(detail::dup_string(name));
80 m_name = m_strings.back();
81 #else
82 m_name = name;
83 #endif
86 void class_registration::register_(lua_State* L) const
88 lua_pushstring(L, m_name);
90 detail::class_rep* crep;
92 detail::class_registry* r = detail::class_registry::get_registry(L);
93 // create a class_rep structure for this class.
94 // allocate it within lua to let lua collect it on
95 // lua_close(). This is better than allocating it
96 // as a static, since it will then be destructed
97 // when the program exits instead.
98 // warning: we assume that lua will not
99 // move the userdata memory.
100 lua_newuserdata(L, sizeof(detail::class_rep));
101 crep = reinterpret_cast<detail::class_rep*>(lua_touserdata(L, -1));
103 new(crep) detail::class_rep(
104 m_type
105 , m_name
107 , m_destructor
108 , m_const_holder_destructor
109 , m_holder_type
110 , m_const_holder_type
111 , m_extractor
112 , m_const_extractor
113 , m_const_converter
114 , m_construct_holder
115 , m_construct_const_holder
116 , m_holder_size
117 , m_holder_alignment);
119 // register this new type in the class registry
120 r->add_class(m_type, crep);
121 if (!(LUABIND_TYPE_INFO_EQUAL(m_holder_type, LUABIND_INVALID_TYPE_INFO)))
123 // if we have a held type
124 // we have to register it in the class-table
125 // but only for the base class, if it already
126 // exists, we don't have to register it
127 detail::class_rep* c = r->find_class(m_holder_type);
128 if (c == 0)
130 r->add_class(m_holder_type, crep);
131 r->add_class(m_const_holder_type, crep);
135 // constructors
136 m_constructor.swap(crep->m_constructor);
138 #ifndef LUABIND_DONT_COPY_STRINGS
139 assert(crep->m_strings.empty() && "Internal error");
140 crep->m_strings.swap(m_strings);
141 #endif
143 crep->m_getters.swap(m_getters);
144 crep->m_setters.swap(m_setters);
146 for(int i = 0; i < detail::number_of_operators; ++i)
147 crep->m_operators[i].swap(m_operators[i]);
149 crep->m_static_constants.swap(m_static_constants);
151 // here!
152 // crep->m_methods = m_methods;
154 for (std::vector<class_base::base_desc>::iterator i = m_bases.begin();
155 i != m_bases.end();
156 ++i)
158 detail::class_registry* r = detail::class_registry::get_registry(L);
160 // the baseclass' class_rep structure
161 detail::class_rep* bcrep = r->find_class(i->type);
163 detail::class_rep::base_info base;
164 base.pointer_offset = i->ptr_offset;
165 base.base = bcrep;
167 crep->add_base_class(base);
169 typedef std::map<const char*, detail::method_rep, detail::ltstr> methods_t;
171 for (methods_t::const_iterator i
172 = bcrep->m_methods.begin()
173 ; i != bcrep->m_methods.end()
174 ; ++i)
176 detail::method_rep& m = m_methods[i->first];
178 typedef std::vector<detail::overload_rep> overloads_t;
180 for (overloads_t::const_iterator j
181 = i->second.overloads().begin()
182 ; j != i->second.overloads().end()
183 ; ++j)
185 detail::overload_rep o = *j;
186 o.add_offset(base.pointer_offset);
187 m.add_overload(o);
191 // copy base class table
192 detail::getref(L, crep->table_ref());
193 detail::getref(L, bcrep->table_ref());
194 lua_pushnil(L);
196 while (lua_next(L, -2))
198 lua_pushvalue(L, -2); // copy key
199 lua_insert(L, -2);
200 lua_settable(L, -5);
203 lua_pop(L, 2);
206 crep->m_methods = m_methods;
208 for (std::map<const char*, detail::method_rep, detail::ltstr>::iterator i
209 = crep->m_methods.begin(); i != crep->m_methods.end(); ++i)
211 i->second.crep = crep;
214 // add methods
215 for (std::map<const char*, detail::method_rep, detail::ltstr>::iterator i
216 = m_methods.begin(); i != m_methods.end(); ++i)
218 detail::getref(L, crep->table_ref());
219 lua_pushstring(L, i->first);
220 lua_pushnil(L);
221 lua_settable(L, -3);
222 lua_pop(L, 1);
224 crep->add_method(L, i->first, crep->m_methods[i->first]);
225 i->second.crep = crep;
228 m_methods.clear();
230 lua_settable(L, -3);
233 // -- interface ---------------------------------------------------------
235 class_base::class_base(char const* name)
236 : scope(std::auto_ptr<registration>(
237 m_registration = new class_registration(name))
242 void class_base::init(
243 LUABIND_TYPE_INFO type_
244 , LUABIND_TYPE_INFO holder_type
245 , LUABIND_TYPE_INFO const_holder_type
246 , void*(*extractor)(void*)
247 , const void*(*const_extractor)(void*)
248 , void(*const_converter_)(void*,void*)
249 , void(*holder_constructor_)(void*,void*)
250 , void(*const_holder_constructor_)(void*,void*)
251 , void(*destructor)(void*)
252 , void(*const_holder_destructor)(void*)
253 , int holder_size
254 , int holder_alignment)
256 m_registration->m_type = type_;
257 m_registration->m_holder_type = holder_type;
258 m_registration->m_const_holder_type = const_holder_type;
259 m_registration->m_extractor = extractor;
260 m_registration->m_const_extractor = const_extractor;
261 m_registration->m_const_converter = const_converter_;
262 m_registration->m_construct_holder = holder_constructor_;
263 m_registration->m_construct_const_holder = const_holder_constructor_;
264 m_registration->m_destructor = destructor;
265 m_registration->m_const_holder_destructor = const_holder_destructor;
266 m_registration->m_holder_size = holder_size;
267 m_registration->m_holder_alignment = holder_alignment;
270 void class_base::add_getter(
271 const char* name, const boost::function2<int, lua_State*, int>& g)
273 detail::class_rep::callback c;
274 c.func = g;
275 c.pointer_offset = 0;
276 #ifndef LUABIND_DONT_COPY_STRINGS
277 m_registration->m_strings.push_back(detail::dup_string(name));
278 m_registration->m_getters[m_registration->m_strings.back()] = c;
279 #else
280 m_registration->m_getters[name] = c;
281 #endif
284 void class_base::add_setter(
285 const char* name, const boost::function2<int, lua_State*, int>& s)
287 detail::class_rep::callback c;
288 c.func = s;
289 c.pointer_offset = 0;
290 #ifndef LUABIND_DONT_COPY_STRINGS
291 m_registration->m_strings.push_back(detail::dup_string(name));
292 m_registration->m_setters[m_registration->m_strings.back()] = c;
293 #else
294 m_registration->m_setters[name] = c;
295 #endif
298 void class_base::add_base(const base_desc& b)
300 m_registration->m_bases.push_back(b);
303 void class_base::add_constructor(const detail::construct_rep::overload_t& o)
305 m_registration->m_constructor.overloads.push_back(o);
308 void class_base::add_method(const char* name, const detail::overload_rep& o)
310 #ifdef LUABIND_DONT_COPY_STRINGS
311 detail::method_rep& method = m_registration->m_methods[name];
312 method.name = name;
313 #else
314 m_registration->m_strings.push_back(detail::dup_string(name));
315 detail::method_rep& method = m_registration->m_methods[
316 m_registration->m_strings.back()];
317 method.name = m_registration->m_strings.back();
318 #endif
319 method.add_overload(o);
320 method.crep = 0;
323 #ifndef LUABIND_NO_ERROR_CHECKING
324 void class_base::add_operator(
325 int op_id, int(*func)(lua_State*), int(*matcher)(lua_State*)
326 , void(*sig)(lua_State*, std::string&), int arity)
327 #else
328 void class_base::add_operator(
329 int op_id, int(*func)(lua_State*)
330 , int(*matcher)(lua_State*), int arity)
331 #endif
333 detail::class_rep::operator_callback o;
334 o.set_fun(func);
335 o.set_match_fun(matcher);
336 o.set_arity(arity);
338 #ifndef LUABIND_NO_ERROR_CHECKING
340 o.set_sig_fun(sig);
342 #endif
343 m_registration->m_operators[op_id].push_back(o);
346 const char* class_base::name() const
348 return m_registration->m_name;
351 void class_base::add_static_constant(const char* name, int val)
353 m_registration->m_static_constants[name] = val;
356 }} // namespace luabind::detail