userdata.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_USERDATA_HPP
  19. #define SOL_USERDATA_HPP
  20. #include <sol/object_base.hpp>
  21. #include <sol/table.hpp>
  22. namespace sol {
  23. template <typename base_type>
  24. class basic_userdata : public basic_table<base_type> {
  25. private:
  26. using base_t = basic_table<base_type>;
  27. public:
  28. using base_t::lua_state;
  29. basic_userdata() noexcept = default;
  30. template <typename T,
  31. meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_userdata>>, meta::neg<std::is_same<base_t, stack_reference>>,
  32. is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  33. basic_userdata(T&& r) noexcept : base_t(std::forward<T>(r)) {
  34. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  35. if (!is_userdata<meta::unqualified_t<T>>::value) {
  36. auto pp = stack::push_pop(*this);
  37. type_assert(lua_state(), -1, type::userdata);
  38. }
  39. #endif // Safety
  40. }
  41. basic_userdata(const basic_userdata&) = default;
  42. basic_userdata(basic_userdata&&) = default;
  43. basic_userdata& operator=(const basic_userdata&) = default;
  44. basic_userdata& operator=(basic_userdata&&) = default;
  45. basic_userdata(const stack_reference& r) : basic_userdata(r.lua_state(), r.stack_index()) {
  46. }
  47. basic_userdata(stack_reference&& r) : basic_userdata(r.lua_state(), r.stack_index()) {
  48. }
  49. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  50. basic_userdata(lua_State* L, T&& r) : base_t(L, std::forward<T>(r)) {
  51. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  52. auto pp = stack::push_pop(*this);
  53. constructor_handler handler {};
  54. stack::check<basic_userdata>(L, -1, handler);
  55. #endif // Safety
  56. }
  57. basic_userdata(lua_State* L, int index = -1) : base_t(detail::no_safety, L, index) {
  58. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  59. constructor_handler handler {};
  60. stack::check<basic_userdata>(L, index, handler);
  61. #endif // Safety
  62. }
  63. basic_userdata(lua_State* L, ref_index index) : base_t(detail::no_safety, L, index) {
  64. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  65. auto pp = stack::push_pop(*this);
  66. constructor_handler handler {};
  67. stack::check<basic_userdata>(L, -1, handler);
  68. #endif // Safety
  69. }
  70. };
  71. template <typename base_type>
  72. class basic_lightuserdata : public basic_object_base<base_type> {
  73. typedef basic_object_base<base_type> base_t;
  74. public:
  75. using base_t::lua_state;
  76. basic_lightuserdata() noexcept = default;
  77. template <typename T,
  78. meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_lightuserdata>>, meta::neg<std::is_same<base_t, stack_reference>>,
  79. is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  80. basic_lightuserdata(T&& r) noexcept : base_t(std::forward<T>(r)) {
  81. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  82. if (!is_lightuserdata<meta::unqualified_t<T>>::value) {
  83. auto pp = stack::push_pop(*this);
  84. type_assert(lua_state(), -1, type::lightuserdata);
  85. }
  86. #endif // Safety
  87. }
  88. basic_lightuserdata(const basic_lightuserdata&) = default;
  89. basic_lightuserdata(basic_lightuserdata&&) = default;
  90. basic_lightuserdata& operator=(const basic_lightuserdata&) = default;
  91. basic_lightuserdata& operator=(basic_lightuserdata&&) = default;
  92. basic_lightuserdata(const stack_reference& r) : basic_lightuserdata(r.lua_state(), r.stack_index()) {
  93. }
  94. basic_lightuserdata(stack_reference&& r) : basic_lightuserdata(r.lua_state(), r.stack_index()) {
  95. }
  96. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  97. basic_lightuserdata(lua_State* L, T&& r) : basic_lightuserdata(L, std::forward<T>(r)) {
  98. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  99. auto pp = stack::push_pop(*this);
  100. constructor_handler handler {};
  101. stack::check<basic_lightuserdata>(lua_state(), -1, handler);
  102. #endif // Safety
  103. }
  104. basic_lightuserdata(lua_State* L, int index = -1) : base_t(L, index) {
  105. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  106. constructor_handler handler {};
  107. stack::check<basic_lightuserdata>(L, index, handler);
  108. #endif // Safety
  109. }
  110. basic_lightuserdata(lua_State* L, ref_index index) : base_t(L, index) {
  111. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  112. auto pp = stack::push_pop(*this);
  113. constructor_handler handler {};
  114. stack::check<basic_lightuserdata>(lua_state(), index, handler);
  115. #endif // Safety
  116. }
  117. };
  118. } // namespace sol
  119. #endif // SOL_USERDATA_HPP