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