xref: /freebsd-src/contrib/llvm-project/libcxx/include/__random/is_valid.h (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
181ad6265SDimitry Andric //===----------------------------------------------------------------------===//
281ad6265SDimitry Andric //
381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
681ad6265SDimitry Andric //
781ad6265SDimitry Andric //===----------------------------------------------------------------------===//
881ad6265SDimitry Andric 
981ad6265SDimitry Andric #ifndef _LIBCPP___RANDOM_IS_VALID_H
1081ad6265SDimitry Andric #define _LIBCPP___RANDOM_IS_VALID_H
1181ad6265SDimitry Andric 
1281ad6265SDimitry Andric #include <__config>
13*06c3fb27SDimitry Andric #include <__type_traits/enable_if.h>
14*06c3fb27SDimitry Andric #include <__type_traits/integral_constant.h>
15*06c3fb27SDimitry Andric #include <__type_traits/is_same.h>
16*06c3fb27SDimitry Andric #include <__type_traits/is_unsigned.h>
17*06c3fb27SDimitry Andric #include <__utility/declval.h>
18fcaf7f86SDimitry Andric #include <cstdint>
1981ad6265SDimitry Andric 
2081ad6265SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2181ad6265SDimitry Andric #  pragma GCC system_header
2281ad6265SDimitry Andric #endif
2381ad6265SDimitry Andric 
2481ad6265SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
2581ad6265SDimitry Andric 
2681ad6265SDimitry Andric // [rand.req.genl]/1.5:
2781ad6265SDimitry Andric // The effect of instantiating a template that has a template type parameter
2881ad6265SDimitry Andric // named IntType is undefined unless the corresponding template argument is
2981ad6265SDimitry Andric // cv-unqualified and is one of short, int, long, long long, unsigned short,
3081ad6265SDimitry Andric // unsigned int, unsigned long, or unsigned long long.
3181ad6265SDimitry Andric 
3281ad6265SDimitry Andric template<class> struct __libcpp_random_is_valid_inttype : false_type {};
33fcaf7f86SDimitry Andric template<> struct __libcpp_random_is_valid_inttype<int8_t> : true_type {}; // extension
3481ad6265SDimitry Andric template<> struct __libcpp_random_is_valid_inttype<short> : true_type {};
3581ad6265SDimitry Andric template<> struct __libcpp_random_is_valid_inttype<int> : true_type {};
3681ad6265SDimitry Andric template<> struct __libcpp_random_is_valid_inttype<long> : true_type {};
3781ad6265SDimitry Andric template<> struct __libcpp_random_is_valid_inttype<long long> : true_type {};
38fcaf7f86SDimitry Andric template<> struct __libcpp_random_is_valid_inttype<uint8_t> : true_type {}; // extension
3981ad6265SDimitry Andric template<> struct __libcpp_random_is_valid_inttype<unsigned short> : true_type {};
4081ad6265SDimitry Andric template<> struct __libcpp_random_is_valid_inttype<unsigned int> : true_type {};
4181ad6265SDimitry Andric template<> struct __libcpp_random_is_valid_inttype<unsigned long> : true_type {};
4281ad6265SDimitry Andric template<> struct __libcpp_random_is_valid_inttype<unsigned long long> : true_type {};
4381ad6265SDimitry Andric 
4481ad6265SDimitry Andric #ifndef _LIBCPP_HAS_NO_INT128
45fcaf7f86SDimitry Andric template<> struct __libcpp_random_is_valid_inttype<__int128_t> : true_type {}; // extension
46fcaf7f86SDimitry Andric template<> struct __libcpp_random_is_valid_inttype<__uint128_t> : true_type {}; // extension
4781ad6265SDimitry Andric #endif // _LIBCPP_HAS_NO_INT128
4881ad6265SDimitry Andric 
4981ad6265SDimitry Andric // [rand.req.urng]/3:
5081ad6265SDimitry Andric // A class G meets the uniform random bit generator requirements if G models
5181ad6265SDimitry Andric // uniform_random_bit_generator, invoke_result_t<G&> is an unsigned integer type,
5281ad6265SDimitry Andric // and G provides a nested typedef-name result_type that denotes the same type
5381ad6265SDimitry Andric // as invoke_result_t<G&>.
5481ad6265SDimitry Andric // (In particular, reject URNGs with signed result_types; our distributions cannot
5581ad6265SDimitry Andric // handle such generator types.)
5681ad6265SDimitry Andric 
5781ad6265SDimitry Andric template<class, class = void> struct __libcpp_random_is_valid_urng : false_type {};
5881ad6265SDimitry Andric template<class _Gp> struct __libcpp_random_is_valid_urng<_Gp, __enable_if_t<
5981ad6265SDimitry Andric     is_unsigned<typename _Gp::result_type>::value &&
60bdd1243dSDimitry Andric     _IsSame<decltype(std::declval<_Gp&>()()), typename _Gp::result_type>::value
6181ad6265SDimitry Andric > > : true_type {};
6281ad6265SDimitry Andric 
6381ad6265SDimitry Andric _LIBCPP_END_NAMESPACE_STD
6481ad6265SDimitry Andric 
6581ad6265SDimitry Andric #endif // _LIBCPP___RANDOM_IS_VALID_H
66