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 <boost/config.hpp> // for static assert
40 #ifndef BOOST_UUID_NO_TYPE_TRAITS
41 #include <boost/type_traits/is_pod.hpp>
42 #include <boost/type_traits/integral_constant.hpp>
43 #endif
44
45 #if defined(_MSC_VER)
46 #pragma warning(push) // Save warning settings.
47 #pragma warning(disable : 4996) // Disable deprecated std::swap_ranges, std::equal
48 #endif
49
50 #ifdef BOOST_NO_STDC_NAMESPACE
51 namespace std {
52 using ::size_t;
53 using ::ptrdiff_t;
54 } //namespace std
55 #endif //BOOST_NO_STDC_NAMESPACE
56
57 namespace boost {
58 namespace uuids {
59
60 struct uuid
61 {
62 public:
63 typedef uint8_t value_type;
64 typedef uint8_t& reference;
65 typedef uint8_t const& const_reference;
66 typedef uint8_t* iterator;
67 typedef uint8_t const* const_iterator;
68 typedef std::size_t size_type;
69 typedef std::ptrdiff_t difference_type;
70
71 // This does not work on some compilers
72 // They seem to want the variable definec in
73 // a cpp file
74 //BOOST_STATIC_CONSTANT(size_type, static_size = 16);
static_sizeboost::uuids::uuid75 static size_type static_size() { return 16; }
76
77 public:
beginboost::uuids::uuid78 iterator begin() { return data; } /* throw() */
beginboost::uuids::uuid79 const_iterator begin() const { return data; } /* throw() */
endboost::uuids::uuid80 iterator end() { return data+size(); } /* throw() */
endboost::uuids::uuid81 const_iterator end() const { return data+size(); } /* throw() */
82
sizeboost::uuids::uuid83 size_type size() const { return static_size(); } /* throw() */
84
is_nilboost::uuids::uuid85 bool is_nil() const /* throw() */
86 {
87 for(size_t i=0; i<static_size(); i++) {
88 if (data[i] != 0U) {
89 return false;
90 }
91 }
92 return true;
93 }
94
95 enum variant_type
96 {
97 variant_ncs, // NCS backward compatibility
98 variant_rfc_4122, // defined in RFC 4122 document
99 variant_microsoft, // Microsoft Corporation backward compatibility
100 variant_future // future definition
101 };
variantboost::uuids::uuid102 variant_type variant() const /* throw() */
103 {
104 // variant is stored in octet 7
105 // which is index 8, since indexes count backwards
106 unsigned char octet7 = data[8]; // octet 7 is array index 8
107 if ( (octet7 & 0x80) == 0x00 ) { // 0b0xxxxxxx
108 return variant_ncs;
109 } else if ( (octet7 & 0xC0) == 0x80 ) { // 0b10xxxxxx
110 return variant_rfc_4122;
111 } else if ( (octet7 & 0xE0) == 0xC0 ) { // 0b110xxxxx
112 return variant_microsoft;
113 } else {
114 //assert( (octet7 & 0xE0) == 0xE0 ) // 0b111xxxx
115 return variant_future;
116 }
117 }
118
119 enum version_type
120 {
121 version_unknown = -1,
122 version_time_based = 1,
123 version_dce_security = 2,
124 version_name_based_md5 = 3,
125 version_random_number_based = 4,
126 version_name_based_sha1 = 5
127 };
versionboost::uuids::uuid128 version_type version() const /* throw() */
129 {
130 //version is stored in octet 9
131 // which is index 6, since indexes count backwards
132 unsigned char octet9 = data[6];
133 if ( (octet9 & 0xF0) == 0x10 ) {
134 return version_time_based;
135 } else if ( (octet9 & 0xF0) == 0x20 ) {
136 return version_dce_security;
137 } else if ( (octet9 & 0xF0) == 0x30 ) {
138 return version_name_based_md5;
139 } else if ( (octet9 & 0xF0) == 0x40 ) {
140 return version_random_number_based;
141 } else if ( (octet9 & 0xF0) == 0x50 ) {
142 return version_name_based_sha1;
143 } else {
144 return version_unknown;
145 }
146 }
147
148 // note: linear complexity
swapboost::uuids::uuid149 void swap(uuid& rhs) /* throw() */
150 {
151 std::swap_ranges(begin(), end(), rhs.begin());
152 }
153
154 public:
155 // or should it be array<uint8_t, 16>
156 uint8_t data[16];
157 };
158
operator ==(uuid const & lhs,uuid const & rhs)159 inline bool operator==(uuid const& lhs, uuid const& rhs) /* throw() */
160 {
161 return std::equal(lhs.begin(), lhs.end(), rhs.begin());
162 }
163
operator !=(uuid const & lhs,uuid const & rhs)164 inline bool operator!=(uuid const& lhs, uuid const& rhs) /* throw() */
165 {
166 return !(lhs == rhs);
167 }
168
operator <(uuid const & lhs,uuid const & rhs)169 inline bool operator<(uuid const& lhs, uuid const& rhs) /* throw() */
170 {
171 return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
172 }
173
operator >(uuid const & lhs,uuid const & rhs)174 inline bool operator>(uuid const& lhs, uuid const& rhs) /* throw() */
175 {
176 return rhs < lhs;
177 }
operator <=(uuid const & lhs,uuid const & rhs)178 inline bool operator<=(uuid const& lhs, uuid const& rhs) /* throw() */
179 {
180 return !(rhs < lhs);
181 }
182
operator >=(uuid const & lhs,uuid const & rhs)183 inline bool operator>=(uuid const& lhs, uuid const& rhs) /* throw() */
184 {
185 return !(lhs < rhs);
186 }
187
swap(uuid & lhs,uuid & rhs)188 inline void swap(uuid& lhs, uuid& rhs) /* throw() */
189 {
190 lhs.swap(rhs);
191 }
192
193 // This is equivalent to boost::hash_range(u.begin(), u.end());
hash_value(uuid const & u)194 inline std::size_t hash_value(uuid const& u) /* throw() */
195 {
196 std::size_t seed = 0;
197 for(uuid::const_iterator i=u.begin(); i != u.end(); ++i)
198 {
199 seed ^= static_cast<std::size_t>(*i) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
200 }
201
202 return seed;
203 }
204
205 }} //namespace boost::uuids
206
207 #ifndef BOOST_UUID_NO_TYPE_TRAITS
208 // type traits specializations
209 namespace boost {
210
211 template <>
212 struct is_pod<uuids::uuid> : true_type {};
213
214 } // namespace boost
215 #endif
216
217 #if defined(_MSC_VER)
218 #pragma warning(pop) // Restore warnings to previous state.
219 #endif
220
221 #endif // BOOST_UUID_HPP
222