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_POLICY_HPP_INCLUDED
25 #define LUABIND_POLICY_HPP_INCLUDED
27 #include <luabind/config.hpp>
33 #include <boost/type_traits/is_enum.hpp>
34 #include <boost/type_traits/is_array.hpp>
35 #include <boost/mpl/bool.hpp>
36 #include <boost/mpl/integral_c.hpp>
37 #include <boost/mpl/equal_to.hpp>
38 #include <boost/mpl/eval_if.hpp>
39 #include <boost/mpl/or.hpp>
40 #include <boost/type_traits/add_reference.hpp>
41 #include <boost/type_traits/remove_reference.hpp>
42 #include <boost/type_traits/is_pointer.hpp>
43 #include <boost/type_traits/is_base_and_derived.hpp>
44 #include <boost/bind/arg.hpp>
45 #include <boost/limits.hpp>
46 #include <boost/tuple/tuple.hpp>
47 #include <boost/version.hpp>
49 #include <luabind/detail/class_registry.hpp>
50 #include <luabind/detail/primitives.hpp>
51 #include <luabind/detail/object_rep.hpp>
52 #include <luabind/detail/typetraits.hpp>
53 #include <luabind/detail/class_cache.hpp>
54 #include <luabind/detail/debug.hpp>
55 #include <luabind/detail/class_rep.hpp>
56 #include <luabind/detail/has_get_pointer.hpp>
57 #include <luabind/detail/make_instance.hpp>
59 #include <boost/type_traits/add_reference.hpp>
61 #include <luabind/detail/decorate_type.hpp>
62 #include <luabind/weak_ref.hpp>
63 #include <luabind/back_reference_fwd.hpp>
65 #include <luabind/value_wrapper.hpp>
66 #include <luabind/from_stack.hpp>
67 #include <luabind/typeid.hpp>
73 struct conversion_policy_base
{};
76 template<int N
, bool HasArg
= true>
77 struct conversion_policy
: detail::conversion_policy_base
79 BOOST_STATIC_CONSTANT(int, index
= N
);
80 BOOST_STATIC_CONSTANT(bool, has_arg
= HasArg
);
86 index_map(const int* m
): m_map(m
) {}
88 int operator[](int index
) const
97 // template<class T> class functor;
101 namespace luabind
{ namespace detail
103 template<class H
, class T
>
110 policy_cons
<U
, policy_cons
<H
,T
> > operator,(policy_cons
<U
,detail::null_type
>)
112 return policy_cons
<U
, policy_cons
<H
,T
> >();
116 policy_cons
<U
, policy_cons
<H
,T
> > operator+(policy_cons
<U
,detail::null_type
>)
118 return policy_cons
<U
, policy_cons
<H
,T
> >();
122 policy_cons
<U
, policy_cons
<H
,T
> > operator|(policy_cons
<U
,detail::null_type
>)
124 return policy_cons
<U
, policy_cons
<H
,T
> >();
128 struct indirection_layer
131 indirection_layer(const T
&);
134 yes_t
is_policy_cons_test(const null_type
&);
135 template<class H
, class T
>
136 yes_t
is_policy_cons_test(const policy_cons
<H
,T
>&);
137 no_t
is_policy_cons_test(...);
140 struct is_policy_cons
144 BOOST_STATIC_CONSTANT(bool, value
=
145 sizeof(is_policy_cons_test(t
)) == sizeof(yes_t
));
147 typedef boost::mpl::bool_
<value
> type
;
151 struct is_string_literal
153 static no_t
helper(indirection_layer
);
154 static yes_t
helper(const char*);
158 struct is_string_literal
<false>
160 static no_t
helper(indirection_layer
);
164 namespace mpl
= boost::mpl
;
166 template <class T
, class Clone
>
167 void make_pointee_instance(lua_State
* L
, T
& x
, mpl::true_
, Clone
)
180 void make_pointee_instance(lua_State
* L
, T
& x
, mpl::false_
, mpl::true_
)
182 std::auto_ptr
<T
> ptr(new T(x
));
183 make_instance(L
, ptr
);
187 void make_pointee_instance(lua_State
* L
, T
& x
, mpl::false_
, mpl::false_
)
189 make_instance(L
, &x
);
192 template <class T
, class Clone
>
193 void make_pointee_instance(lua_State
* L
, T
& x
, Clone
)
195 make_pointee_instance(L
, x
, has_get_pointer
<T
>(), Clone());
198 // ********** pointer converter ***********
200 struct pointer_converter
202 typedef pointer_converter type
;
203 typedef mpl::false_ is_native
;
211 int const consumed_args(...)
217 void apply(lua_State
* L
, T
* ptr
)
225 if (luabind::get_back_reference(L
, ptr
))
228 make_instance(L
, ptr
);
232 T
* apply(lua_State
*, by_pointer
<T
>, int)
234 return static_cast<T
*>(result
);
238 int match(lua_State
* L
, by_pointer
<T
>, int index
)
240 if (lua_isnil(L
, index
)) return 0;
241 object_rep
* obj
= get_instance(L
, index
);
242 if (obj
== 0) return -1;
247 std::pair
<void*, int> s
= obj
->get_instance(registered_class
<T
>::id
);
253 void converter_postcall(lua_State
*, by_pointer
<T
>, int)
257 // ******* value converter *******
259 struct value_converter
261 typedef value_converter type
;
262 typedef mpl::false_ is_native
;
264 int const consumed_args(...)
276 void apply(lua_State
* L
, T x
)
278 if (luabind::get_back_reference(L
, x
))
281 make_pointee_instance(L
, x
, mpl::true_());
285 T
apply(lua_State
*, by_value
<T
>, int)
287 return *static_cast<T
*>(result
);
291 int match(lua_State
* L
, by_value
<T
>, int index
)
293 // special case if we get nil in, try to match the holder type
294 if (lua_isnil(L
, index
))
297 object_rep
* obj
= get_instance(L
, index
);
298 if (obj
== 0) return -1;
300 std::pair
<void*, int> s
= obj
->get_instance(registered_class
<T
>::id
);
306 void converter_postcall(lua_State
*, T
, int) {}
309 // ******* const pointer converter *******
311 struct const_pointer_converter
313 typedef const_pointer_converter type
;
314 typedef mpl::false_ is_native
;
316 int const consumed_args(...)
321 const_pointer_converter()
328 void apply(lua_State
* L
, const T
* ptr
)
336 if (luabind::get_back_reference(L
, ptr
))
339 make_instance(L
, ptr
);
343 T
const* apply(lua_State
*, by_const_pointer
<T
>, int)
345 return static_cast<T
const*>(result
);
349 int match(lua_State
* L
, by_const_pointer
<T
>, int index
)
351 if (lua_isnil(L
, index
)) return 0;
352 object_rep
* obj
= get_instance(L
, index
);
353 if (obj
== 0) return -1; // if the type is not one of our own registered types, classify it as a non-match
354 std::pair
<void*, int> s
= obj
->get_instance(registered_class
<T
>::id
);
355 if (s
.second
>= 0 && !obj
->is_const())
362 void converter_postcall(lua_State
*, T
, int) {}
365 // ******* reference converter *******
367 struct ref_converter
: pointer_converter
369 typedef ref_converter type
;
370 typedef mpl::false_ is_native
;
372 int const consumed_args(...)
378 void apply(lua_State
* L
, T
& ref
)
380 if (luabind::get_back_reference(L
, ref
))
383 make_pointee_instance(L
, ref
, mpl::false_());
387 T
& apply(lua_State
* L
, by_reference
<T
>, int index
)
389 assert(!lua_isnil(L
, index
));
390 return *pointer_converter::apply(L
, by_pointer
<T
>(), index
);
394 int match(lua_State
* L
, by_reference
<T
>, int index
)
396 if (lua_isnil(L
, index
)) return -1;
397 return pointer_converter::match(L
, by_pointer
<T
>(), index
);
401 void converter_postcall(lua_State
*, T
, int) {}
404 // ******** const reference converter *********
406 struct const_ref_converter
408 typedef const_ref_converter type
;
409 typedef mpl::false_ is_native
;
411 int const consumed_args(...)
416 const_ref_converter()
423 void apply(lua_State
* L
, T
const& ref
)
425 if (luabind::get_back_reference(L
, ref
))
428 make_pointee_instance(L
, ref
, mpl::false_());
432 T
const& apply(lua_State
*, by_const_reference
<T
>, int)
434 return *static_cast<T
*>(result
);
438 int match(lua_State
* L
, by_const_reference
<T
>, int index
)
440 object_rep
* obj
= get_instance(L
, index
);
441 if (obj
== 0) return -1; // if the type is not one of our own registered types, classify it as a non-match
443 std::pair
<void*, int> s
= obj
->get_instance(registered_class
<T
>::id
);
444 if (s
.second
>= 0 && !obj
->is_const())
451 void converter_postcall(lua_State
*, by_const_reference
<T
>, int)
456 // ****** enum converter ********
458 struct enum_converter
460 typedef enum_converter type
;
461 typedef mpl::true_ is_native
;
463 int const consumed_args(...)
468 void apply(lua_State
* L
, int val
)
470 lua_pushnumber(L
, val
);
474 T
apply(lua_State
* L
, by_value
<T
>, int index
)
476 return static_cast<T
>(static_cast<int>(lua_tonumber(L
, index
)));
480 static int match(lua_State
* L
, by_value
<T
>, int index
)
482 if (lua_isnumber(L
, index
)) return 0; else return -1;
486 T
apply(lua_State
* L
, by_const_reference
<T
>, int index
)
488 return static_cast<T
>(static_cast<int>(lua_tonumber(L
, index
)));
492 static int match(lua_State
* L
, by_const_reference
<T
>, int index
)
494 if (lua_isnumber(L
, index
)) return 0; else return -1;
498 void converter_postcall(lua_State
*, T
, int) {}
502 struct value_wrapper_converter
504 typedef value_wrapper_converter
<U
> type
;
505 typedef mpl::true_ is_native
;
507 int const consumed_args(...)
513 T
apply(lua_State
* L
, by_const_reference
<T
>, int index
)
515 return T(from_stack(L
, index
));
519 T
apply(lua_State
* L
, by_value
<T
>, int index
)
521 return apply(L
, by_const_reference
<T
>(), index
);
525 static int match(lua_State
* L
, by_const_reference
<T
>, int index
)
527 return value_wrapper_traits
<T
>::check(L
, index
)
528 ? (std::numeric_limits
<int>::max
)() / LUABIND_MAX_ARITY
533 static int match(lua_State
* L
, by_value
<T
>, int index
)
535 return match(L
, by_const_reference
<T
>(), index
);
538 void converter_postcall(...) {}
541 void apply(lua_State
* interpreter
, T
const& value_wrapper
)
543 value_wrapper_traits
<T
>::unwrap(interpreter
, value_wrapper
);
548 struct default_converter_generator
550 is_value_wrapper_arg
<T
>
551 , value_wrapper_converter
<T
>
553 boost::is_enum
<typename
boost::remove_reference
<T
>::type
>
556 is_nonconst_pointer
<T
>
560 , const_pointer_converter
562 is_nonconst_reference
<T
>
565 is_const_reference
<T
>
566 , const_ref_converter
576 } // namespace detail
578 // *********** default_policy *****************
581 struct default_converter
582 : detail::default_converter_generator
<T
>::type
585 template <class T
, class Derived
= default_converter
<T
> >
586 struct native_converter_base
588 typedef boost::mpl::true_ is_native
;
590 int const consumed_args(...)
596 void converter_postcall(lua_State
*, U
const&, int)
599 int match(lua_State
* L
, detail::by_value
<T
>, int index
)
601 return derived().compute_score(L
, index
);
604 int match(lua_State
* L
, detail::by_value
<T
const>, int index
)
606 return derived().compute_score(L
, index
);
609 int match(lua_State
* L
, detail::by_const_reference
<T
>, int index
)
611 return derived().compute_score(L
, index
);
614 T
apply(lua_State
* L
, detail::by_value
<T
>, int index
)
616 return derived().from(L
, index
);
619 T
apply(lua_State
* L
, detail::by_value
<T
const>, int index
)
621 return derived().from(L
, index
);
624 T
apply(lua_State
* L
, detail::by_const_reference
<T
>, int index
)
626 return derived().from(L
, index
);
629 void apply(lua_State
* L
, T
const& value
)
631 derived().to(L
, value
);
636 return static_cast<Derived
&>(*this);
641 lua_Integer
as_lua_integer(T v
)
643 return static_cast<lua_Integer
>(v
);
647 lua_Number
as_lua_number(T v
)
649 return static_cast<lua_Number
>(v
);
652 # define LUABIND_NUMBER_CONVERTER(type, kind) \
654 struct default_converter<type> \
655 : native_converter_base<type> \
657 int compute_score(lua_State* L, int index) \
659 return lua_type(L, index) == LUA_TNUMBER ? 0 : -1; \
662 type from(lua_State* L, int index) \
664 return static_cast<type>(BOOST_PP_CAT(lua_to, kind)(L, index)); \
667 void to(lua_State* L, type const& value) \
669 BOOST_PP_CAT(lua_push, kind)(L, BOOST_PP_CAT(as_lua_, kind)(value)); \
674 struct default_converter<type const> \
675 : default_converter<type> \
679 struct default_converter<type const&> \
680 : default_converter<type> \
683 LUABIND_NUMBER_CONVERTER(char, integer
)
684 LUABIND_NUMBER_CONVERTER(signed char, integer
)
685 LUABIND_NUMBER_CONVERTER(unsigned char, integer
)
686 LUABIND_NUMBER_CONVERTER(signed short, integer
)
687 LUABIND_NUMBER_CONVERTER(unsigned short, integer
)
688 LUABIND_NUMBER_CONVERTER(signed int, integer
)
690 LUABIND_NUMBER_CONVERTER(unsigned int, number
)
691 LUABIND_NUMBER_CONVERTER(unsigned long, number
)
693 LUABIND_NUMBER_CONVERTER(signed long, integer
)
694 LUABIND_NUMBER_CONVERTER(float, number
)
695 LUABIND_NUMBER_CONVERTER(double, number
)
696 LUABIND_NUMBER_CONVERTER(long double, number
)
698 # undef LUABIND_NUMBER_CONVERTER
701 struct default_converter
<bool>
702 : native_converter_base
<bool>
704 static int compute_score(lua_State
* L
, int index
)
706 return lua_type(L
, index
) == LUA_TBOOLEAN
? 0 : -1;
709 bool from(lua_State
* L
, int index
)
711 return lua_toboolean(L
, index
) == 1;
714 void to(lua_State
* L
, bool value
)
716 lua_pushboolean(L
, value
);
721 struct default_converter
<bool const>
722 : default_converter
<bool>
726 struct default_converter
<bool const&>
727 : default_converter
<bool>
731 struct default_converter
<std::string
>
732 : native_converter_base
<std::string
>
734 static int compute_score(lua_State
* L
, int index
)
736 return lua_type(L
, index
) == LUA_TSTRING
? 0 : -1;
739 std::string
from(lua_State
* L
, int index
)
741 return std::string(lua_tostring(L
, index
), lua_strlen(L
, index
));
744 void to(lua_State
* L
, std::string
const& value
)
746 lua_pushlstring(L
, value
.data(), value
.size());
751 struct default_converter
<std::string
const>
752 : default_converter
<std::string
>
756 struct default_converter
<std::string
const&>
757 : default_converter
<std::string
>
761 struct default_converter
<char const*>
763 typedef boost::mpl::true_ is_native
;
765 int const consumed_args(...)
771 static int match(lua_State
* L
, U
, int index
)
773 int type
= lua_type(L
, index
);
774 return (type
== LUA_TSTRING
|| type
== LUA_TNIL
) ? 0 : -1;
778 char const* apply(lua_State
* L
, U
, int index
)
780 return lua_tostring(L
, index
);
783 void apply(lua_State
* L
, char const* str
)
785 lua_pushstring(L
, str
);
789 void converter_postcall(lua_State
*, U
, int)
794 struct default_converter
<const char* const>
795 : default_converter
<char const*>
799 struct default_converter
<char*>
800 : default_converter
<char const*>
803 template <std::size_t N
>
804 struct default_converter
<char const[N
]>
805 : default_converter
<char const*>
808 template <std::size_t N
>
809 struct default_converter
<char[N
]>
810 : default_converter
<char const*>
814 struct default_converter
<lua_State
*>
816 int const consumed_args(...)
822 lua_State
* apply(lua_State
* L
, U
, int)
828 static int match(lua_State
*, U
, int)
834 void converter_postcall(lua_State
*, U
, int) {}
840 struct default_policy
: converter_policy_tag
842 BOOST_STATIC_CONSTANT(bool, has_arg
= true);
845 static void precall(lua_State
*, T
, int) {}
847 template<class T
, class Direction
>
850 typedef default_converter
<T
> type
;
856 : default_converter
<T
>::is_native
859 // ============== new policy system =================
861 template<int, class> struct find_conversion_policy
;
863 template<bool IsConverter
= false>
864 struct find_conversion_impl
866 template<int N
, class Policies
>
869 typedef typename find_conversion_policy
<N
, typename
Policies::tail
>::type type
;
874 struct find_conversion_impl
<true>
876 template<int N
, class Policies
>
879 typedef typename
Policies::head head
;
880 typedef typename
Policies::tail tail
;
882 BOOST_STATIC_CONSTANT(bool, found
= (N
== head::index
));
885 boost::mpl::if_c
<found
887 , typename find_conversion_policy
<N
, tail
>::type
892 template<class Policies
>
893 struct find_conversion_impl2
897 : find_conversion_impl
<
898 boost::is_base_and_derived
<conversion_policy_base
, typename
Policies::head
>::value
899 >::template apply
<N
, Policies
>
905 struct find_conversion_impl2
<detail::null_type
>
910 typedef default_policy type
;
914 template<int N
, class Policies
>
915 struct find_conversion_policy
: find_conversion_impl2
<Policies
>::template apply
<N
>
920 struct policy_list_postcall
922 typedef typename
List::head head
;
923 typedef typename
List::tail tail
;
925 static void apply(lua_State
* L
, const index_map
& i
)
927 head::postcall(L
, i
);
928 policy_list_postcall
<tail
>::apply(L
, i
);
933 struct policy_list_postcall
<detail::null_type
>
935 static void apply(lua_State
*, const index_map
&) {}
938 // ==================================================
940 // ************** precall and postcall on policy_cons *********************
944 struct policy_precall
946 typedef typename
List::head head
;
947 typedef typename
List::tail tail
;
949 static void apply(lua_State
* L
, int index
)
951 head::precall(L
, index
);
952 policy_precall
<tail
>::apply(L
, index
);
957 struct policy_precall
<detail::null_type
>
959 static void apply(lua_State
*, int) {}
963 struct policy_postcall
965 typedef typename
List::head head
;
966 typedef typename
List::tail tail
;
968 static void apply(lua_State
* L
, int index
)
970 head::postcall(L
, index
);
971 policy_postcall
<tail
>::apply(L
, index
);
976 struct policy_postcall
<detail::null_type
>
978 static void apply(lua_State
*, int) {}
981 }} // namespace luabind::detail
984 namespace luabind
{ namespace
986 #if defined(__GNUC__) && ( \
987 (BOOST_VERSION < 103500) \
988 || (BOOST_VERSION < 103900 && (__GNUC__ * 100 + __GNUC_MINOR__ <= 400)) \
989 || (__GNUC__ * 100 + __GNUC_MINOR__ < 400))
990 static inline boost::arg
<0> return_value()
992 return boost::arg
<0>();
995 static inline boost::arg
<0> result()
997 return boost::arg
<0>();
999 # define LUABIND_PLACEHOLDER_ARG(N) boost::arg<N>(*)()
1000 #elif defined(BOOST_MSVC) || defined(__MWERKS__) \
1001 || (BOOST_VERSION >= 103900 && defined(__GNUC__) \
1002 && (__GNUC__ * 100 + __GNUC_MINOR__ == 400))
1003 static boost::arg
<0> return_value
;
1004 static boost::arg
<0> result
;
1005 # define LUABIND_PLACEHOLDER_ARG(N) boost::arg<N>
1007 boost::arg
<0> return_value
;
1008 boost::arg
<0> result
;
1009 # define LUABIND_PLACEHOLDER_ARG(N) boost::arg<N>
1013 #endif // LUABIND_POLICY_HPP_INCLUDED