inheritance.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_INHERITANCE_HPP
  19. #define SOL_INHERITANCE_HPP
  20. #include <sol/types.hpp>
  21. #include <sol/usertype_traits.hpp>
  22. #include <sol/unique_usertype_traits.hpp>
  23. namespace sol {
  24. template <typename... Args>
  25. struct base_list { };
  26. template <typename... Args>
  27. using bases = base_list<Args...>;
  28. typedef bases<> base_classes_tag;
  29. const auto base_classes = base_classes_tag();
  30. template <typename... Args>
  31. struct is_to_stringable<base_list<Args...>> : std::false_type { };
  32. namespace detail {
  33. inline decltype(auto) base_class_check_key() {
  34. static const auto& key = "class_check";
  35. return key;
  36. }
  37. inline decltype(auto) base_class_cast_key() {
  38. static const auto& key = "class_cast";
  39. return key;
  40. }
  41. inline decltype(auto) base_class_index_propogation_key() {
  42. static const auto& key = u8"\xF0\x9F\x8C\xB2.index";
  43. return key;
  44. }
  45. inline decltype(auto) base_class_new_index_propogation_key() {
  46. static const auto& key = u8"\xF0\x9F\x8C\xB2.new_index";
  47. return key;
  48. }
  49. template <typename T>
  50. struct inheritance {
  51. typedef typename base<T>::type bases_t;
  52. static bool type_check_bases(types<>, const string_view&) {
  53. return false;
  54. }
  55. template <typename Base, typename... Args>
  56. static bool type_check_bases(types<Base, Args...>, const string_view& ti) {
  57. return ti == usertype_traits<Base>::qualified_name() || type_check_bases(types<Args...>(), ti);
  58. }
  59. static bool type_check(const string_view& ti) {
  60. return ti == usertype_traits<T>::qualified_name() || type_check_bases(bases_t(), ti);
  61. }
  62. template <typename... Bases>
  63. static bool type_check_with(const string_view& ti) {
  64. return ti == usertype_traits<T>::qualified_name() || type_check_bases(types<Bases...>(), ti);
  65. }
  66. static void* type_cast_bases(types<>, T*, const string_view&) {
  67. return nullptr;
  68. }
  69. template <typename Base, typename... Args>
  70. static void* type_cast_bases(types<Base, Args...>, T* data, const string_view& ti) {
  71. // Make sure to convert to T first, and then dynamic cast to the proper type
  72. return ti != usertype_traits<Base>::qualified_name() ? type_cast_bases(types<Args...>(), data, ti)
  73. : static_cast<void*>(static_cast<Base*>(data));
  74. }
  75. static void* type_cast(void* voiddata, const string_view& ti) {
  76. T* data = static_cast<T*>(voiddata);
  77. return static_cast<void*>(ti != usertype_traits<T>::qualified_name() ? type_cast_bases(bases_t(), data, ti) : data);
  78. }
  79. template <typename... Bases>
  80. static void* type_cast_with(void* voiddata, const string_view& ti) {
  81. T* data = static_cast<T*>(voiddata);
  82. return static_cast<void*>(ti != usertype_traits<T>::qualified_name() ? type_cast_bases(types<Bases...>(), data, ti) : data);
  83. }
  84. template <typename U>
  85. static bool type_unique_cast_bases(types<>, void*, void*, const string_view&) {
  86. return 0;
  87. }
  88. template <typename U, typename Base, typename... Args>
  89. static int type_unique_cast_bases(types<Base, Args...>, void* source_data, void* target_data, const string_view& ti) {
  90. using uu_traits = unique_usertype_traits<U>;
  91. using base_ptr = typename uu_traits::template rebind_actual_type<Base>;
  92. string_view base_ti = usertype_traits<Base>::qualified_name();
  93. if (base_ti == ti) {
  94. if (target_data != nullptr) {
  95. U* source = static_cast<U*>(source_data);
  96. base_ptr* target = static_cast<base_ptr*>(target_data);
  97. // perform proper derived -> base conversion
  98. *target = *source;
  99. }
  100. return 2;
  101. }
  102. return type_unique_cast_bases<U>(types<Args...>(), source_data, target_data, ti);
  103. }
  104. template <typename U>
  105. static int type_unique_cast(void* source_data, void* target_data, const string_view& ti, const string_view& rebind_ti) {
  106. if constexpr (is_actual_type_rebindable_for_v<U>) {
  107. using rebound_actual_type = unique_usertype_rebind_actual_t<U>;
  108. using maybe_bases_or_empty = meta::conditional_t<std::is_void_v<rebound_actual_type>, types<>, bases_t>;
  109. string_view this_rebind_ti = usertype_traits<rebound_actual_type>::qualified_name();
  110. if (rebind_ti != this_rebind_ti) {
  111. // this is not even of the same unique type
  112. return 0;
  113. }
  114. string_view this_ti = usertype_traits<T>::qualified_name();
  115. if (ti == this_ti) {
  116. // direct match, return 1
  117. return 1;
  118. }
  119. return type_unique_cast_bases<U>(maybe_bases_or_empty(), source_data, target_data, ti);
  120. }
  121. else {
  122. (void)rebind_ti;
  123. string_view this_ti = usertype_traits<T>::qualified_name();
  124. if (ti == this_ti) {
  125. // direct match, return 1
  126. return 1;
  127. }
  128. return type_unique_cast_bases<U>(types<>(), source_data, target_data, ti);
  129. }
  130. }
  131. template <typename U, typename... Bases>
  132. static int type_unique_cast_with(void* source_data, void* target_data, const string_view& ti, const string_view& rebind_ti) {
  133. using uc_bases_t = types<Bases...>;
  134. if constexpr (is_actual_type_rebindable_for_v<U>) {
  135. using rebound_actual_type = unique_usertype_rebind_actual_t<U>;
  136. using cond_bases_t = meta::conditional_t<std::is_void_v<rebound_actual_type>, types<>, uc_bases_t>;
  137. string_view this_rebind_ti = usertype_traits<rebound_actual_type>::qualified_name();
  138. if (rebind_ti != this_rebind_ti) {
  139. // this is not even of the same unique type
  140. return 0;
  141. }
  142. string_view this_ti = usertype_traits<T>::qualified_name();
  143. if (ti == this_ti) {
  144. // direct match, return 1
  145. return 1;
  146. }
  147. return type_unique_cast_bases<U>(cond_bases_t(), source_data, target_data, ti);
  148. }
  149. else {
  150. (void)rebind_ti;
  151. string_view this_ti = usertype_traits<T>::qualified_name();
  152. if (ti == this_ti) {
  153. // direct match, return 1
  154. return 1;
  155. }
  156. return type_unique_cast_bases<U>(types<>(), source_data, target_data, ti);
  157. }
  158. }
  159. };
  160. using inheritance_check_function = decltype(&inheritance<void>::type_check);
  161. using inheritance_cast_function = decltype(&inheritance<void>::type_cast);
  162. using inheritance_unique_cast_function = decltype(&inheritance<void>::type_unique_cast<void>);
  163. } // namespace detail
  164. } // namespace sol
  165. #endif // SOL_INHERITANCE_HPP