object.hpp.svn-base
来自「本人找过多个在linux下c++的lua5.1封装库,但很少.luabind已经」· SVN-BASE 代码 · 共 1,206 行 · 第 1/3 页
SVN-BASE
1,206 行
{ return is_valid()?(safe_bool_type*)1:0; }} // namespace adlusing adl::object;template<>struct value_wrapper_traits<object>{ typedef boost::mpl::true_ is_specialized; static lua_State* interpreter(object const& value) { return value.interpreter(); } static void unwrap(lua_State* interpreter, object const& value) { value.push(interpreter); } static bool check(...) { return true; }};template<class Next>inline void adl::index_proxy<Next>::push(lua_State* interpreter){ assert(interpreter == m_interpreter); value_wrapper_traits<Next>::unwrap(m_interpreter, m_next); lua_pushvalue(m_interpreter, m_key_index); lua_gettable(m_interpreter, -2); lua_remove(m_interpreter, -2);}template<class Next>inline adl::index_proxy<Next>::operator object(){ detail::stack_pop pop(m_interpreter, 1); push(m_interpreter); return object(from_stack(m_interpreter, -1));}template<class AccessPolicy>adl::iterator_proxy<AccessPolicy>::operator object(){ lua_pushvalue(m_interpreter, m_key_index); AccessPolicy::get(m_interpreter, m_table_index); detail::stack_pop pop(m_interpreter, 1); return object(from_stack(m_interpreter, -1));}template<class AccessPolicy>object detail::basic_iterator<AccessPolicy>::key() const{ return object(m_key);}namespace detail { template< class T , class ValueWrapper , class Policies , class ErrorPolicy , class ReturnType > ReturnType object_cast_aux( ValueWrapper const& value_wrapper , T* , Policies* , ErrorPolicy* , ReturnType* ) { lua_State* interpreter = value_wrapper_traits<ValueWrapper>::interpreter( value_wrapper );#ifndef LUABIND_NO_ERROR_CHECKING if (!interpreter) return ErrorPolicy::handle_error(interpreter, LUABIND_TYPEID(void));#endif value_wrapper_traits<ValueWrapper>::unwrap(interpreter, value_wrapper); detail::stack_pop pop(interpreter, 1); typedef typename detail::find_conversion_policy< 0 , Policies >::type converter_generator; typename mpl::apply_wrap2<converter_generator, T, lua_to_cpp>::type cv;#ifndef LUABIND_NO_ERROR_CHECKING if (cv.match(interpreter, LUABIND_DECORATE_TYPE(T), -1) < 0) { return ErrorPolicy::handle_error(interpreter, LUABIND_TYPEID(T)); }#endif return cv.apply(interpreter, LUABIND_DECORATE_TYPE(T), -1); } template<class T> struct throw_error_policy { static T handle_error(lua_State* interpreter, LUABIND_TYPE_INFO type_info) {#ifndef LUABIND_NO_EXCEPTIONS throw cast_failed(interpreter, type_info);#else cast_failed_callback_fun e = get_cast_failed_callback(); if (e) e(interpreter, type_info); assert(0 && "object_cast failed. If you want to handle this error use " "luabind::set_error_callback()"); std::terminate();#endif } }; template<class T> struct nothrow_error_policy { static boost::optional<T> handle_error(lua_State*, LUABIND_TYPE_INFO) { return boost::optional<T>(); } };} // namespace detailtemplate<class T, class ValueWrapper>T object_cast(ValueWrapper const& value_wrapper){ return detail::object_cast_aux( value_wrapper , (T*)0 , (detail::null_type*)0 , (detail::throw_error_policy<T>*)0 , (T*)0 );}template<class T, class ValueWrapper, class Policies>T object_cast(ValueWrapper const& value_wrapper, Policies const&){ return detail::object_cast_aux( value_wrapper , (T*)0 , (Policies*)0 , (detail::throw_error_policy<T>*)0 , (T*)0 );}template<class T, class ValueWrapper>boost::optional<T> object_cast_nothrow(ValueWrapper const& value_wrapper){ return detail::object_cast_aux( value_wrapper , (T*)0 , (detail::null_type*)0 , (detail::nothrow_error_policy<T>*)0 , (boost::optional<T>*)0 );}template<class T, class ValueWrapper, class Policies>boost::optional<T> object_cast_nothrow(ValueWrapper const& value_wrapper, Policies const&){ return detail::object_cast_aux( value_wrapper , (T*)0 , (Policies*)0 , (detail::nothrow_error_policy<T>*)0 , (boost::optional<T>*)0 );}namespace detail{ template<int Index> struct push_args_from_tuple { template<class H, class T, class Policies> inline static void apply(lua_State* L, const boost::tuples::cons<H, T>& x, const Policies& p) { convert_to_lua_p<Index>(L, *x.get_head(), p); push_args_from_tuple<Index+1>::apply(L, x.get_tail(), p); } template<class H, class T> inline static void apply(lua_State* L, const boost::tuples::cons<H, T>& x) { convert_to_lua(L, *x.get_head()); push_args_from_tuple<Index+1>::apply(L, x.get_tail()); } template<class Policies> inline static void apply(lua_State*, const boost::tuples::null_type&, const Policies&) {} inline static void apply(lua_State*, const boost::tuples::null_type&) {} };} // namespace detailnamespace adl{ template<class ValueWrapper, class Arguments> struct call_proxy { call_proxy(ValueWrapper& value_wrapper, Arguments arguments) : value_wrapper(&value_wrapper) , arguments(arguments) {} call_proxy(call_proxy const& other) : value_wrapper(other.value_wrapper) , arguments(other.arguments) { other.value_wrapper = 0; } ~call_proxy() { if (value_wrapper) call((detail::null_type*)0); } operator object() { return call((detail::null_type*)0); } template<class Policies> object operator[](Policies const&) { return call((Policies*)0); } template<class Policies> object call(Policies*) { lua_State* interpreter = value_wrapper_traits<ValueWrapper>::interpreter( *value_wrapper ); value_wrapper_traits<ValueWrapper>::unwrap( interpreter , *value_wrapper ); value_wrapper = 0; detail::push_args_from_tuple<1>::apply(interpreter, arguments, Policies()); if (detail::pcall(interpreter, boost::tuples::length<Arguments>::value, 1)) {#ifndef LUABIND_NO_EXCEPTIONS throw luabind::error(interpreter);#else error_callback_fun e = get_error_callback(); if (e) e(interpreter); assert(0 && "the lua function threw an error and exceptions are disabled." "if you want to handle this error use luabind::set_error_callback()"); std::terminate();#endif } detail::stack_pop pop(interpreter, 1); return object(from_stack(interpreter, -1)); } mutable ValueWrapper* value_wrapper; Arguments arguments; }; template<class Derived> call_proxy<Derived, boost::tuples::tuple<> > object_interface<Derived>::operator()() { return call_proxy<Derived, boost::tuples::tuple<> >( derived() , boost::tuples::tuple<>() ); }} // namespace adlinline object newtable(lua_State* interpreter){ lua_newtable(interpreter); detail::stack_pop pop(interpreter, 1); return object(from_stack(interpreter, -1));}// this could be optimized by returning a proxyinline object globals(lua_State* interpreter){ lua_pushvalue(interpreter, LUA_GLOBALSINDEX); detail::stack_pop pop(interpreter, 1); return object(from_stack(interpreter, -1));}// this could be optimized by returning a proxyinline object registry(lua_State* interpreter){ lua_pushvalue(interpreter, LUA_REGISTRYINDEX); detail::stack_pop pop(interpreter, 1); return object(from_stack(interpreter, -1));}template<class ValueWrapper, class K>inline object gettable(ValueWrapper const& table, K const& key){ lua_State* interpreter = value_wrapper_traits<ValueWrapper>::interpreter( table ); value_wrapper_traits<ValueWrapper>::unwrap(interpreter, table); detail::stack_pop pop(interpreter, 2); detail::push(interpreter, key); lua_gettable(interpreter, -2); return object(from_stack(interpreter, -1));}template<class ValueWrapper, class K, class T>inline void settable(ValueWrapper const& table, K const& key, T const& value){ lua_State* interpreter = value_wrapper_traits<ValueWrapper>::interpreter( table ); // TODO: Exception safe? value_wrapper_traits<ValueWrapper>::unwrap(interpreter, table); detail::stack_pop pop(interpreter, 1); detail::push(interpreter, key); detail::push(interpreter, value); lua_settable(interpreter, -3);}template<class ValueWrapper, class K>inline object rawget(ValueWrapper const& table, K const& key){ lua_State* interpreter = value_wrapper_traits<ValueWrapper>::interpreter( table ); value_wrapper_traits<ValueWrapper>::unwrap(interpreter, table); detail::stack_pop pop(interpreter, 2); detail::push(interpreter, key); lua_rawget(interpreter, -2); return object(from_stack(interpreter, -1));}template<class ValueWrapper, class K, class T>inline void rawset(ValueWrapper const& table, K const& key, T const& value){ lua_State* interpreter = value_wrapper_traits<ValueWrapper>::interpreter( table ); // TODO: Exception safe? value_wrapper_traits<ValueWrapper>::unwrap(interpreter, table); detail::stack_pop pop(interpreter, 1); detail::push(interpreter, key); detail::push(interpreter, value); lua_rawset(interpreter, -3);}template<class ValueWrapper>inline int type(ValueWrapper const& value){ lua_State* interpreter = value_wrapper_traits<ValueWrapper>::interpreter( value ); value_wrapper_traits<ValueWrapper>::unwrap(interpreter, value); detail::stack_pop pop(interpreter, 1); return lua_type(interpreter, -1);}} // namespace luabind#endif // LUABIND_OBJECT_050419_HPP
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?