unsafe_function_result.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_UNSAFE_FUNCTION_RESULT_HPP
  19. #define SOL_UNSAFE_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 <cstdint>
  27. namespace sol {
  28. struct unsafe_function_result : public proxy_base<unsafe_function_result> {
  29. private:
  30. lua_State* L;
  31. int index;
  32. int returncount;
  33. public:
  34. typedef stack_proxy reference_type;
  35. typedef stack_proxy value_type;
  36. typedef stack_proxy* pointer;
  37. typedef std::ptrdiff_t difference_type;
  38. typedef std::size_t size_type;
  39. typedef stack_iterator<stack_proxy, false> iterator;
  40. typedef stack_iterator<stack_proxy, true> const_iterator;
  41. typedef std::reverse_iterator<iterator> reverse_iterator;
  42. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  43. unsafe_function_result() noexcept : unsafe_function_result(nullptr) {}
  44. unsafe_function_result(lua_State* Ls, int idx = -1, int retnum = 0) noexcept : L(Ls), index(idx), returncount(retnum) {
  45. }
  46. // We do not want anyone to copy these around willy-nilly
  47. // Will likely break people, but also will probably get rid of quiet bugs that have
  48. // been lurking. (E.g., Vanilla Lua will just quietly discard over-pops and under-pops:
  49. // LuaJIT and other Lua engines will implode and segfault at random later times.)
  50. unsafe_function_result(const unsafe_function_result&) = delete;
  51. unsafe_function_result& operator=(const unsafe_function_result&) = delete;
  52. unsafe_function_result(unsafe_function_result&& o) noexcept : L(o.L), index(o.index), returncount(o.returncount) {
  53. // Must be manual, otherwise destructor will screw us
  54. // return count being 0 is enough to keep things clean
  55. // but will be thorough
  56. o.abandon();
  57. }
  58. unsafe_function_result& operator=(unsafe_function_result&& o) noexcept {
  59. L = o.L;
  60. index = o.index;
  61. returncount = o.returncount;
  62. // Must be manual, otherwise destructor will screw us
  63. // return count being 0 is enough to keep things clean
  64. // but will be thorough
  65. o.abandon();
  66. return *this;
  67. }
  68. unsafe_function_result(const protected_function_result& o) = delete;
  69. unsafe_function_result& operator=(const protected_function_result& o) = delete;
  70. unsafe_function_result(protected_function_result&& o) noexcept;
  71. unsafe_function_result& operator=(protected_function_result&& o) noexcept;
  72. template <typename T>
  73. decltype(auto) get(difference_type index_offset = 0) const {
  74. return stack::get<T>(L, index + static_cast<int>(index_offset));
  75. }
  76. type get_type(difference_type index_offset = 0) const noexcept {
  77. return type_of(L, index + static_cast<int>(index_offset));
  78. }
  79. stack_proxy operator[](difference_type index_offset) const {
  80. return stack_proxy(L, index + static_cast<int>(index_offset));
  81. }
  82. iterator begin() {
  83. return iterator(L, index, stack_index() + return_count());
  84. }
  85. iterator end() {
  86. return iterator(L, stack_index() + return_count(), stack_index() + return_count());
  87. }
  88. const_iterator begin() const {
  89. return const_iterator(L, index, stack_index() + return_count());
  90. }
  91. const_iterator end() const {
  92. return const_iterator(L, stack_index() + return_count(), stack_index() + return_count());
  93. }
  94. const_iterator cbegin() const {
  95. return begin();
  96. }
  97. const_iterator cend() const {
  98. return end();
  99. }
  100. reverse_iterator rbegin() {
  101. return std::reverse_iterator<iterator>(begin());
  102. }
  103. reverse_iterator rend() {
  104. return std::reverse_iterator<iterator>(end());
  105. }
  106. const_reverse_iterator rbegin() const {
  107. return std::reverse_iterator<const_iterator>(begin());
  108. }
  109. const_reverse_iterator rend() const {
  110. return std::reverse_iterator<const_iterator>(end());
  111. }
  112. const_reverse_iterator crbegin() const {
  113. return std::reverse_iterator<const_iterator>(cbegin());
  114. }
  115. const_reverse_iterator crend() const {
  116. return std::reverse_iterator<const_iterator>(cend());
  117. }
  118. call_status status() const noexcept {
  119. return call_status::ok;
  120. }
  121. bool valid() const noexcept {
  122. return status() == call_status::ok || status() == call_status::yielded;
  123. }
  124. lua_State* lua_state() const {
  125. return L;
  126. };
  127. int stack_index() const {
  128. return index;
  129. };
  130. int return_count() const {
  131. return returncount;
  132. };
  133. void abandon() noexcept {
  134. // L = nullptr;
  135. index = 0;
  136. returncount = 0;
  137. }
  138. ~unsafe_function_result() {
  139. if (L != nullptr) {
  140. lua_pop(L, returncount);
  141. }
  142. }
  143. };
  144. namespace stack {
  145. template <>
  146. struct unqualified_pusher<unsafe_function_result> {
  147. static int push(lua_State* L, const unsafe_function_result& fr) {
  148. int p = 0;
  149. for (int i = 0; i < fr.return_count(); ++i) {
  150. lua_pushvalue(L, i + fr.stack_index());
  151. ++p;
  152. }
  153. return p;
  154. }
  155. };
  156. } // namespace stack
  157. } // namespace sol
  158. #endif // SOL_UNSAFE_FUNCTION_RESULT_HPP