New inheritance graph code.
[luabind.git] / luabind / handle.hpp
blob3883e2698d752b31d3b8ff559c9bbed8cde77276
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>
29 namespace luabind {
31 // A reference to a Lua value. Represents an entry in the
32 // registry table.
33 class handle
35 public:
36 handle();
37 handle(lua_State* interpreter, int stack_index);
38 handle(handle const& other);
39 ~handle();
41 handle& operator=(handle const& other);
42 void swap(handle& other);
44 void push(lua_State* interpreter) const;
46 lua_State* interpreter() const;
48 void replace(lua_State* interpreter, int stack_index);
50 private:
51 lua_State* m_interpreter;
52 int m_index;
55 inline handle::handle()
56 : m_interpreter(0)
57 , m_index(LUA_NOREF)
60 inline handle::handle(handle const& other)
61 : m_interpreter(other.m_interpreter)
62 , m_index(LUA_NOREF)
64 if (m_interpreter == 0) return;
65 lua_rawgeti(m_interpreter, LUA_REGISTRYINDEX, other.m_index);
66 m_index = luaL_ref(m_interpreter, LUA_REGISTRYINDEX);
69 inline handle::handle(lua_State* interpreter, int stack_index)
70 : m_interpreter(interpreter)
71 , m_index(LUA_NOREF)
73 lua_pushvalue(interpreter, stack_index);
74 m_index = luaL_ref(interpreter, LUA_REGISTRYINDEX);
77 inline handle::~handle()
79 if (m_interpreter && m_index != LUA_NOREF)
80 luaL_unref(m_interpreter, LUA_REGISTRYINDEX, m_index);
83 inline handle& handle::operator=(handle const& other)
85 handle(other).swap(*this);
86 return *this;
89 inline void handle::swap(handle& other)
91 std::swap(m_interpreter, other.m_interpreter);
92 std::swap(m_index, other.m_index);
95 inline void handle::push(lua_State* interpreter) const
97 lua_rawgeti(interpreter, LUA_REGISTRYINDEX, m_index);
100 inline lua_State* handle::interpreter() const
102 return m_interpreter;
105 inline void handle::replace(lua_State* interpreter, int stack_index)
107 lua_pushvalue(interpreter, stack_index);
108 lua_rawseti(interpreter, LUA_REGISTRYINDEX, m_index);
111 template<>
112 struct value_wrapper_traits<handle>
114 typedef boost::mpl::true_ is_specialized;
116 static lua_State* interpreter(handle const& value)
118 return value.interpreter();
121 static void unwrap(lua_State* interpreter, handle const& value)
123 value.push(interpreter);
126 static bool check(...)
128 return true;
132 } // namespace luabind
134 #endif // LUABIND_HANDLE_050420_HPP