| 39 | } // namespace detail |
| 40 | |
| 41 | class stateless_stack_reference { |
| 42 | private: |
| 43 | friend class stack_reference; |
| 44 | |
| 45 | int m_index = 0; |
| 46 | |
| 47 | int registry_index() const noexcept { |
| 48 | return LUA_NOREF; |
| 49 | } |
| 50 | |
| 51 | public: |
| 52 | stateless_stack_reference() noexcept = default; |
| 53 | stateless_stack_reference(lua_nil_t) noexcept : stateless_stack_reference() {}; |
| 54 | stateless_stack_reference(lua_State* L_, int index_) noexcept : stateless_stack_reference(absolute_index(L_, index_)) { |
| 55 | } |
| 56 | stateless_stack_reference(lua_State*, absolute_index index_) noexcept : stateless_stack_reference(index_) { |
| 57 | } |
| 58 | stateless_stack_reference(lua_State*, raw_index index_) noexcept : stateless_stack_reference(index_) { |
| 59 | } |
| 60 | stateless_stack_reference(absolute_index index_) noexcept : m_index(index_) { |
| 61 | } |
| 62 | stateless_stack_reference(raw_index index_) noexcept : m_index(index_) { |
| 63 | } |
| 64 | stateless_stack_reference(lua_State*, ref_index) noexcept = delete; |
| 65 | stateless_stack_reference(ref_index) noexcept = delete; |
| 66 | stateless_stack_reference(const reference&) noexcept = delete; |
| 67 | stateless_stack_reference(const stateless_stack_reference&) noexcept = default; |
| 68 | stateless_stack_reference(stateless_stack_reference&& o) noexcept = default; |
| 69 | stateless_stack_reference& operator=(stateless_stack_reference&&) noexcept = default; |
| 70 | stateless_stack_reference& operator=(const stateless_stack_reference&) noexcept = default; |
| 71 | |
| 72 | int push(lua_State* L_) const noexcept { |
| 73 | #if SOL_IS_ON(SOL_SAFE_STACK_CHECK) |
| 74 | luaL_checkstack(L_, 1, "not enough Lua stack space to push a single reference value"); |
| 75 | #endif // make sure stack doesn't overflow |
| 76 | lua_pushvalue(L_, m_index); |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | void pop(lua_State* L_, int pop_count = 1) const noexcept { |
| 81 | lua_pop(L_, pop_count); |
| 82 | } |
| 83 | |
| 84 | int stack_index() const noexcept { |
| 85 | return m_index; |
| 86 | } |
| 87 | |
| 88 | const void* pointer(lua_State* L_) const noexcept { |
| 89 | const void* pointer_id = lua_topointer(L_, stack_index()); |
| 90 | return pointer_id; |
| 91 | } |
| 92 | |
| 93 | type get_type(lua_State* L_) const noexcept { |
| 94 | int untyped_value = lua_type(L_, stack_index()); |
| 95 | return static_cast<type>(untyped_value); |
| 96 | } |
| 97 | |
| 98 | bool valid(lua_State* L) const noexcept { |