Add missing scope::operator=.
[luabind.git] / src / scope.cpp
blob6495687b9ed07c86d2dd4ca116c61ba9eef3cf07
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 <luabind/lua_include.hpp>
27 #include <luabind/scope.hpp>
28 #include <luabind/detail/debug.hpp>
29 #include <luabind/detail/stack_utils.hpp>
30 #include <cassert>
32 namespace luabind { namespace detail {
34 registration::registration()
35 : m_next(0)
39 registration::~registration()
41 delete m_next;
44 } // namespace detail
46 scope::scope()
47 : m_chain(0)
51 scope::scope(std::auto_ptr<detail::registration> reg)
52 : m_chain(reg.release())
56 scope::scope(scope const& other)
57 : m_chain(other.m_chain)
59 const_cast<scope&>(other).m_chain = 0;
62 scope& scope::operator=(scope const& other_)
64 delete m_chain;
65 m_chain = other_.m_chain;
66 const_cast<scope&>(other_).m_chain = 0;
67 return *this;
70 scope::~scope()
72 delete m_chain;
75 scope& scope::operator,(scope s)
77 if (!m_chain)
79 m_chain = s.m_chain;
80 s.m_chain = 0;
81 return *this;
84 for (detail::registration* c = m_chain;; c = c->m_next)
86 if (!c->m_next)
88 c->m_next = s.m_chain;
89 s.m_chain = 0;
90 break;
94 return *this;
97 void scope::register_(lua_State* L) const
99 for (detail::registration* r = m_chain; r != 0; r = r->m_next)
101 LUABIND_CHECK_STACK(L);
102 r->register_(L);
106 } // namespace luabind
108 namespace luabind {
110 namespace {
112 struct lua_pop_stack
114 lua_pop_stack(lua_State* L)
115 : m_state(L)
119 ~lua_pop_stack()
121 lua_pop(m_state, 1);
124 lua_State* m_state;
127 } // namespace unnamed
129 module_::module_(lua_State* L, char const* name = 0)
130 : m_state(L)
131 , m_name(name)
135 void module_::operator[](scope s)
137 if (m_name)
139 lua_pushstring(m_state, m_name);
140 lua_gettable(m_state, LUA_GLOBALSINDEX);
142 if (!lua_istable(m_state, -1))
144 lua_pop(m_state, 1);
146 lua_newtable(m_state);
147 lua_pushstring(m_state, m_name);
148 lua_pushvalue(m_state, -2);
149 lua_settable(m_state, LUA_GLOBALSINDEX);
152 else
154 lua_pushvalue(m_state, LUA_GLOBALSINDEX);
157 lua_pop_stack guard(m_state);
159 s.register_(m_state);
162 struct namespace_::registration_ : detail::registration
164 registration_(char const* name)
165 : m_name(name)
169 void register_(lua_State* L) const
171 LUABIND_CHECK_STACK(L);
172 assert(lua_gettop(L) >= 1);
174 lua_pushstring(L, m_name);
175 lua_gettable(L, -2);
177 detail::stack_pop p(L, 1); // pops the table on exit
179 if (!lua_istable(L, -1))
181 lua_pop(L, 1);
183 lua_newtable(L);
184 lua_pushstring(L, m_name);
185 lua_pushvalue(L, -2);
186 lua_settable(L, -4);
189 m_scope.register_(L);
192 char const* m_name;
193 scope m_scope;
196 namespace_::namespace_(char const* name)
197 : scope(std::auto_ptr<detail::registration>(
198 m_registration = new registration_(name)))
202 namespace_& namespace_::operator[](scope s)
204 m_registration->m_scope.operator,(s);
205 return *this;
208 } // namespace luabind