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#if _LIBCPP_HAS_RANDOM_DEVICE
23
24_LIBCPP_BEGIN_NAMESPACE_STD
25_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
26
27class _LIBCPP_EXPORTED_FROM_ABI random_device {
28# ifdef _LIBCPP_USING_DEV_RANDOM
29 int __f_;
30# elif !defined(_LIBCPP_ABI_NO_RANDOM_DEVICE_COMPATIBILITY_LAYOUT)
31 // Apple platforms used to use the `_LIBCPP_USING_DEV_RANDOM` code path, and now
32 // use `arc4random()` as of this comment. In order to avoid breaking the ABI, we
33 // retain the same layout as before.
34# if defined(__APPLE__)
35 [[__maybe_unused__]] int __padding_; // padding to fake the `__f_` field above
36# endif
37
38 // ... vendors can add workarounds here if they switch to a different representation ...
39
40# endif
41
42public:
43 // types
44 typedef unsigned result_type;
45
46 // generator characteristics
47 static _LIBCPP_CONSTEXPR const result_type _Min = 0;
48 static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu;
49
50 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
51 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
52
53 // constructors
54# ifndef _LIBCPP_CXX03_LANG
55 _LIBCPP_HIDE_FROM_ABI random_device() : random_device("/dev/urandom") {}
56 explicit random_device(const string& __token);
57# else
58 explicit random_device(const string& __token = "/dev/urandom");
59# endif
60 ~random_device();
61
62 // generating functions
63 [[__nodiscard__]] result_type operator()();
64
65 // property functions
66 [[__nodiscard__]] double entropy() const _NOEXCEPT;
67
68 random_device(const random_device&) = delete;
69 void operator=(const random_device&) = delete;
70};
71
72_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
73_LIBCPP_END_NAMESPACE_STD
74
75#endif // _LIBCPP_HAS_RANDOM_DEVICE
76
77_LIBCPP_POP_MACROS
78
79#endif // _LIBCPP___RANDOM_RANDOM_DEVICE_H
80