Initial commit, includes Lua with broken Luabind as a backup for branching purposes
[terrastrategy.git] / src / luabind / scope.cpp
blob73ccfdf35578dba9750cdf457f563dca900af9f2
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 <luabind/detail/debug.hpp>
27 #include <luabind/detail/stack_utils.hpp>
28 #include <cassert>
30 namespace luabind { namespace detail {
32 registration::registration()
33 : m_next(0)
37 registration::~registration()
39 delete m_next;
42 } // namespace detail
44 scope::scope()
45 : m_chain(0)
49 scope::scope(std::auto_ptr<detail::registration> reg)
50 : m_chain(reg.release())
54 scope::scope(scope const& other)
55 : m_chain(other.m_chain)
57 const_cast<scope&>(other).m_chain = 0;
60 scope::~scope()
62 delete m_chain;
65 scope& scope::operator,(scope s)
67 if (!m_chain)
69 m_chain = s.m_chain;
70 s.m_chain = 0;
71 return *this;
74 for (detail::registration* c = m_chain;; c = c->m_next)
76 if (!c->m_next)
78 c->m_next = s.m_chain;
79 s.m_chain = 0;
80 break;
84 return *this;
87 void scope::register_(lua_State* L) const
89 for (detail::registration* r = m_chain; r != 0; r = r->m_next)
91 LUABIND_CHECK_STACK(L);
92 r->register_(L);
96 } // namespace luabind
98 namespace luabind {
100 namespace {
102 struct lua_pop_stack
104 lua_pop_stack(lua_State* L)
105 : m_state(L)
109 ~lua_pop_stack()
111 lua_pop(m_state, 1);
114 lua_State* m_state;
117 } // namespace unnamed
119 module_::module_(lua_State* L, char const* name = 0)
120 : m_state(L)
121 , m_name(name)
125 void module_::operator[](scope s)
127 if (m_name)
129 lua_pushstring(m_state, m_name);
130 lua_gettable(m_state, LUA_GLOBALSINDEX);
132 if (!lua_istable(m_state, -1))
134 lua_pop(m_state, 1);
136 lua_newtable(m_state);
137 lua_pushstring(m_state, m_name);
138 lua_pushvalue(m_state, -2);
139 lua_settable(m_state, LUA_GLOBALSINDEX);
142 else
144 lua_pushvalue(m_state, LUA_GLOBALSINDEX);
147 lua_pop_stack guard(m_state);
149 s.register_(m_state);
152 struct namespace_::registration_ : detail::registration
154 registration_(char const* name)
155 : m_name(name)
159 void register_(lua_State* L) const
161 LUABIND_CHECK_STACK(L);
162 assert(lua_gettop(L) >= 1);
164 lua_pushstring(L, m_name);
165 lua_gettable(L, -2);
167 detail::stack_pop p(L, 1); // pops the table on exit
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);
182 char const* m_name;
183 scope m_scope;
186 namespace_::namespace_(char const* name)
187 : scope(std::auto_ptr<detail::registration>(
188 m_registration = new registration_(name)))
192 namespace_& namespace_::operator[](scope s)
194 m_registration->m_scope.operator,(s);
195 return *this;
198 } // namespace luabind