Store main thread lua_State* in shared_ptr_deleter.
[luabind.git] / luabind / class.hpp
blobe83e5d59e940d573eb89806a164bcb507ccfba21
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
28 ISSUES:
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.
52 TODO:
53 ------------------------------------------------------
55 finish smart pointer support
56 * the adopt policy should not be able to adopt pointers to held_types. This
57 must be prohibited.
58 * name_of_type must recognize holder_types and not return "custom"
60 document custom policies, custom converters
62 store the instance object for policies.
64 support the __concat metamethod. This is a bit tricky, since it cannot be
65 treated as a normal operator. It is a binary operator but we want to use the
66 __tostring implementation for both arguments.
70 #include <luabind/prefix.hpp>
71 #include <luabind/config.hpp>
73 #include <string>
74 #include <map>
75 #include <vector>
76 #include <cassert>
78 #include <boost/bind.hpp>
79 #include <boost/preprocessor/repetition/enum_params.hpp>
80 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
81 #include <boost/preprocessor/repetition/repeat.hpp>
82 #include <boost/type_traits/is_same.hpp>
83 #include <boost/type_traits/is_member_object_pointer.hpp>
84 #include <boost/mpl/apply.hpp>
85 #include <boost/mpl/lambda.hpp>
86 #include <boost/mpl/logical.hpp>
87 #include <boost/mpl/find_if.hpp>
88 #include <boost/mpl/eval_if.hpp>
89 #include <boost/mpl/logical.hpp>
91 #include <luabind/config.hpp>
92 #include <luabind/scope.hpp>
93 #include <luabind/back_reference.hpp>
94 #include <luabind/function.hpp>
95 #include <luabind/dependency_policy.hpp>
96 #include <luabind/detail/constructor.hpp>
97 #include <luabind/detail/call.hpp>
98 #include <luabind/detail/deduce_signature.hpp>
99 #include <luabind/detail/compute_score.hpp>
100 #include <luabind/detail/primitives.hpp>
101 #include <luabind/detail/property.hpp>
102 #include <luabind/detail/typetraits.hpp>
103 #include <luabind/detail/class_rep.hpp>
104 #include <luabind/detail/call.hpp>
105 #include <luabind/detail/object_rep.hpp>
106 #include <luabind/detail/calc_arity.hpp>
107 #include <luabind/detail/call_member.hpp>
108 #include <luabind/detail/enum_maker.hpp>
109 #include <luabind/detail/get_signature.hpp>
110 #include <luabind/detail/implicit_cast.hpp>
111 #include <luabind/detail/operator_id.hpp>
112 #include <luabind/detail/pointee_typeid.hpp>
113 #include <luabind/detail/link_compatibility.hpp>
114 #include <luabind/detail/inheritance.hpp>
115 #include <luabind/typeid.hpp>
117 // to remove the 'this' used in initialization list-warning
118 #ifdef _MSC_VER
119 #pragma warning(push)
120 #pragma warning(disable: 4355)
121 #endif
123 namespace boost
126 template <class T> class shared_ptr;
128 } // namespace boost
130 namespace luabind
132 namespace detail
134 struct unspecified {};
136 template<class Derived> struct operator_;
138 struct you_need_to_define_a_get_const_holder_function_for_your_smart_ptr {};
141 template<class T, class X1 = detail::unspecified, class X2 = detail::unspecified, class X3 = detail::unspecified>
142 struct class_;
144 // TODO: this function will only be invoked if the user hasn't defined a correct overload
145 // maybe we should have a static assert in here?
146 inline detail::you_need_to_define_a_get_const_holder_function_for_your_smart_ptr*
147 get_const_holder(...)
149 return 0;
152 template <class T>
153 boost::shared_ptr<T const>* get_const_holder(boost::shared_ptr<T>*)
155 return 0;
158 template <
159 BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(
160 LUABIND_MAX_BASES, class A, detail::null_type)
162 struct bases
165 typedef bases<detail::null_type> no_bases;
167 namespace detail
169 template <class T>
170 struct is_bases
171 : mpl::false_
174 template <BOOST_PP_ENUM_PARAMS(LUABIND_MAX_BASES, class A)>
175 struct is_bases<bases<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_BASES, A)> >
176 : mpl::true_
179 template <class T, class P>
180 struct is_unspecified
181 : mpl::apply1<P, T>
184 template <class P>
185 struct is_unspecified<unspecified, P>
186 : mpl::true_
189 template <class P>
190 struct is_unspecified_mfn
192 template <class T>
193 struct apply
194 : is_unspecified<T, P>
198 template<class Predicate>
199 struct get_predicate
201 typedef mpl::protect<is_unspecified_mfn<Predicate> > type;
204 template <class Result, class Default>
205 struct result_or_default
207 typedef Result type;
210 template <class Default>
211 struct result_or_default<unspecified, Default>
213 typedef Default type;
216 template<class Parameters, class Predicate, class DefaultValue>
217 struct extract_parameter
219 typedef typename get_predicate<Predicate>::type pred;
220 typedef typename boost::mpl::find_if<Parameters, pred>::type iterator;
221 typedef typename result_or_default<
222 typename iterator::type, DefaultValue
223 >::type type;
226 // prints the types of the values on the stack, in the
227 // range [start_index, lua_gettop()]
229 LUABIND_API std::string stack_content_by_name(lua_State* L, int start_index);
231 struct LUABIND_API create_class
233 static int stage1(lua_State* L);
234 static int stage2(lua_State* L);
237 } // detail
239 namespace detail {
241 template<class T>
242 struct static_scope
244 static_scope(T& self_) : self(self_)
248 T& operator[](scope s) const
250 self.add_inner_scope(s);
251 return self;
254 private:
255 template<class U> void operator,(U const&) const;
256 void operator=(static_scope const&);
258 T& self;
261 struct class_registration;
263 struct LUABIND_API class_base : scope
265 public:
266 class_base(char const* name);
268 struct base_desc
270 type_id type;
271 int ptr_offset;
274 void init(
275 type_id const& type, class_id id
276 , type_id const& wrapped_type, class_id wrapper_id);
278 void add_base(type_id const& base, cast_function cast);
280 void add_member(registration* member);
281 void add_default_member(registration* member);
283 const char* name() const;
285 void add_static_constant(const char* name, int val);
286 void add_inner_scope(scope& s);
288 void add_cast(class_id src, class_id target, cast_function cast);
290 private:
291 class_registration* m_registration;
294 // MSVC complains about member being sensitive to alignment (C4121)
295 // when F is a pointer to member of a class with virtual bases.
296 # ifdef BOOST_MSVC
297 # pragma pack(push)
298 # pragma pack(16)
299 # endif
301 template <class Class, class F, class Policies>
302 struct memfun_registration : registration
304 memfun_registration(char const* name, F f, Policies const& policies)
305 : name(name)
306 , f(f)
307 , policies(policies)
310 void register_(lua_State* L) const
312 object fn = make_function(
313 L, f, deduce_signature(f, (Class*)0), policies);
315 add_overload(
316 object(from_stack(L, -1))
317 , name
318 , fn
322 char const* name;
323 F f;
324 Policies policies;
327 # ifdef BOOST_MSVC
328 # pragma pack(pop)
329 # endif
331 template <class P, class T>
332 struct default_pointer
334 typedef P type;
337 template <class T>
338 struct default_pointer<null_type, T>
340 typedef std::auto_ptr<T> type;
343 template <class Class, class Pointer, class Signature, class Policies>
344 struct constructor_registration : registration
346 constructor_registration(Policies const& policies)
347 : policies(policies)
350 void register_(lua_State* L) const
352 typedef typename default_pointer<Pointer, Class>::type pointer;
354 object fn = make_function(
356 , construct<Class, pointer, Signature>(), Signature()
357 , policies
360 add_overload(
361 object(from_stack(L, -1))
362 , "__init"
363 , fn
367 Policies policies;
370 template <class T>
371 struct reference_result
372 : mpl::if_<
373 mpl::or_<boost::is_pointer<T>, is_primitive<T> >
375 , typename boost::add_reference<T>::type
379 template <class T, class Policies>
380 struct inject_dependency_policy
381 : mpl::if_<
382 is_primitive<T>
383 , Policies
384 , policy_cons<dependency_policy<0, 1>, Policies>
388 template <
389 class Class
390 , class Get, class GetPolicies
391 , class Set = null_type, class SetPolicies = null_type
393 struct property_registration : registration
395 property_registration(
396 char const* name
397 , Get const& get
398 , GetPolicies const& get_policies
399 , Set const& set = Set()
400 , SetPolicies const& set_policies = SetPolicies()
402 : name(name)
403 , get(get)
404 , get_policies(get_policies)
405 , set(set)
406 , set_policies(set_policies)
409 void register_(lua_State* L) const
411 object context(from_stack(L, -1));
412 register_aux(
414 , context
415 , make_get(L, get, boost::is_member_object_pointer<Get>())
416 , set
420 template <class F>
421 object make_get(lua_State* L, F const& f, mpl::false_) const
423 return make_function(
424 L, f, deduce_signature(f, (Class*)0), get_policies);
427 template <class T, class D>
428 object make_get(lua_State* L, D T::* mem_ptr, mpl::true_) const
430 typedef typename reference_result<D>::type result_type;
431 typedef typename inject_dependency_policy<
432 D, GetPolicies>::type policies;
434 return make_function(
436 , access_member_ptr<T, D, result_type>(mem_ptr)
437 , mpl::vector2<result_type, Class const&>()
438 , policies()
442 template <class F>
443 object make_set(lua_State* L, F const& f, mpl::false_) const
445 return make_function(
446 L, f, deduce_signature(f, (Class*)0), set_policies);
449 template <class T, class D>
450 object make_set(lua_State* L, D T::* mem_ptr, mpl::true_) const
452 return make_function(
454 , access_member_ptr<T, D>(mem_ptr)
455 , mpl::vector3<void, Class&, D const&>()
456 , set_policies
460 template <class S>
461 void register_aux(
462 lua_State* L, object const& context
463 , object const& get_, S const&) const
465 context[name] = property(
466 get_
467 , make_set(L, set, boost::is_member_object_pointer<Set>())
471 void register_aux(
472 lua_State*, object const& context
473 , object const& get_, null_type) const
475 context[name] = property(get_);
478 char const* name;
479 Get get;
480 GetPolicies get_policies;
481 Set set;
482 SetPolicies set_policies;
485 } // namespace detail
487 // registers a class in the lua environment
488 template<class T, class X1, class X2, class X3>
489 struct class_: detail::class_base
491 typedef class_<T, X1, X2, X3> self_t;
493 private:
495 template<class A, class B, class C, class D>
496 class_(const class_<A,B,C,D>&);
498 public:
500 typedef boost::mpl::vector4<X1, X2, X3, detail::unspecified> parameters_type;
502 // WrappedType MUST inherit from T
503 typedef typename detail::extract_parameter<
504 parameters_type
505 , boost::is_base_and_derived<T, boost::mpl::_>
506 , detail::null_type
507 >::type WrappedType;
509 typedef typename detail::extract_parameter<
510 parameters_type
511 , boost::mpl::not_<
512 boost::mpl::or_<
513 detail::is_bases<boost::mpl::_>
514 , boost::is_base_and_derived<boost::mpl::_, T>
515 , boost::is_base_and_derived<T, boost::mpl::_>
518 , detail::null_type
519 >::type HeldType;
521 template <class Src, class Target>
522 void add_downcast(Src*, Target*, boost::mpl::true_)
524 add_cast(
525 detail::registered_class<Src>::id
526 , detail::registered_class<Target>::id
527 , detail::dynamic_cast_<Src, Target>::execute
531 template <class Src, class Target>
532 void add_downcast(Src*, Target*, boost::mpl::false_)
535 // this function generates conversion information
536 // in the given class_rep structure. It will be able
537 // to implicitly cast to the given template type
538 template<class To>
539 void gen_base_info(detail::type_<To>)
541 add_base(typeid(To), detail::static_cast_<T, To>::execute);
542 add_cast(
543 detail::registered_class<T>::id
544 , detail::registered_class<To>::id
545 , detail::static_cast_<T, To>::execute
548 add_downcast((To*)0, (T*)0, boost::is_polymorphic<To>());
551 void gen_base_info(detail::type_<detail::null_type>)
554 #define LUABIND_GEN_BASE_INFO(z, n, text) gen_base_info(detail::type_<BaseClass##n>());
556 template<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_BASES, class BaseClass)>
557 void generate_baseclass_list(detail::type_<bases<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_BASES, BaseClass)> >)
559 BOOST_PP_REPEAT(LUABIND_MAX_BASES, LUABIND_GEN_BASE_INFO, _)
562 #undef LUABIND_GEN_BASE_INFO
564 class_(const char* name): class_base(name), scope(*this)
566 #ifndef NDEBUG
567 detail::check_link_compatibility();
568 #endif
569 init();
572 template<class F>
573 class_& def(const char* name, F f)
575 return this->virtual_def(
576 name, f, detail::null_type()
577 , detail::null_type(), boost::mpl::true_());
580 // virtual functions
581 template<class F, class DefaultOrPolicies>
582 class_& def(char const* name, F fn, DefaultOrPolicies default_or_policies)
584 return this->virtual_def(
585 name, fn, default_or_policies, detail::null_type()
586 , LUABIND_MSVC_TYPENAME detail::is_policy_cons<DefaultOrPolicies>::type());
589 template<class F, class Default, class Policies>
590 class_& def(char const* name, F fn
591 , Default default_, Policies const& policies)
593 return this->virtual_def(
594 name, fn, default_
595 , policies, boost::mpl::false_());
598 template<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY, class A)>
599 class_& def(constructor<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY, A)> sig)
601 return this->def_constructor(&sig, detail::null_type());
604 template<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY, class A), class Policies>
605 class_& def(constructor<BOOST_PP_ENUM_PARAMS(LUABIND_MAX_ARITY, A)> sig, const Policies& policies)
607 return this->def_constructor(&sig, policies);
610 template <class Getter>
611 class_& property(const char* name, Getter g)
613 this->add_member(
614 new detail::property_registration<T, Getter, detail::null_type>(
615 name, g, detail::null_type()));
616 return *this;
619 template <class Getter, class MaybeSetter>
620 class_& property(const char* name, Getter g, MaybeSetter s)
622 return property_impl(
623 name, g, s
624 , boost::mpl::bool_<detail::is_policy_cons<MaybeSetter>::value>()
628 template<class Getter, class Setter, class GetPolicies>
629 class_& property(const char* name, Getter g, Setter s, const GetPolicies& get_policies)
631 typedef detail::property_registration<
632 T, Getter, GetPolicies, Setter, detail::null_type
633 > registration_type;
635 this->add_member(
636 new registration_type(name, g, get_policies, s));
637 return *this;
640 template<class Getter, class Setter, class GetPolicies, class SetPolicies>
641 class_& property(
642 const char* name
643 , Getter g, Setter s
644 , GetPolicies const& get_policies
645 , SetPolicies const& set_policies)
647 typedef detail::property_registration<
648 T, Getter, GetPolicies, Setter, SetPolicies
649 > registration_type;
651 this->add_member(
652 new registration_type(name, g, get_policies, s, set_policies));
653 return *this;
656 template <class C, class D>
657 class_& def_readonly(const char* name, D C::*mem_ptr)
659 typedef detail::property_registration<T, D C::*, detail::null_type>
660 registration_type;
662 this->add_member(
663 new registration_type(name, mem_ptr, detail::null_type()));
664 return *this;
667 template <class C, class D, class Policies>
668 class_& def_readonly(const char* name, D C::*mem_ptr, Policies const& policies)
670 typedef detail::property_registration<T, D C::*, Policies>
671 registration_type;
673 this->add_member(
674 new registration_type(name, mem_ptr, policies));
675 return *this;
678 template <class C, class D>
679 class_& def_readwrite(const char* name, D C::*mem_ptr)
681 typedef detail::property_registration<
682 T, D C::*, detail::null_type, D C::*
683 > registration_type;
685 this->add_member(
686 new registration_type(
687 name, mem_ptr, detail::null_type(), mem_ptr));
688 return *this;
691 template <class C, class D, class GetPolicies>
692 class_& def_readwrite(
693 const char* name, D C::*mem_ptr, GetPolicies const& get_policies)
695 typedef detail::property_registration<
696 T, D C::*, GetPolicies, D C::*
697 > registration_type;
699 this->add_member(
700 new registration_type(
701 name, mem_ptr, get_policies, mem_ptr));
702 return *this;
705 template <class C, class D, class GetPolicies, class SetPolicies>
706 class_& def_readwrite(
707 const char* name
708 , D C::*mem_ptr
709 , GetPolicies const& get_policies
710 , SetPolicies const& set_policies
713 typedef detail::property_registration<
714 T, D C::*, GetPolicies, D C::*, SetPolicies
715 > registration_type;
717 this->add_member(
718 new registration_type(
719 name, mem_ptr, get_policies, mem_ptr, set_policies));
720 return *this;
723 template<class Derived, class Policies>
724 class_& def(detail::operator_<Derived>, Policies const& policies)
726 return this->def(
727 Derived::name()
728 , &Derived::template apply<T, Policies>::execute
729 , policies
733 template<class Derived>
734 class_& def(detail::operator_<Derived>)
736 return this->def(
737 Derived::name()
738 , &Derived::template apply<T, detail::null_type>::execute
742 detail::enum_maker<self_t> enum_(const char*)
744 return detail::enum_maker<self_t>(*this);
747 detail::static_scope<self_t> scope;
749 private:
750 void operator=(class_ const&);
752 void add_wrapper_cast(detail::null_type*)
755 template <class U>
756 void add_wrapper_cast(U*)
758 add_cast(
759 detail::registered_class<U>::id
760 , detail::registered_class<T>::id
761 , detail::static_cast_<U,T>::execute
764 add_downcast((T*)0, (U*)0, boost::is_polymorphic<T>());
767 void init()
769 typedef typename detail::extract_parameter<
770 parameters_type
771 , boost::mpl::or_<
772 detail::is_bases<boost::mpl::_>
773 , boost::is_base_and_derived<boost::mpl::_, T>
775 , no_bases
776 >::type bases_t;
778 typedef typename
779 boost::mpl::if_<detail::is_bases<bases_t>
780 , bases_t
781 , bases<bases_t>
782 >::type Base;
784 class_base::init(
785 typeid(T)
786 , detail::registered_class<T>::id
787 , typeid(WrappedType)
788 , detail::registered_class<WrappedType>::id
791 add_wrapper_cast((WrappedType*)0);
793 generate_baseclass_list(detail::type_<Base>());
796 template<class Getter, class GetPolicies>
797 class_& property_impl(const char* name,
798 Getter g,
799 GetPolicies policies,
800 boost::mpl::bool_<true>)
802 this->add_member(
803 new detail::property_registration<T, Getter, GetPolicies>(
804 name, g, policies));
805 return *this;
808 template<class Getter, class Setter>
809 class_& property_impl(const char* name,
810 Getter g,
811 Setter s,
812 boost::mpl::bool_<false>)
814 typedef detail::property_registration<
815 T, Getter, detail::null_type, Setter, detail::null_type
816 > registration_type;
818 this->add_member(
819 new registration_type(name, g, detail::null_type(), s));
820 return *this;
823 // these handle default implementation of virtual functions
824 template<class F, class Policies>
825 class_& virtual_def(char const* name, F const& fn
826 , Policies const&, detail::null_type, boost::mpl::true_)
828 this->add_member(
829 new detail::memfun_registration<T, F, Policies>(
830 name, fn, Policies()));
831 return *this;
834 template<class F, class Default, class Policies>
835 class_& virtual_def(char const* name, F const& fn
836 , Default const& default_, Policies const&, boost::mpl::false_)
838 this->add_member(
839 new detail::memfun_registration<T, F, Policies>(
840 name, fn, Policies()));
842 this->add_default_member(
843 new detail::memfun_registration<T, Default, Policies>(
844 name, default_, Policies()));
846 return *this;
849 template<class Signature, class Policies>
850 class_& def_constructor(Signature*, Policies const&)
852 typedef typename Signature::signature signature;
854 typedef typename boost::mpl::if_<
855 boost::is_same<WrappedType, detail::null_type>
857 , WrappedType
858 >::type construct_type;
860 this->add_member(
861 new detail::constructor_registration<
862 construct_type, HeldType, signature, Policies>(
863 Policies()));
865 this->add_default_member(
866 new detail::constructor_registration<
867 construct_type, HeldType, signature, Policies>(
868 Policies()));
870 return *this;
876 #ifdef _MSC_VER
877 #pragma warning(pop)
878 #endif
880 #endif // LUABIND_CLASS_HPP_INCLUDED