My Project
handle.hpp
1 // Copyright (c) 2005 Daniel Wallin and Arvid Norberg
2 
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:
9 
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
12 
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.
22 
23 #ifndef LUABIND_HANDLE_050420_HPP
24 #define LUABIND_HANDLE_050420_HPP
25 
26 #include <luabind/lua_include.hpp>
27 #include <luabind/value_wrapper.hpp>
28 
29 namespace luabind {
30 
31 // A reference to a Lua value. Represents an entry in the
32 // registry table.
33 class handle
34 {
35 public:
36  handle();
37  handle(lua_State* interpreter, int stack_index);
38  handle(lua_State* main, lua_State* interpreter, int stack_index);
39  handle(handle const& other);
40  ~handle();
41 
42  handle& operator=(handle const& other);
43  void swap(handle& other);
44 
45  void push(lua_State* interpreter) const;
46 
47  lua_State* interpreter() const;
48 
49  void replace(lua_State* interpreter, int stack_index);
50 
51 private:
52  lua_State* m_interpreter;
53  int m_index;
54 };
55 
56 inline handle::handle()
57  : m_interpreter(0)
58  , m_index(LUA_NOREF)
59 {}
60 
61 inline handle::handle(handle const& other)
62  : m_interpreter(other.m_interpreter)
63  , m_index(LUA_NOREF)
64 {
65  if (m_interpreter == 0) return;
66  lua_rawgeti(m_interpreter, LUA_REGISTRYINDEX, other.m_index);
67  m_index = luaL_ref(m_interpreter, LUA_REGISTRYINDEX);
68 }
69 
70 inline handle::handle(lua_State* interpreter, int stack_index)
71  : m_interpreter(interpreter)
72  , m_index(LUA_NOREF)
73 {
74  lua_pushvalue(interpreter, stack_index);
75  m_index = luaL_ref(interpreter, LUA_REGISTRYINDEX);
76 }
77 
78 inline handle::handle(lua_State* main, lua_State* interpreter, int stack_index)
79  : m_interpreter(main)
80  , m_index(LUA_NOREF)
81 {
82  lua_pushvalue(interpreter, stack_index);
83  m_index = luaL_ref(interpreter, LUA_REGISTRYINDEX);
84 }
85 
86 inline handle::~handle()
87 {
88  if (m_interpreter && m_index != LUA_NOREF)
89  luaL_unref(m_interpreter, LUA_REGISTRYINDEX, m_index);
90 }
91 
92 inline handle& handle::operator=(handle const& other)
93 {
94  handle(other).swap(*this);
95  return *this;
96 }
97 
98 inline void handle::swap(handle& other)
99 {
100  std::swap(m_interpreter, other.m_interpreter);
101  std::swap(m_index, other.m_index);
102 }
103 
104 inline void handle::push(lua_State* interpreter) const
105 {
106  lua_rawgeti(interpreter, LUA_REGISTRYINDEX, m_index);
107 }
108 
109 inline lua_State* handle::interpreter() const
110 {
111  return m_interpreter;
112 }
113 
114 inline void handle::replace(lua_State* interpreter, int stack_index)
115 {
116  lua_pushvalue(interpreter, stack_index);
117  lua_rawseti(interpreter, LUA_REGISTRYINDEX, m_index);
118 }
119 
120 template<>
122 {
123  typedef boost::mpl::true_ is_specialized;
124 
125  static lua_State* interpreter(handle const& value)
126  {
127  return value.interpreter();
128  }
129 
130  static void unwrap(lua_State* interpreter, handle const& value)
131  {
132  value.push(interpreter);
133  }
134 
135  static bool check(...)
136  {
137  return true;
138  }
139 };
140 
141 } // namespace luabind
142 
143 #endif // LUABIND_HANDLE_050420_HPP
144 
Definition: handle.hpp:33
Definition: other.hpp:41
Definition: minilua.c:461
Definition: PEtypes.h:507
Definition: enum_maker.hpp:46
Definition: value_wrapper.hpp:77