random_device.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #ifndef _LIBCPP___RANDOM_RANDOM_DEVICE_H
  9. #define _LIBCPP___RANDOM_RANDOM_DEVICE_H
  10. #include <__config>
  11. #include <string>
  12. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  13. # pragma GCC system_header
  14. #endif
  15. _LIBCPP_PUSH_MACROS
  16. #include <__undef_macros>
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. #if !defined(_LIBCPP_HAS_NO_RANDOM_DEVICE)
  19. class _LIBCPP_EXPORTED_FROM_ABI random_device {
  20. # ifdef _LIBCPP_USING_DEV_RANDOM
  21. int __f_;
  22. # elif !defined(_LIBCPP_ABI_NO_RANDOM_DEVICE_COMPATIBILITY_LAYOUT)
  23. _LIBCPP_DIAGNOSTIC_PUSH
  24. _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wunused-private-field")
  25. // Apple platforms used to use the `_LIBCPP_USING_DEV_RANDOM` code path, and now
  26. // use `arc4random()` as of this comment. In order to avoid breaking the ABI, we
  27. // retain the same layout as before.
  28. # if defined(__APPLE__)
  29. int __padding_; // padding to fake the `__f_` field above
  30. # endif
  31. // ... vendors can add workarounds here if they switch to a different representation ...
  32. _LIBCPP_DIAGNOSTIC_POP
  33. # endif
  34. public:
  35. // types
  36. typedef unsigned result_type;
  37. // generator characteristics
  38. static _LIBCPP_CONSTEXPR const result_type _Min = 0;
  39. static _LIBCPP_CONSTEXPR const result_type _Max = 0xFFFFFFFFu;
  40. _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
  41. _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
  42. // constructors
  43. # ifndef _LIBCPP_CXX03_LANG
  44. _LIBCPP_HIDE_FROM_ABI random_device() : random_device("/dev/urandom") {}
  45. explicit random_device(const string& __token);
  46. # else
  47. explicit random_device(const string& __token = "/dev/urandom");
  48. # endif
  49. ~random_device();
  50. // generating functions
  51. result_type operator()();
  52. // property functions
  53. double entropy() const _NOEXCEPT;
  54. random_device(const random_device&) = delete;
  55. void operator=(const random_device&) = delete;
  56. };
  57. #endif // !_LIBCPP_HAS_NO_RANDOM_DEVICE
  58. _LIBCPP_END_NAMESPACE_STD
  59. _LIBCPP_POP_MACROS
  60. #endif // _LIBCPP___RANDOM_RANDOM_DEVICE_H