object.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_OBJECT_HPP
  19. #define SOL_OBJECT_HPP
  20. #include <sol/make_reference.hpp>
  21. #include <sol/reference.hpp>
  22. #include <sol/stack.hpp>
  23. #include <sol/object_base.hpp>
  24. namespace sol {
  25. template <typename base_type>
  26. class basic_object : public basic_object_base<base_type> {
  27. private:
  28. typedef basic_object_base<base_type> base_t;
  29. template <bool invert_and_pop = false>
  30. basic_object(std::integral_constant<bool, invert_and_pop>, lua_State* L_, int index_ = -1) noexcept : base_t(L_, index_) {
  31. if (invert_and_pop) {
  32. lua_pop(L_, -index_);
  33. }
  34. }
  35. protected:
  36. basic_object(detail::no_safety_tag, lua_nil_t n) : base_t(n) {
  37. }
  38. basic_object(detail::no_safety_tag, lua_State* L_, int index_) : base_t(L_, index_) {
  39. }
  40. basic_object(detail::no_safety_tag, lua_State* L_, ref_index index_) : base_t(L_, index_) {
  41. }
  42. template <typename T,
  43. meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_object>>, meta::neg<std::is_same<base_type, stack_reference>>,
  44. meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  45. basic_object(detail::no_safety_tag, T&& r) noexcept : base_t(std::forward<T>(r)) {
  46. }
  47. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  48. basic_object(detail::no_safety_tag, lua_State* L_, T&& r) noexcept : base_t(L_, std::forward<T>(r)) {
  49. }
  50. public:
  51. basic_object() noexcept = default;
  52. template <typename T,
  53. meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_object>>, meta::neg<std::is_same<base_type, stack_reference>>,
  54. is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  55. basic_object(T&& r) : base_t(std::forward<T>(r)) {
  56. }
  57. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  58. basic_object(lua_State* L_, T&& r) : base_t(L_, std::forward<T>(r)) {
  59. }
  60. basic_object(lua_State* L_, global_tag_t t) : base_t(L_, t) {
  61. }
  62. basic_object(lua_nil_t r) : base_t(r) {
  63. }
  64. basic_object(const basic_object&) = default;
  65. basic_object(basic_object&&) = default;
  66. basic_object(const stack_reference& r) noexcept : basic_object(r.lua_state(), r.stack_index()) {
  67. }
  68. basic_object(stack_reference&& r) noexcept : basic_object(r.lua_state(), r.stack_index()) {
  69. }
  70. template <typename Super>
  71. basic_object(const proxy_base<Super>& r) noexcept : basic_object(r.operator basic_object()) {
  72. }
  73. template <typename Super>
  74. basic_object(proxy_base<Super>&& r) noexcept : basic_object(r.operator basic_object()) {
  75. }
  76. basic_object(lua_State* L_, lua_nil_t r) noexcept : base_t(L_, r) {
  77. }
  78. basic_object(lua_State* L_, int index_ = -1) noexcept : base_t(L_, index_) {
  79. }
  80. basic_object(lua_State* L_, absolute_index index_) noexcept : base_t(L_, index_) {
  81. }
  82. basic_object(lua_State* L_, raw_index index_) noexcept : base_t(L_, index_) {
  83. }
  84. basic_object(lua_State* L_, ref_index index_) noexcept : base_t(L_, index_) {
  85. }
  86. template <typename T, typename... Args>
  87. basic_object(lua_State* L_, in_place_type_t<T>, Args&&... args) noexcept
  88. : basic_object(std::integral_constant<bool, !is_stack_based<base_t>::value>(), L_, -stack::push<T>(L_, std::forward<Args>(args)...)) {
  89. }
  90. template <typename T, typename... Args>
  91. basic_object(lua_State* L_, in_place_t, T&& arg, Args&&... args) noexcept
  92. : basic_object(L_, in_place_type<T>, std::forward<T>(arg), std::forward<Args>(args)...) {
  93. }
  94. basic_object& operator=(const basic_object&) = default;
  95. basic_object& operator=(basic_object&&) = default;
  96. basic_object& operator=(const base_type& b) {
  97. base_t::operator=(b);
  98. return *this;
  99. }
  100. basic_object& operator=(base_type&& b) {
  101. base_t::operator=(std::move(b));
  102. return *this;
  103. }
  104. template <typename Super>
  105. basic_object& operator=(const proxy_base<Super>& r) {
  106. this->operator=(r.operator basic_object());
  107. return *this;
  108. }
  109. template <typename Super>
  110. basic_object& operator=(proxy_base<Super>&& r) {
  111. this->operator=(r.operator basic_object());
  112. return *this;
  113. }
  114. };
  115. template <typename T>
  116. object make_object(lua_State* L_, T&& value) {
  117. return make_reference<object, true>(L_, std::forward<T>(value));
  118. }
  119. template <typename T, typename... Args>
  120. object make_object(lua_State* L_, Args&&... args) {
  121. return make_reference<T, object, true>(L_, std::forward<Args>(args)...);
  122. }
  123. template <typename T>
  124. object make_object_userdata(lua_State* L_, T&& value) {
  125. return make_reference_userdata<object, true>(L_, std::forward<T>(value));
  126. }
  127. template <typename T, typename... Args>
  128. object make_object_userdata(lua_State* L_, Args&&... args) {
  129. return make_reference_userdata<T, object, true>(L_, std::forward<Args>(args)...);
  130. }
  131. } // namespace sol
  132. #endif // SOL_OBJECT_HPP