2020-12-17 04:25:35 +08:00
|
|
|
// sol3
|
2018-06-16 01:19:09 +08:00
|
|
|
|
|
|
|
// The MIT License (MIT)
|
|
|
|
|
2020-08-13 22:42:37 +08:00
|
|
|
// Copyright (c) 2013-2020 Rapptz, ThePhD and contributors
|
2018-06-16 01:19:09 +08:00
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
|
|
// the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
// subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef SOL_STACK_CHECK_QUALIFIED_HPP
|
|
|
|
#define SOL_STACK_CHECK_QUALIFIED_HPP
|
|
|
|
|
2020-08-10 05:17:06 +08:00
|
|
|
#include <sol/stack_check_unqualified.hpp>
|
2018-06-16 01:19:09 +08:00
|
|
|
|
2020-12-17 04:25:35 +08:00
|
|
|
namespace sol { namespace stack {
|
2018-06-16 01:19:09 +08:00
|
|
|
|
2019-03-18 19:41:51 +08:00
|
|
|
template <typename X, type expected, typename>
|
2019-03-10 09:57:49 +08:00
|
|
|
struct qualified_checker {
|
2018-06-16 01:19:09 +08:00
|
|
|
template <typename Handler>
|
2019-03-10 09:57:49 +08:00
|
|
|
static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
|
2021-01-04 18:52:30 +08:00
|
|
|
using no_cv_X = meta::unqualified_t<X>;
|
|
|
|
if constexpr (!std::is_reference_v<X> && is_unique_usertype_v<no_cv_X>) {
|
|
|
|
using element = unique_usertype_element_t<no_cv_X>;
|
|
|
|
if constexpr (is_actual_type_rebindable_for_v<no_cv_X>) {
|
|
|
|
using rebound_actual_type = unique_usertype_rebind_actual_t<no_cv_X>;
|
2019-03-10 09:57:49 +08:00
|
|
|
// we have a unique pointer type that can be
|
|
|
|
// rebound to a base/derived type
|
|
|
|
const type indextype = type_of(L, index);
|
|
|
|
tracking.use(1);
|
|
|
|
if (indextype != type::userdata) {
|
|
|
|
handler(L, index, type::userdata, indextype, "value is not a userdata");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void* memory = lua_touserdata(L, index);
|
|
|
|
memory = detail::align_usertype_unique_destructor(memory);
|
|
|
|
detail::unique_destructor& pdx = *static_cast<detail::unique_destructor*>(memory);
|
2021-01-04 18:52:30 +08:00
|
|
|
if (&detail::usertype_unique_alloc_destroy<element, no_cv_X> == pdx) {
|
2019-03-10 09:57:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-01-04 18:52:30 +08:00
|
|
|
if constexpr (derive<element>::value) {
|
2019-03-10 09:57:49 +08:00
|
|
|
memory = detail::align_usertype_unique_tag<true, false>(memory);
|
|
|
|
detail::unique_tag& ic = *reinterpret_cast<detail::unique_tag*>(memory);
|
2021-01-04 18:52:30 +08:00
|
|
|
string_view ti = usertype_traits<element>::qualified_name();
|
|
|
|
string_view rebind_ti = usertype_traits<rebound_actual_type>::qualified_name();
|
2019-03-10 09:57:49 +08:00
|
|
|
if (ic(nullptr, nullptr, ti, rebind_ti) != 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handler(L, index, type::userdata, indextype, "value is a userdata but is not the correct unique usertype");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return stack::unqualified_check<X>(L, index, std::forward<Handler>(handler), tracking);
|
2018-06-16 01:19:09 +08:00
|
|
|
}
|
|
|
|
}
|
2021-01-04 18:52:30 +08:00
|
|
|
else if constexpr (!std::is_reference_v<X> && is_container_v<no_cv_X>) {
|
2019-03-10 09:57:49 +08:00
|
|
|
if (type_of(L, index) == type::userdata) {
|
|
|
|
return stack::unqualified_check<X>(L, index, std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return stack::unqualified_check<nested<X>>(L, index, std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
2018-06-16 01:19:09 +08:00
|
|
|
}
|
2020-01-29 02:44:35 +08:00
|
|
|
else if constexpr (!std::is_reference_v<X> && meta::is_specialization_of_v<X, nested>) {
|
|
|
|
using NestedX = typename meta::unqualified_t<X>::nested_type;
|
|
|
|
return stack::check<NestedX>(L, index, ::std::forward<Handler>(handler), tracking);
|
|
|
|
}
|
2018-06-16 01:19:09 +08:00
|
|
|
else {
|
2019-03-10 09:57:49 +08:00
|
|
|
return stack::unqualified_check<X>(L, index, std::forward<Handler>(handler), tracking);
|
2018-06-16 01:19:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-12-17 04:25:35 +08:00
|
|
|
}} // namespace sol::stack
|
2018-06-16 01:19:09 +08:00
|
|
|
|
|
|
|
#endif // SOL_STACK_CHECK_HPP
|