Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:53:31

0001 //
0002 // Copyright (c) 2017 James E. King III
0003 //
0004 // Distributed under the Boost Software License, Version 1.0.
0005 // (See accompanying file LICENSE_1_0.txt or copy at
0006 //   https://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 // Platform-specific random entropy provider
0009 //
0010 
0011 #ifndef BOOST_UUID_DETAIL_RANDOM_PROVIDER_HPP
0012 #define BOOST_UUID_DETAIL_RANDOM_PROVIDER_HPP
0013 
0014 #include <boost/config.hpp>
0015 #include <boost/cstdint.hpp>
0016 #include <boost/limits.hpp>
0017 #include <boost/static_assert.hpp>
0018 #include <boost/move/core.hpp>
0019 #include <boost/move/utility_core.hpp>
0020 #include <boost/type_traits/is_integral.hpp>
0021 #include <boost/type_traits/is_unsigned.hpp>
0022 #include <boost/uuid/entropy_error.hpp>
0023 #include <climits>
0024 #include <iterator>
0025 
0026 // Detection of the platform is separated from inclusion of the correct
0027 // header to facilitate mock testing of the provider implementations.
0028 
0029 #include <boost/uuid/detail/random_provider_detect_platform.hpp>
0030 #include <boost/uuid/detail/random_provider_include_platform.hpp>
0031 
0032 
0033 namespace boost {
0034 namespace uuids {
0035 namespace detail {
0036 
0037 //! \brief Contains code common to all random_provider implementations.
0038 //! \note  random_provider_base is required to provide this method:
0039 //!        void get_random_bytes(void *buf, size_t siz);
0040 //! \note  noncopyable because of some base implementations so
0041 //!        this makes it uniform across platforms to avoid any  
0042 //!        porting surprises
0043 class random_provider :
0044     public detail::random_provider_base
0045 {
0046     BOOST_MOVABLE_BUT_NOT_COPYABLE(random_provider)
0047 
0048 public:
0049     BOOST_DEFAULTED_FUNCTION(random_provider(), {})
0050 
0051     random_provider(BOOST_RV_REF(random_provider) that) BOOST_NOEXCEPT :
0052         detail::random_provider_base(boost::move(static_cast< detail::random_provider_base& >(that)))
0053     {
0054     }
0055 
0056     random_provider& operator= (BOOST_RV_REF(random_provider) that) BOOST_NOEXCEPT
0057     {
0058         static_cast< detail::random_provider_base& >(*this) = boost::move(static_cast< detail::random_provider_base& >(that));
0059         return *this;
0060     }
0061 
0062     //! Leverage the provider as a SeedSeq for
0063     //! PseudoRandomNumberGeneration seeing.
0064     //! \note: See Boost.Random documentation for more details
0065     template<class Iter>
0066     void generate(Iter first, Iter last)
0067     {
0068         typedef typename std::iterator_traits<Iter>::value_type value_type;
0069         BOOST_STATIC_ASSERT(is_integral<value_type>::value);
0070         BOOST_STATIC_ASSERT(is_unsigned<value_type>::value);
0071         BOOST_STATIC_ASSERT(sizeof(value_type) * CHAR_BIT >= 32);
0072 
0073         for (; first != last; ++first)
0074         {
0075             get_random_bytes(&*first, sizeof(*first));
0076             *first &= (std::numeric_limits<boost::uint32_t>::max)();
0077         }
0078     }
0079 
0080     //! Return the name of the selected provider
0081     const char * name() const
0082     {
0083         return BOOST_UUID_RANDOM_PROVIDER_STRINGIFY(BOOST_UUID_RANDOM_PROVIDER_NAME);
0084     }
0085 };
0086 
0087 } // detail
0088 } // uuids
0089 } // boost
0090 
0091 #endif // BOOST_UUID_DETAIL_RANDOM_PROVIDER_HPP