]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/uuid/random_generator.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / uuid / random_generator.hpp
1 // Boost random_generator.hpp header file ----------------------------------------------//
2
3 // Copyright 2010 Andy Tompkins.
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7
8 #ifndef BOOST_UUID_RANDOM_GENERATOR_HPP
9 #define BOOST_UUID_RANDOM_GENERATOR_HPP
10
11 #include <boost/uuid/uuid.hpp>
12 #include <boost/uuid/detail/seed_rng.hpp>
13 #include <boost/random/uniform_int.hpp>
14 #include <boost/random/variate_generator.hpp>
15 #include <boost/random/mersenne_twister.hpp>
16 #include <boost/core/null_deleter.hpp>
17 #include <boost/assert.hpp>
18 #include <boost/shared_ptr.hpp>
19 #include <limits>
20
21 namespace boost {
22 namespace uuids {
23
24 // generate a random-based uuid
25 template <typename UniformRandomNumberGenerator>
26 class basic_random_generator {
27 private:
28 typedef uniform_int<unsigned long> distribution_type;
29 typedef variate_generator<UniformRandomNumberGenerator*, distribution_type> generator_type;
30
31 public:
32 typedef uuid result_type;
33
34 // default constructor creates the random number generator
35 basic_random_generator()
36 : pURNG(new UniformRandomNumberGenerator)
37 , generator
38 ( pURNG.get()
39 , distribution_type
40 ( (std::numeric_limits<unsigned long>::min)()
41 , (std::numeric_limits<unsigned long>::max)()
42 )
43 )
44 {
45 // seed the random number generator
46 detail::seed(*pURNG);
47 }
48
49 // keep a reference to a random number generator
50 // don't seed a given random number generator
51 explicit basic_random_generator(UniformRandomNumberGenerator& gen)
52 : pURNG(&gen, boost::null_deleter())
53 , generator
54 ( pURNG.get()
55 , distribution_type
56 ( (std::numeric_limits<unsigned long>::min)()
57 , (std::numeric_limits<unsigned long>::max)()
58 )
59 )
60 {}
61
62 // keep a pointer to a random number generator
63 // don't seed a given random number generator
64 explicit basic_random_generator(UniformRandomNumberGenerator* pGen)
65 : pURNG(pGen, boost::null_deleter())
66 , generator
67 ( pURNG.get()
68 , distribution_type
69 ( (std::numeric_limits<unsigned long>::min)()
70 , (std::numeric_limits<unsigned long>::max)()
71 )
72 )
73 {
74 BOOST_ASSERT(pURNG);
75 }
76
77 uuid operator()()
78 {
79 uuid u;
80
81 int i=0;
82 unsigned long random_value = generator();
83 for (uuid::iterator it=u.begin(); it!=u.end(); ++it, ++i) {
84 if (i==sizeof(unsigned long)) {
85 random_value = generator();
86 i = 0;
87 }
88
89 // static_cast gets rid of warnings of converting unsigned long to boost::uint8_t
90 *it = static_cast<uuid::value_type>((random_value >> (i*8)) & 0xFF);
91 }
92
93 // set variant
94 // must be 0b10xxxxxx
95 *(u.begin()+8) &= 0xBF;
96 *(u.begin()+8) |= 0x80;
97
98 // set version
99 // must be 0b0100xxxx
100 *(u.begin()+6) &= 0x4F; //0b01001111
101 *(u.begin()+6) |= 0x40; //0b01000000
102
103 return u;
104 }
105
106 private:
107 shared_ptr<UniformRandomNumberGenerator> pURNG;
108 generator_type generator;
109 };
110
111 typedef basic_random_generator<mt19937> random_generator;
112
113 }} // namespace boost::uuids
114
115 #endif //BOOST_UUID_RANDOM_GENERATOR_HPP