is_valid.h 2.9 KB

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