protected_function_result.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_PROTECTED_FUNCTION_RESULT_HPP
  19. #define SOL_PROTECTED_FUNCTION_RESULT_HPP
  20. #include <sol/reference.hpp>
  21. #include <sol/tuple.hpp>
  22. #include <sol/stack.hpp>
  23. #include <sol/proxy_base.hpp>
  24. #include <sol/stack_iterator.hpp>
  25. #include <sol/stack_proxy.hpp>
  26. #include <sol/error.hpp>
  27. #include <sol/stack.hpp>
  28. #include <cstdint>
  29. namespace sol {
  30. struct protected_function_result : public proxy_base<protected_function_result> {
  31. private:
  32. lua_State* L;
  33. int index;
  34. int returncount;
  35. int popcount;
  36. call_status err;
  37. public:
  38. typedef stack_proxy reference_type;
  39. typedef stack_proxy value_type;
  40. typedef stack_proxy* pointer;
  41. typedef std::ptrdiff_t difference_type;
  42. typedef std::size_t size_type;
  43. typedef stack_iterator<stack_proxy, false> iterator;
  44. typedef stack_iterator<stack_proxy, true> const_iterator;
  45. typedef std::reverse_iterator<iterator> reverse_iterator;
  46. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  47. protected_function_result() noexcept : protected_function_result(nullptr) {}
  48. protected_function_result(lua_State* Ls, int idx = -1, int retnum = 0, int popped = 0, call_status pferr = call_status::ok) noexcept
  49. : L(Ls), index(idx), returncount(retnum), popcount(popped), err(pferr) {
  50. }
  51. // We do not want anyone to copy these around willy-nilly
  52. // Will likely break people, but also will probably get rid of quiet bugs that have
  53. // been lurking. (E.g., Vanilla Lua will just quietly discard over-pops and under-pops:
  54. // LuaJIT and other Lua engines will implode and segfault at random later times.)
  55. protected_function_result(const protected_function_result&) = delete;
  56. protected_function_result& operator=(const protected_function_result&) = delete;
  57. protected_function_result(protected_function_result&& o) noexcept
  58. : L(o.L), index(o.index), returncount(o.returncount), popcount(o.popcount), err(o.err) {
  59. // Must be manual, otherwise destructor will screw us
  60. // return count being 0 is enough to keep things clean
  61. // but we will be thorough
  62. o.abandon();
  63. }
  64. protected_function_result& operator=(protected_function_result&& o) noexcept {
  65. L = o.L;
  66. index = o.index;
  67. returncount = o.returncount;
  68. popcount = o.popcount;
  69. err = o.err;
  70. // Must be manual, otherwise destructor will screw us
  71. // return count being 0 is enough to keep things clean
  72. // but we will be thorough
  73. o.abandon();
  74. return *this;
  75. }
  76. protected_function_result(const unsafe_function_result& o) = delete;
  77. protected_function_result& operator=(const unsafe_function_result& o) = delete;
  78. protected_function_result(unsafe_function_result&& o) noexcept;
  79. protected_function_result& operator=(unsafe_function_result&& o) noexcept;
  80. call_status status() const noexcept {
  81. return err;
  82. }
  83. bool valid() const noexcept {
  84. return status() == call_status::ok || status() == call_status::yielded;
  85. }
  86. #if SOL_IS_ON(SOL_COMPILER_GCC)
  87. #pragma GCC diagnostic push
  88. #if !SOL_IS_ON(SOL_COMPILER_CLANG)
  89. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  90. #endif
  91. #endif
  92. template <typename T>
  93. decltype(auto) get(int index_offset = 0) const {
  94. using UT = meta::unqualified_t<T>;
  95. int target = index + index_offset;
  96. if constexpr (meta::is_optional_v<UT>) {
  97. using ValueType = typename UT::value_type;
  98. if constexpr (std::is_same_v<ValueType, error>) {
  99. if (valid()) {
  100. return UT();
  101. }
  102. return UT(error(detail::direct_error, stack::get<std::string>(L, target)));
  103. }
  104. else {
  105. if (!valid()) {
  106. return UT();
  107. }
  108. return stack::get<UT>(L, target);
  109. }
  110. }
  111. else {
  112. if constexpr (std::is_same_v<T, error>) {
  113. #if SOL_IS_ON(SOL_SAFE_PROXIES)
  114. if (valid()) {
  115. type t = type_of(L, target);
  116. type_panic_c_str(L, target, t, type::none, "bad get from protected_function_result (is an error)");
  117. }
  118. #endif // Check Argument Safety
  119. return error(detail::direct_error, stack::get<std::string>(L, target));
  120. }
  121. else {
  122. #if SOL_IS_ON(SOL_SAFE_PROXIES)
  123. if (!valid()) {
  124. type t = type_of(L, target);
  125. type_panic_c_str(L, target, t, type::none, "bad get from protected_function_result (is not an error)");
  126. }
  127. #endif // Check Argument Safety
  128. return stack::get<T>(L, target);
  129. }
  130. }
  131. }
  132. #if SOL_IS_ON(SOL_COMPILER_GCC)
  133. #pragma GCC diagnostic pop
  134. #endif
  135. type get_type(int index_offset = 0) const noexcept {
  136. return type_of(L, index + static_cast<int>(index_offset));
  137. }
  138. stack_proxy operator[](difference_type index_offset) const {
  139. return stack_proxy(L, index + static_cast<int>(index_offset));
  140. }
  141. iterator begin() {
  142. return iterator(L, index, stack_index() + return_count());
  143. }
  144. iterator end() {
  145. return iterator(L, stack_index() + return_count(), stack_index() + return_count());
  146. }
  147. const_iterator begin() const {
  148. return const_iterator(L, index, stack_index() + return_count());
  149. }
  150. const_iterator end() const {
  151. return const_iterator(L, stack_index() + return_count(), stack_index() + return_count());
  152. }
  153. const_iterator cbegin() const {
  154. return begin();
  155. }
  156. const_iterator cend() const {
  157. return end();
  158. }
  159. reverse_iterator rbegin() {
  160. return std::reverse_iterator<iterator>(begin());
  161. }
  162. reverse_iterator rend() {
  163. return std::reverse_iterator<iterator>(end());
  164. }
  165. const_reverse_iterator rbegin() const {
  166. return std::reverse_iterator<const_iterator>(begin());
  167. }
  168. const_reverse_iterator rend() const {
  169. return std::reverse_iterator<const_iterator>(end());
  170. }
  171. const_reverse_iterator crbegin() const {
  172. return std::reverse_iterator<const_iterator>(cbegin());
  173. }
  174. const_reverse_iterator crend() const {
  175. return std::reverse_iterator<const_iterator>(cend());
  176. }
  177. lua_State* lua_state() const noexcept {
  178. return L;
  179. };
  180. int stack_index() const noexcept {
  181. return index;
  182. };
  183. int return_count() const noexcept {
  184. return returncount;
  185. };
  186. int pop_count() const noexcept {
  187. return popcount;
  188. };
  189. void abandon() noexcept {
  190. // L = nullptr;
  191. index = 0;
  192. returncount = 0;
  193. popcount = 0;
  194. err = call_status::runtime;
  195. }
  196. ~protected_function_result() {
  197. if (L == nullptr)
  198. return;
  199. stack::remove(L, index, popcount);
  200. }
  201. };
  202. namespace stack {
  203. template <>
  204. struct unqualified_pusher<protected_function_result> {
  205. static int push(lua_State* L, const protected_function_result& pfr) {
  206. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  207. luaL_checkstack(L, static_cast<int>(pfr.pop_count()), detail::not_enough_stack_space_generic);
  208. #endif // make sure stack doesn't overflow
  209. int p = 0;
  210. for (int i = 0; i < pfr.pop_count(); ++i) {
  211. lua_pushvalue(L, i + pfr.stack_index());
  212. ++p;
  213. }
  214. return p;
  215. }
  216. };
  217. } // namespace stack
  218. } // namespace sol
  219. #endif // SOL_PROTECTED_FUNCTION_RESULT_HPP