resolve.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_RESOLVE_HPP
  19. #define SOL_RESOLVE_HPP
  20. #include <sol/traits.hpp>
  21. #include <sol/tuple.hpp>
  22. namespace sol {
  23. #ifndef __clang__
  24. // constexpr is fine for not-clang
  25. namespace detail {
  26. template <typename R, typename... Args, typename F, typename = std::invoke_result_t<meta::unqualified_t<F>, Args...>>
  27. inline constexpr auto resolve_i(types<R(Args...)>, F&&) -> R (meta::unqualified_t<F>::*)(Args...) {
  28. using Sig = R(Args...);
  29. typedef meta::unqualified_t<F> Fu;
  30. return static_cast<Sig Fu::*>(&Fu::operator());
  31. }
  32. template <typename F, typename U = meta::unqualified_t<F>>
  33. inline constexpr auto resolve_f(std::true_type, F&& f)
  34. -> decltype(resolve_i(types<meta::function_signature_t<decltype(&U::operator())>>(), std::forward<F>(f))) {
  35. return resolve_i(types<meta::function_signature_t<decltype(&U::operator())>>(), std::forward<F>(f));
  36. }
  37. template <typename F>
  38. inline constexpr void resolve_f(std::false_type, F&&) {
  39. static_assert(meta::call_operator_deducible_v<F>, "Cannot use no-template-parameter call with an overloaded functor: specify the signature");
  40. }
  41. template <typename F, typename U = meta::unqualified_t<F>>
  42. inline constexpr auto resolve_i(types<>, F&& f) -> decltype(resolve_f(meta::call_operator_deducible<U>(), std::forward<F>(f))) {
  43. return resolve_f(meta::call_operator_deducible<U> {}, std::forward<F>(f));
  44. }
  45. template <typename... Args, typename F, typename R = std::invoke_result_t<F&, Args...>>
  46. inline constexpr auto resolve_i(types<Args...>, F&& f) -> decltype(resolve_i(types<R(Args...)>(), std::forward<F>(f))) {
  47. return resolve_i(types<R(Args...)>(), std::forward<F>(f));
  48. }
  49. template <typename Sig, typename C>
  50. inline constexpr Sig C::*resolve_v(std::false_type, Sig C::*mem_func_ptr) {
  51. return mem_func_ptr;
  52. }
  53. template <typename Sig, typename C>
  54. inline constexpr Sig C::*resolve_v(std::true_type, Sig C::*mem_variable_ptr) {
  55. return mem_variable_ptr;
  56. }
  57. } // namespace detail
  58. template <typename... Args, typename R>
  59. inline constexpr auto resolve(R fun_ptr(Args...)) -> R (*)(Args...) {
  60. return fun_ptr;
  61. }
  62. template <typename Sig>
  63. inline constexpr Sig* resolve(Sig* fun_ptr) {
  64. return fun_ptr;
  65. }
  66. template <typename... Args, typename R, typename C>
  67. inline constexpr auto resolve(R (C::*mem_ptr)(Args...)) -> R (C::*)(Args...) {
  68. return mem_ptr;
  69. }
  70. template <typename Sig, typename C>
  71. inline constexpr Sig C::*resolve(Sig C::*mem_ptr) {
  72. return detail::resolve_v(std::is_member_object_pointer<Sig C::*>(), mem_ptr);
  73. }
  74. template <typename... Sig, typename F, meta::disable<std::is_function<meta::unqualified_t<F>>> = meta::enabler>
  75. inline constexpr auto resolve(F&& f) -> decltype(detail::resolve_i(types<Sig...>(), std::forward<F>(f))) {
  76. return detail::resolve_i(types<Sig...>(), std::forward<F>(f));
  77. }
  78. #else
  79. // Clang has distinct problems with constexpr arguments,
  80. // so don't use the constexpr versions inside of clang.
  81. namespace detail {
  82. template <typename R, typename... Args, typename F, typename = std::invoke_result_t<meta::unqualified_t<F>, Args...>>
  83. inline auto resolve_i(types<R(Args...)>, F&&) -> R (meta::unqualified_t<F>::*)(Args...) {
  84. using Sig = R(Args...);
  85. typedef meta::unqualified_t<F> Fu;
  86. return static_cast<Sig Fu::*>(&Fu::operator());
  87. }
  88. template <typename F, typename U = meta::unqualified_t<F>>
  89. inline auto resolve_f(std::true_type, F&& f)
  90. -> decltype(resolve_i(types<meta::function_signature_t<decltype(&U::operator())>>(), std::forward<F>(f))) {
  91. return resolve_i(types<meta::function_signature_t<decltype(&U::operator())>>(), std::forward<F>(f));
  92. }
  93. template <typename F>
  94. inline void resolve_f(std::false_type, F&&) {
  95. static_assert(meta::call_operator_deducible_v<F>, "Cannot use no-template-parameter call with an overloaded functor: specify the signature");
  96. }
  97. template <typename F, typename U = meta::unqualified_t<F>>
  98. inline auto resolve_i(types<>, F&& f) -> decltype(resolve_f(meta::call_operator_deducible<U>(), std::forward<F>(f))) {
  99. return resolve_f(meta::call_operator_deducible<U> {}, std::forward<F>(f));
  100. }
  101. template <typename... Args, typename F, typename R = std::invoke_result_t<F&, Args...>>
  102. inline auto resolve_i(types<Args...>, F&& f) -> decltype(resolve_i(types<R(Args...)>(), std::forward<F>(f))) {
  103. return resolve_i(types<R(Args...)>(), std::forward<F>(f));
  104. }
  105. template <typename Sig, typename C>
  106. inline Sig C::*resolve_v(std::false_type, Sig C::*mem_func_ptr) {
  107. return mem_func_ptr;
  108. }
  109. template <typename Sig, typename C>
  110. inline Sig C::*resolve_v(std::true_type, Sig C::*mem_variable_ptr) {
  111. return mem_variable_ptr;
  112. }
  113. } // namespace detail
  114. template <typename... Args, typename R>
  115. inline auto resolve(R fun_ptr(Args...)) -> R (*)(Args...) {
  116. return fun_ptr;
  117. }
  118. template <typename Sig>
  119. inline Sig* resolve(Sig* fun_ptr) {
  120. return fun_ptr;
  121. }
  122. template <typename... Args, typename R, typename C>
  123. inline auto resolve(R (C::*mem_ptr)(Args...)) -> R (C::*)(Args...) {
  124. return mem_ptr;
  125. }
  126. template <typename Sig, typename C>
  127. inline Sig C::*resolve(Sig C::*mem_ptr) {
  128. return detail::resolve_v(std::is_member_object_pointer<Sig C::*>(), mem_ptr);
  129. }
  130. template <typename... Sig, typename F>
  131. inline auto resolve(F&& f) -> decltype(detail::resolve_i(types<Sig...>(), std::forward<F>(f))) {
  132. return detail::resolve_i(types<Sig...>(), std::forward<F>(f));
  133. }
  134. #endif
  135. } // namespace sol
  136. #endif // SOL_RESOLVE_HPP