xref: /llvm-project/libcxx/include/__random/random_device.h (revision c6f3b7bcd0596d30f8dabecdfb9e44f9a07b6e4c)
1344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
2344cef66SArthur O'Dwyer //
3344cef66SArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4344cef66SArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
5344cef66SArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6344cef66SArthur O'Dwyer //
7344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
8344cef66SArthur O'Dwyer 
9344cef66SArthur O'Dwyer #ifndef _LIBCPP___RANDOM_RANDOM_DEVICE_H
10344cef66SArthur O'Dwyer #define _LIBCPP___RANDOM_RANDOM_DEVICE_H
11344cef66SArthur O'Dwyer 
12344cef66SArthur O'Dwyer #include <__config>
13344cef66SArthur O'Dwyer #include <string>
14344cef66SArthur O'Dwyer 
15344cef66SArthur O'Dwyer #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16344cef66SArthur O'Dwyer #  pragma GCC system_header
17344cef66SArthur O'Dwyer #endif
18344cef66SArthur O'Dwyer 
19344cef66SArthur O'Dwyer _LIBCPP_PUSH_MACROS
20344cef66SArthur O'Dwyer #include <__undef_macros>
21344cef66SArthur O'Dwyer 
22344cef66SArthur O'Dwyer _LIBCPP_BEGIN_NAMESPACE_STD
23344cef66SArthur O'Dwyer 
24*c6f3b7bcSNikolas Klauser #if _LIBCPP_HAS_RANDOM_DEVICE
25344cef66SArthur O'Dwyer 
269783f28cSLouis Dionne class _LIBCPP_EXPORTED_FROM_ABI random_device {
27344cef66SArthur O'Dwyer #  ifdef _LIBCPP_USING_DEV_RANDOM
28344cef66SArthur O'Dwyer   int __f_;
29d202c764SLouis Dionne #  elif !defined(_LIBCPP_ABI_NO_RANDOM_DEVICE_COMPATIBILITY_LAYOUT)
30a7c2a628SNikolas Klauser   _LIBCPP_DIAGNOSTIC_PUSH
31a7c2a628SNikolas Klauser   _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wunused-private-field")
32d202c764SLouis Dionne 
33d202c764SLouis Dionne   // Apple platforms used to use the `_LIBCPP_USING_DEV_RANDOM` code path, and now
34d202c764SLouis Dionne   // use `arc4random()` as of this comment. In order to avoid breaking the ABI, we
35d202c764SLouis Dionne   // retain the same layout as before.
36d202c764SLouis Dionne #    if defined(__APPLE__)
37d202c764SLouis Dionne   int __padding_; // padding to fake the `__f_` field above
38d202c764SLouis Dionne #    endif
39d202c764SLouis Dionne 
40d202c764SLouis Dionne   // ... vendors can add workarounds here if they switch to a different representation ...
41d202c764SLouis Dionne 
42a7c2a628SNikolas Klauser   _LIBCPP_DIAGNOSTIC_POP
43d202c764SLouis Dionne #  endif
44d202c764SLouis Dionne 
45344cef66SArthur O'Dwyer public:
46344cef66SArthur O'Dwyer   // types
47344cef66SArthur O'Dwyer   typedef unsigned result_type;
48344cef66SArthur O'Dwyer 
49344cef66SArthur O'Dwyer   // generator characteristics
50344cef66SArthur O'Dwyer   static _LIBCPP_CONSTEXPR const result_type _Min = 0;
51344cef66SArthur O'Dwyer   static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu;
52344cef66SArthur O'Dwyer 
539783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
549783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
55344cef66SArthur O'Dwyer 
56344cef66SArthur O'Dwyer   // constructors
57344cef66SArthur O'Dwyer #  ifndef _LIBCPP_CXX03_LANG
5883ce1397SNikolas Klauser   _LIBCPP_HIDE_FROM_ABI random_device() : random_device("/dev/urandom") {}
59344cef66SArthur O'Dwyer   explicit random_device(const string& __token);
60344cef66SArthur O'Dwyer #  else
61344cef66SArthur O'Dwyer   explicit random_device(const string& __token = "/dev/urandom");
62344cef66SArthur O'Dwyer #  endif
63344cef66SArthur O'Dwyer   ~random_device();
64344cef66SArthur O'Dwyer 
65344cef66SArthur O'Dwyer   // generating functions
66344cef66SArthur O'Dwyer   result_type operator()();
67344cef66SArthur O'Dwyer 
68344cef66SArthur O'Dwyer   // property functions
69344cef66SArthur O'Dwyer   double entropy() const _NOEXCEPT;
70344cef66SArthur O'Dwyer 
71feb80aa9SNikolas Klauser   random_device(const random_device&)  = delete;
72feb80aa9SNikolas Klauser   void operator=(const random_device&) = delete;
73344cef66SArthur O'Dwyer };
74344cef66SArthur O'Dwyer 
75*c6f3b7bcSNikolas Klauser #endif // _LIBCPP_HAS_RANDOM_DEVICE
76344cef66SArthur O'Dwyer 
77344cef66SArthur O'Dwyer _LIBCPP_END_NAMESPACE_STD
78344cef66SArthur O'Dwyer 
79344cef66SArthur O'Dwyer _LIBCPP_POP_MACROS
80344cef66SArthur O'Dwyer 
81344cef66SArthur O'Dwyer #endif // _LIBCPP___RANDOM_RANDOM_DEVICE_H
82