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>
31 namespace luabind
{ namespace detail
{
35 method_name(char const* n
): name(n
) {}
36 bool operator()(method_rep
const& o
) const
37 { return std::strcmp(o
.name
, name
) == 0; }
41 struct class_registration
: registration
43 class_registration(char const* name
);
45 void register_(lua_State
* L
) const;
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*);
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
;
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 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
;
177 crep
->add_base_class(base
);
179 // copy base class table
184 while (lua_next(L
, -2))
186 lua_pushvalue(L
, -2); // copy key
192 // copy base class detaults table
193 crep
->get_default_table(L
);
194 bcrep
->get_default_table(L
);
197 while (lua_next(L
, -2))
199 lua_pushvalue(L
, -2); // copy key
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
);
219 crep
->get_default_table(L
);
220 m_scope
.register_(L
);
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*)
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
;
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(
283 , const boost::function2
<int, lua_State
*, int>& s
)
285 void class_base::add_setter(
287 , const boost::function2
<int, lua_State
*, int>& s
288 , int (*match
)(lua_State
*, int)
289 , void (*get_sig_ptr
)(lua_State
*, std::string
&))
292 detail::class_rep::callback c
;
294 c
.pointer_offset
= 0;
296 #ifndef LUABIND_NO_ERROR_CHECKING
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();
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
)
341 void class_base::add_operator(
342 int op_id
, int(*func
)(lua_State
*)
343 , int(*matcher
)(lua_State
*), int arity
)
346 detail::class_rep::operator_callback o
;
348 o
.set_match_fun(matcher
);
351 #ifndef LUABIND_NO_ERROR_CHECKING
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
);
375 void add_custom_name(T i
, std::string
& s
) {}
377 void add_custom_name(std::type_info
const* i
, std::string
& s
)
384 std::string
get_class_name(lua_State
* L
, LUABIND_TYPE_INFO i
)
390 class_registry
* r
= class_registry::get_registry(L
);
391 class_rep
* crep
= r
->find_class(i
);
396 add_custom_name(i
, ret
);
400 if (LUABIND_TYPE_INFO_EQUAL(i
, crep
->holder_type()))
406 else if (LUABIND_TYPE_INFO_EQUAL(i
, crep
->const_holder_type()))
408 ret
+= "smart_ptr<const ";
420 }} // namespace luabind::detail