base_traits.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_BASE_TRAITS_HPP
  19. #define SOL_BASE_TRAITS_HPP
  20. #include <type_traits>
  21. namespace sol {
  22. namespace detail {
  23. struct unchecked_t { };
  24. const unchecked_t unchecked = unchecked_t {};
  25. } // namespace detail
  26. namespace meta {
  27. using sfinae_yes_t = std::true_type;
  28. using sfinae_no_t = std::false_type;
  29. template <typename...>
  30. using void_t = void;
  31. template <typename T>
  32. using unqualified = std::remove_cv<std::remove_reference_t<T>>;
  33. template <typename T>
  34. using unqualified_t = typename unqualified<T>::type;
  35. namespace meta_detail {
  36. template <typename T>
  37. struct unqualified_non_alias : unqualified<T> { };
  38. template <template <class...> class Test, class, class... Args>
  39. struct is_detected : std::false_type { };
  40. template <template <class...> class Test, class... Args>
  41. struct is_detected<Test, void_t<Test<Args...>>, Args...> : std::true_type { };
  42. } // namespace meta_detail
  43. template <template <class...> class Trait, class... Args>
  44. using is_detected = typename meta_detail::is_detected<Trait, void, Args...>::type;
  45. template <template <class...> class Trait, class... Args>
  46. constexpr inline bool is_detected_v = is_detected<Trait, Args...>::value;
  47. template <typename _Default, typename _Void, template <typename...> typename _Op, typename... _Args>
  48. class detector {
  49. public:
  50. using value_t = ::std::false_type;
  51. using type = _Default;
  52. };
  53. template <typename _Default, template <typename...> typename _Op, typename... _Args>
  54. class detector<_Default, void_t<_Op<_Args...>>, _Op, _Args...> {
  55. public:
  56. using value_t = ::std::true_type;
  57. using type = _Op<_Args...>;
  58. };
  59. class nonesuch {
  60. public:
  61. ~nonesuch() = delete;
  62. nonesuch(nonesuch const&) = delete;
  63. nonesuch& operator=(nonesuch const&) = delete;
  64. };
  65. template <template <typename...> typename _Op, typename... _Args>
  66. using detected_t = typename detector<nonesuch, void, _Op, _Args...>::type;
  67. template <typename _Default, template <typename...> typename _Op, typename... _Args>
  68. using detected_or = detector<_Default, void, _Op, _Args...>;
  69. template <typename _Default, template <typename...> typename _Op, typename... _Args>
  70. using detected_or_t = typename detector<_Default, void, _Op, _Args...>::type;
  71. template <typename _Default, template <typename...> typename _Op, typename... _Args>
  72. constexpr inline bool detected_or_v = detector<_Default, void, _Op, _Args...>::value;
  73. template <std::size_t I>
  74. using index_value = std::integral_constant<std::size_t, I>;
  75. template <bool>
  76. struct conditional {
  77. template <typename T, typename U>
  78. using type = T;
  79. };
  80. template <>
  81. struct conditional<false> {
  82. template <typename T, typename U>
  83. using type = U;
  84. };
  85. template <bool B, typename T, typename U>
  86. using conditional_t = typename conditional<B>::template type<T, U>;
  87. namespace meta_detail {
  88. template <typename T, template <typename...> class Templ>
  89. struct is_specialization_of : std::false_type { };
  90. template <typename... T, template <typename...> class Templ>
  91. struct is_specialization_of<Templ<T...>, Templ> : std::true_type { };
  92. } // namespace meta_detail
  93. template <typename T, template <typename...> class Templ>
  94. using is_specialization_of = meta_detail::is_specialization_of<std::remove_cv_t<T>, Templ>;
  95. template <typename T, template <typename...> class Templ>
  96. inline constexpr bool is_specialization_of_v = is_specialization_of<std::remove_cv_t<T>, Templ>::value;
  97. template <typename T>
  98. struct identity {
  99. typedef T type;
  100. };
  101. template <typename T>
  102. using identity_t = typename identity<T>::type;
  103. template <typename T>
  104. using is_builtin_type = std::integral_constant<bool, std::is_arithmetic<T>::value || std::is_pointer<T>::value || std::is_array<T>::value>;
  105. namespace meta_detail {
  106. template <typename T, typename = void>
  107. struct has_internal_marker_impl : std::false_type { };
  108. template <typename T>
  109. struct has_internal_marker_impl<T, void_t<typename T::SOL_INTERNAL_UNSPECIALIZED_MARKER_>> : std::true_type { };
  110. template <typename T>
  111. using has_internal_marker = has_internal_marker_impl<T>;
  112. template <typename T>
  113. constexpr inline bool has_internal_marker_v = has_internal_marker<T>::value;
  114. } // namespace meta_detail
  115. } // namespace meta
  116. } // namespace sol
  117. #endif // SOL_BASE_TRAITS_HPP