*** empty log message ***
[luabind.git] / src / scope.cpp
blobede7a4db1248c3c281c1041c7d1f77ec7b2211e1
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 #include <luabind/lua_include.hpp>
25 #include <luabind/scope.hpp>
26 #include <cassert>
28 namespace luabind { namespace detail {
30 registration::registration()
31 : m_next(0)
35 registration::~registration()
37 delete m_next;
40 } // namespace detail
42 scope::scope()
43 : m_chain(0)
47 scope::scope(std::auto_ptr<detail::registration> reg)
48 : m_chain(reg.release())
52 scope::scope(scope const& other)
53 : m_chain(other.m_chain)
55 const_cast<scope&>(other).m_chain = 0;
58 scope::~scope()
60 delete m_chain;
63 scope& scope::operator,(scope s)
65 if (!m_chain)
67 m_chain = s.m_chain;
68 s.m_chain = 0;
69 return *this;
72 for (detail::registration* c = m_chain;; c = c->m_next)
74 if (!c->m_next)
76 c->m_next = s.m_chain;
77 s.m_chain = 0;
78 break;
82 return *this;
85 void scope::register_(lua_State* L) const
87 for (detail::registration* r = m_chain; r != 0; r = r->m_next)
89 int n = lua_gettop(L);
91 r->register_(L);
93 assert(n == lua_gettop(L));
97 } // namespace luabind
99 namespace luabind {
101 namespace {
103 struct lua_pop_stack
105 lua_pop_stack(lua_State* L)
106 : m_state(L)
110 ~lua_pop_stack()
112 lua_pop(m_state, 1);
115 lua_State* m_state;
118 } // namespace unnamed
120 module_::module_(lua_State* L, char const* name = 0)
121 : m_state(L)
122 , m_name(name)
126 void module_::operator[](scope s)
128 if (m_name)
130 lua_pushstring(m_state, m_name);
131 lua_gettable(m_state, LUA_GLOBALSINDEX);
133 if (!lua_istable(m_state, -1))
135 lua_pop(m_state, 1);
137 lua_newtable(m_state);
138 lua_pushstring(m_state, m_name);
139 lua_pushvalue(m_state, -2);
140 lua_settable(m_state, LUA_GLOBALSINDEX);
143 else
145 lua_pushvalue(m_state, LUA_GLOBALSINDEX);
148 lua_pop_stack guard(m_state);
150 s.register_(m_state);
153 struct namespace_::registration_ : detail::registration
155 registration_(char const* name)
156 : m_name(name)
160 void register_(lua_State* L) const
162 int n = lua_gettop(L);
164 assert(n >= 1);
166 lua_pushstring(L, m_name);
167 lua_gettable(L, -2);
169 if (!lua_istable(L, -1))
171 lua_pop(L, 1);
173 lua_newtable(L);
174 lua_pushstring(L, m_name);
175 lua_pushvalue(L, -2);
176 lua_settable(L, -4);
179 m_scope.register_(L);
181 lua_pop(L, 1);
183 assert(lua_gettop(L) == n);
186 char const* m_name;
187 scope m_scope;
190 namespace_::namespace_(char const* name)
191 : scope(std::auto_ptr<detail::registration>(
192 m_registration = new registration_(name)))
196 namespace_& namespace_::operator[](scope s)
198 m_registration->m_scope.operator,(s);
199 return *this;
202 } // namespace luabind