stack_check_qualified.hpp 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_STACK_CHECK_QUALIFIED_HPP
  19. #define SOL_STACK_CHECK_QUALIFIED_HPP
  20. #include <sol/stack_check_unqualified.hpp>
  21. namespace sol { namespace stack {
  22. template <typename X, type expected, typename>
  23. struct qualified_checker {
  24. template <typename Handler>
  25. static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
  26. using no_cv_X = meta::unqualified_t<X>;
  27. if constexpr (!std::is_reference_v<X> && is_unique_usertype_v<no_cv_X>) {
  28. using element = unique_usertype_element_t<no_cv_X>;
  29. if constexpr (is_actual_type_rebindable_for_v<no_cv_X>) {
  30. using rebound_actual_type = unique_usertype_rebind_actual_t<no_cv_X>;
  31. // we have a unique pointer type that can be
  32. // rebound to a base/derived type
  33. const type indextype = type_of(L, index);
  34. tracking.use(1);
  35. if (indextype != type::userdata) {
  36. handler(L, index, type::userdata, indextype, "value is not a userdata");
  37. return false;
  38. }
  39. void* memory = lua_touserdata(L, index);
  40. memory = detail::align_usertype_unique_destructor(memory);
  41. detail::unique_destructor& pdx = *static_cast<detail::unique_destructor*>(memory);
  42. if (&detail::usertype_unique_alloc_destroy<element, no_cv_X> == pdx) {
  43. return true;
  44. }
  45. if constexpr (derive<element>::value) {
  46. memory = detail::align_usertype_unique_tag<true, false>(memory);
  47. detail::unique_tag& ic = *reinterpret_cast<detail::unique_tag*>(memory);
  48. string_view ti = usertype_traits<element>::qualified_name();
  49. string_view rebind_ti = usertype_traits<rebound_actual_type>::qualified_name();
  50. if (ic(nullptr, nullptr, ti, rebind_ti) != 0) {
  51. return true;
  52. }
  53. }
  54. handler(L, index, type::userdata, indextype, "value is a userdata but is not the correct unique usertype");
  55. return false;
  56. }
  57. else {
  58. return stack::unqualified_check<X>(L, index, std::forward<Handler>(handler), tracking);
  59. }
  60. }
  61. else if constexpr (!std::is_reference_v<X> && is_container_v<no_cv_X>) {
  62. if (type_of(L, index) == type::userdata) {
  63. return stack::unqualified_check<X>(L, index, std::forward<Handler>(handler), tracking);
  64. }
  65. else {
  66. return stack::unqualified_check<nested<X>>(L, index, std::forward<Handler>(handler), tracking);
  67. }
  68. }
  69. else if constexpr (!std::is_reference_v<X> && meta::is_specialization_of_v<X, nested>) {
  70. using NestedX = typename meta::unqualified_t<X>::nested_type;
  71. return stack::check<NestedX>(L, index, ::std::forward<Handler>(handler), tracking);
  72. }
  73. else {
  74. return stack::unqualified_check<X>(L, index, std::forward<Handler>(handler), tracking);
  75. }
  76. }
  77. };
  78. }} // namespace sol::stack
  79. #endif // SOL_STACK_CHECK_HPP