is_valid.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 <cstdint>
  12. #include <type_traits>
  13. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  14. # pragma GCC system_header
  15. #endif
  16. _LIBCPP_BEGIN_NAMESPACE_STD
  17. // [rand.req.genl]/1.5:
  18. // The effect of instantiating a template that has a template type parameter
  19. // named IntType is undefined unless the corresponding template argument is
  20. // cv-unqualified and is one of short, int, long, long long, unsigned short,
  21. // unsigned int, unsigned long, or unsigned long long.
  22. template<class> struct __libcpp_random_is_valid_inttype : false_type {};
  23. template<> struct __libcpp_random_is_valid_inttype<int8_t> : true_type {}; // extension
  24. template<> struct __libcpp_random_is_valid_inttype<short> : true_type {};
  25. template<> struct __libcpp_random_is_valid_inttype<int> : true_type {};
  26. template<> struct __libcpp_random_is_valid_inttype<long> : true_type {};
  27. template<> struct __libcpp_random_is_valid_inttype<long long> : true_type {};
  28. template<> struct __libcpp_random_is_valid_inttype<uint8_t> : true_type {}; // extension
  29. template<> struct __libcpp_random_is_valid_inttype<unsigned short> : true_type {};
  30. template<> struct __libcpp_random_is_valid_inttype<unsigned int> : true_type {};
  31. template<> struct __libcpp_random_is_valid_inttype<unsigned long> : true_type {};
  32. template<> struct __libcpp_random_is_valid_inttype<unsigned long long> : true_type {};
  33. #ifndef _LIBCPP_HAS_NO_INT128
  34. template<> struct __libcpp_random_is_valid_inttype<__int128_t> : true_type {}; // extension
  35. template<> struct __libcpp_random_is_valid_inttype<__uint128_t> : true_type {}; // extension
  36. #endif // _LIBCPP_HAS_NO_INT128
  37. // [rand.req.urng]/3:
  38. // A class G meets the uniform random bit generator requirements if G models
  39. // uniform_random_bit_generator, invoke_result_t<G&> is an unsigned integer type,
  40. // and G provides a nested typedef-name result_type that denotes the same type
  41. // as invoke_result_t<G&>.
  42. // (In particular, reject URNGs with signed result_types; our distributions cannot
  43. // handle such generator types.)
  44. template<class, class = void> struct __libcpp_random_is_valid_urng : false_type {};
  45. template<class _Gp> struct __libcpp_random_is_valid_urng<_Gp, __enable_if_t<
  46. is_unsigned<typename _Gp::result_type>::value &&
  47. _IsSame<decltype(std::declval<_Gp&>()()), typename _Gp::result_type>::value
  48. > > : true_type {};
  49. _LIBCPP_END_NAMESPACE_STD
  50. #endif // _LIBCPP___RANDOM_IS_VALID_H