Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-13 10:07:21

0001 #ifndef BOOST_UUID_DETAIL_RANDOM_DEVICE_HPP_INCLUDED
0002 #define BOOST_UUID_DETAIL_RANDOM_DEVICE_HPP_INCLUDED
0003 
0004 // Copyright 2024 Peter Dimov
0005 // Distributed under the Boost Software License, Version 1.0.
0006 // https://www.boost.org/LICENSE_1_0.txt
0007 
0008 #if defined(__MINGW32__)
0009 
0010 // Under MinGW up to GCC 9, std::random_device is
0011 // deterministic and always produces the same
0012 // sequence
0013 
0014 #include <boost/throw_exception.hpp>
0015 #include <system_error>
0016 #include <limits>
0017 #include <stdlib.h>
0018 
0019 extern "C" int __cdecl rand_s( unsigned int *randomValue );
0020 
0021 namespace boost {
0022 namespace uuids {
0023 namespace detail {
0024 
0025 struct random_device
0026 {
0027     // noncopyable to match std::random_device
0028     random_device() = default;
0029     random_device( random_device&& ) = delete;
0030     random_device& operator=( random_device&& ) = delete;
0031 
0032     using result_type = unsigned;
0033 
0034     static constexpr result_type min()
0035     {
0036         return std::numeric_limits<result_type>::min();
0037     }
0038 
0039     static constexpr result_type max()
0040     {
0041         return std::numeric_limits<result_type>::max();
0042     }
0043 
0044     result_type operator()()
0045     {
0046         unsigned v;
0047 
0048         auto r = rand_s( &v );
0049 
0050         if( r != 0 )
0051         {
0052             BOOST_THROW_EXCEPTION( std::system_error( r, std::generic_category(), "rand_s" ) );
0053         }
0054 
0055         return v;
0056     }
0057 };
0058 
0059 } // detail
0060 } // uuids
0061 } // boost
0062 
0063 #else
0064 
0065 #include <random>
0066 
0067 namespace boost {
0068 namespace uuids {
0069 namespace detail {
0070 
0071 using std::random_device;
0072 
0073 } // detail
0074 } // uuids
0075 } // boost
0076 
0077 #endif
0078 
0079 #endif // BOOST_UUID_DETAIL_RANDOM_DEVICE_HPP_INCLUDED