1 // sol3 2 3 // The MIT License (MIT) 4 5 // Copyright (c) 2013-2021 Rapptz, ThePhD and contributors 6 7 // Permission is hereby granted, free of charge, to any person obtaining a copy of 8 // this software and associated documentation files (the "Software"), to deal in 9 // the Software without restriction, including without limitation the rights to 10 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 11 // the Software, and to permit persons to whom the Software is furnished to do so, 12 // subject to the following conditions: 13 14 // The above copyright notice and this permission notice shall be included in all 15 // copies or substantial portions of the Software. 16 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 19 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 20 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 21 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 #ifndef SOL_UNIQUE_USERTYPE_TRAITS_HPP 25 #define SOL_UNIQUE_USERTYPE_TRAITS_HPP 26 27 #include <sol/base_traits.hpp> 28 #include <sol/pointer_like.hpp> 29 30 #include <sol/forward.hpp> 31 32 #include <memory> 33 34 namespace sol { 35 36 namespace detail { 37 template <typename T> 38 struct unique_fallback { 39 using SOL_INTERNAL_UNSPECIALIZED_MARKER_ = int; 40 }; 41 42 template <typename T> 43 struct unique_fallback<std::shared_ptr<T>> { 44 private: 45 using pointer = typename std::pointer_traits<std::shared_ptr<T>>::element_type*; 46 47 public: 48 // rebind is non-void 49 // if and only if unique usertype 50 // is cast-capable 51 template <typename X> 52 using rebind_actual_type = std::shared_ptr<X>; 53 is_nullsol::detail::unique_fallback54 static bool is_null(lua_State*, const std::shared_ptr<T>& p) noexcept { 55 return p == nullptr; 56 } 57 getsol::detail::unique_fallback58 static pointer get(lua_State*, const std::shared_ptr<T>& p) noexcept { 59 return p.get(); 60 } 61 }; 62 63 template <typename T, typename D> 64 struct unique_fallback<std::unique_ptr<T, D>> { 65 private: 66 using pointer = typename std::unique_ptr<T, D>::pointer; 67 68 public: is_nullsol::detail::unique_fallback69 static bool is_null(lua_State*, const std::unique_ptr<T, D>& p) noexcept { 70 return p == nullptr; 71 } 72 getsol::detail::unique_fallback73 static pointer get(lua_State*, const std::unique_ptr<T, D>& p) noexcept { 74 return p.get(); 75 } 76 }; 77 } // namespace detail 78 79 namespace meta { namespace meta_detail { 80 template <typename T, typename = void> 81 struct unique_actual_type; 82 83 template <typename T> 84 struct unique_actual_type<T, meta::void_t<typename T::actual_type>> { 85 using type = typename T::actual_type; 86 }; 87 88 template <typename T, typename... Rest, template <typename...> class Templ> 89 struct unique_actual_type<Templ<T, Rest...>> { 90 using type = T; 91 }; 92 93 }} // namespace meta::meta_detail 94 95 template <typename T> 96 using unique_usertype_actual_t = typename meta::meta_detail::unique_actual_type<unique_usertype_traits<T>>::type; 97 98 namespace meta { namespace meta_detail { 99 template <typename T> 100 using value_test_t = decltype(T::value); 101 102 template <typename T> 103 using type_test_t = typename T::type; 104 105 template <typename T> 106 using type_element_type_t = typename T::element_type; 107 108 template <typename T, typename = void> 109 struct unique_element_type { 110 using type = typename std::pointer_traits<typename unique_actual_type<T>::type>::element_type; 111 }; 112 113 template <typename T> 114 struct unique_element_type<T, std::enable_if_t<meta::is_detected_v<type_element_type_t, T>>> { 115 using type = typename T::element_type; 116 }; 117 118 template <typename T> 119 struct unique_element_type<T, std::enable_if_t<meta::is_detected_v<type_test_t, T>>> { 120 using type = typename T::type; 121 }; 122 123 template <typename T, typename = void> 124 struct unique_valid : std::integral_constant<bool, !has_internal_marker_v<T>> { }; 125 126 template <typename T> 127 struct unique_valid<T, meta::void_t<decltype(T::value)>> : std::integral_constant<bool, T::value> { }; 128 }} // namespace meta::meta_detail 129 130 template <typename T> 131 using unique_usertype_element_t = typename meta::meta_detail::unique_element_type<unique_usertype_traits<T>>::type; 132 133 template <typename T, typename Element = void> 134 using unique_usertype_rebind_actual_t = typename unique_usertype_traits<T>::template rebind_actual_type<Element>; 135 136 template <typename T> 137 struct unique_usertype_traits : public detail::unique_fallback<T> { }; 138 139 template <typename T> 140 struct is_unique_usertype : std::integral_constant<bool, meta::meta_detail::unique_valid<unique_usertype_traits<T>>::value> { }; 141 142 template <typename T> 143 inline constexpr bool is_unique_usertype_v = is_unique_usertype<T>::value; 144 145 namespace meta { namespace meta_detail { 146 template <typename T> 147 using adl_sol_lua_check_access_test_t 148 = decltype(sol_lua_check_access(types<T>(), static_cast<lua_State*>(nullptr), -1, std::declval<stack::record&>())); 149 150 template <typename T> 151 inline constexpr bool is_adl_sol_lua_check_access_v = meta::is_detected_v<adl_sol_lua_check_access_test_t, T>; 152 153 template <typename T> 154 using unique_usertype_get_with_state_test_t 155 = decltype(unique_usertype_traits<T>::get(static_cast<lua_State*>(nullptr), std::declval<unique_usertype_actual_t<T>>())); 156 157 template <typename T> 158 inline constexpr bool unique_usertype_get_with_state_v = meta::is_detected_v<unique_usertype_get_with_state_test_t, T>; 159 160 template <typename T> 161 using unique_usertype_is_null_with_state_test_t 162 = decltype(unique_usertype_traits<T>::is_null(static_cast<lua_State*>(nullptr), std::declval<unique_usertype_actual_t<T>>())); 163 164 template <typename T> 165 inline constexpr bool unique_usertype_is_null_with_state_v = meta::is_detected_v<unique_usertype_is_null_with_state_test_t, T>; 166 }} // namespace meta::meta_detail 167 168 namespace detail { 169 template <typename T> unique_is_null_noexcept()170 constexpr bool unique_is_null_noexcept() noexcept { 171 if constexpr (meta::meta_detail::unique_usertype_is_null_with_state_v<std::remove_cv_t<T>>) { 172 return noexcept( 173 unique_usertype_traits<T>::is_null(static_cast<lua_State*>(nullptr), std::declval<unique_usertype_actual_t<std::remove_cv_t<T>>>())); 174 } 175 else { 176 return noexcept(unique_usertype_traits<T>::is_null(std::declval<unique_usertype_actual_t<std::remove_cv_t<T>>>())); 177 } 178 } 179 180 template <typename T> unique_is_null(lua_State * L_,T & value_)181 bool unique_is_null(lua_State* L_, T& value_) noexcept(unique_is_null_noexcept<std::remove_cv_t<T>>()) { 182 using Tu = std::remove_cv_t<T>; 183 if constexpr (meta::meta_detail::unique_usertype_is_null_with_state_v<Tu>) { 184 return unique_usertype_traits<Tu>::is_null(L_, value_); 185 } 186 else { 187 return unique_usertype_traits<Tu>::is_null(value_); 188 } 189 } 190 191 template <typename T> unique_get_noexcept()192 constexpr bool unique_get_noexcept() noexcept { 193 if constexpr (meta::meta_detail::unique_usertype_get_with_state_v<std::remove_cv_t<T>>) { 194 return noexcept( 195 unique_usertype_traits<T>::get(static_cast<lua_State*>(nullptr), std::declval<unique_usertype_actual_t<std::remove_cv_t<T>>>())); 196 } 197 else { 198 return noexcept(unique_usertype_traits<T>::get(std::declval<unique_usertype_actual_t<std::remove_cv_t<T>>>())); 199 } 200 } 201 202 template <typename T> unique_get(lua_State * L_,T & value_)203 auto unique_get(lua_State* L_, T& value_) noexcept(unique_get_noexcept<std::remove_cv_t<T>>()) { 204 using Tu = std::remove_cv_t<T>; 205 if constexpr (meta::meta_detail::unique_usertype_get_with_state_v<Tu>) { 206 return unique_usertype_traits<Tu>::get(L_, value_); 207 } 208 else { 209 return unique_usertype_traits<Tu>::get(value_); 210 } 211 } 212 } // namespace detail 213 214 namespace meta { namespace meta_detail { 215 template <typename T, typename Element = void> 216 using is_rebind_actual_type_test_t = typename T::template rebind_actual_type<Element>; 217 218 template <typename T, typename Element = void> 219 using is_rebind_actual_type = meta::is_detected<is_rebind_actual_type_test_t, T, Element>; 220 221 template <typename T, typename Element = void> 222 inline constexpr bool is_rebind_actual_type_v = is_rebind_actual_type<T, Element>::value; 223 224 template <typename T, typename Element, bool = is_rebind_actual_type_v<T, Element>> 225 struct is_actual_type_rebindable_for_test : std::false_type { }; 226 227 template <typename T, typename Element> 228 struct is_actual_type_rebindable_for_test<T, Element, true> 229 : std::integral_constant<bool, !std::is_void_v<typename T::template rebind_actual_type<Element>>> { }; 230 }} // namespace meta::meta_detail 231 232 template <typename T, typename Element = void> 233 using is_actual_type_rebindable_for = typename meta::meta_detail::is_actual_type_rebindable_for_test<unique_usertype_traits<T>, Element>::type; 234 235 template <typename T, typename Element = void> 236 inline constexpr bool is_actual_type_rebindable_for_v = is_actual_type_rebindable_for<T, Element>::value; 237 238 } // namespace sol 239 240 #endif // SOL_UNIQUE_USERTYPE_TRAITS_HPP 241