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