*** empty log message ***
[luabind.git] / src / class.cpp
blobbedc1a3accfb9e06bb6cedbcfd078d5a5f78f1e3
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>
25 #include <luabind/config.hpp>
26 #include <luabind/class.hpp>
28 #include <cstring>
29 #include <iostream>
31 namespace luabind { namespace detail {
33 struct method_name
35 method_name(char const* n): name(n) {}
36 bool operator()(method_rep const& o) const
37 { return std::strcmp(o.name, name) == 0; }
38 char const* name;
41 struct class_registration : registration
43 class_registration(char const* name);
45 void register_(lua_State* L) const;
47 const char* m_name;
49 mutable std::list<detail::method_rep> m_methods;
51 // datamembers, some members may be readonly, and
52 // only have a getter function
53 mutable std::map<const char*, detail::class_rep::callback, detail::ltstr> m_getters;
54 mutable std::map<const char*, detail::class_rep::callback, detail::ltstr> m_setters;
56 // the operators in lua
57 mutable std::vector<detail::class_rep::operator_callback> m_operators[detail::number_of_operators];
58 mutable std::map<const char*, int, detail::ltstr> m_static_constants;
60 mutable std::vector<class_base::base_desc> m_bases;
61 mutable detail::construct_rep m_constructor;
63 void(*m_destructor)(void*);
64 void(*m_const_holder_destructor)(void*);
66 void*(*m_extractor)(void*);
67 const void*(*m_const_extractor)(void*);
69 void(*m_const_converter)(void*,void*);
71 void(*m_construct_holder)(void*, void*);
72 void(*m_construct_const_holder)(void*, void*);
74 void(*m_default_construct_holder)(void*);
75 void(*m_default_construct_const_holder)(void*);
77 void(*m_adopt_fun)(void*);
79 int m_holder_size;
80 int m_holder_alignment;
82 LUABIND_TYPE_INFO m_type;
83 LUABIND_TYPE_INFO m_holder_type;
84 LUABIND_TYPE_INFO m_const_holder_type;
86 scope m_scope;
89 class_registration::class_registration(char const* name)
91 m_name = name;
94 void class_registration::register_(lua_State* L) const
96 LUABIND_CHECK_STACK(L);
98 assert(lua_type(L, -1) == LUA_TTABLE);
100 lua_pushstring(L, m_name);
102 detail::class_rep* crep;
104 detail::class_registry* r = detail::class_registry::get_registry(L);
105 // create a class_rep structure for this class.
106 // allocate it within lua to let lua collect it on
107 // lua_close(). This is better than allocating it
108 // as a static, since it will then be destructed
109 // when the program exits instead.
110 // warning: we assume that lua will not
111 // move the userdata memory.
112 lua_newuserdata(L, sizeof(detail::class_rep));
113 crep = reinterpret_cast<detail::class_rep*>(lua_touserdata(L, -1));
115 new(crep) detail::class_rep(
116 m_type
117 , m_name
119 , m_destructor
120 , m_const_holder_destructor
121 , m_holder_type
122 , m_const_holder_type
123 , m_extractor
124 , m_const_extractor
125 , m_const_converter
126 , m_construct_holder
127 , m_construct_const_holder
128 , m_default_construct_holder
129 , m_default_construct_const_holder
130 , m_adopt_fun
131 , m_holder_size
132 , m_holder_alignment);
134 // register this new type in the class registry
135 r->add_class(m_type, crep);
136 if (!(LUABIND_TYPE_INFO_EQUAL(m_holder_type, LUABIND_INVALID_TYPE_INFO)))
138 // if we have a held type
139 // we have to register it in the class-table
140 // but only for the base class, if it already
141 // exists, we don't have to register it
142 detail::class_rep* c = r->find_class(m_holder_type);
143 if (c == 0)
145 r->add_class(m_holder_type, crep);
146 r->add_class(m_const_holder_type, crep);
150 // constructors
151 m_constructor.swap(crep->m_constructor);
153 crep->m_getters.swap(m_getters);
154 crep->m_setters.swap(m_setters);
156 for (int i = 0; i < detail::number_of_operators; ++i)
157 crep->m_operators[i].swap(m_operators[i]);
159 crep->m_static_constants.swap(m_static_constants);
161 typedef std::list<detail::method_rep> methods_t;
163 detail::class_registry* registry = detail::class_registry::get_registry(L);
165 for (std::vector<class_base::base_desc>::iterator i = m_bases.begin();
166 i != m_bases.end(); ++i)
168 LUABIND_CHECK_STACK(L);
170 // the baseclass' class_rep structure
171 detail::class_rep* bcrep = registry->find_class(i->type);
173 detail::class_rep::base_info base;
174 base.pointer_offset = i->ptr_offset;
175 base.base = bcrep;
177 crep->add_base_class(base);
179 // copy base class table
180 crep->get_table(L);
181 bcrep->get_table(L);
182 lua_pushnil(L);
184 while (lua_next(L, -2))
186 lua_pushvalue(L, -2); // copy key
187 lua_insert(L, -2);
188 lua_settable(L, -5);
190 lua_pop(L, 2);
192 // copy base class detaults table
193 crep->get_default_table(L);
194 bcrep->get_default_table(L);
195 lua_pushnil(L);
197 while (lua_next(L, -2))
199 lua_pushvalue(L, -2); // copy key
200 lua_insert(L, -2);
201 lua_settable(L, -5);
203 lua_pop(L, 2);
207 // add methods
208 for (std::list<detail::method_rep>::iterator i
209 = m_methods.begin(); i != m_methods.end(); ++i)
211 LUABIND_CHECK_STACK(L);
212 crep->add_method(*i);
215 crep->register_methods(L);
217 m_methods.clear();
219 crep->get_default_table(L);
220 m_scope.register_(L);
221 lua_pop(L, 1);
223 lua_settable(L, -3);
226 // -- interface ---------------------------------------------------------
228 class_base::class_base(char const* name)
229 : scope(std::auto_ptr<registration>(
230 m_registration = new class_registration(name))
235 void class_base::init(
236 LUABIND_TYPE_INFO type_
237 , LUABIND_TYPE_INFO holder_type
238 , LUABIND_TYPE_INFO const_holder_type
239 , void*(*extractor)(void*)
240 , const void*(*const_extractor)(void*)
241 , void(*const_converter_)(void*,void*)
242 , void(*holder_constructor_)(void*,void*)
243 , void(*const_holder_constructor_)(void*,void*)
244 , void(*holder_default_constructor_)(void*)
245 , void(*const_holder_default_constructor_)(void*)
246 , void(*adopt_fun)(void*)
247 , void(*destructor)(void*)
248 , void(*const_holder_destructor)(void*)
249 , int holder_size
250 , int holder_alignment)
252 m_registration->m_type = type_;
253 m_registration->m_holder_type = holder_type;
254 m_registration->m_const_holder_type = const_holder_type;
255 m_registration->m_extractor = extractor;
256 m_registration->m_const_extractor = const_extractor;
257 m_registration->m_const_converter = const_converter_;
258 m_registration->m_construct_holder = holder_constructor_;
259 m_registration->m_construct_const_holder = const_holder_constructor_;
260 m_registration->m_default_construct_holder = holder_default_constructor_;
261 m_registration->m_default_construct_const_holder = const_holder_default_constructor_;
262 m_registration->m_destructor = destructor;
263 m_registration->m_const_holder_destructor = const_holder_destructor;
264 m_registration->m_adopt_fun = adopt_fun;
265 m_registration->m_holder_size = holder_size;
266 m_registration->m_holder_alignment = holder_alignment;
269 void class_base::add_getter(
270 const char* name, const boost::function2<int, lua_State*, int>& g)
272 detail::class_rep::callback c;
273 c.func = g;
274 c.pointer_offset = 0;
276 const char* key = name;
277 m_registration->m_getters[key] = c;
280 #ifdef LUABIND_NO_ERROR_CHECKING
281 void class_base::add_setter(
282 const char* name
283 , const boost::function2<int, lua_State*, int>& s)
284 #else
285 void class_base::add_setter(
286 const char* name
287 , const boost::function2<int, lua_State*, int>& s
288 , int (*match)(lua_State*, int)
289 , void (*get_sig_ptr)(lua_State*, std::string&))
290 #endif
292 detail::class_rep::callback c;
293 c.func = s;
294 c.pointer_offset = 0;
296 #ifndef LUABIND_NO_ERROR_CHECKING
297 c.match = match;
298 c.sig = get_sig_ptr;
299 #endif
302 const char* key = name;
303 m_registration->m_setters[key] = c;
306 void class_base::add_base(const base_desc& b)
308 m_registration->m_bases.push_back(b);
311 void class_base::add_constructor(const detail::construct_rep::overload_t& o)
313 m_registration->m_constructor.overloads.push_back(o);
316 void class_base::add_method(const char* name, const detail::overload_rep& o)
318 typedef std::list<detail::method_rep> methods_t;
320 methods_t::iterator m = std::find_if(
321 m_registration->m_methods.begin()
322 , m_registration->m_methods.end()
323 , method_name(name));
324 if (m == m_registration->m_methods.end())
326 m_registration->m_methods.push_back(method_rep());
327 m = m_registration->m_methods.end();
328 std::advance(m, -1);
329 m->name = name;
332 m->add_overload(o);
333 m->crep = 0;
336 #ifndef LUABIND_NO_ERROR_CHECKING
337 void class_base::add_operator(
338 int op_id, int(*func)(lua_State*), int(*matcher)(lua_State*)
339 , void(*sig)(lua_State*, std::string&), int arity)
340 #else
341 void class_base::add_operator(
342 int op_id, int(*func)(lua_State*)
343 , int(*matcher)(lua_State*), int arity)
344 #endif
346 detail::class_rep::operator_callback o;
347 o.set_fun(func);
348 o.set_match_fun(matcher);
349 o.set_arity(arity);
351 #ifndef LUABIND_NO_ERROR_CHECKING
353 o.set_sig_fun(sig);
355 #endif
356 m_registration->m_operators[op_id].push_back(o);
359 const char* class_base::name() const
361 return m_registration->m_name;
364 void class_base::add_static_constant(const char* name, int val)
366 m_registration->m_static_constants[name] = val;
369 void class_base::add_inner_scope(scope& s)
371 m_registration->m_scope.operator,(s);
374 template<class T>
375 void add_custom_name(T i, std::string& s) {}
377 void add_custom_name(std::type_info const* i, std::string& s)
379 s += " [";
380 s += i->name();
381 s += "]";
384 std::string get_class_name(lua_State* L, LUABIND_TYPE_INFO i)
386 std::string ret;
388 assert(L);
390 class_registry* r = class_registry::get_registry(L);
391 class_rep* crep = r->find_class(i);
393 if (crep == 0)
395 ret = "custom";
396 add_custom_name(i, ret);
398 else
400 if (LUABIND_TYPE_INFO_EQUAL(i, crep->holder_type()))
402 ret += "smart_ptr<";
403 ret += crep->name();
404 ret += ">";
406 else if (LUABIND_TYPE_INFO_EQUAL(i, crep->const_holder_type()))
408 ret += "smart_ptr<const ";
409 ret += crep->name();
410 ret += ">";
412 else
414 ret += crep->name();
417 return ret;
420 }} // namespace luabind::detail