2021-03-06 23:14:48 +08:00
|
|
|
// sol2
|
2018-01-24 14:10:35 +08:00
|
|
|
|
|
|
|
// The MIT License (MIT)
|
|
|
|
|
2022-06-25 16:00:53 +08:00
|
|
|
// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
|
2018-01-24 14:10:35 +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_INHERITANCE_HPP
|
|
|
|
#define SOL_INHERITANCE_HPP
|
|
|
|
|
2020-08-10 05:17:06 +08:00
|
|
|
#include <sol/types.hpp>
|
|
|
|
#include <sol/usertype_traits.hpp>
|
|
|
|
#include <sol/unique_usertype_traits.hpp>
|
2018-01-24 14:10:35 +08:00
|
|
|
|
|
|
|
namespace sol {
|
|
|
|
template <typename... Args>
|
2020-12-17 04:25:35 +08:00
|
|
|
struct base_list { };
|
2018-01-24 14:10:35 +08:00
|
|
|
template <typename... Args>
|
|
|
|
using bases = base_list<Args...>;
|
|
|
|
|
|
|
|
typedef bases<> base_classes_tag;
|
|
|
|
const auto base_classes = base_classes_tag();
|
|
|
|
|
2019-08-28 08:04:55 +08:00
|
|
|
template <typename... Args>
|
2020-12-17 04:25:35 +08:00
|
|
|
struct is_to_stringable<base_list<Args...>> : std::false_type { };
|
2019-08-28 08:04:55 +08:00
|
|
|
|
2018-01-24 14:10:35 +08:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
inline decltype(auto) base_class_check_key() {
|
|
|
|
static const auto& key = "class_check";
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline decltype(auto) base_class_cast_key() {
|
|
|
|
static const auto& key = "class_cast";
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline decltype(auto) base_class_index_propogation_key() {
|
|
|
|
static const auto& key = u8"\xF0\x9F\x8C\xB2.index";
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline decltype(auto) base_class_new_index_propogation_key() {
|
|
|
|
static const auto& key = u8"\xF0\x9F\x8C\xB2.new_index";
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2018-06-18 03:42:24 +08:00
|
|
|
template <typename T>
|
2018-01-24 14:10:35 +08:00
|
|
|
struct inheritance {
|
2018-06-18 03:42:24 +08:00
|
|
|
typedef typename base<T>::type bases_t;
|
2018-09-28 13:27:38 +08:00
|
|
|
|
|
|
|
static bool type_check_bases(types<>, const string_view&) {
|
2018-01-24 14:10:35 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename... Args>
|
2018-09-28 13:27:38 +08:00
|
|
|
static bool type_check_bases(types<Base, Args...>, const string_view& ti) {
|
2018-01-24 14:10:35 +08:00
|
|
|
return ti == usertype_traits<Base>::qualified_name() || type_check_bases(types<Args...>(), ti);
|
|
|
|
}
|
|
|
|
|
2018-09-28 13:27:38 +08:00
|
|
|
static bool type_check(const string_view& ti) {
|
2018-06-18 03:42:24 +08:00
|
|
|
return ti == usertype_traits<T>::qualified_name() || type_check_bases(bases_t(), ti);
|
2018-01-24 14:10:35 +08:00
|
|
|
}
|
|
|
|
|
2020-12-17 04:25:35 +08:00
|
|
|
template <typename... Bases>
|
2018-12-20 12:17:15 +08:00
|
|
|
static bool type_check_with(const string_view& ti) {
|
|
|
|
return ti == usertype_traits<T>::qualified_name() || type_check_bases(types<Bases...>(), ti);
|
|
|
|
}
|
|
|
|
|
2018-09-28 13:27:38 +08:00
|
|
|
static void* type_cast_bases(types<>, T*, const string_view&) {
|
2018-01-24 14:10:35 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename... Args>
|
2018-09-28 13:27:38 +08:00
|
|
|
static void* type_cast_bases(types<Base, Args...>, T* data, const string_view& ti) {
|
2018-01-24 14:10:35 +08:00
|
|
|
// Make sure to convert to T first, and then dynamic cast to the proper type
|
2020-12-17 04:25:35 +08:00
|
|
|
return ti != usertype_traits<Base>::qualified_name() ? type_cast_bases(types<Args...>(), data, ti)
|
|
|
|
: static_cast<void*>(static_cast<Base*>(data));
|
2018-01-24 14:10:35 +08:00
|
|
|
}
|
|
|
|
|
2018-09-28 13:27:38 +08:00
|
|
|
static void* type_cast(void* voiddata, const string_view& ti) {
|
2018-01-24 14:10:35 +08:00
|
|
|
T* data = static_cast<T*>(voiddata);
|
2018-06-18 03:42:24 +08:00
|
|
|
return static_cast<void*>(ti != usertype_traits<T>::qualified_name() ? type_cast_bases(bases_t(), data, ti) : data);
|
2018-01-24 14:10:35 +08:00
|
|
|
}
|
2018-06-16 01:19:09 +08:00
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
template <typename... Bases>
|
|
|
|
static void* type_cast_with(void* voiddata, const string_view& ti) {
|
|
|
|
T* data = static_cast<T*>(voiddata);
|
|
|
|
return static_cast<void*>(ti != usertype_traits<T>::qualified_name() ? type_cast_bases(types<Bases...>(), data, ti) : data);
|
|
|
|
}
|
|
|
|
|
2018-06-16 01:19:09 +08:00
|
|
|
template <typename U>
|
2018-06-18 03:42:24 +08:00
|
|
|
static bool type_unique_cast_bases(types<>, void*, void*, const string_view&) {
|
|
|
|
return 0;
|
2018-06-16 01:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U, typename Base, typename... Args>
|
2018-06-18 03:42:24 +08:00
|
|
|
static int type_unique_cast_bases(types<Base, Args...>, void* source_data, void* target_data, const string_view& ti) {
|
2019-04-13 16:05:34 +08:00
|
|
|
using uu_traits = unique_usertype_traits<U>;
|
2021-01-04 18:52:30 +08:00
|
|
|
using base_ptr = typename uu_traits::template rebind_actual_type<Base>;
|
2018-06-16 01:19:09 +08:00
|
|
|
string_view base_ti = usertype_traits<Base>::qualified_name();
|
|
|
|
if (base_ti == ti) {
|
|
|
|
if (target_data != nullptr) {
|
|
|
|
U* source = static_cast<U*>(source_data);
|
|
|
|
base_ptr* target = static_cast<base_ptr*>(target_data);
|
|
|
|
// perform proper derived -> base conversion
|
|
|
|
*target = *source;
|
|
|
|
}
|
2018-06-18 03:42:24 +08:00
|
|
|
return 2;
|
2018-06-16 01:19:09 +08:00
|
|
|
}
|
2018-06-18 03:42:24 +08:00
|
|
|
return type_unique_cast_bases<U>(types<Args...>(), source_data, target_data, ti);
|
2018-06-16 01:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
2018-06-18 03:42:24 +08:00
|
|
|
static int type_unique_cast(void* source_data, void* target_data, const string_view& ti, const string_view& rebind_ti) {
|
2021-01-04 18:52:30 +08:00
|
|
|
if constexpr (is_actual_type_rebindable_for_v<U>) {
|
|
|
|
using rebound_actual_type = unique_usertype_rebind_actual_t<U>;
|
|
|
|
using maybe_bases_or_empty = meta::conditional_t<std::is_void_v<rebound_actual_type>, types<>, bases_t>;
|
|
|
|
string_view this_rebind_ti = usertype_traits<rebound_actual_type>::qualified_name();
|
2019-04-13 16:05:34 +08:00
|
|
|
if (rebind_ti != this_rebind_ti) {
|
|
|
|
// this is not even of the same unique type
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
string_view this_ti = usertype_traits<T>::qualified_name();
|
|
|
|
if (ti == this_ti) {
|
|
|
|
// direct match, return 1
|
|
|
|
return 1;
|
|
|
|
}
|
2021-01-04 18:52:30 +08:00
|
|
|
return type_unique_cast_bases<U>(maybe_bases_or_empty(), source_data, target_data, ti);
|
2018-06-16 01:19:09 +08:00
|
|
|
}
|
2019-04-13 16:05:34 +08:00
|
|
|
else {
|
|
|
|
(void)rebind_ti;
|
|
|
|
string_view this_ti = usertype_traits<T>::qualified_name();
|
|
|
|
if (ti == this_ti) {
|
|
|
|
// direct match, return 1
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return type_unique_cast_bases<U>(types<>(), source_data, target_data, ti);
|
2018-06-18 03:42:24 +08:00
|
|
|
}
|
2018-06-16 01:19:09 +08:00
|
|
|
}
|
2018-12-20 12:17:15 +08:00
|
|
|
|
|
|
|
template <typename U, typename... Bases>
|
|
|
|
static int type_unique_cast_with(void* source_data, void* target_data, const string_view& ti, const string_view& rebind_ti) {
|
|
|
|
using uc_bases_t = types<Bases...>;
|
2021-01-04 18:52:30 +08:00
|
|
|
if constexpr (is_actual_type_rebindable_for_v<U>) {
|
|
|
|
using rebound_actual_type = unique_usertype_rebind_actual_t<U>;
|
|
|
|
using cond_bases_t = meta::conditional_t<std::is_void_v<rebound_actual_type>, types<>, uc_bases_t>;
|
2021-01-05 03:58:54 +08:00
|
|
|
string_view this_rebind_ti = usertype_traits<rebound_actual_type>::qualified_name();
|
2019-04-13 16:05:34 +08:00
|
|
|
if (rebind_ti != this_rebind_ti) {
|
|
|
|
// this is not even of the same unique type
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
string_view this_ti = usertype_traits<T>::qualified_name();
|
|
|
|
if (ti == this_ti) {
|
|
|
|
// direct match, return 1
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return type_unique_cast_bases<U>(cond_bases_t(), source_data, target_data, ti);
|
2018-12-20 12:17:15 +08:00
|
|
|
}
|
2019-04-13 16:05:34 +08:00
|
|
|
else {
|
|
|
|
(void)rebind_ti;
|
|
|
|
string_view this_ti = usertype_traits<T>::qualified_name();
|
|
|
|
if (ti == this_ti) {
|
|
|
|
// direct match, return 1
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return type_unique_cast_bases<U>(types<>(), source_data, target_data, ti);
|
2018-12-20 12:17:15 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-24 14:10:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
using inheritance_check_function = decltype(&inheritance<void>::type_check);
|
|
|
|
using inheritance_cast_function = decltype(&inheritance<void>::type_cast);
|
2018-06-16 01:19:09 +08:00
|
|
|
using inheritance_unique_cast_function = decltype(&inheritance<void>::type_unique_cast<void>);
|
2018-01-24 14:10:35 +08:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace sol
|
|
|
|
|
|
|
|
#endif // SOL_INHERITANCE_HPP
|