Initial commit, includes Lua with broken Luabind as a backup for branching purposes
[terrastrategy.git] / include / luabind / handle.hpp
blob1416c46362639e58883b8a9a62a34f9e6398354b
1 // Copyright (c) 2005 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 #ifndef LUABIND_HANDLE_050420_HPP
24 #define LUABIND_HANDLE_050420_HPP
26 #include <luabind/lua_include.hpp>
27 #include <luabind/value_wrapper.hpp>
28 #include <luabind/detail/ref.hpp>
30 namespace luabind {
32 // A reference to a Lua value. Represents an entry in the
33 // registry table.
34 class handle
36 public:
37 handle();
38 handle(lua_State* interpreter, int stack_index);
39 handle(handle const& other);
40 ~handle();
42 handle& operator=(handle const& other);
43 void swap(handle& other);
45 void push(lua_State* interpreter) const;
47 lua_State* interpreter() const;
49 void replace(lua_State* interpreter, int stack_index);
51 private:
52 lua_State* m_interpreter;
53 int m_index;
56 inline handle::handle()
57 : m_interpreter(0)
58 , m_index(LUA_NOREF)
61 inline handle::handle(handle const& other)
62 : m_interpreter(other.m_interpreter)
63 , m_index(LUA_NOREF)
65 if (m_interpreter == 0) return;
66 detail::getref(m_interpreter, other.m_index);
67 m_index = detail::ref(m_interpreter);
70 inline handle::handle(lua_State* interpreter, int stack_index)
71 : m_interpreter(interpreter)
72 , m_index(LUA_NOREF)
74 lua_pushvalue(interpreter, stack_index);
75 m_index = detail::ref(interpreter);
78 inline handle::~handle()
80 if (m_interpreter && m_index != LUA_NOREF)
81 detail::unref(m_interpreter, m_index);
84 inline handle& handle::operator=(handle const& other)
86 handle(other).swap(*this);
87 return *this;
90 inline void handle::swap(handle& other)
92 std::swap(m_interpreter, other.m_interpreter);
93 std::swap(m_index, other.m_index);
96 inline void handle::push(lua_State* interpreter) const
98 detail::getref(interpreter, m_index);
101 inline lua_State* handle::interpreter() const
103 return m_interpreter;
106 inline void handle::replace(lua_State* interpreter, int stack_index)
108 lua_pushvalue(interpreter, stack_index);
109 lua_rawseti(interpreter, LUA_REGISTRYINDEX, m_index);
112 template<>
113 struct value_wrapper_traits<handle>
115 typedef boost::mpl::true_ is_specialized;
117 static lua_State* interpreter(handle const& value)
119 return value.interpreter();
122 static void unwrap(lua_State* interpreter, handle const& value)
124 value.push(interpreter);
127 static bool check(...)
129 return true;
133 } // namespace luabind
135 #endif // LUABIND_HANDLE_050420_HPP