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

📄 object.hpp

📁 魔兽世界的私服源程序
💻 HPP
📖 第 1 页 / 共 3 页
字号:
				return object(m_state, ref, true);									\
			}

#if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
			template<class T>	
			inline object at(const T& key)
			LUABIND_PROXY_ARRAY_AT_BODY
#else
			template<class T>
			inline object at(const T& key);
#endif


#if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
			template<class T>	
			inline object raw_at(const T& key)
			LUABIND_PROXY_ARRAY_RAW_AT_BODY
#else
			template<class T>
			inline object raw_at(const T& key);
#endif

			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);
			}

			inline bool is_valid() const { return true; }
			lua_State* lua_state() const;
			void pushvalue() const;
			void set() const;

			// this is a safe substitute for an implicit converter to bool
			typedef void (proxy_array_object::*member_ptr)() const;
			operator member_ptr() const
			{
				if (is_valid()) return &proxy_array_object::dummy;
				return 0;
			}

		private:

			void dummy() const {}

			proxy_array_object(luabind::object* o, int key)
				: m_obj(o)
				, m_key(key)
			{
			}
			luabind::object* m_obj;
			int m_key;
		};

		template<class T>
		struct primitive_converter;

	} // detail




	class object
	{

#if !(defined (BOOST_MSVC) && (BOOST_MSVC <= 1200))

	template<class T>
	friend T object_cast(const object& obj);
	template<class T>
	friend struct detail::primitive_converter;

#endif

	friend object get_globals(lua_State*);
	friend object get_registry(lua_State*);
	friend object newtable(lua_State*);
	friend class detail::proxy_object;
	friend class detail::proxy_array_object;
	friend class detail::proxy_raw_object;

	public:

		class array_iterator
		{
		friend class object;
		public:

			typedef std::forward_iterator_tag iterator_category;
			typedef luabind::object value_type;
			typedef value_type& reference;
			typedef value_type* pointer;
			typedef void difference_type;

			array_iterator()
				: m_obj(0)
				, m_key(LUA_NOREF)
			{
			}

			array_iterator(const array_iterator& iter)
				: m_obj(iter.m_obj)
				, m_key(iter.m_key)
			{
			}

			~array_iterator() {}

			array_iterator& operator=(const array_iterator& rhs)
			{
				//std::cout << "===\n";
				m_obj = rhs.m_obj;
				m_key = rhs.m_key;
				return *this;
			}

			detail::proxy_array_object operator*()
			{
				return m_obj->make_array_proxy(m_key);
			}

			inline array_iterator& operator++()
			{
				m_key++;

				// invalidate the iterator if we hit a nil element
				lua_State* L = m_obj->lua_state();
				m_obj->pushvalue();
				lua_rawgeti(L, -1, m_key);
				if (lua_isnil(L, -1)) m_key = LUA_NOREF;
				lua_pop(L, 1);

				return *this;
			}

			inline array_iterator operator++(int)
			{
				int old_key = m_key;
				m_key++;

				// invalidate the iterator if we hit a nil element
				lua_State* L = m_obj->lua_state();
				m_obj->pushvalue();
				lua_rawgeti(L, -1, m_key);
				if (lua_isnil(L, -1)) m_key = LUA_NOREF;
				lua_pop(L, 1);

				return array_iterator(m_obj, old_key);
			}

			bool operator!=(const array_iterator& rhs) const
			{
				return m_obj != rhs.m_obj || m_key != rhs.m_key;
			}

		private:

			array_iterator(object* obj, int key)
				: m_obj(obj)
				, m_key(key)
			{
			}

			object* m_obj;
			int m_key;
		};






		class iterator
		{
		friend class object;
		public:

			typedef std::forward_iterator_tag iterator_category;
			typedef luabind::object value_type;
			typedef value_type& reference;
			typedef value_type* pointer;
			typedef void difference_type;

			iterator()
				: m_obj(0)
				, m_key(LUA_NOREF)
			{
			}

			iterator(const iterator& iter)
				: m_obj(iter.m_obj)
				, m_key(LUA_NOREF)
			{
				if (m_obj)
				{
					lua_State* L = m_obj->lua_state();
					detail::getref(L, iter.m_key);
					m_key = detail::ref(L);
				}
			}

			~iterator()
			{
				if (m_obj && m_key != LUA_NOREF) detail::unref(m_obj->lua_state(), m_key);
			}

			iterator& operator=(const iterator& rhs)
			{
				//std::cout << "===\n";
				m_obj = rhs.m_obj;
				if (m_obj)
				{
					lua_State* L = m_obj->lua_state();
					detail::getref(L, rhs.m_key);
					m_key = detail::ref(L);
				}
				else
				{
					m_key = LUA_NOREF;
				}
				return *this;
			}

			detail::proxy_object operator*()
			{
				return m_obj->make_proxy(m_key);
			}

			iterator& operator++()
			{
				lua_State* L = m_obj->lua_state();
				m_obj->pushvalue();
				detail::getref(L, m_key);

				if (lua_next(L, -2) != 0)
				{
					lua_pop(L, 1);
					lua_rawseti(L, LUA_REGISTRYINDEX, m_key);
					lua_pop(L, 1);
				}
				else
				{
					lua_pop(L, 1);
					detail::unref(L, m_key);
					m_obj = 0;
					m_key = LUA_NOREF;
				}

				return *this;
			}

			bool operator!=(const iterator& rhs) const
			{
				return m_obj != rhs.m_obj || m_key != rhs.m_key;
			}

			object key() const;

		private:

			iterator(object* obj, int key)
				: m_obj(obj)
				, m_key(key)
			{
			}

			object* m_obj;
			int m_key;
		};




		class raw_iterator
		{
		friend class object;
		public:

			typedef std::forward_iterator_tag iterator_category;
			typedef luabind::object value_type;
			typedef value_type& reference;
			typedef value_type* pointer;
			typedef void difference_type;

			raw_iterator()
				: m_obj(0)
				, m_key(LUA_NOREF)
			{
			}

			raw_iterator(const raw_iterator& iter)
				: m_obj(iter.m_obj)
				, m_key(LUA_NOREF)
			{
				if (m_obj)
				{
					lua_State* L = m_obj->lua_state();
					detail::getref(L, iter.m_key);
					m_key = detail::ref(L);
				}
			}

			~raw_iterator()
			{
				if (m_obj && m_key != LUA_NOREF) detail::unref(m_obj->lua_state(), m_key);
			}

			raw_iterator& operator=(const raw_iterator& rhs)
			{
				//std::cout << "===\n";
				m_obj = rhs.m_obj;
				if (m_obj)
				{
					lua_State* L = m_obj->lua_state();
					detail::getref(L, rhs.m_key);
					m_key = detail::ref(L);
				}
				else
				{
					m_key = LUA_NOREF;
				}
				return *this;
			}

			detail::proxy_raw_object operator*()
			{
				return m_obj->make_raw_proxy(m_key);
			}

			raw_iterator& operator++()
			{
				lua_State* L = m_obj->lua_state();
				m_obj->pushvalue();
				detail::getref(L, m_key);

				if (lua_next(L, -2) != 0)
				{
					lua_pop(L, 1);
					lua_rawseti(L, LUA_REGISTRYINDEX, m_key);
					lua_pop(L, 1);
				}
				else
				{
					lua_pop(L, 1);
					detail::unref(L, m_key);
					m_obj = 0;
					m_key = LUA_NOREF;
				}

				return *this;
			}

			object key() const;

			bool operator!=(const raw_iterator& rhs) const
			{
				return m_obj != rhs.m_obj || m_key != rhs.m_key;
			}

		private:

			raw_iterator(object* obj, int key)
				: m_obj(obj)
				, m_key(key)
			{
			}

			object* m_obj;
			int m_key;
		};






		object()
			: m_state(0)
			, m_ref(LUA_NOREF)
		{
		}

		object(lua_State* L)
			: m_state(L)
			, m_ref(LUA_NOREF)
		{
		}

		template<class T>
		object(lua_State* L, const T& val)
			: m_state(L)
			, m_ref(LUA_NOREF)
		{
			*this = val;
		}

		object(const object& o)
			: m_state(o.m_state)
			, m_ref(LUA_NOREF)
		{
			lua_getref(m_state, o.m_ref);
			m_ref = detail::ref(m_state);
		}

		inline ~object()
		{
			// If you crash in the detail::unref() call you have probably
			// closed the lua_State before destructing all object instances.
			if (m_ref != LUA_NOREF) detail::unref(m_state, m_ref);
		}

		inline bool is_valid() const { return m_ref != LUA_NOREF; }

		// this is a safe substitute for an implicit converter to bool
		typedef void (object::*member_ptr)() const;
		operator member_ptr() const
		{
			if (is_valid()) return &object::dummy;
			return 0;
		}

		int type() const
		{
			pushvalue();
			detail::stack_pop p(lua_state(), 1);
			return lua_type(lua_state(), -1);
		}

		inline iterator begin() const
		{
			lua_getref(m_state, m_ref);
			lua_pushnil(m_state);
			lua_next(m_state, -2);
			lua_pop(m_state, 1);
			iterator i(const_cast<object*>(this), detail::ref(m_state));
			lua_pop(m_state, 1);
			return i;
		}

		inline iterator end() const
		{
			return iterator(0, LUA_NOREF);
		}

		inline array_iterator abegin() const
		{
			return array_iterator(const_cast<object*>(this), 1);
		}

		inline array_iterator aend() const
		{
			return array_iterator(const_cast<object*>(this), LUA_NOREF);
		}

		raw_iterator raw_begin() const
		{
			lua_getref(m_state, m_ref);
			lua_pushnil(m_state);
			lua_next(m_state, -2);
			lua_pop(m_state, 1);
			raw_iterator i(const_cast<object*>(this), detail::ref(m_state));
			lua_pop(m_state, 1);
			return i;
		}

⌨️ 快捷键说明

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