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/bind/placeholders.hpp> 46 #include <boost/limits.hpp> 47 #include <boost/tuple/tuple.hpp> 48 #include <boost/version.hpp> 50 #include <luabind/detail/class_registry.hpp> 51 #include <luabind/detail/primitives.hpp> 52 #include <luabind/detail/object_rep.hpp> 53 #include <luabind/detail/typetraits.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> 76 template<
int N,
bool HasArg = true>
79 BOOST_STATIC_CONSTANT(
int, index = N);
80 BOOST_STATIC_CONSTANT(
bool, has_arg = HasArg);
88 int operator[](
int index)
const 101 namespace luabind {
namespace detail
103 template<
class H,
class T>
134 yes_t is_policy_cons_test(
const null_type&);
135 template<
class H,
class T>
137 no_t is_policy_cons_test(...);
144 BOOST_STATIC_CONSTANT(
bool,
value =
145 sizeof(is_policy_cons_test(t)) ==
sizeof(yes_t));
147 typedef boost::mpl::bool_<value> type;
154 static yes_t helper(
const char*);
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)
203 typedef mpl::false_ is_native;
211 int const consumed_args(...)
225 if (luabind::get_back_reference(L, ptr))
228 make_instance(L, ptr);
234 return static_cast<T*
>(result);
240 if (lua_isnil(L, index))
return 0;
242 if (obj == 0)
return -1;
262 typedef mpl::false_ is_native;
264 int const consumed_args(...)
278 if (luabind::get_back_reference(L, x))
281 make_pointee_instance(L, x, mpl::true_());
287 return *
static_cast<T*
>(result);
294 if (lua_isnil(L, index))
298 if (obj == 0)
return -1;
306 void converter_postcall(
lua_State*, T,
int) {}
314 typedef mpl::false_ is_native;
316 int const consumed_args(...)
336 if (luabind::get_back_reference(L, ptr))
339 make_instance(L, ptr);
345 return static_cast<T const*
>(result);
351 if (lua_isnil(L, index))
return 0;
353 if (obj == 0)
return -1;
355 if (s.second >= 0 && !obj->is_const())
362 void converter_postcall(
lua_State*, T,
int) {}
370 typedef mpl::false_ is_native;
372 int const consumed_args(...)
380 if (luabind::get_back_reference(L, ref))
383 make_pointee_instance(L, ref, mpl::false_());
389 assert(!lua_isnil(L, index));
397 if (obj == 0)
return -1;
408 void converter_postcall(
lua_State*, T,
int) {}
416 typedef mpl::false_ is_native;
418 int const consumed_args(...)
432 if (luabind::get_back_reference(L, ref))
435 make_pointee_instance(L, ref, mpl::false_());
441 return *
static_cast<T*
>(result);
448 if (obj == 0)
return -1;
451 if (s.second >= 0 && !obj->is_const())
468 typedef mpl::true_ is_native;
470 int const consumed_args(...)
477 lua_pushnumber(L, val);
483 return static_cast<T
>(
static_cast<int>(lua_tonumber(L, index)));
489 if (lua_isnumber(L, index))
return 0;
else return -1;
495 return static_cast<T
>(
static_cast<int>(lua_tonumber(L, index)));
501 if (lua_isnumber(L, index))
return 0;
else return -1;
505 void converter_postcall(
lua_State*, T,
int) {}
512 typedef mpl::true_ is_native;
514 int const consumed_args(...)
535 ? (std::numeric_limits<int>::max)() / LUABIND_MAX_ARITY
545 void converter_postcall(...) {}
548 void apply(
lua_State* interpreter, T
const& value_wrapper)
557 is_value_wrapper_arg<T>
558 , value_wrapper_converter<T>
560 boost::is_enum<typename boost::remove_reference<T>::type>
563 is_nonconst_pointer<T>
567 , const_pointer_converter
569 is_nonconst_reference<T>
572 is_const_reference<T>
573 , const_ref_converter
592 template <
class T,
class Derived = default_converter<T> >
595 typedef boost::mpl::true_ is_native;
597 int const consumed_args(...)
603 void converter_postcall(
lua_State*, U
const&,
int)
608 return derived().compute_score(L, index);
613 return derived().compute_score(L, index);
618 return derived().compute_score(L, index);
623 return derived().from(L, index);
628 return derived().from(L, index);
633 return derived().from(L, index);
638 derived().to(L, value);
643 return static_cast<Derived&
>(*this);
648 lua_Integer as_lua_integer(T v)
650 return static_cast<lua_Integer
>(v);
654 lua_Number as_lua_number(T v)
656 return static_cast<lua_Number
>(v);
659 # define LUABIND_NUMBER_CONVERTER(type, kind) \ 661 struct default_converter<type> \ 662 : native_converter_base<type> \ 664 int compute_score(lua_State* L, int index) \ 666 return lua_type(L, index) == LUA_TNUMBER ? 0 : -1; \ 669 type from(lua_State* L, int index) \ 671 return static_cast<type>(BOOST_PP_CAT(lua_to, kind)(L, index)); \ 674 void to(lua_State* L, type const& value) \ 676 BOOST_PP_CAT(lua_push, kind)(L, BOOST_PP_CAT(as_lua_, kind)(value)); \ 681 struct default_converter<type const> \ 682 : default_converter<type> \ 686 struct default_converter<type const&> \ 687 : default_converter<type> \ 690 LUABIND_NUMBER_CONVERTER(
char, integer)
691 LUABIND_NUMBER_CONVERTER(
signed char, integer)
692 LUABIND_NUMBER_CONVERTER(
unsigned char, integer)
693 LUABIND_NUMBER_CONVERTER(
signed short, integer)
694 LUABIND_NUMBER_CONVERTER(
unsigned short, integer)
695 LUABIND_NUMBER_CONVERTER(
signed int, integer)
697 LUABIND_NUMBER_CONVERTER(
unsigned int, number)
698 LUABIND_NUMBER_CONVERTER(
unsigned long, number)
700 LUABIND_NUMBER_CONVERTER(
signed long, integer)
701 LUABIND_NUMBER_CONVERTER(
float, number)
702 LUABIND_NUMBER_CONVERTER(
double, number)
703 LUABIND_NUMBER_CONVERTER(
long double, number)
705 # undef LUABIND_NUMBER_CONVERTER 711 static int compute_score(
lua_State* L,
int index)
713 return lua_type(L, index) == LUA_TBOOLEAN ? 0 : -1;
718 return lua_toboolean(L, index) == 1;
723 lua_pushboolean(L, value);
741 static int compute_score(
lua_State* L,
int index)
743 return lua_type(L, index) == LUA_TSTRING ? 0 : -1;
746 std::string from(
lua_State* L,
int index)
748 return std::string(lua_tostring(L, index), lua_strlen(L, index));
753 lua_pushlstring(L, value.data(), value.size());
770 typedef boost::mpl::true_ is_native;
772 int const consumed_args(...)
778 static int match(
lua_State* L, U,
int index)
780 int type = lua_type(L, index);
781 return (type == LUA_TSTRING || type == LUA_TNIL) ? 0 : -1;
785 char const* apply(
lua_State* L, U,
int index)
787 return lua_tostring(L, index);
790 void apply(
lua_State* L,
char const* str)
792 lua_pushstring(L, str);
796 void converter_postcall(
lua_State*, U,
int)
810 template <std::
size_t N>
815 template <std::
size_t N>
823 int const consumed_args(...)
841 void converter_postcall(
lua_State*, U,
int) {}
849 BOOST_STATIC_CONSTANT(
bool, has_arg =
true);
852 static void precall(
lua_State*, T,
int) {}
854 template<
class T,
class Direction>
870 template<
bool IsConverter = false>
873 template<
int N,
class Policies>
883 template<
int N,
class Policies>
886 typedef typename Policies::head head;
887 typedef typename Policies::tail tail;
889 BOOST_STATIC_CONSTANT(
bool, found = (N == head::index));
892 boost::mpl::if_c<found
899 template<
class Policies>
905 boost::is_base_and_derived<conversion_policy_base, typename Policies::head>::value
906 >::template
apply<N, Policies>
921 template<
int N,
class Policies>
929 typedef typename List::head head;
930 typedef typename List::tail tail;
934 head::postcall(L, i);
953 typedef typename List::head head;
954 typedef typename List::tail tail;
956 static void apply(
lua_State* L,
int index)
958 head::precall(L, index);
972 typedef typename List::head head;
973 typedef typename List::tail tail;
975 static void apply(
lua_State* L,
int index)
977 head::postcall(L, index);
988 template <
class Policies,
class Sought>
991 boost::is_same<typename Policies::head, Sought>
993 , has_policy<typename Policies::tail, Sought>
997 template <
class Sought>
1007 #if defined(__GNUC__) && ( \ 1008 (BOOST_VERSION < 103500) \ 1009 || (BOOST_VERSION < 103900 && (__GNUC__ * 100 + __GNUC_MINOR__ <= 400)) \ 1010 || (__GNUC__ * 100 + __GNUC_MINOR__ < 400)) 1011 static inline boost::arg<0> return_value()
1013 return boost::arg<0>();
1016 static inline boost::arg<0> result()
1018 return boost::arg<0>();
1020 # define LUABIND_PLACEHOLDER_ARG(N) boost::arg<N>(*)() 1021 #elif defined(BOOST_MSVC) || defined(__MWERKS__) \ 1022 || (BOOST_VERSION >= 103900 && defined(__GNUC__) \ 1023 && (__GNUC__ * 100 + __GNUC_MINOR__ == 400)) 1024 static boost::arg<0> return_value;
1025 static boost::arg<0> result;
1026 # define LUABIND_PLACEHOLDER_ARG(N) boost::arg<N> 1028 boost::arg<0> return_value;
1029 boost::arg<0> result;
1030 # define LUABIND_PLACEHOLDER_ARG(N) boost::arg<N> 1034 #endif // LUABIND_POLICY_HPP_INCLUDED Definition: policy.hpp:593
Definition: policy.hpp:77
Definition: primitives.hpp:52
Definition: policy.hpp:151
Definition: has_get_pointer.hpp:100
Definition: policy.hpp:555
Definition: policy.hpp:259
Definition: primitives.hpp:54
Definition: policy.hpp:951
Definition: policy.hpp:588
Definition: primitives.hpp:58
Definition: policy.hpp:868
Definition: weak_ref.hpp:32
Definition: policy.hpp:83
Definition: policy.hpp:874
Definition: policy.hpp:73
Definition: policy.hpp:311
Definition: policy.hpp:465
Definition: inheritance.hpp:153
Definition: policy.hpp:509
Definition: policy.hpp:413
Definition: policy.hpp:200
Definition: policy.hpp:927
Definition: policy.hpp:847
Definition: minilua.c:461
Definition: object_rep.hpp:41
Definition: policy.hpp:140
Definition: PEtypes.h:507
Definition: enum_maker.hpp:46
Definition: policy.hpp:855
Definition: policy.hpp:989
Definition: policy.hpp:708
Definition: value_wrapper.hpp:77
Definition: policy.hpp:970
Definition: primitives.hpp:44
Definition: policy.hpp:367
Definition: primitives.hpp:55
Definition: policy.hpp:900
Definition: primitives.hpp:53
Definition: policy.hpp:903
Definition: primitives.hpp:56
Definition: policy.hpp:871
Definition: policy.hpp:862
Definition: policy.hpp:128
Definition: policy.hpp:104
Definition: from_stack.hpp:28