Add missing scope::operator=.
[luabind.git] / src / weak_ref.cpp
blob245b26c2e1b9b1b8aa335717274fcf471a7fb0c5
1 // Copyright (c) 2004 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 #define LUABIND_BUILDING
25 #include <algorithm>
27 #include <luabind/lua_include.hpp>
29 #include <luabind/config.hpp>
30 #include <luabind/weak_ref.hpp>
31 #include <cassert>
33 namespace luabind {
35 namespace
38 int weak_table_tag;
40 } // namespace unnamed
42 LUABIND_API void get_weak_table(lua_State* L)
44 lua_pushlightuserdata(L, &weak_table_tag);
45 lua_rawget(L, LUA_REGISTRYINDEX);
47 if (lua_isnil(L, -1))
49 lua_pop(L, 1);
50 lua_newtable(L);
51 // metatable
52 lua_newtable(L);
53 lua_pushliteral(L, "__mode");
54 lua_pushliteral(L, "v");
55 lua_rawset(L, -3);
56 // set metatable
57 lua_setmetatable(L, -2);
59 lua_pushlightuserdata(L, &weak_table_tag);
60 lua_pushvalue(L, -2);
61 lua_rawset(L, LUA_REGISTRYINDEX);
65 } // namespace luabind
67 namespace luabind
70 struct weak_ref::impl
72 impl(lua_State* main, lua_State* s, int index)
73 : count(0)
74 , state(main)
75 , ref(0)
77 get_weak_table(s);
78 lua_pushvalue(s, index);
79 ref = luaL_ref(s, -2);
80 lua_pop(s, 1);
83 ~impl()
85 get_weak_table(state);
86 luaL_unref(state, -1, ref);
87 lua_pop(state, 1);
90 int count;
91 lua_State* state;
92 int ref;
95 weak_ref::weak_ref()
96 : m_impl(0)
100 weak_ref::weak_ref(lua_State* main, lua_State* L, int index)
101 : m_impl(new impl(main, L, index))
103 m_impl->count = 1;
106 weak_ref::weak_ref(weak_ref const& other)
107 : m_impl(other.m_impl)
109 if (m_impl) ++m_impl->count;
112 weak_ref::~weak_ref()
114 if (m_impl && --m_impl->count == 0)
116 delete m_impl;
120 weak_ref& weak_ref::operator=(weak_ref const& other)
122 weak_ref(other).swap(*this);
123 return *this;
126 void weak_ref::swap(weak_ref& other)
128 std::swap(m_impl, other.m_impl);
131 int weak_ref::id() const
133 assert(m_impl);
134 return m_impl->ref;
137 // L may not be the same pointer as
138 // was used when creating this reference
139 // since it may be a thread that shares
140 // the same globals table.
141 void weak_ref::get(lua_State* L) const
143 assert(m_impl);
144 assert(L);
145 get_weak_table(L);
146 lua_rawgeti(L, -1, m_impl->ref);
147 lua_remove(L, -2);
150 lua_State* weak_ref::state() const
152 assert(m_impl);
153 return m_impl->state;
156 } // namespace luabind