Fix bug when passing type with holder by value to Lua.
[luabind.git] / luabind / detail / ref.hpp
blobe89c0eec653957ee3babd97946f68ebc0c5d3a86
1 // Copyright (c) 2003 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.
24 #ifndef LUABIND_REF_HPP_INCLUDED
25 #define LUABIND_REF_HPP_INCLUDED
27 #include <cassert>
28 #include <algorithm>
30 #include <luabind/config.hpp>
31 #include <luabind/lua_include.hpp>
33 struct lua_State;
35 namespace luabind
38 #if LUA_VERSION_NUM >= 501
39 #define LUA_REFNIL (-1)
40 #undef luaL_setn
41 #undef luaL_getn
42 #endif
44 namespace detail
47 int LUABIND_API ref(lua_State *L);
48 void LUABIND_API unref(lua_State *L, int ref);
50 inline void getref(lua_State* L, int r)
52 lua_rawgeti(L, LUA_REGISTRYINDEX, r);
55 struct lua_reference
57 lua_reference(lua_State* L_ = 0)
58 : L(L_)
59 , m_ref(LUA_NOREF)
61 lua_reference(lua_reference const& r)
62 : L(r.L)
63 , m_ref(LUA_NOREF)
65 if (!r.is_valid()) return;
66 r.get(L);
67 set(L);
69 ~lua_reference() { reset(); }
71 lua_State* state() const { return L; }
73 void operator=(lua_reference const& r)
75 // TODO: self assignment problems
76 reset();
77 if (!r.is_valid()) return;
78 r.get(r.state());
79 set(r.state());
82 bool is_valid() const
83 { return m_ref != LUA_NOREF; }
85 void set(lua_State* L_)
87 reset();
88 L = L_;
89 m_ref = ref(L);
92 void replace(lua_State* L_)
94 lua_rawseti(L_, LUA_REGISTRYINDEX, m_ref);
97 // L may not be the same pointer as
98 // was used when creating this reference
99 // since it may be a thread that shares
100 // the same globals table.
101 void get(lua_State* L_) const
103 assert(m_ref != LUA_NOREF);
104 assert(L_);
105 getref(L_, m_ref);
108 void reset()
110 if (L && m_ref != LUA_NOREF) unref(L, m_ref);
111 m_ref = LUA_NOREF;
114 void swap(lua_reference& r)
116 assert(r.L == L);
117 std::swap(r.m_ref, m_ref);
120 private:
121 lua_State* L;
122 int m_ref;
127 #endif // LUABIND_REF_HPP_INCLUDED