xref: /freebsd/contrib/llvm-project/libcxx/include/__random/random_device.h (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
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_RANDOM_DEVICE_H
10 #define _LIBCPP___RANDOM_RANDOM_DEVICE_H
11 
12 #include <__config>
13 #include <string>
14 
15 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16 #pragma GCC system_header
17 #endif
18 
19 _LIBCPP_PUSH_MACROS
20 #include <__undef_macros>
21 
22 _LIBCPP_BEGIN_NAMESPACE_STD
23 
24 #if !defined(_LIBCPP_HAS_NO_RANDOM_DEVICE)
25 
26 class _LIBCPP_TYPE_VIS random_device
27 {
28 #ifdef _LIBCPP_USING_DEV_RANDOM
29     int __f_;
30 #elif !defined(_LIBCPP_ABI_NO_RANDOM_DEVICE_COMPATIBILITY_LAYOUT)
31 #   if defined(__clang__)
32 #       pragma clang diagnostic push
33 #       pragma clang diagnostic ignored "-Wunused-private-field"
34 #   endif
35 
36     // Apple platforms used to use the `_LIBCPP_USING_DEV_RANDOM` code path, and now
37     // use `arc4random()` as of this comment. In order to avoid breaking the ABI, we
38     // retain the same layout as before.
39 #   if defined(__APPLE__)
40     int __padding_; // padding to fake the `__f_` field above
41 #   endif
42 
43     // ... vendors can add workarounds here if they switch to a different representation ...
44 
45 #   if defined(__clang__)
46 #       pragma clang diagnostic pop
47 #   endif
48 #endif
49 
50 public:
51     // types
52     typedef unsigned result_type;
53 
54     // generator characteristics
55     static _LIBCPP_CONSTEXPR const result_type _Min = 0;
56     static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu;
57 
58     _LIBCPP_INLINE_VISIBILITY
59     static _LIBCPP_CONSTEXPR result_type min() { return _Min;}
60     _LIBCPP_INLINE_VISIBILITY
61     static _LIBCPP_CONSTEXPR result_type max() { return _Max;}
62 
63     // constructors
64 #ifndef _LIBCPP_CXX03_LANG
65     random_device() : random_device("/dev/urandom") {}
66     explicit random_device(const string& __token);
67 #else
68     explicit random_device(const string& __token = "/dev/urandom");
69 #endif
70     ~random_device();
71 
72     // generating functions
73     result_type operator()();
74 
75     // property functions
76     double entropy() const _NOEXCEPT;
77 
78     random_device(const random_device&) = delete;
79     void operator=(const random_device&) = delete;
80 };
81 
82 #endif // !_LIBCPP_HAS_NO_RANDOM_DEVICE
83 
84 _LIBCPP_END_NAMESPACE_STD
85 
86 _LIBCPP_POP_MACROS
87 
88 #endif // _LIBCPP___RANDOM_RANDOM_DEVICE_H
89