load_result.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_LOAD_RESULT_HPP
  19. #define SOL_LOAD_RESULT_HPP
  20. #include <sol/stack.hpp>
  21. #include <sol/function.hpp>
  22. #include <sol/proxy_base.hpp>
  23. #include <cstdint>
  24. namespace sol {
  25. struct load_result : public proxy_base<load_result> {
  26. private:
  27. lua_State* L;
  28. int index;
  29. int returncount;
  30. int popcount;
  31. load_status err;
  32. public:
  33. load_result() noexcept : load_result(nullptr) {}
  34. load_result(lua_State* Ls, int stackindex = -1, int retnum = 0, int popnum = 0, load_status lerr = load_status::ok) noexcept
  35. : L(Ls), index(stackindex), returncount(retnum), popcount(popnum), err(lerr) {
  36. }
  37. // We do not want anyone to copy these around willy-nilly
  38. // Will likely break people, but also will probably get rid of quiet bugs that have
  39. // been lurking. (E.g., Vanilla Lua will just quietly discard over-pops and under-pops:
  40. // LuaJIT and other Lua engines will implode and segfault at random later times.)
  41. load_result(const load_result&) = delete;
  42. load_result& operator=(const load_result&) = delete;
  43. load_result(load_result&& o) noexcept : L(o.L), index(o.index), returncount(o.returncount), popcount(o.popcount), err(o.err) {
  44. // Must be manual, otherwise destructor will screw us
  45. // return count being 0 is enough to keep things clean
  46. // but we will be thorough
  47. o.L = nullptr;
  48. o.index = 0;
  49. o.returncount = 0;
  50. o.popcount = 0;
  51. o.err = load_status::syntax;
  52. }
  53. load_result& operator=(load_result&& o) noexcept {
  54. L = o.L;
  55. index = o.index;
  56. returncount = o.returncount;
  57. popcount = o.popcount;
  58. 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.L = nullptr;
  63. o.index = 0;
  64. o.returncount = 0;
  65. o.popcount = 0;
  66. o.err = load_status::syntax;
  67. return *this;
  68. }
  69. load_status status() const noexcept {
  70. return err;
  71. }
  72. bool valid() const noexcept {
  73. return status() == load_status::ok;
  74. }
  75. template <typename T>
  76. T get() const {
  77. using UT = meta::unqualified_t<T>;
  78. if constexpr (meta::is_optional_v<UT>) {
  79. using ValueType = typename UT::value_type;
  80. if constexpr (std::is_same_v<ValueType, error>) {
  81. if (valid()) {
  82. return UT(nullopt);
  83. }
  84. return error(detail::direct_error, stack::get<std::string>(L, index));
  85. }
  86. else {
  87. if (!valid()) {
  88. return UT(nullopt);
  89. }
  90. return stack::get<UT>(L, index);
  91. }
  92. }
  93. else {
  94. if constexpr (std::is_same_v<T, error>) {
  95. #if SOL_IS_ON(SOL_SAFE_PROXIES)
  96. if (valid()) {
  97. type_panic_c_str(L, index, type_of(L, index), type::none, "expecting an error type (a string, from Lua)");
  98. }
  99. #endif // Check proxy type's safety
  100. return error(detail::direct_error, stack::get<std::string>(L, index));
  101. }
  102. else {
  103. #if SOL_IS_ON(SOL_SAFE_PROXIES)
  104. if (!valid()) {
  105. type_panic_c_str(L, index, type_of(L, index), type::none);
  106. }
  107. #endif // Check proxy type's safety
  108. return stack::get<T>(L, index);
  109. }
  110. }
  111. }
  112. template <typename... Ret, typename... Args>
  113. decltype(auto) call(Args&&... args) {
  114. return get<protected_function>().template call<Ret...>(std::forward<Args>(args)...);
  115. }
  116. template <typename... Args>
  117. decltype(auto) operator()(Args&&... args) {
  118. return call<>(std::forward<Args>(args)...);
  119. }
  120. lua_State* lua_state() const noexcept {
  121. return L;
  122. };
  123. int stack_index() const noexcept {
  124. return index;
  125. };
  126. ~load_result() {
  127. if (L != nullptr) {
  128. stack::remove(L, index, popcount);
  129. }
  130. }
  131. };
  132. } // namespace sol
  133. #endif // SOL_LOAD_RESULT_HPP