xref: /llvm-project/libcxx/include/__random/is_valid.h (revision ba87515fea90b5d55836a8e3be63a7e683ce299d)
1a3255f21SArthur O'Dwyer //===----------------------------------------------------------------------===//
2a3255f21SArthur O'Dwyer //
3a3255f21SArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a3255f21SArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
5a3255f21SArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a3255f21SArthur O'Dwyer //
7a3255f21SArthur O'Dwyer //===----------------------------------------------------------------------===//
8a3255f21SArthur O'Dwyer 
9a3255f21SArthur O'Dwyer #ifndef _LIBCPP___RANDOM_IS_VALID_H
10a3255f21SArthur O'Dwyer #define _LIBCPP___RANDOM_IS_VALID_H
11a3255f21SArthur O'Dwyer 
12a3255f21SArthur O'Dwyer #include <__config>
13e698c595SNikolas Klauser #include <__type_traits/enable_if.h>
14e698c595SNikolas Klauser #include <__type_traits/integral_constant.h>
15e698c595SNikolas Klauser #include <__type_traits/is_same.h>
16e698c595SNikolas Klauser #include <__type_traits/is_unsigned.h>
17e698c595SNikolas Klauser #include <__utility/declval.h>
1807e984bcSLouis Dionne #include <cstdint>
19a3255f21SArthur O'Dwyer 
20a3255f21SArthur O'Dwyer #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21a3255f21SArthur O'Dwyer #  pragma GCC system_header
22a3255f21SArthur O'Dwyer #endif
23a3255f21SArthur O'Dwyer 
24a3255f21SArthur O'Dwyer _LIBCPP_BEGIN_NAMESPACE_STD
25a3255f21SArthur O'Dwyer 
26727fef79SNhat Nguyen // [rand.req.genl]/1.4:
27727fef79SNhat Nguyen // The effect of instantiating a template that has a template type parameter
28727fef79SNhat Nguyen // named RealType is undefined unless the corresponding template argument is
29727fef79SNhat Nguyen // cv-unqualified and is one of float, double, or long double.
30727fef79SNhat Nguyen 
31727fef79SNhat Nguyen template <class>
32727fef79SNhat Nguyen struct __libcpp_random_is_valid_realtype : false_type {};
33727fef79SNhat Nguyen template <>
34727fef79SNhat Nguyen struct __libcpp_random_is_valid_realtype<float> : true_type {};
35727fef79SNhat Nguyen template <>
36727fef79SNhat Nguyen struct __libcpp_random_is_valid_realtype<double> : true_type {};
37727fef79SNhat Nguyen template <>
38727fef79SNhat Nguyen struct __libcpp_random_is_valid_realtype<long double> : true_type {};
39727fef79SNhat Nguyen 
40a3255f21SArthur O'Dwyer // [rand.req.genl]/1.5:
41a3255f21SArthur O'Dwyer // The effect of instantiating a template that has a template type parameter
42a3255f21SArthur O'Dwyer // named IntType is undefined unless the corresponding template argument is
43a3255f21SArthur O'Dwyer // cv-unqualified and is one of short, int, long, long long, unsigned short,
44a3255f21SArthur O'Dwyer // unsigned int, unsigned long, or unsigned long long.
45a3255f21SArthur O'Dwyer 
469783f28cSLouis Dionne template <class>
479783f28cSLouis Dionne struct __libcpp_random_is_valid_inttype : false_type {};
489783f28cSLouis Dionne template <>
499783f28cSLouis Dionne struct __libcpp_random_is_valid_inttype<int8_t> : true_type {}; // extension
509783f28cSLouis Dionne template <>
519783f28cSLouis Dionne struct __libcpp_random_is_valid_inttype<short> : true_type {};
529783f28cSLouis Dionne template <>
539783f28cSLouis Dionne struct __libcpp_random_is_valid_inttype<int> : true_type {};
549783f28cSLouis Dionne template <>
559783f28cSLouis Dionne struct __libcpp_random_is_valid_inttype<long> : true_type {};
569783f28cSLouis Dionne template <>
579783f28cSLouis Dionne struct __libcpp_random_is_valid_inttype<long long> : true_type {};
589783f28cSLouis Dionne template <>
599783f28cSLouis Dionne struct __libcpp_random_is_valid_inttype<uint8_t> : true_type {}; // extension
609783f28cSLouis Dionne template <>
619783f28cSLouis Dionne struct __libcpp_random_is_valid_inttype<unsigned short> : true_type {};
629783f28cSLouis Dionne template <>
639783f28cSLouis Dionne struct __libcpp_random_is_valid_inttype<unsigned int> : true_type {};
649783f28cSLouis Dionne template <>
659783f28cSLouis Dionne struct __libcpp_random_is_valid_inttype<unsigned long> : true_type {};
669783f28cSLouis Dionne template <>
679783f28cSLouis Dionne struct __libcpp_random_is_valid_inttype<unsigned long long> : true_type {};
68a3255f21SArthur O'Dwyer 
69*ba87515fSNikolas Klauser #if _LIBCPP_HAS_INT128
709783f28cSLouis Dionne template <>
719783f28cSLouis Dionne struct __libcpp_random_is_valid_inttype<__int128_t> : true_type {}; // extension
729783f28cSLouis Dionne template <>
739783f28cSLouis Dionne struct __libcpp_random_is_valid_inttype<__uint128_t> : true_type {}; // extension
74*ba87515fSNikolas Klauser #endif                                                               // _LIBCPP_HAS_INT128
75a3255f21SArthur O'Dwyer 
767624552eSArthur O'Dwyer // [rand.req.urng]/3:
777624552eSArthur O'Dwyer // A class G meets the uniform random bit generator requirements if G models
787624552eSArthur O'Dwyer // uniform_random_bit_generator, invoke_result_t<G&> is an unsigned integer type,
797624552eSArthur O'Dwyer // and G provides a nested typedef-name result_type that denotes the same type
807624552eSArthur O'Dwyer // as invoke_result_t<G&>.
817624552eSArthur O'Dwyer // (In particular, reject URNGs with signed result_types; our distributions cannot
827624552eSArthur O'Dwyer // handle such generator types.)
837624552eSArthur O'Dwyer 
849783f28cSLouis Dionne template <class, class = void>
859783f28cSLouis Dionne struct __libcpp_random_is_valid_urng : false_type {};
869783f28cSLouis Dionne template <class _Gp>
879783f28cSLouis Dionne struct __libcpp_random_is_valid_urng<
889783f28cSLouis Dionne     _Gp,
899783f28cSLouis Dionne     __enable_if_t< is_unsigned<typename _Gp::result_type>::value &&
909783f28cSLouis Dionne                    _IsSame<decltype(std::declval<_Gp&>()()), typename _Gp::result_type>::value > > : true_type {};
917624552eSArthur O'Dwyer 
92a3255f21SArthur O'Dwyer _LIBCPP_END_NAMESPACE_STD
93a3255f21SArthur O'Dwyer 
94a3255f21SArthur O'Dwyer #endif // _LIBCPP___RANDOM_IS_VALID_H
95