lua_value.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_LUA_VALUE_HPP
  19. #define SOL_LUA_VALUE_HPP
  20. #include <sol/stack.hpp>
  21. #include <sol/reference.hpp>
  22. #include <sol/make_reference.hpp>
  23. namespace sol {
  24. struct lua_value {
  25. public:
  26. struct arr : detail::ebco<std::initializer_list<lua_value>> {
  27. private:
  28. using base_t = detail::ebco<std::initializer_list<lua_value>>;
  29. public:
  30. using base_t::base_t;
  31. };
  32. private:
  33. template <typename T>
  34. using is_reference_or_lua_value_init_list
  35. = meta::any<meta::is_specialization_of<T, std::initializer_list>, std::is_same<T, reference>, std::is_same<T, arr>>;
  36. template <typename T>
  37. using is_lua_value_single_constructible = meta::any<std::is_same<T, lua_value>, is_reference_or_lua_value_init_list<T>>;
  38. static lua_State*& thread_local_lua_state() {
  39. #if SOL_IS_ON(SOL_USE_THREAD_LOCAL)
  40. static thread_local lua_State* L = nullptr;
  41. #else
  42. static lua_State* L = nullptr;
  43. #endif
  44. return L;
  45. }
  46. reference ref_value;
  47. public:
  48. static void set_lua_state(lua_State* L) {
  49. thread_local_lua_state() = L;
  50. }
  51. template <typename T, meta::disable<is_reference_or_lua_value_init_list<meta::unqualified_t<T>>> = meta::enabler>
  52. lua_value(lua_State* L_, T&& value) : lua_value(((set_lua_state(L_)), std::forward<T>(value))) {
  53. }
  54. template <typename T, meta::disable<is_lua_value_single_constructible<meta::unqualified_t<T>>> = meta::enabler>
  55. lua_value(T&& value) : ref_value(make_reference(thread_local_lua_state(), std::forward<T>(value))) {
  56. }
  57. lua_value(lua_State* L_, std::initializer_list<std::pair<lua_value, lua_value>> il)
  58. : lua_value([&L_, &il]() {
  59. set_lua_state(L_);
  60. return std::move(il);
  61. }()) {
  62. }
  63. lua_value(std::initializer_list<std::pair<lua_value, lua_value>> il) : ref_value(make_reference(thread_local_lua_state(), std::move(il))) {
  64. }
  65. lua_value(lua_State* L_, arr il)
  66. : lua_value([&L_, &il]() {
  67. set_lua_state(L_);
  68. return std::move(il);
  69. }()) {
  70. }
  71. lua_value(arr il) : ref_value(make_reference(thread_local_lua_state(), std::move(il.value()))) {
  72. }
  73. lua_value(lua_State* L_, reference r)
  74. : lua_value([&L_, &r]() {
  75. set_lua_state(L_);
  76. return std::move(r);
  77. }()) {
  78. }
  79. lua_value(reference r) : ref_value(std::move(r)) {
  80. }
  81. lua_value(const lua_value&) noexcept = default;
  82. lua_value(lua_value&&) = default;
  83. lua_value& operator=(const lua_value&) = default;
  84. lua_value& operator=(lua_value&&) = default;
  85. const reference& value() const& {
  86. return ref_value;
  87. }
  88. reference& value() & {
  89. return ref_value;
  90. }
  91. reference&& value() && {
  92. return std::move(ref_value);
  93. }
  94. template <typename T>
  95. decltype(auto) as() const {
  96. ref_value.push();
  97. return stack::pop<T>(ref_value.lua_state());
  98. }
  99. template <typename T>
  100. bool is() const {
  101. int r = ref_value.registry_index();
  102. if (r == LUA_REFNIL)
  103. return meta::any_same<meta::unqualified_t<T>, lua_nil_t, nullopt_t, std::nullptr_t>::value ? true : false;
  104. if (r == LUA_NOREF)
  105. return false;
  106. auto pp = stack::push_pop(ref_value);
  107. return stack::check<T>(ref_value.lua_state(), -1, &no_panic);
  108. }
  109. };
  110. using array_value = typename lua_value::arr;
  111. namespace stack {
  112. template <>
  113. struct unqualified_pusher<lua_value> {
  114. static int push(lua_State* L, const lua_value& lv) {
  115. return stack::push(L, lv.value());
  116. }
  117. static int push(lua_State* L, lua_value&& lv) {
  118. return stack::push(L, std::move(lv).value());
  119. }
  120. };
  121. template <>
  122. struct unqualified_getter<lua_value> {
  123. static lua_value get(lua_State* L, int index, record& tracking) {
  124. return lua_value(L, stack::get<reference>(L, index, tracking));
  125. }
  126. };
  127. } // namespace stack
  128. } // namespace sol
  129. #endif // SOL_LUA_VALUE_HPP