1 | // Boost uuid.hpp header file ----------------------------------------------//
|
---|
2 |
|
---|
3 | // Copyright 2006 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 | // Revision History
|
---|
9 | // 06 Feb 2006 - Initial Revision
|
---|
10 | // 09 Nov 2006 - fixed variant and version bits for v4 guids
|
---|
11 | // 13 Nov 2006 - added serialization
|
---|
12 | // 17 Nov 2006 - added name-based guid creation
|
---|
13 | // 20 Nov 2006 - add fixes for gcc (from Tim Blechmann)
|
---|
14 | // 07 Mar 2007 - converted to header only
|
---|
15 | // 10 May 2007 - removed need for Boost.Thread
|
---|
16 | // - added better seed - thanks Peter Dimov
|
---|
17 | // - removed null()
|
---|
18 | // - replaced byte_count() and output_bytes() with size() and begin() and end()
|
---|
19 | // 11 May 2007 - fixed guid(ByteInputIterator first, ByteInputIterator last)
|
---|
20 | // - optimized operator>>
|
---|
21 | // 14 May 2007 - converted from guid to uuid
|
---|
22 | // 29 May 2007 - uses new implementation of sha1
|
---|
23 | // 01 Jun 2007 - removed using namespace directives
|
---|
24 | // 09 Nov 2007 - moved implementation to uuid.ipp file
|
---|
25 | // 12 Nov 2007 - moved serialize code to uuid_serialize.hpp file
|
---|
26 | // 25 Feb 2008 - moved to namespace boost::uuids
|
---|
27 | // 19 Mar 2009 - changed to a POD, reorganized files
|
---|
28 | // 28 Nov 2009 - disabled deprecated warnings for MSVC
|
---|
29 | // 30 Nov 2009 - used BOOST_STATIC_CONSTANT
|
---|
30 | // 02 Dec 2009 - removed BOOST_STATIC_CONSTANT - not all compilers like it
|
---|
31 |
|
---|
32 | #ifndef BOOST_UUID_HPP
|
---|
33 | #define BOOST_UUID_HPP
|
---|
34 |
|
---|
35 | #include <boost/config.hpp>
|
---|
36 | #include <stddef.h>
|
---|
37 | #include <boost/cstdint.hpp>
|
---|
38 | #include <algorithm>
|
---|
39 | #include <array>
|
---|
40 | #include <functional>
|
---|
41 | #include <boost/config.hpp> // for static assert
|
---|
42 | #ifndef BOOST_UUID_NO_TYPE_TRAITS
|
---|
43 | #include <boost/type_traits/is_pod.hpp>
|
---|
44 | #include <boost/type_traits/integral_constant.hpp>
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | /* Changed to use std::array to remove this actually being an error in vc2012 with secure checks on!
|
---|
48 | #if defined(_MSC_VER)
|
---|
49 | #pragma warning(push) // Save warning settings.
|
---|
50 | #pragma warning(disable : 4996) // Disable deprecated std::swap_ranges, std::equal
|
---|
51 | #endif
|
---|
52 | */
|
---|
53 |
|
---|
54 | #ifdef BOOST_NO_STDC_NAMESPACE
|
---|
55 | namespace std {
|
---|
56 | using ::size_t;
|
---|
57 | using ::ptrdiff_t;
|
---|
58 | } //namespace std
|
---|
59 | #endif //BOOST_NO_STDC_NAMESPACE
|
---|
60 |
|
---|
61 | namespace boost {
|
---|
62 | namespace uuids {
|
---|
63 |
|
---|
64 | struct uuid
|
---|
65 | {
|
---|
66 | public:
|
---|
67 | enum { static_size = 16 };
|
---|
68 |
|
---|
69 | typedef uint8_t value_type;
|
---|
70 | typedef std::array<value_type, static_size> data_type;
|
---|
71 |
|
---|
72 | typedef data_type::size_type size_type;
|
---|
73 | typedef data_type::difference_type difference_type;
|
---|
74 | typedef data_type::iterator iterator;
|
---|
75 | typedef data_type::const_iterator const_iterator;
|
---|
76 | typedef data_type::reference reference;
|
---|
77 | typedef data_type::const_reference const_reference;
|
---|
78 |
|
---|
79 | public:
|
---|
80 | iterator begin() { return data_.begin(); } /* throw() */
|
---|
81 | const_iterator begin() const { return data_.begin(); } /* throw() */
|
---|
82 | iterator end() { return data_.end(); } /* throw() */
|
---|
83 | const_iterator end() const { return data_.end(); } /* throw() */
|
---|
84 |
|
---|
85 | // size is constant
|
---|
86 | static size_type size() { return static_size; }
|
---|
87 | static size_type max_size() { return static_size; }
|
---|
88 | static bool empty() { return false; }
|
---|
89 |
|
---|
90 | bool is_nil() const /* throw() */
|
---|
91 | {
|
---|
92 | struct IsNot0 {
|
---|
93 | bool operator() (const value_type& x) const {return x != 0;}
|
---|
94 | typedef value_type argument_type;
|
---|
95 | };
|
---|
96 | return std::find_if( begin(), end(), IsNot0() ) != end();
|
---|
97 | }
|
---|
98 |
|
---|
99 | enum variant_type
|
---|
100 | {
|
---|
101 | variant_ncs, // NCS backward compatibility
|
---|
102 | variant_rfc_4122, // defined in RFC 4122 document
|
---|
103 | variant_microsoft, // Microsoft Corporation backward compatibility
|
---|
104 | variant_future // future definition
|
---|
105 | };
|
---|
106 | variant_type variant() const /* throw() */
|
---|
107 | {
|
---|
108 | // variant is stored in octet 7
|
---|
109 | // which is index 8, since indexes count backwards
|
---|
110 | value_type octet7 = data_[8]; // octet 7 is array index 8
|
---|
111 | if ( (octet7 & 0x80) == 0x00 ) { // 0b0xxxxxxx
|
---|
112 | return variant_ncs;
|
---|
113 | } else if ( (octet7 & 0xC0) == 0x80 ) { // 0b10xxxxxx
|
---|
114 | return variant_rfc_4122;
|
---|
115 | } else if ( (octet7 & 0xE0) == 0xC0 ) { // 0b110xxxxx
|
---|
116 | return variant_microsoft;
|
---|
117 | } else {
|
---|
118 | //assert( (octet7 & 0xE0) == 0xE0 ) // 0b111xxxx
|
---|
119 | return variant_future;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | enum version_type
|
---|
124 | {
|
---|
125 | version_unknown = -1,
|
---|
126 | version_time_based = 1,
|
---|
127 | version_dce_security = 2,
|
---|
128 | version_name_based_md5 = 3,
|
---|
129 | version_random_number_based = 4,
|
---|
130 | version_name_based_sha1 = 5
|
---|
131 | };
|
---|
132 | version_type version() const /* throw() */
|
---|
133 | {
|
---|
134 | //version is stored in octet 9
|
---|
135 | // which is index 6, since indexes count backwards
|
---|
136 | value_type octet9 = data_[6];
|
---|
137 | if ( (octet9 & 0xF0) == 0x10 ) {
|
---|
138 | return version_time_based;
|
---|
139 | } else if ( (octet9 & 0xF0) == 0x20 ) {
|
---|
140 | return version_dce_security;
|
---|
141 | } else if ( (octet9 & 0xF0) == 0x30 ) {
|
---|
142 | return version_name_based_md5;
|
---|
143 | } else if ( (octet9 & 0xF0) == 0x40 ) {
|
---|
144 | return version_random_number_based;
|
---|
145 | } else if ( (octet9 & 0xF0) == 0x50 ) {
|
---|
146 | return version_name_based_sha1;
|
---|
147 | } else {
|
---|
148 | return version_unknown;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | // note: linear complexity
|
---|
153 | void swap(uuid& rhs) /* throw() */
|
---|
154 | {
|
---|
155 | std::swap( this->data_, rhs.data_ );
|
---|
156 | }
|
---|
157 |
|
---|
158 | const data_type& cdata() const
|
---|
159 | { return data_; }
|
---|
160 |
|
---|
161 | public:
|
---|
162 | // or should it be array<uint8_t, 16>
|
---|
163 | data_type data_;
|
---|
164 | };
|
---|
165 |
|
---|
166 | inline bool operator==(uuid const& lhs, uuid const& rhs) /* throw() */
|
---|
167 | {
|
---|
168 | return lhs.cdata() == rhs.cdata();
|
---|
169 | //return std::equal(lhs.begin(), lhs.end(), rhs.begin());
|
---|
170 | }
|
---|
171 |
|
---|
172 | inline bool operator!=(uuid const& lhs, uuid const& rhs) /* throw() */
|
---|
173 | {
|
---|
174 | return !(lhs == rhs);
|
---|
175 | }
|
---|
176 |
|
---|
177 | inline bool operator<(uuid const& lhs, uuid const& rhs) /* throw() */
|
---|
178 | {
|
---|
179 | return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
|
---|
180 | }
|
---|
181 |
|
---|
182 | inline bool operator>(uuid const& lhs, uuid const& rhs) /* throw() */
|
---|
183 | {
|
---|
184 | return rhs < lhs;
|
---|
185 | }
|
---|
186 | inline bool operator<=(uuid const& lhs, uuid const& rhs) /* throw() */
|
---|
187 | {
|
---|
188 | return !(rhs < lhs);
|
---|
189 | }
|
---|
190 |
|
---|
191 | inline bool operator>=(uuid const& lhs, uuid const& rhs) /* throw() */
|
---|
192 | {
|
---|
193 | return !(lhs < rhs);
|
---|
194 | }
|
---|
195 |
|
---|
196 | inline void swap(uuid& lhs, uuid& rhs) /* throw() */
|
---|
197 | {
|
---|
198 | lhs.swap(rhs);
|
---|
199 | }
|
---|
200 |
|
---|
201 | // This is equivalent to boost::hash_range(u.begin(), u.end());
|
---|
202 | inline std::size_t hash_value(uuid const& u) /* throw() */
|
---|
203 | {
|
---|
204 | std::size_t seed = 0;
|
---|
205 | for(uuid::const_iterator i=u.begin(); i != u.end(); ++i)
|
---|
206 | {
|
---|
207 | seed ^= static_cast<std::size_t>(*i) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
|
---|
208 | }
|
---|
209 |
|
---|
210 | return seed;
|
---|
211 | }
|
---|
212 |
|
---|
213 | }} //namespace boost::uuids
|
---|
214 |
|
---|
215 | #ifndef BOOST_UUID_NO_TYPE_TRAITS
|
---|
216 | // type traits specializations
|
---|
217 | namespace boost {
|
---|
218 |
|
---|
219 | template <>
|
---|
220 | struct is_pod<uuids::uuid> : true_type {};
|
---|
221 |
|
---|
222 | } // namespace boost
|
---|
223 | #endif
|
---|
224 |
|
---|
225 | #endif // BOOST_UUID_HPP
|
---|