is_valid.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___RANDOM_IS_VALID_H
  9. #define _LIBCPP___RANDOM_IS_VALID_H
  10. #include <__config>
  11. #include <__type_traits/enable_if.h>
  12. #include <__type_traits/integral_constant.h>
  13. #include <__type_traits/is_same.h>
  14. #include <__type_traits/is_unsigned.h>
  15. #include <__utility/declval.h>
  16. #include <cstdint>
  17. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  18. # pragma GCC system_header
  19. #endif
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. // [rand.req.genl]/1.4:
  22. // The effect of instantiating a template that has a template type parameter
  23. // named RealType is undefined unless the corresponding template argument is
  24. // cv-unqualified and is one of float, double, or long double.
  25. template <class>
  26. struct __libcpp_random_is_valid_realtype : false_type {};
  27. template <>
  28. struct __libcpp_random_is_valid_realtype<float> : true_type {};
  29. template <>
  30. struct __libcpp_random_is_valid_realtype<double> : true_type {};
  31. template <>
  32. struct __libcpp_random_is_valid_realtype<long double> : true_type {};
  33. // [rand.req.genl]/1.5:
  34. // The effect of instantiating a template that has a template type parameter
  35. // named IntType is undefined unless the corresponding template argument is
  36. // cv-unqualified and is one of short, int, long, long long, unsigned short,
  37. // unsigned int, unsigned long, or unsigned long long.
  38. template <class>
  39. struct __libcpp_random_is_valid_inttype : false_type {};
  40. template <>
  41. struct __libcpp_random_is_valid_inttype<int8_t> : true_type {}; // extension
  42. template <>
  43. struct __libcpp_random_is_valid_inttype<short> : true_type {};
  44. template <>
  45. struct __libcpp_random_is_valid_inttype<int> : true_type {};
  46. template <>
  47. struct __libcpp_random_is_valid_inttype<long> : true_type {};
  48. template <>
  49. struct __libcpp_random_is_valid_inttype<long long> : true_type {};
  50. template <>
  51. struct __libcpp_random_is_valid_inttype<uint8_t> : true_type {}; // extension
  52. template <>
  53. struct __libcpp_random_is_valid_inttype<unsigned short> : true_type {};
  54. template <>
  55. struct __libcpp_random_is_valid_inttype<unsigned int> : true_type {};
  56. template <>
  57. struct __libcpp_random_is_valid_inttype<unsigned long> : true_type {};
  58. template <>
  59. struct __libcpp_random_is_valid_inttype<unsigned long long> : true_type {};
  60. #ifndef _LIBCPP_HAS_NO_INT128
  61. template <>
  62. struct __libcpp_random_is_valid_inttype<__int128_t> : true_type {}; // extension
  63. template <>
  64. struct __libcpp_random_is_valid_inttype<__uint128_t> : true_type {}; // extension
  65. #endif // _LIBCPP_HAS_NO_INT128
  66. // [rand.req.urng]/3:
  67. // A class G meets the uniform random bit generator requirements if G models
  68. // uniform_random_bit_generator, invoke_result_t<G&> is an unsigned integer type,
  69. // and G provides a nested typedef-name result_type that denotes the same type
  70. // as invoke_result_t<G&>.
  71. // (In particular, reject URNGs with signed result_types; our distributions cannot
  72. // handle such generator types.)
  73. template <class, class = void>
  74. struct __libcpp_random_is_valid_urng : false_type {};
  75. template <class _Gp>
  76. struct __libcpp_random_is_valid_urng<
  77. _Gp,
  78. __enable_if_t< is_unsigned<typename _Gp::result_type>::value &&
  79. _IsSame<decltype(std::declval<_Gp&>()()), typename _Gp::result_type>::value > > : true_type {};
  80. _LIBCPP_END_NAMESPACE_STD
  81. #endif // _LIBCPP___RANDOM_IS_VALID_H