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_HPP_INCLUDED
25 #define LUABIND_CLASS_HPP_INCLUDED
29 ------------------------------------------------------
31 * solved for member functions, not application operator *
32 if we have a base class that defines a function a derived class must be able to
33 override that function (not just overload). Right now we just add the other overload
34 to the overloads list and will probably get an ambiguity. If we want to support this
35 each method_rep must include a vector of type_info pointers for each parameter.
36 Operators do not have this problem, since operators always have to have
37 it's own type as one of the arguments, no ambiguity can occur. Application
38 operator, on the other hand, would have this problem.
39 Properties cannot be overloaded, so they should always be overridden.
40 If this is to work for application operator, we really need to specify if an application
41 operator is const or not.
43 If one class registers two functions with the same name and the same
44 signature, there's currently no error. The last registered function will
45 be the one that's used.
46 How do we know which class registered the function? If the function was
47 defined by the base class, it is a legal operation, to override it.
48 we cannot look at the pointer offset, since it always will be zero for one of the bases.
53 ------------------------------------------------------
56 classes with the same name in different scopes will have the same (fully qualified) name.
57 functions that are renamed will still have the same name in error-messages
59 finish smart pointer support
60 * make sure there are no bugs in the conversion from holder to const_holder
61 * the adopt policy should not be able to adopt pointers to held_types. This
63 * name_of_type must recognize holder_types and not return "custom"
65 support calling functions on lua threads (i.e. use lua_resume() instead of lua_pcall()).
67 document the new yield-policy more
69 document custom policies, custom converters
71 store the instance object for policies.
73 support the __concat metamethod. This is a bit tricky, since it cannot be
74 treated as a normal operator. It is a binary operator but we want to use the
75 __tostring implementation for both arguments.
79 #include <luabind/config.hpp>
86 #include <boost/static_assert.hpp>
87 #include <boost/type_traits.hpp>
88 #include <boost/bind.hpp>
89 #include <boost/function.hpp>
90 #include <boost/preprocessor/repetition/enum_params.hpp>
91 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
92 #include <boost/preprocessor/repetition/repeat.hpp>
93 #include <boost/type_traits/is_same.hpp>
94 #include <boost/mpl/list.hpp>
95 #include <boost/mpl/apply.hpp>
96 #include <boost/mpl/lambda.hpp>
97 #include <boost/mpl/logical.hpp>
98 #include <boost/mpl/find_if.hpp>
99 #include <boost/mpl/apply_if.hpp>
100 #include <boost/mpl/logical.hpp>
102 #include <luabind/config.hpp>
103 #include <luabind/scope.hpp>
104 #include <luabind/detail/constructor.hpp>
105 #include <luabind/detail/call.hpp>
106 #include <luabind/detail/signature_match.hpp>
107 #include <luabind/detail/primitives.hpp>
108 #include <luabind/detail/property.hpp>
109 #include <luabind/detail/typetraits.hpp>
110 #include <luabind/detail/class_rep.hpp>
111 #include <luabind/detail/method_rep.hpp>
112 #include <luabind/detail/construct_rep.hpp>
113 #include <luabind/detail/object_rep.hpp>
114 #include <luabind/detail/operators.hpp>
115 #include <luabind/detail/calc_arity.hpp>
116 #include <luabind/detail/call_member.hpp>
117 #include <luabind/detail/enum_maker.hpp>
118 #include <luabind/detail/get_signature.hpp>
119 #include <luabind/detail/implicit_cast.hpp>
120 #include <luabind/detail/operator_id.hpp>
121 #include <luabind/detail/pointee_typeid.hpp>
123 //#include <boost/langbinding/inheritance.hpp>
129 struct unspecified
{};
134 template<class T
, class X1
= detail::unspecified
, class X2
= detail::unspecified
, class X3
= detail::unspecified
>
137 // TODO: this function will only be invoked if the user hasn't defined a correct overload
138 // maybe we should have a static assert in here?
139 inline detail::null_type
* get_const_holder(...)
146 template<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_BASES
, class A
)>
147 double is_bases_helper(const bases
<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_BASES
, A
)>&);
151 char is_bases_helper(const T
&);
153 char is_bases_helper(...);
161 BOOST_STATIC_CONSTANT(bool, value
= sizeof(is_bases_helper(t
)) == sizeof(double));
162 typedef boost::mpl::bool_
<value
> type
;
163 BOOST_MPL_AUX_LAMBDA_SUPPORT(1,is_bases
,(T
))
166 double is_not_unspecified_helper(const unspecified
*);
167 char is_not_unspecified_helper(...);
170 struct is_not_unspecified
172 BOOST_STATIC_CONSTANT(bool, value
= sizeof(is_not_unspecified_helper(static_cast<T
*>(0))) == sizeof(char));
173 typedef boost::mpl::bool_
<value
> type
;
174 BOOST_MPL_AUX_LAMBDA_SUPPORT(1,is_not_unspecified
,(T
))
177 template<class Predicate
>
180 typedef typename
boost::mpl::and_
<
182 , is_not_unspecified
<boost::mpl::_1
>
186 template<class Parameters
, class Predicate
, class DefaultValue
>
187 struct extract_parameter
189 typedef typename get_predicate
<Predicate
>::type pred
;
190 typedef typename
boost::mpl::find_if
<Parameters
, pred
>::type iterator
;
191 typedef typename
boost::mpl::apply_if
<boost::is_same
<iterator
, typename
boost::mpl::end
<Parameters
>::type
>
192 , boost::mpl::identity
<DefaultValue
>
197 // this should know about the smart pointer type.
198 // and should really do:
199 // get_pointer(*static_cast<SmartPointer*>(obj_ptr))
200 // to extract the held pointer.
201 template<class HeldType
, class T
, class F
, class Policies
>
202 struct function_callback_non_null
: Policies
204 function_callback_non_null(F f_
): f(f_
) {}
205 inline int operator()(lua_State
* L
, void* obj_ptr
)
207 // HeldType& held_obj = *static_cast<HeldType*>(obj_ptr);
208 // T* ptr = static_cast<T*>(luabind::get_pointer(held_obj));
210 T
* ptr
= static_cast<T
*>(obj_ptr
);
212 return call(f
, ptr
, L
, static_cast<Policies
*>(this));
217 template<class T
, class F
, class Policies
>
218 struct function_callback_null_type
: Policies
220 function_callback_null_type(F f_
): f(f_
) {}
221 inline int operator()(lua_State
* L
, void* obj_ptr
)
223 // std::cout << "HeldType: null_type\n";
224 T
* ptr
= static_cast<T
*>(obj_ptr
);
225 return call(f
, ptr
, L
, static_cast<Policies
*>(this));
230 template<class HeldType
, class T
, class F
, class Policies
>
231 struct function_callback_s
234 boost::mpl::if_
<boost::is_same
<HeldType
,detail::null_type
>
235 , function_callback_null_type
<T
,F
,Policies
>
236 , function_callback_non_null
<HeldType
,T
,F
,Policies
>
240 template<class T
, class F
, class Policies
>
241 struct match_function_callback_s
243 static inline int apply(lua_State
* L
)
245 object_rep
* obj
= static_cast<object_rep
*>(lua_touserdata(L
, 1));
247 return match(fptr
, L
, obj
->flags() & object_rep::constant
, static_cast<Policies
*>(0));
251 // prints the types of the values on the stack, in the
252 // range [start_index, lua_gettop()]
254 LUABIND_API
std::string
stack_content_by_name(lua_State
* L
, int start_index
);
256 struct LUABIND_API create_class
258 static int stage1(lua_State
* L
);
259 static int stage2(lua_State
* L
);
263 struct register_wrapped_type
265 template<class Signature
, class Policies
>
266 static void apply(detail::construct_rep::overload_t
& o
, const Signature
*, const Policies
*)
268 o
.set_wrapped_constructor(
269 &detail::construct_wrapped_class
<Type
, Policies
, Signature
>::apply
275 struct register_wrapped_type
<detail::null_type
>
277 template<class Signature
, class Policies
>
278 static void apply(detail::construct_rep::overload_t
&, const Signature
*, const Policies
*) {}
282 // if the class is held by a smart pointer, we need to be able to
283 // implicitly dereference the pointer when needed.
285 template<class UnderlyingT
, class HeldT
>
286 struct extract_underlying_type
288 static void* extract(void* ptr
)
290 HeldT
& held_obj
= *reinterpret_cast<HeldT
*>(ptr
);
291 UnderlyingT
* underlying_ptr
= static_cast<UnderlyingT
*>(get_pointer(held_obj
));
292 return underlying_ptr
;
296 template<class UnderlyingT
, class HeldT
>
297 struct extract_underlying_const_type
299 static const void* extract(void* ptr
)
301 HeldT
& held_obj
= *reinterpret_cast<HeldT
*>(ptr
);
302 const UnderlyingT
* underlying_ptr
= static_cast<const UnderlyingT
*>(get_pointer(held_obj
));
303 return underlying_ptr
;
307 template<class HeldType
>
308 struct internal_holder_extractor
310 typedef void*(*extractor_fun
)(void*);
313 static extractor_fun
apply(detail::type
<T
>)
315 return &detail::extract_underlying_type
<T
, HeldType
>::extract
;
320 struct internal_holder_extractor
<detail::null_type
>
322 typedef void*(*extractor_fun
)(void*);
325 static extractor_fun
apply(detail::type
<T
>)
332 template<class HeldType
, class ConstHolderType
>
333 struct convert_holder
335 static void apply(void* holder
, void* target
)
337 new(target
) ConstHolderType(*reinterpret_cast<HeldType
*>(holder
));
342 template<class HeldType
>
343 struct const_converter
345 typedef void(*converter_fun
)(void*, void*);
347 template<class ConstHolderType
>
348 static converter_fun
apply(ConstHolderType
*)
350 return &detail::convert_holder
<HeldType
, ConstHolderType
>::apply
;
355 struct const_converter
<detail::null_type
>
357 typedef void(*converter_fun
)(void*, void*);
360 static converter_fun
apply(T
*)
369 template<class HeldType
>
370 struct internal_const_holder_extractor
372 typedef const void*(*extractor_fun
)(void*);
375 static extractor_fun
apply(detail::type
<T
>)
377 return get_extractor(detail::type
<T
>(), luabind::get_const_holder(static_cast<HeldType
*>(0)));
380 template<class T
, class ConstHolderType
>
381 static extractor_fun
get_extractor(detail::type
<T
>, ConstHolderType
*)
383 return &detail::extract_underlying_const_type
<T
, ConstHolderType
>::extract
;
388 struct internal_const_holder_extractor
<detail::null_type
>
390 typedef const void*(*extractor_fun
)(void*);
393 static extractor_fun
apply(detail::type
<T
>)
401 // this is simply a selector that returns the type_info
402 // of the held type, or invalid_type_info if we don't have
404 template<class HeldType
>
405 struct internal_holder_type
407 static LUABIND_TYPE_INFO
apply()
409 return LUABIND_TYPEID(HeldType
);
414 struct internal_holder_type
<detail::null_type
>
416 static LUABIND_TYPE_INFO
apply()
418 return LUABIND_INVALID_TYPE_INFO
;
423 // this is the actual held_type constructor
424 template<class HeldType
, class T
>
425 struct internal_construct_holder
427 static void apply(void* target
, void* raw_pointer
)
429 new(target
) HeldType(static_cast<T
*>(raw_pointer
));
433 // the following two functions are the ones that returns
434 // a pointer to a held_type_constructor, or 0 if there
436 template<class HeldType
>
437 struct holder_constructor
439 typedef void(*constructor
)(void*,void*);
441 static constructor
apply(detail::type
<T
>)
443 return &internal_construct_holder
<HeldType
, T
>::apply
;
448 struct holder_constructor
<detail::null_type
>
450 typedef void(*constructor
)(void*,void*);
452 static constructor
apply(detail::type
<T
>)
458 // the following two functions are the ones that returns
459 // a pointer to a const_held_type_constructor, or 0 if there
461 template<class HolderType
>
462 struct const_holder_constructor
464 typedef void(*constructor
)(void*,void*);
466 static constructor
apply(detail::type
<T
>)
468 return get_const_holder_constructor(detail::type
<T
>(), luabind::get_const_holder(static_cast<HolderType
*>(0)));
473 template<class T
, class ConstHolderType
>
474 static constructor
get_const_holder_constructor(detail::type
<T
>, ConstHolderType
*)
476 return &internal_construct_holder
<ConstHolderType
, T
>::apply
;
481 struct const_holder_constructor
<detail::null_type
>
483 typedef void(*constructor
)(void*,void*);
485 static constructor
apply(detail::type
<T
>)
493 // this is a selector that returns the size of the held_type
494 // or 0 if we don't have a held_type
495 template <class HolderType
>
496 struct internal_holder_size
498 static int apply() { return get_internal_holder_size(luabind::get_const_holder(static_cast<HolderType
*>(0))); }
500 template<class ConstHolderType
>
501 static int get_internal_holder_size(ConstHolderType
*)
503 return max_c
<sizeof(HolderType
), sizeof(ConstHolderType
)>::value
;
508 struct internal_holder_size
<detail::null_type
>
510 static int apply() { return 0; }
514 // if we have a held type, return the destructor to it
515 // note the difference. The held_type should only be destructed (not deleted)
516 // since it's constructed in the lua userdata
517 template<class HeldType
>
518 struct internal_holder_destructor
520 typedef void(*destructor_t
)(void*);
522 static destructor_t
apply(detail::type
<T
>)
524 return &detail::destruct_only_s
<HeldType
>::apply
;
528 // if we don't have a held type, return the destructor of the raw type
530 struct internal_holder_destructor
<detail::null_type
>
532 typedef void(*destructor_t
)(void*);
534 static destructor_t
apply(detail::type
<T
>)
536 return &detail::delete_s
<T
>::apply
;
541 // if we have a held type, return the destructor to it's const version
542 template<class HolderType
>
543 struct internal_const_holder_destructor
545 typedef void(*destructor_t
)(void*);
547 static destructor_t
apply(detail::type
<T
>)
549 return const_holder_type_destructor(luabind::get_const_holder(static_cast<HolderType
*>(0)));
554 template<class ConstHolderType
>
555 static destructor_t
const_holder_type_destructor(ConstHolderType
*)
557 return &detail::destruct_only_s
<ConstHolderType
>::apply
;
562 // if we don't have a held type, return the destructor of the raw type
564 struct internal_const_holder_destructor
<detail::null_type
>
566 typedef void(*destructor_t
)(void*);
568 static destructor_t
apply(detail::type
<T
>)
577 template<class HolderType
>
578 struct get_holder_alignment
582 return internal_alignment(luabind::get_const_holder(static_cast<HolderType
*>(0)));
587 template<class ConstHolderType
>
588 static int internal_alignment(ConstHolderType
*)
590 return detail::max_c
<boost::alignment_of
<HolderType
>::value
591 , boost::alignment_of
<ConstHolderType
>::value
>::value
;
596 struct get_holder_alignment
<detail::null_type
>
620 struct class_base: detail::scoped_object
626 LUABIND_TYPE_INFO type;
638 std::map<const char*, detail::method_rep, detail::ltstr> m_methods;
640 // datamembers, some members may be readonly, and
641 // only have a getter function
642 std::map<const char*, detail::class_rep::callback, detail::ltstr> m_getters;
643 std::map<const char*, detail::class_rep::callback, detail::ltstr> m_setters;
645 // the operators in lua
646 std::vector<detail::class_rep::operator_callback> m_operators[detail::number_of_operators];
647 std::map<const char*, int, detail::ltstr> m_static_constants;
649 std::vector<base_desc> m_bases;
650 detail::construct_rep m_constructor;
652 void(*m_destructor)(void*);
653 void(*m_const_holder_destructor)(void*);
655 void*(*m_extractor)(void*);
656 const void*(*m_const_extractor)(void*);
658 void(*m_const_converter)(void*,void*);
660 void(*m_construct_holder)(void*, void*);
661 void(*m_construct_const_holder)(void*, void*);
664 int m_holder_alignment;
666 LUABIND_TYPE_INFO m_type;
667 LUABIND_TYPE_INFO m_holder_type;
668 LUABIND_TYPE_INFO m_const_holder_type;
670 #ifndef LUABIND_DONT_COPY_STRINGS
671 // the maps that contains char pointers points into
672 // this vector of strings.
673 std::vector<char*> m_strings;
679 // public 'cause of enum_maker, FIX
680 void add_static_constant(const char* name, int val)
682 m_static_constants[name] = val;
687 mutable std::vector<detail::scoped_object*> m_children;
689 void init(LUABIND_TYPE_INFO type
690 , LUABIND_TYPE_INFO holder_type
691 , void*(*extractor)(void*)
692 , const void*(*const_extractor)(void*)
693 , void(*const_converter)(void*,void*)
694 , void(*holder_constructor)(void*,void*)
695 , void(*const_holder_constructor)(void*,void*)
696 , void(*destructor)(void*)
697 , void(*const_holder_destructor)(void*)
699 , int holder_alignment)
702 m_holder_type = holder_type;
703 m_extractor = extractor;
704 m_const_extractor = const_extractor;
705 m_const_converter = const_converter;
706 m_construct_holder = holder_constructor;
707 m_construct_const_holder = const_holder_constructor;
708 m_destructor = destructor;
709 m_const_holder_destructor = const_holder_destructor;
710 m_holder_size = holder_size;
711 m_holder_alignment = holder_alignment;
715 void set_const_holder_type(T*)
717 m_const_holder_type = LUABIND_TYPEID(T);
720 inline void add_getter(const char* name, const boost::function2<int, lua_State*, int>& g)
722 detail::class_rep::callback c;
724 c.pointer_offset = 0;
725 #ifndef LUABIND_DONT_COPY_STRINGS
726 m_strings.push_back(detail::dup_string(name));
727 m_getters[m_strings.back()] = c;
733 inline void add_setter(const char* name, const boost::function2<int, lua_State*, int>& s)
735 detail::class_rep::callback c;
737 c.pointer_offset = 0;
738 #ifndef LUABIND_DONT_COPY_STRINGS
739 m_strings.push_back(detail::dup_string(name));
740 m_setters[m_strings.back()] = c;
746 void add_base(const base_desc& b)
748 m_bases.push_back(b);
753 void add_constructor(const detail::construct_rep::overload_t& o)
755 m_constructor.overloads.push_back(o);
758 void add_method(const char* name, const detail::overload_rep& o)
760 #ifdef LUABIND_DONT_COPY_STRINGS
761 detail::method_rep& method = m_methods[name];
764 m_strings.push_back(detail::dup_string(name));
765 detail::method_rep& method = m_methods[m_strings.back()];
766 method.name = m_strings.back();
768 method.add_overload(o);
772 #ifndef LUABIND_NO_ERROR_CHECKING
773 inline void add_operator(int op_id, int(*func)(lua_State*), int(*matcher)(lua_State*), void(*sig)(lua_State*, std::string&), int arity)
775 inline void add_operator(int op_id, int(*func)(lua_State*), int(*matcher)(lua_State*), int arity)
778 detail::class_rep::operator_callback o;
780 o.set_match_fun(matcher);
783 #ifndef LUABIND_NO_ERROR_CHECKING
788 m_operators[op_id].push_back(o);
797 const char* name() const { return m_name; }
799 class_base(const char* name)
801 #ifndef LUABIND_DONT_COPY_STRINGS
802 m_strings.push_back(detail::dup_string(name));
803 m_name = m_strings.back();
813 virtual ~class_base()
815 for (std::vector<detail::scoped_object*>::iterator
816 i = m_children.begin(); i != m_children.end(); ++i)
819 // if we are copying strings, we have to destroy them too
820 #ifndef LUABIND_DONT_COPY_STRINGS
821 for (std::vector<char*>::iterator i = m_strings.begin(); i != m_strings.end(); ++i)
826 // pushes the class_rep on the lua stack
827 virtual void commit(lua_State* L)
829 assert(!m_cloned && "class already commited");
831 detail::getref(L, scope_stack::top(L));
832 lua_pushstring(L, m_name);
834 detail::class_rep* crep;
836 detail::class_registry* r = detail::class_registry::get_registry(L);
837 // create a class_rep structure for this class.
838 // allocate it within lua to let lua collect it on
839 // lua_close(). This is better than allocating it
840 // as a static, since it will then be destructed
841 // when the program exits instead.
842 // warning: we assume that lua will not
843 // move the userdata memory.
844 lua_newuserdata(L, sizeof(detail::class_rep));
845 crep = reinterpret_cast<detail::class_rep*>(lua_touserdata(L, -1));
847 new(crep) detail::class_rep( m_type
851 , m_const_holder_destructor
853 , m_const_holder_type
858 , m_construct_const_holder
860 , m_holder_alignment);
862 // register this new type in the class registry
863 r->add_class(m_type, crep);
864 if (!(LUABIND_TYPE_INFO_EQUAL(m_holder_type, LUABIND_INVALID_TYPE_INFO)))
866 // if we have a held type
867 // we have to register it in the class-table
868 // but only for the base class, if it already
869 // exists, we don't have to register it
870 detail::class_rep* c = r->find_class(m_holder_type);
873 r->add_class(m_holder_type, crep);
874 r->add_class(m_const_holder_type, crep);
879 m_constructor.swap(crep->m_constructor);
881 #ifndef LUABIND_DONT_COPY_STRINGS
882 assert(crep->m_strings.empty() && "Internal error");
883 std::swap(crep->m_strings, m_strings);
886 std::swap(crep->m_getters, m_getters);
887 std::swap(crep->m_setters, m_setters);
889 for(int i = 0; i < detail::number_of_operators; ++i)
890 std::swap(crep->m_operators[i], m_operators[i]);
892 std::swap(crep->m_static_constants, m_static_constants);
895 // crep->m_methods = m_methods;
897 for (std::vector<base_desc>::iterator i = m_bases.begin();
901 detail::class_registry* r = detail::class_registry::get_registry(L);
903 // the baseclass' class_rep structure
904 detail::class_rep* bcrep = r->find_class(i->type);
906 detail::class_rep::base_info base;
907 base.pointer_offset = i->ptr_offset;
910 crep->add_base_class(base);
912 typedef std::map<const char*, detail::method_rep, detail::ltstr> methods_t;
914 for (methods_t::const_iterator i
915 = bcrep->m_methods.begin()
916 ; i != bcrep->m_methods.end()
919 detail::method_rep& m = m_methods[i->first];
921 typedef std::vector<detail::overload_rep> overloads_t;
923 for (overloads_t::const_iterator j
924 = i->second.overloads().begin()
925 ; j != i->second.overloads().end()
928 detail::overload_rep o = *j;
929 o.add_offset(base.pointer_offset);
934 // copy base class table
935 detail::getref(L, crep->table_ref());
936 detail::getref(L, bcrep->table_ref());
939 while (lua_next(L, -2))
941 lua_pushvalue(L, -2); // copy key
949 crep->m_methods = m_methods;
951 for (std::map<const char*, detail::method_rep, detail::ltstr>::iterator i
952 = crep->m_methods.begin(); i != crep->m_methods.end(); ++i)
954 i->second.crep = crep;
958 for (std::map<const char*, detail::method_rep, detail::ltstr>::iterator i
959 = m_methods.begin(); i != m_methods.end(); ++i)
961 detail::getref(L, crep->table_ref());
962 lua_pushstring(L, i->first);
967 crep->add_method(L, i->first, crep->m_methods[i->first]);
968 i->second.crep = crep;
979 virtual luabind::detail::scoped_object* clone()
981 assert(m_cloned == false);
987 class_base* ret = new class_base(m_name);
989 std::swap(ret->m_getters, m_getters);
990 std::swap(ret->m_setters, m_setters);
992 for(int i = 0; i < detail::number_of_operators; ++i)
993 std::swap(ret->m_operators[i], m_operators[i]);
995 std::swap(ret->m_static_constants, m_static_constants);
1002 , m_construct_holder
1003 , m_construct_const_holder
1005 , m_const_holder_destructor
1007 , m_holder_alignment);
1009 ret->m_const_holder_type = m_const_holder_type;
1011 ret->m_bases.swap(m_bases);
1012 ret->m_methods.swap(m_methods);
1013 m_constructor.swap(ret->m_constructor);
1015 ret->m_name = m_name;
1017 #ifndef LUABIND_DONT_COPY_STRINGS
1018 std::swap(ret->m_strings, m_strings);
1021 m_children.swap(ret->m_children);
1029 struct class_registration
;
1031 struct class_base
: detail::scope
1034 class_base(char const* name
);
1038 LUABIND_TYPE_INFO type
;
1043 LUABIND_TYPE_INFO type
1044 , LUABIND_TYPE_INFO holder_type
1045 , LUABIND_TYPE_INFO const_holder_type
1046 , void*(*extractor
)(void*)
1047 , const void*(*const_extractor
)(void*)
1048 , void(*const_converter
)(void*,void*)
1049 , void(*holder_constructor
)(void*,void*)
1050 , void(*const_holder_constructor
)(void*,void*)
1051 , void(*destructor
)(void*)
1052 , void(*const_holder_destructor
)(void*)
1054 , int holder_alignment
);
1058 , const boost::function2
<int, lua_State
*, int>& g
);
1060 #ifdef LUABIND_NO_ERROR_CHECKING
1061 void class_base::add_setter(
1063 , const boost::function2
<int, lua_State
*, int>& s
);
1065 void class_base::add_setter(
1067 , const boost::function2
<int, lua_State
*, int>& s
1068 , int (*match
)(lua_State
*, int)
1069 , void (*get_sig_ptr
)(lua_State
*, std::string
&));
1072 void add_base(const base_desc
& b
);
1073 void add_constructor(const detail::construct_rep::overload_t
& o
);
1074 void add_method(const char* name
, const detail::overload_rep
& o
);
1076 #ifndef LUABIND_NO_ERROR_CHECKING
1079 , int(*func
)(lua_State
*)
1080 , int(*matcher
)(lua_State
*)
1081 , void(*sig
)(lua_State
*
1087 , int(*func
)(lua_State
*)
1088 , int(*matcher
)(lua_State
*)
1092 const char* name() const;
1094 void add_static_constant(const char* name
, int val
);
1097 class_registration
* m_registration
;
1100 } // namespace detail
1102 // registers a class in the lua environment
1103 template<class T
, class X1
, class X2
, class X3
>
1104 struct class_
: detail::class_base
1106 typedef class_
<T
, X1
, X2
, X3
> self_t
;
1112 template<class A
, class B
, class C
, class D
>
1113 class_(const class_
<A
,B
,C
,D
>&);
1117 // WrappedType MUST inherit from T
1118 typedef typename
detail::extract_parameter
<
1119 boost::mpl::vector3
<X1
,X2
,X3
>
1120 , boost::is_base_and_derived
<T
, boost::mpl::_
>
1121 /* , boost::mpl::not_<
1123 detail::is_bases<boost::mpl::_>
1124 , boost::is_base_and_derived<boost::mpl::_, T>
1128 >::type WrappedType
;
1130 typedef typename
detail::extract_parameter
<
1131 boost::mpl::list3
<X1
,X2
,X3
>
1135 detail::is_bases
<boost::mpl::_
>
1136 , boost::is_base_and_derived
<boost::mpl::_
, T
>
1138 , boost::is_base_and_derived
<T
, boost::mpl::_
>
1145 void register_downcast(boost::mpl::true_, detail::type<To>* = 0)
1146 { boost::langbinding::register_conversion<To, T>(true); }
1149 void register_downcast(boost::mpl::false_, detail::type<To>* = 0)
1152 // this function generates conversion information
1153 // in the given class_rep structure. It will be able
1154 // to implicitly cast to the given template type
1156 void gen_base_info(detail::type
<To
>)
1158 // boost::langbinding::register_dynamic_id<To>();
1159 // boost::langbinding::register_conversion<T, To>(false);
1161 // register_downcast<To>(boost::mpl::bool_<boost::is_polymorphic<To>::value>());
1163 // fist, make sure the given base class is registered.
1164 // if it's not registered we can't push it's lua table onto
1165 // the stack because it doesn't have a table
1167 // try to cast this type to the base type and remember
1168 // the pointer offset. For multiple inheritance the pointer
1169 // may change when casting. Since we need to be able to
1170 // cast we need this pointer offset.
1171 // store the information in this class' base class-vector
1173 base
.type
= LUABIND_TYPEID(To
);
1174 base
.ptr_offset
= detail::ptr_offset(detail::type
<T
>(), detail::type
<To
>());
1178 void gen_base_info(detail::type
<detail::null_type
>)
1181 #define LUABIND_GEN_BASE_INFO(z, n, text) gen_base_info(detail::type<B##n>());
1183 template<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_BASES
, class B
)>
1184 void generate_baseclass_list(detail::type
<bases
<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_BASES
, B
)> >)
1186 BOOST_PP_REPEAT(LUABIND_MAX_BASES
, LUABIND_GEN_BASE_INFO
, _
)
1189 #undef LUABIND_GEN_BASE_INFO
1191 // this is the internal version of def() it is run from both overloads
1192 // of def. It has two versions, one where a contstructor is registered
1193 // and one where a function is registered
1194 template<class Policies
>
1195 struct internal_def_s
1198 static void apply(const char* name
, F f
, detail::class_base
* c
)
1200 // std::cout << "HeldType2: " << typeid(HeldType).name() << "\n";
1202 detail::overload_rep
o(f
, static_cast<Policies
*>(0));
1204 typedef LUABIND_MSVC_TYPENAME
detail::function_callback_s
<HeldType
,T
,F
,Policies
>::type call_t
;
1206 o
.set_match_fun(&detail::match_function_callback_s
<T
,F
,Policies
>::apply
);
1207 o
.call_fun
= boost::bind
<int>(call_t(f
), _1
, _2
);
1209 #ifndef LUABIND_NO_ERROR_CHECKING
1211 o
.set_sig_fun(&detail::get_member_signature
<F
>::apply
);
1215 c
->add_method(name
, o
);
1218 template<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
, class A
)>
1219 static void apply(constructor
<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
, A
)>, detail::class_base
* c
)
1221 // std::cout << "HeldType2: " << typeid(HeldType).name() << "\n";
1223 detail::construct_rep::overload_t o
;
1226 &detail::construct_class
<
1229 ,constructor
<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
, A
)>
1233 // if we have a WrappedType, we have to register it's constructor
1234 // but if it's null_type (no WrappedType) we should not register it
1235 detail::register_wrapped_type
<WrappedType
>::apply(o
,
1236 static_cast<const constructor
<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
, A
)>*>(0),
1237 static_cast<const Policies
*>(0));
1241 &detail::constructor_match
<
1242 constructor
<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
, A
)>
1247 #ifndef LUABIND_NO_ERROR_CHECKING
1249 o
.set_sig_fun(&detail::get_signature
<constructor
<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
, A
)> >::apply
);
1253 typedef constructor
<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
, A
)> con_t
;
1255 o
.set_arity(detail::calc_arity
<con_t::arity
>::apply(con_t(), static_cast<Policies
*>(0)));
1257 c
->add_constructor(o
);
1261 class_(lua_State
* L
, const char* name
): class_base(name
), m_L(L
) { init(); }
1262 class_(const char* name
): class_base(name
), m_L(0) { init(); }
1269 lua_pushvalue(m_L, LUA_GLOBALSINDEX);
1270 scope_stack::push(m_L);
1272 scope_stack::pop(m_L);
1277 class_
& def(const char* name
, F f
)
1279 internal_def_s
<detail::null_type
>::apply(name
, f
, this);
1283 template<class F
, class Policies
>
1284 class_
& def(const char* name
, F f
, const Policies
&)
1286 internal_def_s
<Policies
>::apply(name
, f
, this);
1290 template<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
, class A
)>
1291 class_
& def(constructor
<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
, A
)> sig
)
1293 internal_def_s
<detail::null_type
>::apply(sig
, this);
1297 template<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
, class A
), class Policies
>
1298 class_
& def(constructor
<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY
, A
)> sig
, const Policies
& policies
)
1300 internal_def_s
<Policies
>::apply(sig
, this);
1304 template<class Getter
>
1305 class_
& property(const char* name
, Getter g
)
1307 add_getter(name
, boost::bind
<int>(detail::get_caller
<T
, Getter
, detail::null_type
>(), _1
, _2
, g
));
1311 template<class Getter
, class MaybeSetter
>
1312 class_
& property(const char* name
, Getter g
, MaybeSetter s
)
1314 return property_impl(name
, g
, s
, boost::mpl::bool_
<detail::is_policy_cons
<MaybeSetter
>::value
>());
1317 template<class Getter
, class Setter
, class GetPolicies
>
1318 class_
& property(const char* name
, Getter g
, Setter s
, const GetPolicies
& get_policies
)
1320 add_getter(name
, boost::bind
<int>(detail::get_caller
<T
, Getter
, GetPolicies
>(get_policies
), _1
, _2
, g
));
1321 #ifndef LUABIND_NO_ERROR_CHECKING
1324 , boost::bind
<int>(detail::set_caller
<T
, Setter
, detail::null_type
>(), _1
, _2
, s
)
1325 , detail::gen_set_matcher((Setter
)0, (detail::null_type
*)0)
1326 , &detail::get_member_signature
<Setter
>::apply
);
1330 , boost::bind
<int>(detail::set_caller
<T
, Setter
, detail::null_type
>(), _1
, _2
, s
));
1335 template<class Getter
, class Setter
, class GetPolicies
, class SetPolicies
>
1336 class_
& property(const char* name
1337 , Getter g
, Setter s
1338 , const GetPolicies
& get_policies
1339 , const SetPolicies
& set_policies
)
1341 add_getter(name
, boost::bind
<int>(detail::get_caller
<T
, Getter
, GetPolicies
>(get_policies
), _1
, _2
, g
));
1342 #ifndef LUABIND_NO_ERROR_CHECKING
1345 , boost::bind
<int>(detail::set_caller
<T
, Setter
, SetPolicies
>(), _1
, _2
, s
)
1346 , detail::gen_set_matcher((Setter
)0, (SetPolicies
*)0)
1347 , &detail::get_member_signature
<Setter
>::apply
);
1349 add_setter(name
, boost::bind
<int>(detail::set_caller
<T
, Setter
, SetPolicies
>(set_policies
), _1
, _2
, s
));
1355 class_
& def_readonly(const char* name
, D
T::*member_ptr
)
1357 add_getter(name
, boost::bind
<int>(detail::auto_get
<T
,D
,detail::null_type
>(), _1
, _2
, member_ptr
));
1361 template<class D
, class Policies
>
1362 class_
& def_readonly(const char* name
, D
T::*member_ptr
, const Policies
& policies
)
1364 add_getter(name
, boost::bind
<int>(detail::auto_get
<T
,D
,Policies
>(policies
), _1
, _2
, member_ptr
));
1369 class_
& def_readwrite(const char* name
, D
T::*member_ptr
)
1371 add_getter(name
, boost::bind
<int>(detail::auto_get
<T
,D
,detail::null_type
>(), _1
, _2
, member_ptr
));
1372 #ifndef LUABIND_NO_ERROR_CHECKING
1375 , boost::bind
<int>(detail::auto_set
<T
,D
,detail::null_type
>(), _1
, _2
, member_ptr
)
1376 , &detail::set_matcher
<D
, detail::null_type
>::apply
1377 , &detail::get_setter_signature
<D
>::apply
);
1379 add_setter(name
, boost::bind
<int>(detail::auto_set
<T
,D
,detail::null_type
>(), _1
, _2
, member_ptr
));
1384 template<class D
, class GetPolicies
>
1385 class_
& def_readwrite(const char* name
, D
T::*member_ptr
, const GetPolicies
& get_policies
)
1387 add_getter(name
, boost::bind
<int>(detail::auto_get
<T
,D
,GetPolicies
>(get_policies
), _1
, _2
, member_ptr
));
1388 #ifndef LUABIND_NO_ERROR_CHECKING
1391 , boost::bind
<int>(detail::auto_set
<T
,D
,detail::null_type
>(), _1
, _2
, member_ptr
)
1392 , &detail::set_matcher
<D
, detail::null_type
>::apply
1393 , &detail::get_setter_signature
<D
>::apply
);
1395 add_setter(name
, boost::bind
<int>(detail::auto_set
<T
,D
,detail::null_type
>(), _1
, _2
, member_ptr
));
1400 template<class D
, class GetPolicies
, class SetPolicies
>
1401 class_
& def_readwrite(const char* name
, D
T::*member_ptr
, const GetPolicies
& get_policies
, const SetPolicies
& set_policies
)
1403 add_getter(name
, boost::bind
<int>(detail::auto_get
<T
,D
,GetPolicies
>(get_policies
), _1
, _2
, member_ptr
));
1404 #ifndef LUABIND_NO_ERROR_CHECKING
1407 , boost::bind
<int>(detail::auto_set
<T
,D
,SetPolicies
>(), _1
, _2
, member_ptr
)
1408 , &detail::set_matcher
<D
, SetPolicies
>::apply
1409 , &detail::get_setter_signature
<D
>::apply
);
1411 add_setter(name
, boost::bind
<int>(detail::auto_set
<T
,D
,SetPolicies
>(set_policies
), _1
, _2
, member_ptr
));
1416 template<class op_id
, class Left
, class Right
, class Policies
>
1417 class_
& def(detail::operator_
<op_id
, Left
, Right
>, const Policies
& policies
)
1419 typedef typename
detail::operator_unwrapper
<Policies
, op_id
, T
, Left
, Right
> op_type
;
1420 #ifndef LUABIND_NO_ERROR_CHECKING
1421 add_operator(op_type::get_id()
1424 , &detail::get_signature
<constructor
<typename
op_type::left_t
, typename
op_type::right_t
> >::apply
1425 , detail::is_unary(op_type::get_id()) ? 1 : 2);
1427 add_operator(op_type::get_id()
1430 , detail::is_unary(op_type::get_id()) ? 1 : 2);
1435 template<class op_id
, class Left
, class Right
>
1436 class_
& def(detail::operator_
<op_id
, Left
, Right
>)
1438 typedef typename
detail::operator_unwrapper
<detail::null_type
, op_id
, T
, Left
, Right
> op_type
;
1440 #ifndef LUABIND_NO_ERROR_CHECKING
1441 add_operator(op_type::get_id()
1444 , &detail::get_signature
<constructor
<LUABIND_MSVC_TYPENAME
op_type::left_t
, LUABIND_MSVC_TYPENAME
op_type::right_t
> >::apply
1445 , detail::is_unary(op_type::get_id()) ? 1 : 2);
1447 add_operator(op_type::get_id()
1450 , detail::is_unary(op_type::get_id()) ? 1 : 2);
1455 template<class Signature
, bool Constant
>
1456 class_
& def(detail::application_operator
<Signature
, Constant
>*)
1458 typedef detail::application_operator
<Signature
, Constant
, detail::null_type
> op_t
;
1460 int arity
= detail::calc_arity
<Signature::arity
>::apply(Signature(), static_cast<detail::null_type
*>(0));
1462 #ifndef LUABIND_NO_ERROR_CHECKING
1463 add_operator(detail::op_call
, &op_t::template apply
<T
>::execute
, &op_t::match
, &detail::get_signature
<Signature
>::apply
, arity
+ 1);
1465 add_operator(detail::op_call
, &op_t::template apply
<T
>::execute
, &op_t::match
, arity
+ 1);
1471 template<class Signature
, bool Constant
, class Policies
>
1472 class_
& def(detail::application_operator
<Signature
, Constant
>*, const Policies
& policies
)
1474 typedef detail::application_operator
<Signature
, Constant
, Policies
> op_t
;
1476 int arity
= detail::calc_arity
<Signature::arity
>::apply(Signature(), static_cast<Policies
*>(0));
1478 #ifndef LUABIND_NO_ERROR_CHECKING
1479 add_operator(detail::op_call
, &op_t::template apply
<T
>::execute
, &op_t::match
, &detail::get_signature
<Signature
>::apply
, arity
+ 1);
1481 add_operator(detail::op_call
, &op_t::template apply
<T
>::execute
, &op_t::match
, arity
+ 1);
1487 detail::enum_maker
<self_t
> enum_(const char*)
1489 return detail::enum_maker
<self_t
>(*this);
1496 typedef typename
detail::extract_parameter
<
1497 boost::mpl::list3
<X1
,X2
,X3
>
1499 detail::is_bases
<boost::mpl::_
>
1500 , boost::is_base_and_derived
<boost::mpl::_
, T
>
1506 boost::mpl::if_
<detail::is_bases
<bases_t
>
1511 class_base::init(LUABIND_TYPEID(T
)
1512 , detail::internal_holder_type
<HeldType
>::apply()
1513 , detail::pointee_typeid(
1514 get_const_holder(static_cast<HeldType
*>(0)))
1515 , detail::internal_holder_extractor
<HeldType
>::apply(detail::type
<T
>())
1516 , detail::internal_const_holder_extractor
<HeldType
>::apply(detail::type
<T
>())
1517 , detail::const_converter
<HeldType
>::apply(
1518 luabind::get_const_holder((HeldType
*)0))
1519 , detail::holder_constructor
<HeldType
>::apply(detail::type
<T
>())
1520 , detail::const_holder_constructor
<HeldType
>::apply(detail::type
<T
>())
1521 , detail::internal_holder_destructor
<HeldType
>::apply(detail::type
<T
>())
1522 , detail::internal_const_holder_destructor
<HeldType
>::apply(detail::type
<T
>())
1523 , detail::internal_holder_size
<HeldType
>::apply()
1524 , detail::get_holder_alignment
<HeldType
>::apply());
1526 generate_baseclass_list(detail::type
<Base
>());
1529 template<class Getter
, class GetPolicies
>
1530 class_
& property_impl(const char* name
,
1532 GetPolicies policies
,
1533 boost::mpl::bool_
<true>)
1535 add_getter(name
, boost::bind
<int>(detail::get_caller
<T
,Getter
,GetPolicies
>(policies
), _1
, _2
, g
));
1539 template<class Getter
, class Setter
>
1540 class_
& property_impl(const char* name
,
1543 boost::mpl::bool_
<false>)
1545 add_getter(name
, boost::bind
<int>(detail::get_caller
<T
,Getter
,detail::null_type
>(), _1
, _2
, g
));
1546 #ifndef LUABIND_NO_ERROR_CHECKING
1549 , boost::bind
<int>(detail::set_caller
<T
, Setter
, detail::null_type
>(), _1
, _2
, s
)
1550 , detail::gen_set_matcher((Setter
)0, (detail::null_type
*)0)
1551 , &detail::get_member_signature
<Setter
>::apply
);
1553 add_setter(name
, boost::bind
<int>(detail::set_caller
<T
,Setter
,detail::null_type
>(), _1
, _2
, s
));
1561 #endif // LUABIND_CLASS_HPP_INCLUDED