metatable.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // sol2
  2. // The MIT License (MIT)
  3. // Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. // this software and associated documentation files (the "Software"), to deal in
  6. // the Software without restriction, including without limitation the rights to
  7. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. // the Software, and to permit persons to whom the Software is furnished to do so,
  9. // subject to the following conditions:
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #ifndef SOL_METATABLE_HPP
  19. #define SOL_METATABLE_HPP
  20. #include <sol/table_core.hpp>
  21. #include <sol/usertype.hpp>
  22. namespace sol {
  23. template <typename base_type>
  24. class basic_metatable : public basic_table<base_type> {
  25. typedef basic_table<base_type> base_t;
  26. friend class state;
  27. friend class state_view;
  28. protected:
  29. basic_metatable(detail::no_safety_tag, lua_nil_t n) : base_t(n) {
  30. }
  31. basic_metatable(detail::no_safety_tag, lua_State* L, int index) : base_t(L, index) {
  32. }
  33. basic_metatable(detail::no_safety_tag, lua_State* L, ref_index index) : base_t(L, index) {
  34. }
  35. template <typename T,
  36. meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_metatable>>, meta::neg<std::is_same<base_type, stack_reference>>,
  37. meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  38. basic_metatable(detail::no_safety_tag, T&& r) noexcept : base_t(std::forward<T>(r)) {
  39. }
  40. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  41. basic_metatable(detail::no_safety_tag, lua_State* L, T&& r) noexcept : base_t(L, std::forward<T>(r)) {
  42. }
  43. template <typename R, typename... Args, typename Fx, typename Key, typename = std::invoke_result_t<Fx, Args...>>
  44. void set_fx(types<R(Args...)>, Key&& key, Fx&& fx) {
  45. set_resolved_function<R(Args...)>(std::forward<Key>(key), std::forward<Fx>(fx));
  46. }
  47. template <typename Fx, typename Key, meta::enable<meta::is_specialization_of<meta::unqualified_t<Fx>, overload_set>> = meta::enabler>
  48. void set_fx(types<>, Key&& key, Fx&& fx) {
  49. set(std::forward<Key>(key), std::forward<Fx>(fx));
  50. }
  51. template <typename Fx, typename Key, typename... Args,
  52. meta::disable<meta::is_specialization_of<meta::unqualified_t<Fx>, overload_set>> = meta::enabler>
  53. void set_fx(types<>, Key&& key, Fx&& fx, Args&&... args) {
  54. set(std::forward<Key>(key), as_function_reference(std::forward<Fx>(fx), std::forward<Args>(args)...));
  55. }
  56. template <typename... Sig, typename... Args, typename Key>
  57. void set_resolved_function(Key&& key, Args&&... args) {
  58. set(std::forward<Key>(key), as_function_reference<function_sig<Sig...>>(std::forward<Args>(args)...));
  59. }
  60. public:
  61. using base_t::lua_state;
  62. basic_metatable() noexcept = default;
  63. basic_metatable(const basic_metatable&) = default;
  64. basic_metatable(basic_metatable&&) = default;
  65. basic_metatable& operator=(const basic_metatable&) = default;
  66. basic_metatable& operator=(basic_metatable&&) = default;
  67. basic_metatable(const stack_reference& r) : basic_metatable(r.lua_state(), r.stack_index()) {
  68. }
  69. basic_metatable(stack_reference&& r) : basic_metatable(r.lua_state(), r.stack_index()) {
  70. }
  71. template <typename T, meta::enable_any<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  72. basic_metatable(lua_State* L, T&& r) : base_t(L, std::forward<T>(r)) {
  73. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  74. auto pp = stack::push_pop(*this);
  75. constructor_handler handler {};
  76. stack::check<basic_metatable>(lua_state(), -1, handler);
  77. #endif // Safety
  78. }
  79. basic_metatable(lua_State* L, int index = -1) : basic_metatable(detail::no_safety, L, index) {
  80. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  81. constructor_handler handler {};
  82. stack::check<basic_metatable>(L, index, handler);
  83. #endif // Safety
  84. }
  85. basic_metatable(lua_State* L, ref_index index) : basic_metatable(detail::no_safety, L, index) {
  86. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  87. auto pp = stack::push_pop(*this);
  88. constructor_handler handler {};
  89. stack::check<basic_metatable>(lua_state(), -1, handler);
  90. #endif // Safety
  91. }
  92. template <typename T,
  93. meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_metatable>>, meta::neg<std::is_same<base_type, stack_reference>>,
  94. meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  95. basic_metatable(T&& r) noexcept : basic_metatable(detail::no_safety, std::forward<T>(r)) {
  96. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  97. if (!is_table<meta::unqualified_t<T>>::value) {
  98. auto pp = stack::push_pop(*this);
  99. constructor_handler handler {};
  100. stack::check<basic_metatable>(base_t::lua_state(), -1, handler);
  101. }
  102. #endif // Safety
  103. }
  104. basic_metatable(lua_nil_t r) noexcept : basic_metatable(detail::no_safety, r) {
  105. }
  106. template <typename Key, typename Value>
  107. basic_metatable<base_type>& set(Key&& key, Value&& value);
  108. template <typename Sig, typename Key, typename... Args>
  109. basic_metatable& set_function(Key&& key, Args&&... args) {
  110. set_fx(types<Sig>(), std::forward<Key>(key), std::forward<Args>(args)...);
  111. return *this;
  112. }
  113. template <typename Key, typename... Args>
  114. basic_metatable& set_function(Key&& key, Args&&... args) {
  115. set_fx(types<>(), std::forward<Key>(key), std::forward<Args>(args)...);
  116. return *this;
  117. }
  118. void unregister() {
  119. using ustorage_base = u_detail::usertype_storage_base;
  120. lua_State* L = this->lua_state();
  121. auto pp = stack::push_pop(*this);
  122. int top = lua_gettop(L);
  123. stack_reference mt(L, -1);
  124. stack::get_field(L, meta_function::gc_names, mt.stack_index());
  125. if (type_of(L, -1) != type::table) {
  126. lua_settop(L, top);
  127. return;
  128. }
  129. stack_reference gc_names_table(L, -1);
  130. stack::get_field(L, meta_function::storage, mt.stack_index());
  131. if (type_of(L, -1) != type::lightuserdata) {
  132. lua_settop(L, top);
  133. return;
  134. }
  135. ustorage_base& base_storage = *static_cast<ustorage_base*>(stack::get<void*>(L, -1));
  136. std::array<string_view, 6> registry_traits;
  137. for (std::size_t i = 0; i < registry_traits.size(); ++i) {
  138. u_detail::submetatable_type smt = static_cast<u_detail::submetatable_type>(i);
  139. stack::get_field<false, true>(L, smt, gc_names_table.stack_index());
  140. registry_traits[i] = stack::get<string_view>(L, -1);
  141. }
  142. // get the registry
  143. stack_reference registry(L, raw_index(LUA_REGISTRYINDEX));
  144. registry.push();
  145. // eliminate all named entries for this usertype
  146. // in the registry (luaL_newmetatable does
  147. // [name] = new table
  148. // in registry upon creation)
  149. for (std::size_t i = 0; i < registry_traits.size(); ++i) {
  150. u_detail::submetatable_type smt = static_cast<u_detail::submetatable_type>(i);
  151. const string_view& gcmetakey = registry_traits[i];
  152. if (smt == u_detail::submetatable_type::named) {
  153. // use .data() to make it treat it like a c string,
  154. // which it is...
  155. stack::set_field<true>(L, gcmetakey.data(), lua_nil);
  156. }
  157. else {
  158. // do not change the values in the registry: they need to be present
  159. // no matter what, for safety's sake
  160. // stack::set_field(L, gcmetakey, lua_nil, registry.stack_index());
  161. }
  162. }
  163. // destroy all storage and tables
  164. base_storage.clear();
  165. // 6 strings from gc_names table,
  166. // + 1 registry,
  167. // + 1 gc_names table
  168. // + 1 light userdata of storage
  169. // + 1 registry
  170. // 10 total, 4 left since popping off 6 gc_names tables
  171. lua_settop(L, top);
  172. }
  173. };
  174. } // namespace sol
  175. #endif // SOL_METATABLE_HPP