⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 object.hpp

📁 魔兽世界的私服源程序
💻 HPP
📖 第 1 页 / 共 3 页
字号:
		raw_iterator raw_end() const
		{
			return raw_iterator(0, LUA_NOREF);
		}

		inline void set() const
		{
			// you are trying to access an invalid object
			assert((m_state != 0) && "you are trying to access an invalid (uninitialized) object");

			allocate_slot();
			lua_rawseti(m_state, LUA_REGISTRYINDEX, m_ref);
		}
		inline lua_State* lua_state() const { return m_state; }
		inline void pushvalue() const
		{
			// you are trying to dereference an invalid object
			assert((m_ref != LUA_NOREF) && "you are trying to access an invalid (uninitialized) object");
			assert((m_state != 0) && "internal error, please report");

			lua_getref(m_state, m_ref);
		}

		void swap(object& rhs);

		template<class T>
		inline object raw_at(const T& key)
		{
			lua_State* L = lua_state();
			pushvalue();
			detail::convert_to_lua(L, key);
			lua_rawget(L, -2);
			int ref = detail::ref(L);
			lua_pop(L, 1);
			return object(L, ref, true);
		}

		template<class T>
		inline object at(const T& key)
		{
			lua_State* L = lua_state();
			pushvalue();
			detail::convert_to_lua(L, key);
			lua_gettable(L, -2);
			int ref = detail::ref(L);
			lua_pop(L, 1);
			return object(L, ref, true);
		}

		template<class T>
		inline detail::proxy_object operator[](const T& key) const
		{
			detail::convert_to_lua(m_state, key);
			int ref = detail::ref(m_state);
			return detail::proxy_object(const_cast<object*>(this), ref);
		}



		// *****************************
		// OPERATOR =

		object& operator=(const object& o) const;
		object& operator=(const detail::proxy_object& o) const;
		object& operator=(const detail::proxy_raw_object& o) const;
		object& operator=(const detail::proxy_array_object& o) const;

		template<class T>
		object& operator=(const T& val) const
		{
			assert((m_state != 0) && "you cannot assign a non-lua value to an uninitialized object");
			// you cannot assign a non-lua value to an uninitialized object

			detail::convert_to_lua(m_state, val);
			set();
			return const_cast<luabind::object&>(*this);
		}


		// *****************************
		// OPERATOR()

		#define BOOST_PP_ITERATION_PARAMS_1 (4, (0, LUABIND_MAX_ARITY, <luabind/object.hpp>, 1))
		#include BOOST_PP_ITERATE()



		inline detail::proxy_object make_proxy(int key)
		{
			return detail::proxy_object(this, key);
		}

		inline detail::proxy_raw_object make_raw_proxy(int key)
		{
			return detail::proxy_raw_object(this, key);
		}

		inline detail::proxy_array_object make_array_proxy(int key)
		{
			return detail::proxy_array_object(this, key);
		}

		// TODO: it's not possible to make object friend with wrapped_constructor_helper::apply (since
		// it's an inner class), that's why this interface is public
//	private:

		object(lua_State* L, int ref, bool/*, reference*/)
			: m_state(L)
			, m_ref(ref)
		{
		}

private:

		void dummy() const {}

		void allocate_slot() const
		{
			if (m_ref == LUA_NOREF)
			{
				lua_pushboolean(m_state, 0);
				m_ref = detail::ref(m_state);
			}
		}

		mutable lua_State* m_state;
		mutable int m_ref;
	};


	// *************************************
	// OBJECT

	inline void object::swap(object& rhs)
	{
		// you cannot swap objects from different lua states
		assert((lua_state() == rhs.lua_state()) && "you cannot swap objects from different lua states");
		std::swap(m_ref, rhs.m_ref);
	}

	inline object object::iterator::key() const
	{
		lua_State* L = m_obj->lua_state();
		detail::getref(L, m_key);
		return object(L, detail::ref(L), true);
	}

	inline object object::raw_iterator::key() const
	{
		lua_State* L = m_obj->lua_state();
		detail::getref(L, m_key);
		return object(L, detail::ref(L), true);
	}

	namespace detail
	{

		// *************************************
		// PROXY CALLER

#if !defined(BOOST_MSVC) || (defined(BOOST_MSVC) && (BOOST_MSVC > 1300))
		template<class Tuple>
		template<class Policies>
		luabind::object proxy_caller<Tuple>::operator[](const Policies& p)
		{
			m_called = true;
			lua_State* L = m_obj->lua_state();
			m_obj->pushvalue();
			detail::push_args_from_tuple<1>::apply(L, m_args, p);
			if (lua_pcall(L, boost::tuples::length<Tuple>::value, 1, 0))
			{ 
#ifndef LUABIND_NO_EXCEPTIONS
				throw error(L);
#else
				error_callback_fun e = detail::error_callback::get().err;
				if (e) e(L);
	
				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
			}
			int ref = detail::ref(L);
			return luabind::object(m_obj->lua_state(), ref, true/*luabind::object::reference()*/);
		}
#endif
		// *************************************
		// PROXY OBJECT

#if !defined(BOOST_MSVC) || (defined(BOOST_MSVC) && (BOOST_MSVC > 1300))
		template<class T>
		inline object proxy_object::raw_at(const T& key)
		LUABIND_PROXY_RAW_AT_BODY

		template<class T>
		inline object proxy_object::at(const T& key)
		LUABIND_PROXY_AT_BODY
#endif

		inline lua_State* proxy_object::lua_state() const
		{
			return m_obj->lua_state();
		}

		inline proxy_object::operator luabind::object()
		{
			lua_State* L = m_obj->lua_state();
			pushvalue();
			int ref = detail::ref(L);
			return luabind::object(L, ref, true/*luabind::object::reference()*/);
		}


		// *************************************
		// PROXY ARRAY OBJECT

#if !defined(BOOST_MSVC) || (defined(BOOST_MSVC) && (BOOST_MSVC > 1300))
		template<class T>
		inline object proxy_array_object::raw_at(const T& key)
		LUABIND_PROXY_ARRAY_RAW_AT_BODY

		template<class T>
		inline object proxy_array_object::at(const T& key)
		LUABIND_PROXY_ARRAY_AT_BODY
#endif

#undef LUABIND_PROXY_ARRAY_AT_BODY
#undef LUABIND_PROXY_ARRAY_RAW_AT_BODY

		inline lua_State* proxy_array_object::lua_state() const
		{
			return m_obj->lua_state();
		}

		inline proxy_array_object::operator luabind::object()
		{
			lua_State* L = m_obj->lua_state();
			pushvalue();
			int ref = detail::ref(L);
			return luabind::object(L, ref, true/*luabind::object::reference()*/);
		}


		// *************************************
		// PROXY RAW OBJECT

#if !defined(BOOST_MSVC) || (defined(BOOST_MSVC) && (BOOST_MSVC > 1300))
		template<class T>
		inline object proxy_raw_object::raw_at(const T& key)
		LUABIND_PROXY_RAW_AT_BODY

		template<class T>
		inline object proxy_raw_object::at(const T& key)
		LUABIND_PROXY_AT_BODY
#endif

#undef LUABIND_PROXY_RAW_AT_BODY
#undef LUABIND_PROXY_AT_BODY

		inline lua_State* proxy_raw_object::lua_state() const
		{
			return m_obj->lua_state();
		}

		inline proxy_raw_object::operator luabind::object()
		{
			lua_State* L = lua_state();
			pushvalue();
			int ref = detail::ref(L);
			return luabind::object(L, ref, true/*luabind::object::reference()*/);
		}


		// *************************************
		// PROXY CALLER


		template<class Tuple>
		proxy_caller<Tuple>::~proxy_caller()
		{
			if (m_called) return;

			m_called = true;
			lua_State* L = m_obj->lua_state();
			m_obj->pushvalue();

			push_args_from_tuple<1>::apply(L, m_args);
			if (lua_pcall(L, boost::tuples::length<Tuple>::value, 0, 0))
			{ 
#ifndef LUABIND_NO_EXCEPTIONS
				throw luabind::error(L);
#else
				error_callback_fun e = detail::error_callback::get().err;
				if (e) e(L);
	
				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
			}
		}

		template<class Tuple>
		proxy_caller<Tuple>::operator luabind::object()
		{
			m_called = true;
			lua_State* L = m_obj->lua_state();
			m_obj->pushvalue();

			push_args_from_tuple<1>::apply(L, m_args);
			if (lua_pcall(L, boost::tuples::length<Tuple>::value, 1, 0))
			{ 
#ifndef LUABIND_NO_EXCEPTIONS
				throw luabind::error(L);
#else
				error_callback_fun e = detail::error_callback::get().err;
				if (e) e(L);
	
				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
			}
			int ref = detail::ref(L);
			return luabind::object(m_obj->lua_state(), ref, true/*luabind::object::reference()*/);
		}

	}

#define LUABIND_DECLARE_OPERATOR(MACRO)\
	MACRO(object, object) \
	MACRO(object, detail::proxy_object) \
	MACRO(object, detail::proxy_array_object) \
	MACRO(object, detail::proxy_raw_object) \
	MACRO(detail::proxy_object, object) \
	MACRO(detail::proxy_object, detail::proxy_object) \
	MACRO(detail::proxy_object, detail::proxy_array_object) \
	MACRO(detail::proxy_object, detail::proxy_raw_object) \
	MACRO(detail::proxy_array_object, object) \
	MACRO(detail::proxy_array_object, detail::proxy_object) \
	MACRO(detail::proxy_array_object, detail::proxy_array_object) \
	MACRO(detail::proxy_array_object, detail::proxy_raw_object) \
	MACRO(detail::proxy_raw_object, object) \
	MACRO(detail::proxy_raw_object, detail::proxy_object) \
	MACRO(detail::proxy_raw_object, detail::proxy_array_object) \
	MACRO(detail::proxy_raw_object, detail::proxy_raw_object)


#define LUABIND_EQUALITY_OPERATOR(lhs, rhs) bool operator==(const lhs&, const rhs&);
	LUABIND_DECLARE_OPERATOR(LUABIND_EQUALITY_OPERATOR)
#undef LUABIND_EQUALITY_OPERATOR

#define LUABIND_LESSTHAN_OPERATOR(lhs, rhs) bool operator<(const lhs&, const rhs&);
	LUABIND_DECLARE_OPERATOR(LUABIND_LESSTHAN_OPERATOR)
#undef LUABIND_LESSTHAN_OPERATOR

#define LUABIND_LESSOREQUAL_OPERATOR(lhs_t, rhs_t) bool operator<=(const lhs_t&, const rhs_t&);
	LUABIND_DECLARE_OPERATOR(LUABIND_LESSOREQUAL_OPERATOR)
#undef LUABIND_LESSOREQUAL_OPERATOR

#define LUABIND_INEQUALITY_OPERATOR(lhs_t, rhs_t)\
	inline bool operator!=(const rhs_t& rhs, const lhs_t& lhs) \
	{ \
		return !(rhs == lhs); \
	}

	LUABIND_DECLARE_OPERATOR(LUABIND_INEQUALITY_OPERATOR)

#undef LUABIND_INEQUALITY_OPERATOR

#define LUABIND_GREATEROREQUAL_OPERATOR(lhs_t, rhs_t)\
	inline bool operator>=(const rhs_t& rhs, const lhs_t& lhs) \
	{ \
		return !(rhs < lhs); \
	}

	LUABIND_DECLARE_OPERATOR(LUABIND_GREATEROREQUAL_OPERATOR)

#undef LUABIND_GREATEROREQUAL_OPERATOR

#define LUABIND_GREATERTHAN_OPERATOR(lhs_t, rhs_t)\
	inline bool operator>(const lhs_t& lhs, const rhs_t& rhs) \
	{ \
		return !(lhs <= rhs); \
	}

	LUABIND_DECLARE_OPERATOR(LUABIND_GREATERTHAN_OPERATOR)
#undef LUABIND_GREATERTHAN_OPERATOR

#undef LUABIND_DECLARE_OPERATOR

}

namespace std
{

#define LUABIND_DEFINE_SWAP(t1,t2)\
	inline void swap(t1 lhs, t2 rhs)\
	{\
			assert((lhs.lua_state() == rhs.lua_state()) && "you cannot swap objects from different lua states");\
			rhs.pushvalue();\
			lhs.pushvalue();\
			rhs.set();\
			lhs.set();\
	}

	inline void swap(luabind::object& lhs, luabind::object& rhs)
	{
		lhs.swap(rhs);
	}

	// object against all other
	LUABIND_DEFINE_SWAP(luabind::object&, const luabind::detail::proxy_object&)
	LUABIND_DEFINE_SWAP(luabind::object&, const luabind::detail::proxy_raw_object&)
	LUABIND_DEFINE_SWAP(luabind::object&, const luabind::detail::proxy_array_object&)
	LUABIND_DEFINE_SWAP(const luabind::detail::proxy_object&, luabind::object&)
	LUABIND_DEFINE_SWAP(const luabind::detail::proxy_raw_object&, luabind::object&)
	LUABIND_DEFINE_SWAP(const luabind::detail::proxy_array_object&, luabind::object&)

	// proxy_object against all other
	LUABIND_DEFINE_SWAP(const luabind::detail::proxy_object&, const luabind::detail::proxy_object&)
	LUABIND_DEFINE_SWAP(const luabind::detail::proxy_object&, const luabind::detail::proxy_raw_object&)
	LUABIND_DEFINE_SWAP(const luabind::detail::proxy_object&, const luabind::detail::proxy_array_object&)
	LUABIND_DEFINE_SWAP(const luabind::detail::proxy_raw_object&, const luabind::detail::proxy_object&)
	LUABIND_DEFINE_SWAP(const luabind::detail::proxy_array_object&, const luabind::detail::proxy_object&)

	// proxy_raw_object against all other
	LUABIND_DEFINE_SWAP(const luabind::detail::proxy_raw_object&, const luabind::detail::proxy_raw_object&)
	LUABIND_DEFINE_SWAP(const luabind::detail::proxy_raw_object&, const luabind::detail::proxy_array_object&)
	LUABIND_DEFINE_SWAP(const luabind::detail::proxy_array_object&, const luabind::detail::proxy_raw_object&)

	// proxy_array_object against all other
	LUABIND_DEFINE_SWAP(const luabind::detail::proxy_array_object&, const luabind::detail::proxy_array_object&)

#undef LUABIND_DEFINE_SWAP

} // std

#endif // LUABIND_OBJECT_HPP_INCLUDED

#elif BOOST_PP_ITERATION_FLAGS() == 1

#define LUABIND_TUPLE_PARAMS(z, n, data) const A##n *
#define LUABIND_OPERATOR_PARAMS(z, n, data) const A##n & a##n

#if BOOST_PP_ITERATION() > 0
	template<BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), class A)>
#endif
	detail::proxy_caller<boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> >
	operator()(BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_OPERATOR_PARAMS, _)) const
	{
		typedef boost::tuples::tuple<BOOST_PP_ENUM(BOOST_PP_ITERATION(), LUABIND_TUPLE_PARAMS, _)> tuple_t;
#if BOOST_PP_ITERATION() == 0
		tuple_t args;
#else
		tuple_t args(BOOST_PP_ENUM_PARAMS(BOOST_PP_ITERATION(), &a));
#endif
		return detail::proxy_caller<tuple_t>(const_cast<luabind::object*>(this), args);
	}

#undef LUABIND_OPERATOR_PARAMS
#undef LUABIND_TUPLE_PARAMS

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -