object.hpp.svn-base
来自「本人找过多个在linux下c++的lua5.1封装库,但很少.luabind已经」· SVN-BASE 代码 · 共 1,206 行 · 第 1/3 页
SVN-BASE
1,206 行
mutable lua_State* m_interpreter; int m_table_index; int m_key_index; };} // namespace adlnamespace detail{ struct basic_access { static void set(lua_State* interpreter, int table) { lua_settable(interpreter, table); } static void get(lua_State* interpreter, int table) { lua_gettable(interpreter, table); } }; struct raw_access { static void set(lua_State* interpreter, int table) { lua_rawset(interpreter, table); } static void get(lua_State* interpreter, int table) { lua_rawget(interpreter, table); } }; template<class AccessPolicy> class basic_iterator : public boost::iterator_facade< basic_iterator<AccessPolicy> , adl::iterator_proxy<AccessPolicy> , boost::single_pass_traversal_tag , adl::iterator_proxy<AccessPolicy> > { public: basic_iterator() : m_interpreter(0) {} template<class ValueWrapper> explicit basic_iterator(ValueWrapper const& value_wrapper) : m_interpreter( value_wrapper_traits<ValueWrapper>::interpreter(value_wrapper) ) { detail::stack_pop pop(m_interpreter, 1); value_wrapper_traits<ValueWrapper>::unwrap(m_interpreter, value_wrapper); lua_pushnil(m_interpreter); if (lua_next(m_interpreter, -2) != 0) { detail::stack_pop pop(m_interpreter, 2); handle(m_interpreter, -2).swap(m_key); } else { m_interpreter = 0; return; } handle(m_interpreter, -1).swap(m_table); } adl::object key() const; private: friend class boost::iterator_core_access; void increment() { m_table.push(m_interpreter); m_key.push(m_interpreter); detail::stack_pop pop(m_interpreter, 1); if (lua_next(m_interpreter, -2) != 0) { m_key.replace(m_interpreter, -2); lua_pop(m_interpreter, 2); } else { m_interpreter = 0; handle().swap(m_table); handle().swap(m_key); } } bool equal(basic_iterator const& other) const { if (m_interpreter == 0 && other.m_interpreter == 0) return true; if (m_interpreter != other.m_interpreter) return false; detail::stack_pop pop(m_interpreter, 2); m_key.push(m_interpreter); other.m_key.push(m_interpreter); return lua_equal(m_interpreter, -2, -1) != 0; } adl::iterator_proxy<AccessPolicy> dereference() const { return adl::iterator_proxy<AccessPolicy>(m_interpreter, m_table, m_key); } lua_State* m_interpreter; handle m_table; handle m_key; };// Needed because of some strange ADL issues.#define LUABIND_OPERATOR_ADL_WKND(op) \ inline bool operator op( \ basic_iterator<basic_access> const& x \ , basic_iterator<basic_access> const& y) \ { \ return boost::operator op(x, y); \ } \ \ inline bool operator op( \ basic_iterator<raw_access> const& x \ , basic_iterator<raw_access> const& y) \ { \ return boost::operator op(x, y); \ } LUABIND_OPERATOR_ADL_WKND(==) LUABIND_OPERATOR_ADL_WKND(!=)#undef LUABIND_OPERATOR_ADL_WKND } // namespace detailnamespace adl{ #ifdef LUABIND_USE_VALUE_WRAPPER_TAG struct index_proxy_tag; #endif template<class Next> class index_proxy : public object_interface<index_proxy<Next> > { public:#ifdef LUABIND_USE_VALUE_WRAPPER_TAG typedef index_proxy_tag value_wrapper_tag;#endif typedef index_proxy<Next> this_type; template<class Key> index_proxy(Next const& next, lua_State* interpreter, Key const& key) : m_interpreter(interpreter) , m_key_index(lua_gettop(interpreter) + 1) , m_next(next) { detail::push(m_interpreter, key); } index_proxy(index_proxy const& other) : m_interpreter(other.m_interpreter) , m_key_index(other.m_key_index) , m_next(other.m_next) { other.m_interpreter = 0; } ~index_proxy() { if (m_interpreter) lua_pop(m_interpreter, 1); } // This is non-const to prevent conversion on lvalues. operator object(); // this will set the value to nil this_type& operator=(luabind::detail::nil_type) { value_wrapper_traits<Next>::unwrap(m_interpreter, m_next); detail::stack_pop pop(m_interpreter, 1); lua_pushvalue(m_interpreter, m_key_index); lua_pushnil(m_interpreter); lua_settable(m_interpreter, -3); return *this; } template<class T> this_type& operator=(T const& value) { value_wrapper_traits<Next>::unwrap(m_interpreter, m_next); detail::stack_pop pop(m_interpreter, 1); lua_pushvalue(m_interpreter, m_key_index); detail::push(m_interpreter, value); lua_settable(m_interpreter, -3); return *this; } this_type& operator=(this_type const& value) { value_wrapper_traits<Next>::unwrap(m_interpreter, m_next); detail::stack_pop pop(m_interpreter, 1); lua_pushvalue(m_interpreter, m_key_index); detail::push(m_interpreter, value); lua_settable(m_interpreter, -3); return *this; } template<class T> index_proxy<this_type> operator[](T const& key) { return index_proxy<this_type>(*this, m_interpreter, key); } void push(lua_State* interpreter); lua_State* interpreter() const { return m_interpreter; } private: struct hidden_type {}; // this_type& operator=(index_proxy<Next> const&); mutable lua_State* m_interpreter; int m_key_index; Next const& m_next; };} // namespace adltypedef detail::basic_iterator<detail::basic_access> iterator;typedef detail::basic_iterator<detail::raw_access> raw_iterator;#ifndef LUABIND_USE_VALUE_WRAPPER_TAGtemplate<class T>struct value_wrapper_traits<adl::index_proxy<T> >#elsetemplate<>struct value_wrapper_traits<adl::index_proxy_tag>#endif{ typedef boost::mpl::true_ is_specialized; template<class Next> static lua_State* interpreter(adl::index_proxy<Next> const& proxy) { return proxy.interpreter(); } template<class Next> static void unwrap(lua_State* interpreter, adl::index_proxy<Next> const& proxy) { const_cast<adl::index_proxy<Next>&>(proxy).push(interpreter); }};#ifndef LUABIND_USE_VALUE_WRAPPER_TAGtemplate<class AccessPolicy>struct value_wrapper_traits<adl::iterator_proxy<AccessPolicy> >#elsetemplate<>struct value_wrapper_traits<adl::iterator_proxy_tag>#endif{ typedef boost::mpl::true_ is_specialized; template<class Proxy> static lua_State* interpreter(Proxy const& p) { return p.interpreter(); } template<class Proxy> static void unwrap(lua_State* interpreter, Proxy const& p) { // TODO: Why const_cast? const_cast<Proxy&>(p).push(interpreter); }};namespace adl{ class object_init { protected: object_init() {} explicit object_init(from_stack const& stack_reference, boost::mpl::true_) : m_handle(stack_reference.interpreter, stack_reference.index) { } template<class ValueWrapper> explicit object_init(ValueWrapper const& value_wrapper, boost::mpl::false_) { lua_State* interpreter = value_wrapper_traits<ValueWrapper>::interpreter( value_wrapper ); value_wrapper_traits<ValueWrapper>::unwrap(interpreter, value_wrapper); detail::stack_pop pop(interpreter, 1); handle(interpreter, -1).swap(m_handle); } handle m_handle; }; // An object holds a reference to a Lua value residing // in the registry. class object : public object_interface<object> { struct safe_bool_type {}; public: object() {} explicit object(handle const& other) : m_handle(other) {} explicit object(from_stack const& stack_reference) : m_handle(stack_reference.interpreter, stack_reference.index) { } template<class T> object(lua_State* interpreter, T const& value) { detail::push(interpreter, value); detail::stack_pop pop(interpreter, 1); handle(interpreter, -1).swap(m_handle); } template<class T, class Policies> object(lua_State* interpreter, T const& value, Policies const&) { detail::push(interpreter, value, Policies()); detail::stack_pop pop(interpreter, 1); handle(interpreter, -1).swap(m_handle); } void push(lua_State* interpreter) const; lua_State* interpreter() const; bool is_valid() const; operator safe_bool_type*() const; template<class T> index_proxy<object> operator[](T const& key) const { return index_proxy<object>( *this, m_handle.interpreter(), key ); } void swap(object& other) { m_handle.swap(other.m_handle); } private: handle m_handle; }; inline void object::push(lua_State* interpreter) const { m_handle.push(interpreter); } inline lua_State* object::interpreter() const { return m_handle.interpreter(); } inline bool object::is_valid() const { return m_handle.interpreter() != 0; } inline object::operator object::safe_bool_type*() const
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?