wrapper.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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_WRAPPER_HPP
  19. #define SOL_WRAPPER_HPP
  20. #include <sol/types.hpp>
  21. namespace sol {
  22. namespace detail {
  23. template <typename T>
  24. using array_return_type = meta::conditional_t<std::is_array<T>::value, std::add_lvalue_reference_t<T>, T>;
  25. }
  26. template <typename F, typename = void>
  27. struct wrapper {
  28. typedef lua_bind_traits<meta::unqualified_t<F>> traits_type;
  29. typedef typename traits_type::args_list args_list;
  30. typedef typename traits_type::args_list free_args_list;
  31. typedef typename traits_type::returns_list returns_list;
  32. template <typename... Args>
  33. static decltype(auto) call(F& f, Args&&... args) {
  34. return f(std::forward<Args>(args)...);
  35. }
  36. struct caller {
  37. template <typename... Args>
  38. decltype(auto) operator()(F& fx, Args&&... args) const {
  39. return call(fx, std::forward<Args>(args)...);
  40. }
  41. };
  42. };
  43. template <typename F>
  44. struct wrapper<F, std::enable_if_t<std::is_function<std::remove_pointer_t<meta::unqualified_t<F>>>::value>> {
  45. typedef lua_bind_traits<std::remove_pointer_t<meta::unqualified_t<F>>> traits_type;
  46. typedef typename traits_type::args_list args_list;
  47. typedef typename traits_type::args_list free_args_list;
  48. typedef typename traits_type::returns_list returns_list;
  49. template <F fx, typename... Args>
  50. static decltype(auto) invoke(Args&&... args) {
  51. return fx(std::forward<Args>(args)...);
  52. }
  53. template <typename... Args>
  54. static decltype(auto) call(F& fx, Args&&... args) {
  55. return fx(std::forward<Args>(args)...);
  56. }
  57. struct caller {
  58. template <typename... Args>
  59. decltype(auto) operator()(F& fx, Args&&... args) const {
  60. return call(fx, std::forward<Args>(args)...);
  61. }
  62. };
  63. template <F fx>
  64. struct invoker {
  65. template <typename... Args>
  66. decltype(auto) operator()(Args&&... args) const {
  67. return invoke<fx>(std::forward<Args>(args)...);
  68. }
  69. };
  70. };
  71. template <typename F>
  72. struct wrapper<F, std::enable_if_t<std::is_member_object_pointer<meta::unqualified_t<F>>::value>> {
  73. typedef lua_bind_traits<meta::unqualified_t<F>> traits_type;
  74. typedef typename traits_type::object_type object_type;
  75. typedef typename traits_type::return_type return_type;
  76. typedef typename traits_type::args_list args_list;
  77. typedef types<object_type&, return_type> free_args_list;
  78. typedef typename traits_type::returns_list returns_list;
  79. template <F fx>
  80. static auto call(object_type& mem) -> detail::array_return_type<decltype(mem.*fx)> {
  81. return mem.*fx;
  82. }
  83. template <F fx, typename Arg, typename... Args>
  84. static decltype(auto) invoke(object_type& mem, Arg&& arg, Args&&...) {
  85. return mem.*fx = std::forward<Arg>(arg);
  86. }
  87. template <typename Fx>
  88. static auto call(Fx&& fx, object_type& mem) -> detail::array_return_type<decltype(mem.*fx)> {
  89. return mem.*fx;
  90. }
  91. template <typename Fx, typename Arg, typename... Args>
  92. static void call(Fx&& fx, object_type& mem, Arg&& arg, Args&&...) {
  93. using actual_type = meta::unqualified_t<detail::array_return_type<decltype(mem.*fx)>>;
  94. if constexpr (std::is_array_v<actual_type>) {
  95. using std::cbegin;
  96. using std::cend;
  97. auto first = cbegin(arg);
  98. auto last = cend(arg);
  99. for (std::size_t i = 0; first != last; ++i, ++first) {
  100. (mem.*fx)[i] = *first;
  101. }
  102. }
  103. else {
  104. (mem.*fx) = std::forward<Arg>(arg);
  105. }
  106. }
  107. struct caller {
  108. template <typename Fx, typename... Args>
  109. decltype(auto) operator()(Fx&& fx, object_type& mem, Args&&... args) const {
  110. return call(std::forward<Fx>(fx), mem, std::forward<Args>(args)...);
  111. }
  112. };
  113. template <F fx>
  114. struct invoker {
  115. template <typename... Args>
  116. decltype(auto) operator()(Args&&... args) const {
  117. return invoke<fx>(std::forward<Args>(args)...);
  118. }
  119. };
  120. };
  121. template <typename F, typename R, typename O, typename... FArgs>
  122. struct member_function_wrapper {
  123. typedef O object_type;
  124. typedef lua_bind_traits<F> traits_type;
  125. typedef typename traits_type::args_list args_list;
  126. typedef types<object_type&, FArgs...> free_args_list;
  127. typedef meta::tuple_types<R> returns_list;
  128. template <F fx, typename... Args>
  129. static R invoke(O& mem, Args&&... args) {
  130. return (mem.*fx)(std::forward<Args>(args)...);
  131. }
  132. template <typename Fx, typename... Args>
  133. static R call(Fx&& fx, O& mem, Args&&... args) {
  134. return (mem.*fx)(std::forward<Args>(args)...);
  135. }
  136. struct caller {
  137. template <typename Fx, typename... Args>
  138. decltype(auto) operator()(Fx&& fx, O& mem, Args&&... args) const {
  139. return call(std::forward<Fx>(fx), mem, std::forward<Args>(args)...);
  140. }
  141. };
  142. template <F fx>
  143. struct invoker {
  144. template <typename... Args>
  145. decltype(auto) operator()(O& mem, Args&&... args) const {
  146. return invoke<fx>(mem, std::forward<Args>(args)...);
  147. }
  148. };
  149. };
  150. template <typename R, typename O, typename... Args>
  151. struct wrapper<R (O::*)(Args...)> : public member_function_wrapper<R (O::*)(Args...), R, O, Args...> { };
  152. template <typename R, typename O, typename... Args>
  153. struct wrapper<R (O::*)(Args...) const> : public member_function_wrapper<R (O::*)(Args...) const, R, O, Args...> { };
  154. template <typename R, typename O, typename... Args>
  155. struct wrapper<R (O::*)(Args...) const volatile> : public member_function_wrapper<R (O::*)(Args...) const volatile, R, O, Args...> { };
  156. template <typename R, typename O, typename... Args>
  157. struct wrapper<R (O::*)(Args...)&> : public member_function_wrapper<R (O::*)(Args...)&, R, O, Args...> { };
  158. template <typename R, typename O, typename... Args>
  159. struct wrapper<R (O::*)(Args...) const&> : public member_function_wrapper<R (O::*)(Args...) const&, R, O, Args...> { };
  160. template <typename R, typename O, typename... Args>
  161. struct wrapper<R (O::*)(Args...) const volatile&> : public member_function_wrapper<R (O::*)(Args...) const volatile&, R, O, Args...> { };
  162. template <typename R, typename O, typename... Args>
  163. struct wrapper<R (O::*)(Args..., ...)&> : public member_function_wrapper<R (O::*)(Args..., ...)&, R, O, Args...> { };
  164. template <typename R, typename O, typename... Args>
  165. struct wrapper<R (O::*)(Args..., ...) const&> : public member_function_wrapper<R (O::*)(Args..., ...) const&, R, O, Args...> { };
  166. template <typename R, typename O, typename... Args>
  167. struct wrapper<R (O::*)(Args..., ...) const volatile&> : public member_function_wrapper<R (O::*)(Args..., ...) const volatile&, R, O, Args...> { };
  168. template <typename R, typename O, typename... Args>
  169. struct wrapper<R (O::*)(Args...) &&> : public member_function_wrapper<R (O::*)(Args...)&, R, O, Args...> { };
  170. template <typename R, typename O, typename... Args>
  171. struct wrapper<R (O::*)(Args...) const&&> : public member_function_wrapper<R (O::*)(Args...) const&, R, O, Args...> { };
  172. template <typename R, typename O, typename... Args>
  173. struct wrapper<R (O::*)(Args...) const volatile&&> : public member_function_wrapper<R (O::*)(Args...) const volatile&, R, O, Args...> { };
  174. template <typename R, typename O, typename... Args>
  175. struct wrapper<R (O::*)(Args..., ...) &&> : public member_function_wrapper<R (O::*)(Args..., ...)&, R, O, Args...> { };
  176. template <typename R, typename O, typename... Args>
  177. struct wrapper<R (O::*)(Args..., ...) const&&> : public member_function_wrapper<R (O::*)(Args..., ...) const&, R, O, Args...> { };
  178. template <typename R, typename O, typename... Args>
  179. struct wrapper<R (O::*)(Args..., ...) const volatile&&> : public member_function_wrapper<R (O::*)(Args..., ...) const volatile&, R, O, Args...> { };
  180. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE)
  181. // noexcept has become a part of a function's type
  182. template <typename R, typename O, typename... Args>
  183. struct wrapper<R (O::*)(Args...) noexcept> : public member_function_wrapper<R (O::*)(Args...) noexcept, R, O, Args...> { };
  184. template <typename R, typename O, typename... Args>
  185. struct wrapper<R (O::*)(Args...) const noexcept> : public member_function_wrapper<R (O::*)(Args...) const noexcept, R, O, Args...> { };
  186. template <typename R, typename O, typename... Args>
  187. struct wrapper<R (O::*)(Args...) const volatile noexcept> : public member_function_wrapper<R (O::*)(Args...) const volatile noexcept, R, O, Args...> { };
  188. template <typename R, typename O, typename... Args>
  189. struct wrapper<R (O::*)(Args...)& noexcept> : public member_function_wrapper<R (O::*)(Args...)& noexcept, R, O, Args...> { };
  190. template <typename R, typename O, typename... Args>
  191. struct wrapper<R (O::*)(Args...) const& noexcept> : public member_function_wrapper<R (O::*)(Args...) const& noexcept, R, O, Args...> { };
  192. template <typename R, typename O, typename... Args>
  193. struct wrapper<R (O::*)(Args...) const volatile& noexcept> : public member_function_wrapper<R (O::*)(Args...) const volatile& noexcept, R, O, Args...> { };
  194. template <typename R, typename O, typename... Args>
  195. struct wrapper<R (O::*)(Args..., ...)& noexcept> : public member_function_wrapper<R (O::*)(Args..., ...)& noexcept, R, O, Args...> { };
  196. template <typename R, typename O, typename... Args>
  197. struct wrapper<R (O::*)(Args..., ...) const& noexcept> : public member_function_wrapper<R (O::*)(Args..., ...) const& noexcept, R, O, Args...> { };
  198. template <typename R, typename O, typename... Args>
  199. struct wrapper<R (O::*)(Args..., ...) const volatile& noexcept>
  200. : public member_function_wrapper<R (O::*)(Args..., ...) const volatile& noexcept, R, O, Args...> { };
  201. template <typename R, typename O, typename... Args>
  202. struct wrapper<R (O::*)(Args...)&& noexcept> : public member_function_wrapper<R (O::*)(Args...)& noexcept, R, O, Args...> { };
  203. template <typename R, typename O, typename... Args>
  204. struct wrapper<R (O::*)(Args...) const&& noexcept> : public member_function_wrapper<R (O::*)(Args...) const& noexcept, R, O, Args...> { };
  205. template <typename R, typename O, typename... Args>
  206. struct wrapper<R (O::*)(Args...) const volatile&& noexcept> : public member_function_wrapper<R (O::*)(Args...) const volatile& noexcept, R, O, Args...> {
  207. };
  208. template <typename R, typename O, typename... Args>
  209. struct wrapper<R (O::*)(Args..., ...)&& noexcept> : public member_function_wrapper<R (O::*)(Args..., ...)& noexcept, R, O, Args...> { };
  210. template <typename R, typename O, typename... Args>
  211. struct wrapper<R (O::*)(Args..., ...) const&& noexcept> : public member_function_wrapper<R (O::*)(Args..., ...) const& noexcept, R, O, Args...> { };
  212. template <typename R, typename O, typename... Args>
  213. struct wrapper<R (O::*)(Args..., ...) const volatile&& noexcept>
  214. : public member_function_wrapper<R (O::*)(Args..., ...) const volatile& noexcept, R, O, Args...> { };
  215. #endif // noexcept is part of a function's type
  216. } // namespace sol
  217. #endif // SOL_WRAPPER_HPP