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 #define LUABIND_BUILDING
25 #include <luabind/lua_include.hpp>
27 #include <luabind/config.hpp>
28 #include <luabind/class.hpp>
29 #include <luabind/nil.hpp>
36 LUABIND_API
detail::nil_type nil
;
39 namespace luabind
{ namespace detail
{
41 struct class_registration
: registration
43 class_registration(char const* name
);
45 void register_(lua_State
* L
) const;
49 // datamembers, some members may be readonly, and
50 // only have a getter function
51 mutable std::map
<const char*, detail::class_rep::callback
, detail::ltstr
> m_getters
;
52 mutable std::map
<const char*, detail::class_rep::callback
, detail::ltstr
> m_setters
;
54 // the operators in lua
55 mutable std::vector
<detail::class_rep::operator_callback
> m_operators
[detail::number_of_operators
];
56 mutable std::map
<const char*, int, detail::ltstr
> m_static_constants
;
58 mutable std::vector
<class_base::base_desc
> m_bases
;
59 mutable detail::construct_rep m_constructor
;
61 void(*m_destructor
)(void*);
62 void(*m_const_holder_destructor
)(void*);
64 void*(*m_extractor
)(void*);
65 const void*(*m_const_extractor
)(void*);
67 void(*m_const_converter
)(void*,void*);
69 void(*m_construct_holder
)(void*, void*);
70 void(*m_construct_const_holder
)(void*, void*);
72 void(*m_default_construct_holder
)(void*);
73 void(*m_default_construct_const_holder
)(void*);
75 void(*m_adopt_fun
)(void*);
78 int m_holder_alignment
;
80 LUABIND_TYPE_INFO m_type
;
81 LUABIND_TYPE_INFO m_holder_type
;
82 LUABIND_TYPE_INFO m_const_holder_type
;
86 scope m_default_members
;
89 class_registration::class_registration(char const* 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(
120 , m_const_holder_destructor
122 , m_const_holder_type
127 , m_construct_const_holder
128 , m_default_construct_holder
129 , m_default_construct_const_holder
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
);
145 r
->add_class(m_holder_type
, crep
);
146 r
->add_class(m_const_holder_type
, crep
);
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 detail::class_registry
* registry
= detail::class_registry::get_registry(L
);
163 crep
->get_default_table(L
);
164 m_scope
.register_(L
);
165 m_default_members
.register_(L
);
169 m_members
.register_(L
);
172 for (std::vector
<class_base::base_desc
>::iterator i
= m_bases
.begin();
173 i
!= m_bases
.end(); ++i
)
175 LUABIND_CHECK_STACK(L
);
177 // the baseclass' class_rep structure
178 detail::class_rep
* bcrep
= registry
->find_class(i
->type
);
180 detail::class_rep::base_info base
;
181 base
.pointer_offset
= i
->ptr_offset
;
184 crep
->add_base_class(base
);
186 // copy base class table
191 while (lua_next(L
, -2))
193 lua_pushvalue(L
, -2); // copy key
196 if (!lua_isnil(L
, -1))
203 lua_pushvalue(L
, -2); // copy key
210 // copy base class detaults table
211 crep
->get_default_table(L
);
212 bcrep
->get_default_table(L
);
215 while (lua_next(L
, -2))
217 lua_pushvalue(L
, -2); // copy key
220 if (!lua_isnil(L
, -1))
227 lua_pushvalue(L
, -2); // copy key
239 // -- interface ---------------------------------------------------------
241 class_base::class_base(char const* name
)
242 : scope(std::auto_ptr
<registration
>(
243 m_registration
= new class_registration(name
))
248 void class_base::init(
249 LUABIND_TYPE_INFO type_id
250 , LUABIND_TYPE_INFO holder_type
251 , LUABIND_TYPE_INFO const_holder_type
252 , void*(*extractor
)(void*)
253 , const void*(*const_extractor
)(void*)
254 , void(*const_converter_
)(void*,void*)
255 , void(*holder_constructor_
)(void*,void*)
256 , void(*const_holder_constructor_
)(void*,void*)
257 , void(*holder_default_constructor_
)(void*)
258 , void(*const_holder_default_constructor_
)(void*)
259 , void(*adopt_fun
)(void*)
260 , void(*destructor
)(void*)
261 , void(*const_holder_destructor
)(void*)
263 , int holder_alignment
)
265 m_registration
->m_type
= type_id
;
266 m_registration
->m_holder_type
= holder_type
;
267 m_registration
->m_const_holder_type
= const_holder_type
;
268 m_registration
->m_extractor
= extractor
;
269 m_registration
->m_const_extractor
= const_extractor
;
270 m_registration
->m_const_converter
= const_converter_
;
271 m_registration
->m_construct_holder
= holder_constructor_
;
272 m_registration
->m_construct_const_holder
= const_holder_constructor_
;
273 m_registration
->m_default_construct_holder
= holder_default_constructor_
;
274 m_registration
->m_default_construct_const_holder
= const_holder_default_constructor_
;
275 m_registration
->m_destructor
= destructor
;
276 m_registration
->m_const_holder_destructor
= const_holder_destructor
;
277 m_registration
->m_adopt_fun
= adopt_fun
;
278 m_registration
->m_holder_size
= holder_size
;
279 m_registration
->m_holder_alignment
= holder_alignment
;
282 void class_base::add_getter(
283 const char* name
, const boost::function2
<int, lua_State
*, int>& g
)
285 detail::class_rep::callback c
;
287 c
.pointer_offset
= 0;
289 const char* key
= name
;
290 m_registration
->m_getters
[key
] = c
;
293 #ifdef LUABIND_NO_ERROR_CHECKING
294 void class_base::add_setter(
296 , const boost::function2
<int, lua_State
*, int>& s
)
298 void class_base::add_setter(
300 , const boost::function2
<int, lua_State
*, int>& s
301 , int (*match
)(lua_State
*, int)
302 , void (*get_sig_ptr
)(lua_State
*, std::string
&))
305 detail::class_rep::callback c
;
307 c
.pointer_offset
= 0;
309 #ifndef LUABIND_NO_ERROR_CHECKING
315 const char* key
= name
;
316 m_registration
->m_setters
[key
] = c
;
319 void class_base::add_base(const base_desc
& b
)
321 m_registration
->m_bases
.push_back(b
);
324 void class_base::add_constructor(const detail::construct_rep::overload_t
& o
)
326 m_registration
->m_constructor
.overloads
.push_back(o
);
329 void class_base::add_member(registration
* member
)
331 std::auto_ptr
<registration
> ptr(member
);
332 m_registration
->m_members
.operator,(scope(ptr
));
335 void class_base::add_default_member(registration
* member
)
337 std::auto_ptr
<registration
> ptr(member
);
338 m_registration
->m_default_members
.operator,(scope(ptr
));
341 #ifndef LUABIND_NO_ERROR_CHECKING
342 void class_base::add_operator(
343 int op_id
, int(*func
)(lua_State
*), int(*matcher
)(lua_State
*)
344 , void(*sig
)(lua_State
*, std::string
&), int arity
)
346 void class_base::add_operator(
347 int op_id
, int(*func
)(lua_State
*)
348 , int(*matcher
)(lua_State
*), int arity
)
351 detail::class_rep::operator_callback o
;
353 o
.set_match_fun(matcher
);
356 #ifndef LUABIND_NO_ERROR_CHECKING
361 m_registration
->m_operators
[op_id
].push_back(o
);
364 const char* class_base::name() const
366 return m_registration
->m_name
;
369 void class_base::add_static_constant(const char* name
, int val
)
371 m_registration
->m_static_constants
[name
] = val
;
374 void class_base::add_inner_scope(scope
& s
)
376 m_registration
->m_scope
.operator,(s
);
380 void add_custom_name(T i
, std::string
& s
) {}
382 void add_custom_name(std::type_info
const* i
, std::string
& s
)
389 std::string
get_class_name(lua_State
* L
, LUABIND_TYPE_INFO i
)
395 class_registry
* r
= class_registry::get_registry(L
);
396 class_rep
* crep
= r
->find_class(i
);
401 add_custom_name(i
, ret
);
405 if (LUABIND_TYPE_INFO_EQUAL(i
, crep
->holder_type()))
411 else if (LUABIND_TYPE_INFO_EQUAL(i
, crep
->const_holder_type()))
413 ret
+= "smart_ptr<const ";
425 }} // namespace luabind::detail