function.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_FUNCTION_HPP
  19. #define SOL_FUNCTION_HPP
  20. #include <sol/stack.hpp>
  21. #include <sol/unsafe_function.hpp>
  22. #include <sol/protected_function.hpp>
  23. #include <sol/bytecode.hpp>
  24. #include <functional>
  25. namespace sol {
  26. template <typename... Ret, typename... Args>
  27. decltype(auto) stack_proxy::call(Args&&... args) {
  28. stack_function sf(this->lua_state(), this->stack_index());
  29. return sf.template call<Ret...>(std::forward<Args>(args)...);
  30. }
  31. inline protected_function_result::protected_function_result(unsafe_function_result&& o) noexcept
  32. : L(o.lua_state()), index(o.stack_index()), returncount(o.return_count()), popcount(o.return_count()), err(o.status()) {
  33. // Must be manual, otherwise destructor will screw us
  34. // return count being 0 is enough to keep things clean
  35. // but we will be thorough
  36. o.abandon();
  37. }
  38. inline protected_function_result& protected_function_result::operator=(unsafe_function_result&& o) noexcept {
  39. L = o.lua_state();
  40. index = o.stack_index();
  41. returncount = o.return_count();
  42. popcount = o.return_count();
  43. err = o.status();
  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.abandon();
  48. return *this;
  49. }
  50. inline unsafe_function_result::unsafe_function_result(protected_function_result&& o) noexcept
  51. : L(o.lua_state()), index(o.stack_index()), returncount(o.return_count()) {
  52. // Must be manual, otherwise destructor will screw us
  53. // return count being 0 is enough to keep things clean
  54. // but we will be thorough
  55. o.abandon();
  56. }
  57. inline unsafe_function_result& unsafe_function_result::operator=(protected_function_result&& o) noexcept {
  58. L = o.lua_state();
  59. index = o.stack_index();
  60. returncount = o.return_count();
  61. // Must be manual, otherwise destructor will screw us
  62. // return count being 0 is enough to keep things clean
  63. // but we will be thorough
  64. o.abandon();
  65. return *this;
  66. }
  67. namespace detail {
  68. template <typename... R>
  69. struct std_shim {
  70. unsafe_function lua_func_;
  71. std_shim(unsafe_function lua_func) : lua_func_(std::move(lua_func)) {
  72. }
  73. template <typename... Args>
  74. meta::return_type_t<R...> operator()(Args&&... args) {
  75. return lua_func_.call<R...>(std::forward<Args>(args)...);
  76. }
  77. };
  78. template <>
  79. struct std_shim<void> {
  80. unsafe_function lua_func_;
  81. std_shim(unsafe_function lua_func) : lua_func_(std::move(lua_func)) {
  82. }
  83. template <typename... Args>
  84. void operator()(Args&&... args) {
  85. lua_func_.call<void>(std::forward<Args>(args)...);
  86. }
  87. };
  88. } // namespace detail
  89. namespace stack {
  90. template <typename Signature>
  91. struct unqualified_getter<std::function<Signature>> {
  92. typedef meta::bind_traits<Signature> fx_t;
  93. typedef typename fx_t::args_list args_lists;
  94. typedef meta::tuple_types<typename fx_t::return_type> return_types;
  95. template <typename... R>
  96. static std::function<Signature> get_std_func(types<R...>, lua_State* L, int index) {
  97. detail::std_shim<R...> fx(unsafe_function(L, index));
  98. return fx;
  99. }
  100. static std::function<Signature> get(lua_State* L, int index, record& tracking) {
  101. tracking.use(1);
  102. type t = type_of(L, index);
  103. if (t == type::none || t == type::lua_nil) {
  104. return nullptr;
  105. }
  106. return get_std_func(return_types(), L, index);
  107. }
  108. };
  109. template <typename Allocator>
  110. struct unqualified_getter<basic_bytecode<Allocator>> {
  111. static basic_bytecode<Allocator> get(lua_State* L, int index, record& tracking) {
  112. tracking.use(1);
  113. stack_function sf(L, index);
  114. return sf.dump(&dump_panic_on_error);
  115. }
  116. };
  117. } // namespace stack
  118. } // namespace sol
  119. #endif // SOL_FUNCTION_HPP