23 #ifndef LUABIND_OBJECT_050419_HPP 24 #define LUABIND_OBJECT_050419_HPP 26 #include <boost/implicit_cast.hpp> 27 #include <boost/ref.hpp> 28 #include <boost/mpl/bool.hpp> 29 #include <boost/mpl/apply_wrap.hpp> 33 # include <boost/tuple/tuple.hpp> 35 #include <boost/optional.hpp> 37 #include <luabind/nil.hpp> 38 #include <luabind/value_wrapper.hpp> 39 #include <luabind/detail/pcall.hpp> 40 #include <luabind/handle.hpp> 41 #include <luabind/from_stack.hpp> 42 #include <luabind/detail/policy.hpp> 43 #include <luabind/detail/stack_utils.hpp> 44 #include <luabind/detail/convert_to_lua.hpp> 45 #include <luabind/typeid.hpp> 47 #include <boost/iterator/iterator_facade.hpp> 50 #include <boost/preprocessor/iteration/iterate.hpp> 52 #include <boost/utility/enable_if.hpp> 58 namespace mpl = boost::mpl;
60 template<
class T,
class ConverterGenerator>
61 void push_aux(
lua_State* interpreter, T& value, ConverterGenerator*)
63 typedef typename boost::mpl::if_<
64 boost::is_reference_wrapper<T>
65 , BOOST_DEDUCED_TYPENAME boost::unwrap_reference<T>::type&
67 >::type unwrapped_type;
69 typename mpl::apply_wrap2<
70 ConverterGenerator,unwrapped_type,cpp_to_lua
75 , boost::implicit_cast<
76 BOOST_DEDUCED_TYPENAME boost::unwrap_reference<T>::type&
81 template<
class T,
class Policies>
82 void push(
lua_State* interpreter, T& value, Policies
const&)
84 typedef typename find_conversion_policy<
87 >::type converter_policy;
89 push_aux(interpreter, value, (converter_policy*)0);
93 void push(
lua_State* interpreter, T& value)
95 push(interpreter, value, null_type());
102 namespace mpl = boost::mpl;
107 namespace is_object_interface_aux
109 typedef char (&yes)[1];
110 typedef char (&no)[2];
119 BOOST_STATIC_CONSTANT(
bool, value =
120 sizeof(is_object_interface_aux::check((T*)0)) ==
sizeof(yes)
123 typedef mpl::bool_<value> type;
133 template <
class R,
class T,
class U>
135 # ifndef BOOST_NO_SFINAE
138 is_object_interface<T>
139 , is_object_interface<U>
150 template<
class T,
class U>
151 int binary_interpreter(
lua_State*& L, T
const& lhs, U
const& rhs
152 , boost::mpl::true_, boost::mpl::true_)
159 assert(L == L2 || L == 0 || L2 == 0);
164 if (L != L2)
return -1;
165 if (L == 0)
return 1;
169 template<
class T,
class U>
170 int binary_interpreter(
lua_State*& L, T
const& x, U
const&
171 , boost::mpl::true_, boost::mpl::false_)
177 template<
class T,
class U>
178 int binary_interpreter(
lua_State*& L, T
const&, U
const& x, boost::mpl::false_, boost::mpl::true_)
184 template<
class T,
class U>
185 int binary_interpreter(
lua_State*& L, T
const& x, U
const& y)
187 return binary_interpreter(
196 #define LUABIND_BINARY_OP_DEF(op, fn) \ 197 template<class LHS, class RHS> \ 198 typename enable_binary<bool,LHS,RHS>::type \ 199 operator op(LHS const& lhs, RHS const& rhs) \ 202 switch (binary_interpreter(L, lhs, rhs)) \ 212 detail::stack_pop pop1(L, 1); \ 213 detail::push(L, lhs); \ 214 detail::stack_pop pop2(L, 1); \ 215 detail::push(L, rhs); \ 217 return fn(L, -1, -2) != 0; \ 220 LUABIND_BINARY_OP_DEF(==, lua_equal)
221 LUABIND_BINARY_OP_DEF(<, lua_lessthan)
223 template<
class ValueWrapper>
224 std::ostream& operator<<(std::ostream& os
229 static_cast<ValueWrapper const&>(v));
232 , static_cast<ValueWrapper const&>(v));
233 char const* p = lua_tostring(interpreter, -1);
234 std::size_t len = lua_strlen(interpreter, -1);
235 std::copy(p, p + len, std::ostream_iterator<char>(os));
239 #undef LUABIND_BINARY_OP_DEF 241 template<
class LHS,
class RHS>
243 operator>(LHS
const& lhs, RHS
const& rhs)
245 return !(lhs < rhs || lhs == rhs);
248 template<
class LHS,
class RHS>
250 operator<=(LHS
const& lhs, RHS
const& rhs)
252 return lhs < rhs || lhs == rhs;
255 template<
class LHS,
class RHS>
257 operator>=(LHS
const& lhs, RHS
const& rhs)
262 template<
class LHS,
class RHS>
264 operator!=(LHS
const& lhs, RHS
const& rhs)
266 return !(lhs == rhs);
269 template<
class ValueWrapper,
class Arguments>
277 template<
class Derived>
280 struct safe_bool_type {};
284 # ifdef LUABIND_CPP0x 286 template <
class... Args>
288 Derived, std::tuple<Args
const*...>
289 > operator()(Args
const& ...args)
291 typedef std::tuple<Args
const*...> arguments;
302 , boost::tuples::tuple<A0 const*>
303 > operator()(A0
const& a0)
305 typedef boost::tuples::tuple<A0 const*> arguments;
313 template<
class A0,
class A1>
316 , boost::tuples::tuple<A0 const*, A1 const*>
317 > operator()(A0
const& a0, A1
const& a1)
319 typedef boost::tuples::tuple<A0 const*, A1 const*> arguments;
323 , arguments(&a0, &a1)
328 #define BOOST_PP_ITERATION_PARAMS_1 (3, \ 329 (3, LUABIND_MAX_ARITY, <luabind/detail/object_call.hpp>)) 330 #include BOOST_PP_ITERATE() 332 # endif // LUABIND_CPP0x 334 operator safe_bool_type*()
const 344 return lua_toboolean(L, -1) == 1 ? (safe_bool_type*)1 : 0;
350 return *
static_cast<Derived*
>(
this);
353 Derived
const& derived()
const 355 return *
static_cast<Derived const*
>(
this);
359 #ifdef LUABIND_USE_VALUE_WRAPPER_TAG 360 struct iterator_proxy_tag;
363 template<
class AccessPolicy>
368 #ifdef LUABIND_USE_VALUE_WRAPPER_TAG 369 typedef iterator_proxy_tag value_wrapper_tag;
373 : m_interpreter(interpreter)
374 , m_table_index(lua_gettop(interpreter) + 1)
375 , m_key_index(m_table_index + 1)
377 table.push(m_interpreter);
378 key.push(m_interpreter);
382 : m_interpreter(other.m_interpreter)
383 , m_table_index(other.m_table_index)
384 , m_key_index(other.m_key_index)
386 other.m_interpreter = 0;
392 lua_pop(m_interpreter, 2);
398 lua_pushvalue(m_interpreter, m_key_index);
399 lua_pushnil(m_interpreter);
400 AccessPolicy::set(m_interpreter, m_table_index);
407 lua_pushvalue(m_interpreter, m_key_index);
408 detail::push(m_interpreter, value);
409 AccessPolicy::set(m_interpreter, m_table_index);
417 *
this, m_interpreter, key
426 return m_interpreter;
432 assert(interpreter == m_interpreter);
433 lua_pushvalue(m_interpreter, m_key_index);
434 AccessPolicy::get(m_interpreter, m_table_index);
451 lua_settable(interpreter,
table);
456 lua_gettable(interpreter,
table);
464 lua_rawset(interpreter,
table);
469 lua_rawget(interpreter,
table);
473 template<
class AccessPolicy>
475 :
public boost::iterator_facade<
476 basic_iterator<AccessPolicy>
477 , adl::iterator_proxy<AccessPolicy>
478 , boost::single_pass_traversal_tag
479 , adl::iterator_proxy<AccessPolicy>
487 template<
class ValueWrapper>
496 lua_pushnil(m_interpreter);
497 if (lua_next(m_interpreter, -2) != 0)
500 handle(m_interpreter, -2).swap(m_key);
508 handle(m_interpreter, -1).swap(m_table);
514 friend class boost::iterator_core_access;
518 m_table.push(m_interpreter);
519 m_key.push(m_interpreter);
523 if (lua_next(m_interpreter, -2) != 0)
525 m_key.replace(m_interpreter, -2);
526 lua_pop(m_interpreter, 2);
538 if (m_interpreter == 0 && other.m_interpreter == 0)
541 if (m_interpreter != other.m_interpreter)
545 m_key.push(m_interpreter);
546 other.m_key.push(m_interpreter);
547 return lua_equal(m_interpreter, -2, -1) != 0;
562 #if BOOST_VERSION < 105700 565 #define LUABIND_OPERATOR_ADL_WKND(op) \ 566 inline bool operator op( \ 567 basic_iterator<basic_access> const& x \ 568 , basic_iterator<basic_access> const& y) \ 570 return boost::operator op(x, y); \ 573 inline bool operator op( \ 574 basic_iterator<raw_access> const& x \ 575 , basic_iterator<raw_access> const& y) \ 577 return boost::operator op(x, y); \ 580 LUABIND_OPERATOR_ADL_WKND(==)
581 LUABIND_OPERATOR_ADL_WKND(!=)
583 #undef LUABIND_OPERATOR_ADL_WKND 592 #ifdef LUABIND_USE_VALUE_WRAPPER_TAG 593 struct index_proxy_tag;
598 :
public object_interface<index_proxy<Next> >
601 #ifdef LUABIND_USE_VALUE_WRAPPER_TAG 602 typedef index_proxy_tag value_wrapper_tag;
605 typedef index_proxy<Next> this_type;
608 index_proxy(Next
const& next,
lua_State* interpreter, Key
const& key)
609 : m_interpreter(interpreter)
610 , m_key_index(lua_gettop(interpreter) + 1)
613 detail::push(m_interpreter, key);
616 index_proxy(index_proxy
const&
other)
617 : m_interpreter(other.m_interpreter)
618 , m_key_index(other.m_key_index)
619 , m_next(other.m_next)
621 other.m_interpreter = 0;
627 lua_pop(m_interpreter, 1);
639 lua_pushvalue(m_interpreter, m_key_index);
640 lua_pushnil(m_interpreter);
641 lua_settable(m_interpreter, -3);
646 this_type& operator=(T
const& value)
651 lua_pushvalue(m_interpreter, m_key_index);
652 detail::push(m_interpreter, value);
653 lua_settable(m_interpreter, -3);
657 this_type& operator=(this_type
const& value)
662 lua_pushvalue(m_interpreter, m_key_index);
663 detail::push(m_interpreter, value);
664 lua_settable(m_interpreter, -3);
669 index_proxy<this_type> operator[](T
const& key)
671 return index_proxy<this_type>(*
this, m_interpreter, key);
678 return m_interpreter;
682 struct hidden_type {};
697 #ifndef LUABIND_USE_VALUE_WRAPPER_TAG 705 typedef boost::mpl::true_ is_specialized;
710 return proxy.interpreter();
720 #ifndef LUABIND_USE_VALUE_WRAPPER_TAG 721 template<
class AccessPolicy>
728 typedef boost::mpl::true_ is_specialized;
730 template<
class Proxy>
731 static lua_State* interpreter(Proxy
const& p)
733 return p.interpreter();
736 template<
class Proxy>
737 static void unwrap(
lua_State* interpreter, Proxy
const& p)
740 const_cast<Proxy&
>(p).push(interpreter);
760 : m_handle(stack_reference.interpreter, stack_reference.index)
767 detail::push(interpreter, value);
769 handle(interpreter, -1).swap(m_handle);
772 template<
class T,
class Policies>
775 detail::push(interpreter, value, Policies());
777 handle(interpreter, -1).swap(m_handle);
782 bool is_valid()
const;
788 *
this, m_handle.interpreter(), key
792 void swap(
object& other)
794 m_handle.swap(other.m_handle);
801 inline void object::push(
lua_State* interpreter)
const 803 m_handle.push(interpreter);
806 inline lua_State* object::interpreter()
const 808 return m_handle.interpreter();
811 inline bool object::is_valid()
const 813 return m_handle.interpreter() != 0;
820 : m_interpreter(stack_reference.interpreter)
821 , m_index(stack_reference.index)
824 m_index = lua_gettop(m_interpreter) - m_index + 1;
835 lua_pushvalue(L, m_index);
840 return m_interpreter;
853 #ifndef LUABIND_USE_VALUE_WRAPPER_TAG 854 template <
class ValueWrapper,
class Arguments>
861 typedef boost::mpl::true_ is_specialized;
863 template<
class W,
class A>
869 template<
class W,
class A>
873 result.push(result.interpreter());
880 typedef boost::mpl::true_ is_specialized;
882 static lua_State* interpreter(
object const& value)
884 return value.interpreter();
887 static void unwrap(
lua_State* interpreter,
object const& value)
889 value.push(interpreter);
892 static bool check(...)
901 typedef boost::mpl::true_ is_specialized;
905 return value.interpreter();
910 value.push(interpreter);
913 static bool check(...)
922 assert(interpreter == m_interpreter);
926 lua_pushvalue(m_interpreter, m_key_index);
927 lua_gettable(m_interpreter, -2);
928 lua_remove(m_interpreter, -2);
939 template<
class AccessPolicy>
942 lua_pushvalue(m_interpreter, m_key_index);
943 AccessPolicy::get(m_interpreter, m_table_index);
948 template<
class AccessPolicy>
964 ReturnType object_cast_aux(
965 ValueWrapper
const& value_wrapper
976 #ifndef LUABIND_NO_ERROR_CHECKING 978 return ErrorPolicy::handle_error(interpreter,
typeid(
void));
988 >::type converter_generator;
990 typename mpl::apply_wrap2<converter_generator, T, lua_to_cpp>::type cv;
992 if (cv.match(interpreter, LUABIND_DECORATE_TYPE(T), -1) < 0)
994 return ErrorPolicy::handle_error(interpreter,
typeid(T));
997 return cv.apply(interpreter, LUABIND_DECORATE_TYPE(T), -1);
1001 # pragma warning(push) 1002 # pragma warning(disable:4702) // unreachable code 1010 #ifndef LUABIND_NO_EXCEPTIONS 1013 cast_failed_callback_fun e = get_cast_failed_callback();
1014 if (e) e(interpreter, type_info);
1016 assert(0 &&
"object_cast failed. If you want to handle this error use " 1017 "luabind::set_error_callback()");
1020 return *(
typename boost::remove_reference<T>::type*)0;
1025 # pragma warning(pop) 1033 return boost::optional<T>();
1039 template<
class T,
class ValueWrapper>
1040 T object_cast(ValueWrapper
const& value_wrapper)
1042 return detail::object_cast_aux(
1051 template<
class T,
class ValueWrapper,
class Policies>
1052 T object_cast(ValueWrapper
const& value_wrapper, Policies
const&)
1054 return detail::object_cast_aux(
1063 template<
class T,
class ValueWrapper>
1064 boost::optional<T> object_cast_nothrow(ValueWrapper
const& value_wrapper)
1066 return detail::object_cast_aux(
1071 , (boost::optional<T>*)0
1075 template<
class T,
class ValueWrapper,
class Policies>
1076 boost::optional<T> object_cast_nothrow(ValueWrapper
const& value_wrapper, Policies
const&)
1078 return detail::object_cast_aux(
1083 , (boost::optional<T>*)0
1093 # ifdef LUABIND_CPP0x 1095 template <
class Args,
class Policies,
class N,
class E>
1096 static void push_args(
1097 lua_State* L, Args
const& args, Policies
const& policies, N, E)
1099 convert_to_lua_p<N::value + 1>(L, *std::get<N::value>(args), policies);
1101 L, args, policies, std::integral_constant<int, N::value + 1>(), E());
1104 template <
class Args,
class Policies,
class E>
1105 static void push_args(
lua_State* L, Args
const&, Policies
const&, E, E)
1108 template <
class... Args,
class Policies =
null_type>
1110 lua_State* L, std::tuple<Args...>
const& args
1111 , Policies
const policies = Policies())
1117 , std::integral_constant<int, 0>()
1118 , std::integral_constant<
int,
sizeof...(Args)>()
1122 # else // LUABIND_CPP0x 1124 template<
class H,
class T,
class Policies>
1125 inline static void apply(
lua_State* L,
const boost::tuples::cons<H, T>& x,
const Policies& p)
1127 convert_to_lua_p<Index>(L, *x.get_head(), p);
1131 template<
class H,
class T>
1132 inline static void apply(
lua_State* L,
const boost::tuples::cons<H, T>& x)
1134 convert_to_lua(L, *x.get_head());
1138 template<
class Policies>
1139 inline static void apply(
lua_State*,
const boost::tuples::null_type&,
const Policies&) {}
1141 inline static void apply(
lua_State*,
const boost::tuples::null_type&) {}
1143 # endif // LUABIND_CPP0x 1151 template<
class ValueWrapper,
class Arguments>
1154 call_proxy(ValueWrapper& value_wrapper, Arguments arguments)
1155 : value_wrapper(&value_wrapper)
1156 , arguments(arguments)
1159 call_proxy(call_proxy
const&
other)
1160 : value_wrapper(other.value_wrapper)
1161 , arguments(other.arguments)
1163 other.value_wrapper = 0;
1177 template<
class Policies>
1178 object operator[](Policies
const&)
1180 return call((Policies*)0);
1183 template<
class Policies>
1184 object call(Policies*)
1199 # ifdef LUABIND_CPP0x 1200 if (detail::pcall(interpreter, std::tuple_size<Arguments>::value, 1))
1202 if (detail::pcall(interpreter, boost::tuples::length<Arguments>::value, 1))
1205 #ifndef LUABIND_NO_EXCEPTIONS 1208 error_callback_fun e = get_error_callback();
1209 if (e) e(interpreter);
1211 assert(0 &&
"the lua function threw an error and exceptions are disabled." 1212 "if you want to handle this error use luabind::set_error_callback()");
1221 mutable ValueWrapper* value_wrapper;
1222 Arguments arguments;
1225 # ifndef LUABIND_CPP0x 1227 template<
class Derived>
1228 call_proxy<Derived, boost::tuples::tuple<> >
1229 object_interface<Derived>::operator()()
1231 return call_proxy<Derived, boost::tuples::tuple<> >(
1233 , boost::tuples::tuple<>()
1242 template <
class Base =
object>
1246 : Base(stack_reference)
1254 template <
class Base>
1258 static bool check(
lua_State* L,
int idx)
1261 lua_istable(L, idx);
1265 inline object newtable(
lua_State* interpreter)
1267 lua_newtable(interpreter);
1273 inline object globals(
lua_State* interpreter)
1275 lua_pushvalue(interpreter, LUA_GLOBALSINDEX);
1281 inline object registry(
lua_State* interpreter)
1283 lua_pushvalue(interpreter, LUA_REGISTRYINDEX);
1288 template<
class ValueWrapper,
class K>
1289 inline object gettable(ValueWrapper
const&
table, K
const& key)
1297 detail::push(interpreter, key);
1298 lua_gettable(interpreter, -2);
1302 template<
class ValueWrapper,
class K,
class T>
1303 inline void settable(ValueWrapper
const& table, K
const& key, T
const& value)
1313 detail::push(interpreter, key);
1314 detail::push(interpreter, value);
1315 lua_settable(interpreter, -3);
1318 template<
class ValueWrapper,
class K>
1319 inline object rawget(ValueWrapper
const& table, K
const& key)
1327 detail::push(interpreter, key);
1328 lua_rawget(interpreter, -2);
1332 template<
class ValueWrapper,
class K,
class T>
1333 inline void rawset(ValueWrapper
const& table, K
const& key, T
const& value)
1343 detail::push(interpreter, key);
1344 detail::push(interpreter, value);
1345 lua_rawset(interpreter, -3);
1348 template<
class ValueWrapper>
1349 inline int type(ValueWrapper
const& value)
1357 return lua_type(interpreter, -1);
1360 template <
class ValueWrapper>
1361 inline object getmetatable(ValueWrapper
const& obj)
1369 lua_getmetatable(interpreter, -1);
1373 template <
class ValueWrapper1,
class ValueWrapper2>
1374 inline void setmetatable(
1375 ValueWrapper1
const& obj, ValueWrapper2
const& metatable)
1384 lua_setmetatable(interpreter, -2);
1387 template <
class ValueWrapper>
1388 inline lua_CFunction tocfunction(ValueWrapper
const& value)
1396 return lua_tocfunction(interpreter, -1);
1399 template <
class T,
class ValueWrapper>
1400 inline T* touserdata(ValueWrapper
const& value)
1408 return static_cast<T*
>(lua_touserdata(interpreter, -1));
1411 template <
class ValueWrapper>
1412 inline object getupvalue(ValueWrapper
const& value,
int index)
1420 lua_getupvalue(interpreter, -1, index);
1424 template <
class ValueWrapper1,
class ValueWrapper2>
1425 inline void setupvalue(
1426 ValueWrapper1
const&
function,
int index, ValueWrapper2
const& value)
1435 lua_setupvalue(interpreter, -2, index);
1438 template <
class GetValueWrapper>
1439 object property(GetValueWrapper
const&
get)
1446 lua_pushnil(interpreter);
1448 lua_pushcclosure(interpreter, &detail::property_tag, 2);
1454 template <
class GetValueWrapper,
class SetValueWrapper>
1455 object property(GetValueWrapper
const&
get, SetValueWrapper
const&
set)
1464 lua_pushcclosure(interpreter, &detail::property_tag, 2);
1474 #endif // LUABIND_OBJECT_050419_HPP Definition: value_wrapper.hpp:101
Definition: handle.hpp:33
Definition: object.hpp:1091
Definition: object.hpp:447
Definition: typeid.hpp:22
Definition: policy.hpp:868
Definition: format_signature.hpp:22
Definition: NPLHelper.h:11
Definition: object.hpp:105
Definition: object.hpp:1006
Definition: object.hpp:749
Definition: object.hpp:117
Definition: minilua.c:461
Definition: object.hpp:816
Definition: PEtypes.h:507
Definition: object.hpp:134
Definition: object.hpp:474
Definition: object.hpp:364
Definition: stack_utils.hpp:31
Definition: value_wrapper.hpp:77
Definition: primitives.hpp:44
Definition: object.hpp:270
Definition: object.hpp:460
Definition: object.hpp:1029
Definition: iterator_policy.hpp:15
Definition: object.hpp:129
Definition: from_stack.hpp:28