random_device.h 2.4 KB

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