Fix wrapper_base back reference being initialized with wrong value.
[luabind.git] / luabind / handle.hpp
blob14adacbae658941c9a7d1ebd0f6291d0029a10c4
1 // Copyright (c) 2005 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
23 #ifndef LUABIND_HANDLE_050420_HPP
24 #define LUABIND_HANDLE_050420_HPP
26 #include <luabind/lua_include.hpp>
27 #include <luabind/value_wrapper.hpp>
29 namespace luabind {
31 // A reference to a Lua value. Represents an entry in the
32 // registry table.
33 class handle
35 public:
36 handle();
37 handle(lua_State* interpreter, int stack_index);
38 handle(lua_State* main, lua_State* interpreter, int stack_index);
39 handle(handle const& other);
40 ~handle();
42 handle& operator=(handle const& other);
43 void swap(handle& other);
45 void push(lua_State* interpreter) const;
47 lua_State* interpreter() const;
49 void replace(lua_State* interpreter, int stack_index);
51 private:
52 lua_State* m_interpreter;
53 int m_index;
56 inline handle::handle()
57 : m_interpreter(0)
58 , m_index(LUA_NOREF)
61 inline handle::handle(handle const& other)
62 : m_interpreter(other.m_interpreter)
63 , m_index(LUA_NOREF)
65 if (m_interpreter == 0) return;
66 lua_rawgeti(m_interpreter, LUA_REGISTRYINDEX, other.m_index);
67 m_index = luaL_ref(m_interpreter, LUA_REGISTRYINDEX);
70 inline handle::handle(lua_State* interpreter, int stack_index)
71 : m_interpreter(interpreter)
72 , m_index(LUA_NOREF)
74 lua_pushvalue(interpreter, stack_index);
75 m_index = luaL_ref(interpreter, LUA_REGISTRYINDEX);
78 inline handle::handle(lua_State* main, lua_State* interpreter, int stack_index)
79 : m_interpreter(main)
80 , m_index(LUA_NOREF)
82 lua_pushvalue(interpreter, stack_index);
83 m_index = luaL_ref(interpreter, LUA_REGISTRYINDEX);
86 inline handle::~handle()
88 if (m_interpreter && m_index != LUA_NOREF)
89 luaL_unref(m_interpreter, LUA_REGISTRYINDEX, m_index);
92 inline handle& handle::operator=(handle const& other)
94 handle(other).swap(*this);
95 return *this;
98 inline void handle::swap(handle& other)
100 std::swap(m_interpreter, other.m_interpreter);
101 std::swap(m_index, other.m_index);
104 inline void handle::push(lua_State* interpreter) const
106 lua_rawgeti(interpreter, LUA_REGISTRYINDEX, m_index);
109 inline lua_State* handle::interpreter() const
111 return m_interpreter;
114 inline void handle::replace(lua_State* interpreter, int stack_index)
116 lua_pushvalue(interpreter, stack_index);
117 lua_rawseti(interpreter, LUA_REGISTRYINDEX, m_index);
120 template<>
121 struct value_wrapper_traits<handle>
123 typedef boost::mpl::true_ is_specialized;
125 static lua_State* interpreter(handle const& value)
127 return value.interpreter();
130 static void unwrap(lua_State* interpreter, handle const& value)
132 value.push(interpreter);
135 static bool check(...)
137 return true;
141 } // namespace luabind
143 #endif // LUABIND_HANDLE_050420_HPP