handle.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (c) 2005 Daniel Wallin and Arvid Norberg
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #ifndef LUABIND_HANDLE_050420_HPP
  20. #define LUABIND_HANDLE_050420_HPP
  21. #include <luabind/lua_include.hpp>
  22. #include <luabind/value_wrapper.hpp>
  23. namespace luabind {
  24. // A reference to a Lua value. Represents an entry in the
  25. // registry table.
  26. class handle
  27. {
  28. public:
  29. handle();
  30. handle(lua_State* interpreter, int stack_index);
  31. handle(lua_State* main, lua_State* interpreter, int stack_index);
  32. handle(handle const& other);
  33. ~handle();
  34. handle& operator=(handle const& other);
  35. void swap(handle& other);
  36. void push(lua_State* interpreter) const;
  37. lua_State* interpreter() const;
  38. void replace(lua_State* interpreter, int stack_index);
  39. private:
  40. lua_State* m_interpreter;
  41. int m_index;
  42. };
  43. inline handle::handle()
  44. : m_interpreter(0)
  45. , m_index(LUA_NOREF)
  46. {}
  47. inline handle::handle(handle const& other)
  48. : m_interpreter(other.m_interpreter)
  49. , m_index(LUA_NOREF)
  50. {
  51. if (m_interpreter == 0) return;
  52. lua_rawgeti(m_interpreter, LUA_REGISTRYINDEX, other.m_index);
  53. m_index = luaL_ref(m_interpreter, LUA_REGISTRYINDEX);
  54. }
  55. inline handle::handle(lua_State* interpreter, int stack_index)
  56. : m_interpreter(interpreter)
  57. , m_index(LUA_NOREF)
  58. {
  59. lua_pushvalue(interpreter, stack_index);
  60. m_index = luaL_ref(interpreter, LUA_REGISTRYINDEX);
  61. }
  62. inline handle::handle(lua_State* main, lua_State* interpreter, int stack_index)
  63. : m_interpreter(main)
  64. , m_index(LUA_NOREF)
  65. {
  66. lua_pushvalue(interpreter, stack_index);
  67. m_index = luaL_ref(interpreter, LUA_REGISTRYINDEX);
  68. }
  69. inline handle::~handle()
  70. {
  71. if (m_interpreter && m_index != LUA_NOREF)
  72. luaL_unref(m_interpreter, LUA_REGISTRYINDEX, m_index);
  73. }
  74. inline handle& handle::operator=(handle const& other)
  75. {
  76. handle(other).swap(*this);
  77. return *this;
  78. }
  79. inline void handle::swap(handle& other)
  80. {
  81. std::swap(m_interpreter, other.m_interpreter);
  82. std::swap(m_index, other.m_index);
  83. }
  84. inline void handle::push(lua_State* interpreter) const
  85. {
  86. lua_rawgeti(interpreter, LUA_REGISTRYINDEX, m_index);
  87. }
  88. inline lua_State* handle::interpreter() const
  89. {
  90. return m_interpreter;
  91. }
  92. inline void handle::replace(lua_State* interpreter, int stack_index)
  93. {
  94. lua_pushvalue(interpreter, stack_index);
  95. lua_rawseti(interpreter, LUA_REGISTRYINDEX, m_index);
  96. }
  97. template<>
  98. struct value_wrapper_traits<handle>
  99. {
  100. typedef boost::mpl::true_ is_specialized;
  101. static lua_State* interpreter(handle const& value)
  102. {
  103. return value.interpreter();
  104. }
  105. static void unwrap(lua_State* interpreter, handle const& value)
  106. {
  107. value.push(interpreter);
  108. }
  109. static bool check(...)
  110. {
  111. return true;
  112. }
  113. };
  114. } // namespace luabind
  115. #endif // LUABIND_HANDLE_050420_HPP