1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 4<title>Uuid Library</title> 5<style type="text/css"> 6 pre { margin: 1em; padding: 1em; border: 1px solid #ddd; } 7</style> 8</head> 9<body> 10<h1><img src="../../../boost.png" alt="Boost" WIDTH="277" HEIGHT="86">Uuid</h1> 11 12<h2><a name="Contents">Contents</a></h2> 13 14<ol> 15 <li><a href="#Introduction">Introduction</a></li> 16 <li><a href="#Configuration">Configuration</a></li> 17 <li><a href="#Examples">Examples</a></li> 18 <ul> 19 <li><a href="#Tagging">Tagging</a></li> 20 <li><a href="#POD Efficiencies">POD Efficiencies</a></li> 21 <li><a href="#Byte Extraction">Byte Extraction</a></li> 22 </ul 23 <li><a href="#Reference">Reference</a></li> 24 <ul> 25 <li><a href="#boost/uuid/uuid.hpp">boost/uuid/uuid.hpp</a></li> 26 <ul> 27 <li><a href="#Synopsis">Synopsis</a></li> 28 <li><a href="#Size">Size</a></li> 29 <li><a href="#Iteration">Iteration</a></li> 30 <li><a href="#Nil">Nil uuid</a></li> 31 <li><a href="#Variant">Variant</a></li> 32 <li><a href="#Version">Version</a></li> 33 <li><a href="#Swap">Swap</a></li> 34 <li><a href="#Operators">Operators</a></li> 35 <li><a href="#Hash">Hash</a></li> 36 </ul> 37 <li><a href="#boost/uuid/uuid_generators.hpp">boost/uuid/uuid_generators.hpp</a></li> 38 <ul> 39 <li><a href="#Synopsis_generators">Synopsis</a></li> 40 </ul> 41 <li><a href="#boost/uuid/nil_generator.hpp">boost/uuid/nil_generator.hpp</a></li> 42 <ul> 43 <li><a href="#Synopsis_nil_generator">Synopsis</a></li> 44 <li><a href="#Nil Generator">Nil Generator</a></li> 45 </ul> 46 <li><a href="#boost/uuid/string_generator.hpp">boost/uuid/string_generator.hpp</a></li> 47 <ul> 48 <li><a href="#Synopsis_string_generator">Synopsis</a></li> 49 <li><a href="#String Generator">String Generator</a></li> 50 </ul> 51 <li><a href="#boost/uuid/name_generator.hpp">boost/uuid/name_generator.hpp</a></li> 52 <ul> 53 <li><a href="#Synopsis_name_generator">Synopsis</a></li> 54 <li><a href="#Name Generator">Name Generator</a></li> 55 </ul> 56 <li><a href="#boost/uuid/random_generator.hpp">boost/uuid/random_generator.hpp</a></li> 57 <ul> 58 <li><a href="#Synopsis_random_generator">Synopsis</a></li> 59 <li><a href="#Random Generator">Random Generator</a></li> 60 </ul> 61 <li><a href="#boost/uuid/uuid_io.hpp">boost/uuid/uuid_io.hpp</a></li> 62 <ul> 63 <li><a href="#Synopsis_io">Synopsis</a></li> 64 <li><a href="#Stream_operators">Stream Operators</a></li> 65 <li><a href="#to_string">To String</a></li> 66 </ul> 67 <li><a href="#boost/uuid/uuid_serialize.hpp">boost/uuid/uuid_serialize.hpp</a></li> 68 <ul> 69 <li><a href="#Synopsis_serialize">Synopsis</a></li> 70 <li><a href="#Serialization">Serialization</a></li> 71 </ul> 72 </ul> 73 <li><a href="#Concepts">Concepts</a></li> 74 <ul> 75 <li><a href="#Concept:NameHashProvider">NameHashProvider</a></li> 76 </ul> 77 <li><a href="#Design notes">Design notes</a></li> 78 <li><a href="#History and Acknowledgements">History and Acknowledgements</a></li> 79</ol> 80 81<h2><a name="Introduction">Introduction</a></h2> 82<p> 83A UUID, or Universally unique identifier, is intended to uniquely identify 84information in a distributed environment without significant central 85coordination. It can be used to tag objects with very short lifetimes, or 86to reliably identify very persistent objects across a network. 87<p> 88A formal definition for UUID can be found in <A HREF="https://www.ietf.org/rfc/rfc4122.txt">RFC 4122</A>. 89<p> 90UUIDs have many applications. Some examples follow: Databases may use UUIDs 91to identify rows or records in order to ensure that they are unique across 92different databases, or for publication/subscription services. Network messages 93may be identified with a UUID to ensure that different parts of a message are put 94back together again. Distributed computing may use UUIDs to identify a remote 95procedure call. Transactions and classes involved in serialization may be 96identified by UUIDs. Microsoft's component object model (COM) uses UUIDs to 97distinguish different software component interfaces. UUIDs are inserted into 98documents from Microsoft Office programs. UUIDs identify audio or 99video streams in the Advanced Systems Format (ASF). UUIDs are also a basis 100for OIDs (object identifiers), and URNs (uniform resource name). 101 102<p> 103An attractive feature of UUIDs when compared to alternatives is their relative 104small size, of 128-bits, or 16-bytes. Another is that the creation of UUIDs 105does not require a centralized authority. 106 107<p>When UUIDs are generated by one of the defined 108mechanisms, they are either guaranteed to be unique, different from all other 109generated UUIDs (that is, it has never been generated before and it will 110never be generated again), or it is extremely likely to be unique (depending 111on the mechanism). 112 113<h2><a name="Configuration">Configuration</a></h2> 114 115<p> 116The library does not require building or any special configuration to be used. 117However, there are a few options that can be enabled by defining macros prior to 118including library headers. These macros are summarized in the following table. 119 120<p> 121<table border="1"> 122<tr> 123<th>Macro</th><th>Description</th> 124</tr> 125<tr> 126<td><tt>BOOST_UUID_NO_SIMD</tt></td><td>If defined, disables any optimizations for <a href="http://en.wikipedia.org/wiki/SIMD">SIMD</a>-enabled processors. Generic versions of algorithms will be used instead. This may result in suboptimal performance. By default, optimized algorithms are used, when the library is able to detect the availability of SIMD extensions at compile time.</td> 127</tr> 128<tr> 129<td><tt>BOOST_UUID_USE_SSE2</tt></td><td>If defined, enables optimizations for <a href="http://en.wikipedia.org/wiki/SSE2">SSE2</a> exstensions available in modern x86 processors.</td> 130</tr> 131<tr> 132<td><tt>BOOST_UUID_USE_SSE3</tt></td><td>If defined, enables optimizations for <a href="http://en.wikipedia.org/wiki/SSE3">SSE3</a> exstensions available in modern x86 processors.</td> 133</tr> 134<tr> 135<td><tt>BOOST_UUID_USE_SSE41</tt></td><td>If defined, enables optimizations for <a href="http://en.wikipedia.org/wiki/SSE4#SSE4.1">SSE4.1</a> exstensions available in modern x86 processors.</td> 136</tr> 137</table> 138 139<p> 140By default the library attempts to detect the availability of SIMD extensions in the target CPU at compile time and automatically defines the appropriate macros if succeeded. The <tt>BOOST_UUID_USE_SSE*</tt> macros can be defined by users, if auto-detection fails and it is known that the target CPU will have the extension. Do not enable these extensions unless you're certain that they will always be available on any machine that will run your program. The library performs no run time checks, so if an extension is missing, the program will likely crash. Note that enabling more advanced extensions implies that more basic ones are also available. 141 142<h2><a name="Examples">Examples</a></h2> 143<h3><a name="Tagging">Tagging</a></h3> 144<pre> 145// example of tagging an object with a uuid 146// see <a href="../test/test_tagging.cpp"><tt>boost/libs/uuid/test/test_tagging.cpp</tt></a> 147 148#include <<a href="../../../boost/uuid/uuid.hpp"><tt>boost/uuid/uuid.hpp</tt></a>> 149#include <<a href="../../../boost/uuid/uuid_generators.hpp"><tt>boost/uuid/uuid_generators.hpp</tt></a>> 150 151class object 152{ 153public: 154 object() 155 : tag(boost::uuids::random_generator()()) 156 , state(0) 157 {} 158 159 explicit object(int state) 160 : tag(boost::uuids::random_generator()()) 161 , state(state) 162 {} 163 164 object(object const& rhs) 165 : tag(rhs.tag) 166 , state(rhs.state) 167 {} 168 169 bool operator==(object const& rhs) const { 170 return tag == rhs.tag; 171 } 172 173 object& operator=(object const& rhs) { 174 tag = rhs.tag; 175 state = rhs.state; 176 } 177 178 int get_state() const { return state; } 179 void set_state(int new_state) { state = new_state; } 180 181private: 182 boost::uuids::uuid tag; 183 184 int state; 185}; 186 187object o1(1); 188object o2 = o1; 189o2.set_state(2); 190assert(o1 == o2); 191 192object o3(3); 193assert(o1 != o3); 194assert(o2 != o3); 195</pre> 196 197<h3><a name="POD Efficiencies">POD Efficiencies</a></h3> 198<p> 199This library implements a UUID as a POD allowing a UUID 200to be used in the most efficient ways, including using memcpy, 201and aggregate initializers. A drawback is that a POD can 202not have any constructors, and thus declaring a UUID will not 203initialize it to a value generated by one of the defined 204mechanisms. But a class based on a UUID can be defined 205that does initialize itself to a value generated by one of 206the defined mechanisms. 207<p> 208Note that <tt>boost::is_pod</tt> is specialized for <tt>boost::uuids::uuid</tt> 209and depends on <a href="http://www.boost.org/libs/type_traits">Boost.TypeTraits</a>. 210Define <tt>BOOST_UUID_NO_TYPE_TRAITS</tt> before including <a href="../../../boost/uuid/uuid.hpp"><tt>boost/uuid/uuid.hpp</tt></a> 211to remove the dependency on <a href="http://www.boost.org/libs/type_traits">Boost.TypeTraits</a>. 212<pre> 213// example using memcpy and aggregate initializers 214// example of a class uuid see <a href="../test/test_uuid_class.cpp"><tt>boost/libs/uuid/test/test_uuid_class.cpp</tt></a> 215 216#include <<a href="../../../boost/uuid/uuid.hpp"><tt>boost/uuid/uuid.hpp</tt></a>> 217#include <<a href="../../../boost/uuid/uuid_generators.hpp"><tt>boost/uuid/uuid_generators.hpp</tt></a>> 218 219{ // example using memcpy 220 unsigned char uuid_data[16]; 221 // fill uuid_data 222 223 boost::uuids::uuid u; 224 225 memcpy(&u, uuid_data, 16); 226} 227 228{ // example using aggregate initializers 229 boost::uuids::uuid u = 230 { 0x12 ,0x34, 0x56, 0x78 231 , 0x90, 0xab 232 , 0xcd, 0xef 233 , 0x12, 0x34 234 , 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef 235 }; 236} 237 238// example of creating a uuid class that 239// initializes the uuid in the constructor 240// using a defined mechanism 241 242class uuid_class : public boost::uuids::uuid 243{ 244public: 245 uuid_class() 246 : boost::uuids::uuid(boost::uuids::random_generator()()) 247 {} 248 249 explicit uuid_class(boost::uuids::uuid const& u) 250 : boost::uuids::uuid(u) 251 {} 252 253 operator boost::uuids::uuid() { 254 return static_cast<boost::uuids::uuid&>(*this); 255 } 256 257 operator boost::uuids::uuid() const { 258 return static_cast<boost::uuids::uuid const&>(*this); 259 } 260}; 261 262uuid_class u1; 263uuid_class u2; 264 265assert(u1 != u2); 266</pre> 267 268<h3><a name="Byte Extraction">Byte Extraction</a></h3> 269<p>It is sometimes useful to get at the 16 bytes of a <b>uuid</b> directly. 270Typical use is as follows: 271 272<pre> 273boost::uuids::uuid u; 274std::vector<uint8_t> v(u.size()); 275std::copy(u.begin(), u.end(), v.begin()); 276</pre> 277 278<p>Note: <tt>boost::uuids::uuid::size()</tt> always returns 16. 279 280<h2><a name="Reference">Reference</a></h2> 281<h3><a name="boost/uuid/uuid.hpp" href="../../../boost/uuid/uuid.hpp">boost/uuid/uuid.hpp</a></h3> 282<h4><a name="Synopsis">Synopsis</a></h4> 283<pre> 284namespace boost { 285namespace uuids { 286 287class uuid { 288public: 289 typedef uint8_t value_type; 290 typedef uint8_t& reference; 291 typedef uint8_t const& const_reference; 292 typedef uint8_t* iterator; 293 typedef uint8_t const* const_iterator; 294 typedef std::size_t size_type; 295 typedef std::ptrdiff_t difference_type; 296 297 static constexpr size_type static_size() noexcept; 298 299 // iteration 300 iterator begin() noexcept; 301 iterator end() noexcept; 302 const_iterator begin() const noexcept; 303 const_iterator end() const noexcept; 304 305 constexpr size_type size() const noexcept; 306 307 bool is_nil() const noexcept; 308 309 enum variant_type { 310 variant_ncs, // NCS backward compatibility 311 variant_rfc_4122, // defined in RFC 4122 document 312 variant_microsoft, // Microsoft Corporation backward compatibility 313 variant_future // future definition 314 }; 315 variant_type variant() const noexcept; 316 317 enum version_type { 318 version_unknown = -1, 319 version_time_based = 1, 320 version_dce_security = 2, 321 version_name_based_md5 = 3, 322 version_random_number_based = 4, 323 version_name_based_sha1 = 5 324 }; 325 version_type version() const noexcept; 326 327 // Swap function 328 void swap(uuid& rhs) noexcept; 329 330 uint8_t data[static_size()]; 331}; 332 333// standard operators 334bool operator==(uuid const& lhs, uuid const& rhs) noexcept; 335bool operator!=(uuid const& lhs, uuid const& rhs) noexcept; 336bool operator<(uuid const& lhs, uuid const& rhs) noexcept; 337bool operator>(uuid const& lhs, uuid const& rhs) noexcept; 338bool operator<=(uuid const& lhs, uuid const& rhs) noexcept; 339bool operator>=(uuid const& lhs, uuid const& rhs) noexcept; 340 341void swap(uuid& lhs, uuid& rhs) noexcept; 342 343std::size_t hash_value(uuid const& u) noexcept; 344 345}} // namespace boost::uuids 346</pre> 347 348<h4><a name="Size">Size</a></h4> 349 350<p>The size of a <b>uuid</b> (in bytes) can be obtained either by 351calling the function <tt>boost::uuids::uuid::size()</tt> or by 352calling the static function <tt>boost::uuids::uuid::static_size()</tt>, 353both always return 16. 354<pre> 355 boost::uuids::uuid u; 356 assert(16 == u.size()); 357 assert(16 == boost::uuids::uuid::static_size()); 358</pre> 359 360<h4><a name="Iteration">Iteration</a></h4> 361 362<p>Both iterators and constant iterators are provided. 363<pre> 364 boost::uuids::uuid u; 365 for (boost::uuids::uuid::const_iterator it=u.begin(); it!=u.end(); ++it) { 366 boost::uuids::uuid::value_type v = *it; 367 } 368 for (boost::uuids::uuid::iterator it=u.begin(); it!=u.end(); ++it) { 369 *it = 0; 370 } 371</pre> 372 373<h4><a name="Nil">Nil uuid</a></h4> 374<p>The function, <tt>boost::uuids::uuid::is_nil()</tt> returns true if and 375only if the <b>uuid</b> is equal to {00000000-0000-0000-0000-000000000000}. 376</p> 377 378<h4><a name="Variant">Variant</a></h4> 379<p>Three bits of a <b>uuid</b> determine the variant.</p> 380<pre> 381 boost::uuids::uuid u; 382 boost::uuids::uuid::variant_type v = u.variant(); 383</pre> 384 385<h4><a name="Version">Version</a></h4> 386<p>Four bits of a <b>uuid</b> determine the variant, that is the mechanism 387used to generate the <b>uuid</b>. 388</p> 389<pre> 390 boost::uuids::uuid u; 391 boost::uuids::uuid::version_type v = u.version(); 392</pre> 393 394<h4><a name="Swap">Swap</a></h4> 395<p>Both <tt>boost::uuids::uuid::swap()</tt> and <tt>boost::uuids::swap()</tt> 396are provided. 397</p> 398<pre> 399 boost::uuids::uuid u1, u2; 400 u1.swap(u2); 401 swap(u1, u2); 402</pre> 403 404<h4><a name="Operators">Operators</a></h4> 405<p> 406All of the standard numeric operators are defined for the <b>uuid</b> 407class. These include: 408<pre> 409 operator== 410 operator!= 411 operator< 412 operator> 413 operator<= 414 operator>= 415</pre> 416 417<h4><a name="Hash">Hash Function</a></h4> 418<p> 419This function allows <b>uuid</b>s to be used with 420<a href="http://www.boost.org/doc/html/hash.html"><b>boost::hash</b></a> 421 422<pre> 423boost::hash<boost::uuids::uuid> uuid_hasher; 424std::size_t uuid_hash_value = uuid_hasher(boost::uuids::uuid()); 425</pre> 426 427<h3><a name="boost/uuid/uuid_generators.hpp" href="../../../boost/uuid/uuid_generators.hpp">boost/uuid/uuid_generators.hpp</a></h3> 428<h4><a name="Synopsis_generators">Synopsis</a></h4> 429This file includes all the <b>uuid</b> generators for convenience. 430<pre> 431#include <<a href="../../../boost/uuid/nil_generator.hpp"><tt>boost/uuid/nil_generator.hpp</tt></a>> 432#include <<a href="../../../boost/uuid/string_generator.hpp"><tt>boost/uuid/string_generator.hpp</tt></a>> 433#include <<a href="../../../boost/uuid/name_generator.hpp"><tt>boost/uuid/name_generator.hpp</tt></a>> 434#include <<a href="../../../boost/uuid/random_generator.hpp"><tt>boost/uuid/random_generator.hpp</tt></a>> 435</pre> 436 437<h3><a name="boost/uuid/nil_generator.hpp" href="../../../boost/uuid/nil_generator.hpp">boost/uuid/nil_generator.hpp</a></h3> 438<h4><a name="Synopsis_nil_generator">Synopsis</a></h4> 439<pre> 440namespace boost { 441namespace uuids { 442 443struct nil_generator { 444 typedef uuid result_type; 445 446 uuid operator()() const; 447}; 448uuid nil_uuid(); 449 450}} //namespace boost::uuids 451</pre> 452 453<h4><a name="Nil Generator">Nil Generator</a></h4> 454<p>The <tt>boost::uuids::nil_generator</tt> class always generates a nil <b>uuid</b>. 455<pre> 456boost::uuids::nil_generator gen; 457boost::uuids::uuid u = gen(); 458assert(u.is_nil() == true); 459 460// or for convenience 461boost::uuids::uuid u = boost::uuids::nil_uuid(); 462assert(u.is_nil() == true); 463</pre> 464 465<h3><a name="boost/uuid/string_generator.hpp" href="../../../boost/uuid/string_generator.hpp">boost/uuid/string_generator.hpp</a></h3> 466<h4><a name="Synopsis_string_generator">Synopsis</a></h4> 467 468<pre> 469namespace boost { 470namespace uuids { 471 472struct string_generator { 473 typedef uuid result_type; 474 475 template <typename ch, typename char_traits, typename alloc> 476 uuid operator()(std::basic_string<ch, char_traits, alloc> const& s) const; 477}; 478 479}} //namespace boost::uuids 480</pre> 481 482<h4><a name="String Generator">String Generator</a></h4> 483<p>The <tt>boost::uuids::string_generator</tt> class generates a <b>uuid</b> from a string. In addition to the standards-defined string 484format in <A HREF="https://www.ietf.org/rfc/rfc4122.txt">RFC 4122</A> (p. 3), the string generator accepts a few variants. Valid strings 485match the following PCRE regular expression: 486<pre> 487^({)?([0-9a-fA-F]{8})(?<DASH>-)?([0-9a-fA-F]{4})(?(DASH)-)([0-9a-fA-F]{4})(?(DASH)-)([0-9a-fA-F]{4})(?(DASH)-)([0-9a-fA-F]{12})(?(1)})$ 488</pre> 489Or more generally, the following formats are accepted as valid string formats, where <tt>h</tt> is hexadecimal, without case sensitivity, and without any leading or trailing whitespace: 490<pre> 491hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh 492{hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh} 493hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh 494{hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh} 495</pre> 496For example: 497<pre> 498boost::uuids::string_generator gen; 499boost::uuids::uuid u1 = gen("{01234567-89ab-cdef-0123-456789abcdef}"); 500boost::uuids::uuid u2 = gen(L"01234567-89ab-cdef-0123-456789abcdef"); 501boost::uuids::uuid u3 = gen(std::string("0123456789abcdef0123456789abcdef")); 502boost::uuids::uuid u4 = gen(std::wstring(L"01234567-89AB-CDEF-0123-456789ABCDEF")); 503</pre> 504Invalid input will generate a <tt>std::runtime_error</tt> exception. 505 506<h3><a name="boost/uuid/name_generator.hpp" href="../../../boost/uuid/name_generator.hpp">boost/uuid/name_generator.hpp</a></h3> 507<h4><a name="Synopsis_name_generator">Synopsis</a></h4> 508 509<pre> 510namespace boost { 511namespace uuids { 512 513template<class NameHashProvider> 514class basic_name_generator { 515public: 516 typedef uuid result_type; 517 518 explicit basic_name_generator(uuid const& namespace_uuid); 519 520 uuid operator()(const char* name) const; 521 uuid operator()(const wchar_t* name) const; 522 tempate <typename ch, typename char_traits, typename alloc> 523 uuid operator()(std::basic_string<ch, char_traits, alloc> const& name) const; 524 uuid operator()(void const* buffer, std::size_t byte_count) const; 525}; 526 527typedef basic_name_generator<detail::md5> name_generator_md5; 528typedef basic_name_generator<detail::sha1> name_generator_sha1; 529typedef name_generator_sha1 name_generator; // deprecated 530typedef name_generator_sha1 name_generator_latest; 531 532}} //namespace boost::uuids 533</pre> 534 535<h4><a name="Name Generator">Name Generator</a></h4> 536<p> 537RFC 4122 specifies that a name-based <b>uuid</b> is derived from content in 538a namespace. A <b>uuid</b> with identical content in an identical namespace shall 539yield the same <b>uuid</b> as long as the same hasing algorithm is used. 540<h5>Hashing Algorithms</h5> 541RFC 4122 defines two hashing mechanisms for <b>uuid</b> generation: 542<ul> 543<li>MD5</li> 544<li>SHA1</li> 545</ul> 546Since RFC 4122 was written, both of these hash algorithms have been rendered 547insecure. In anticipation of a new RFC for <b>uuid</b> arriving, 548<tt>name_generator</tt> has been deprecated, and replaced with: 549<ul> 550<li><tt>boost::uuids::name_generator_latest</tt></li> 551<li><tt>boost::uuids::name_generator_md5</tt></li> 552<li><tt>boost::uuids::name_generator_sha1</tt></li> 553</ul> 554<tt>name_generator</tt>, while deprecated, remains a type alias for 555<tt>name_generator_sha1</tt> so the behavior is identical to previous 556releases. When the successor to SHA1 is chosen, 557it will be implemented under a new name similar to those above. 558If your application does not rely on stable hashing over time, you can 559use <tt>name_generator_latest</tt> to always use the latest hashing 560algorithm available. 561</p> 562<p>Consumers are free to use their own hash provider as long as it satisfies the 563<tt><A href="#Concept:NameHashProvider">NameHashProvider</A></tt> concept. 564<h5>Namespaces</h5> 565There are four well-known namespaces defined in 566<A HREF="https://tools.ietf.org/html/rfc4122#appendix-C">RFC 4122, Appendix C</A> which 567are defined as: 568<pre> 569boost::uuids::ns::dns() // == {6ba7b810-9dad-11d1-80b4-00c04fd430c8} 570boost::uuids::ns::url() // == {6ba7b811-9dad-11d1-80b4-00c04fd430c8} 571boost::uuids::ns::oid() // == {6ba7b812-9dad-11d1-80b4-00c04fd430c8} 572boost::uuids::ns::x500dn() // == {6ba7b814-9dad-11d1-80b4-00c04fd430c8} 573</pre> 574of course, you are free to use your own namespace if you prefer. Here is an 575example of name generator usage: 576<pre> 577boost::uuids::name_generator_sha1 gen(boost::uuids::ns::dns()); 578boost::uuids::uuid udoc = gen("boost.org"); 579std::cout << "boost.org uuid in dns namespace, sha1 version: " << udoc << std::endl; 580</pre> 581produces the output: 582<pre> 583boost.org uuid in dns namespace, sha1 version: 0043f363-bbb4-5369-840a-322df6ec1926 584</pre> 585 586<h3><a name="boost/uuid/random_generator.hpp" href="../../../boost/uuid/random_generator.hpp">boost/uuid/random_generator.hpp</a></h3> 587<h4><a name="Synopsis_random_generator">Synopsis</a></h4> 588 589<pre> 590namespace boost { 591namespace uuids { 592 593class random_generator { 594public: 595 typedef uuid result_type; 596 result_type operator()(); 597}; 598 599template <typename UniformRandomNumberGenerator> 600class basic_random_generator { 601public: 602 typedef uuid result_type; 603 604 basic_random_generator(); 605 explicit basic_random_generator(UniformRandomNumberGenerator& gen); 606 explicit basic_random_generator(UniformRandomNumberGenerator* pGen); 607 608 result_type operator()(); 609}; 610 611typedef basic_random_generator<boost::mt19937> random_generator_mt19937; 612 613}} // namespace boost::uuids 614</pre> 615 616<h4><a name="Random Generator">Random Generator</a></h4> 617 <p> 618 <tt>boost::uuids::random_generator</tt> class generates uuids using 619 operating system provided entropy. For the majority of use cases this 620 should be sufficient, as this generator has very low startup overhead 621 when compared to a generator with seeding requirements. 622 </p> 623 <p> 624 <tt>boost::uuids::basic_random_generator</tt> class generates a random number 625 based uuid from a random number generator (one that conforms to the 626 <a href="http://www.boost.org/libs/random/random-concepts.html#uniform-rng">UniformRandomNumberGenerator</a> 627 concept). The default constructor will properly seed the random number generator 628 if it requires the behavior for proper operation. Additional constructors allow you 629 to provide your own <tt>UniformRandomNumberGenerator</tt> and you are responsible for 630 properly seeding it if necessary. 631 </p> 632 <p> 633 <tt>boost::uuids::random_generator_mt19937</tt> is a type definition for: 634 <tt>boost::uuids::basic_random_generator<mt19937></tt> and is provided for 635 convenience. 636 </p> 637 638<h5>Platforms</h5> 639 <p> 640 The following platforms are supported and entropy selection logic explained: 641 <ul> 642 <li>CloudABI</li> 643 <li>Unix 644 <ul> 645 <li>OpenBSD: use <a href="https://man.openbsd.org/arc4random.3"><tt>arc4random(3)</tt></a></li> 646 <li>Linux 3.17 or later: use <a href="http://man7.org/linux/man-pages/man2/getrandom.2.html"><tt>getrandom(2)</tt></a></li> 647 <li>Other systems with glibc 2.25 or later: use <tt>getentropy(3)</tt></li> 648 <li>All other cases: use <tt>/dev/urandom</tt></li> 649 </ul> 650 </li> 651 <li>Windows (UWP Compatible): use BCrypt if available, otherwise use Wincrypt 652 </ul> 653 </p> 654 655<h5>Preprocessor Macros</h5> 656 <p> 657 <ul> 658 <li> 659 <tt>BOOST_UUID_RANDOM_PROVIDER_FORCE_POSIX</tt>: on Unix this will force the 660 selection of <tt>/dev/*random</tt> over <tt>getrandom(2)</tt> or <tt>getentropy(3)</tt>. 661 </li> 662 <li> 663 <tt>BOOST_UUID_RANDOM_PROVIDER_FORCE_WINCRYPT</tt>: on Windows this will force the 664 selection of Wincrypt over BCrypt. 665 </li> 666 <li> 667 <tt>BOOST_UUID_RANDOM_PROVIDER_NO_LIB</tt> (or <tt>BOOST_ALL_NO_LIB</tt>): disable Windows auto linking. 668 </li> 669 </ul> 670 </p> 671 672<h5>Performance</h5> 673 <p> 674 In most cases <tt>random_generator</tt> will be optimal. A benchmark can be found 675 in the tests (test_bench_random) that will determine the cutoff point where 676 <tt>random_generator_mt19937</tt> outperforms <tt>random_generator</tt> in wall time. 677 </p> 678 <p> 679 On Windows when using the wincrypt entropy provider, a measurable delay may occur the 680 first time a <tt>random_generator</tt> is constructed within a running instance. This 681 has been observed using <tt>test_bench_random</tt> and was so significant that the 682 benchmark was changed to throw out the first loop of measurements. 683 </p> 684 685<h5>Exceptions</h5> 686 <p> 687 The exception <tt>boost::uuids::entropy_error</tt> is thrown if there is an error 688 getting entropy from the operating system. 689 </p> 690 691<h5>Examples</h5> 692<pre> 693// Depending on the platform there may be a setup cost in 694// initializing the generator so plan to reuse it if you can. 695boost::uuids::random_generator gen; 696boost::uuids::uuid id = gen(); 697std::cout << id << std::endl; 698boost::uuids::uuid id2 = gen(); 699std::cout << id2 << std::endl; 700assert(id != id2); 701 702// You can still use a PseudoRandomNumberGenerator to create 703// UUIDs, however this is not the preferred mechanism. 704boost::uuids::random_generator_mt19937 bulkgen; 705for (size_t i = 0; i < 1000; ++i) 706{ 707 boost::uuids::uuid u = bulkgen(); 708 // do something with u 709 boost::ignore_unused(u); 710} 711</pre> 712 713<h3><a name="boost/uuid/uuid_io.hpp" href="../../../boost/uuid/uuid_io.hpp">boost/uuid/uuid_io.hpp</a></h3> 714<h4><a name="Synopsis_io">Synopsis</a></h4> 715<pre> 716namespace boost { 717namespace uuids { 718 719template <typename ch, typename char_traits> 720 std::basic_ostream<ch, char_traits>& operator<<(std::basic_ostream<ch, char_traits> &os, uuid const& u); 721 722template <typename ch, typename char_traits> 723 std::basic_istream<ch, char_traits>& operator>>(std::basic_istream<ch, char_traits> &is, uuid &u); 724 725std::string to_string(uuid const& u); 726std::wstring to_wstring(uuid const& u); 727 728}} // namespace boost::uuids 729</pre> 730 731<h4><a name="Stream_operators">Stream Operators</a></h4> 732<p> 733The standard input and output stream operators <tt><<</tt> and <tt>>></tt> 734are provided by including <a href="../../../boost/uuid/uuid_io.hpp"><tt>boost/uuid/uuid_io.hpp</tt></a>. 735The string representation of a <b>uuid</b> is <tt>hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh</tt> 736where <tt>h</tt> is hexadecimal. 737<pre> 738boost::uuids::uuid u1; // initialize uuid 739 740std::stringstream ss; 741ss << u1; 742 743boost::uuids::uuid u2; 744ss >> u2; 745 746assert(u1, u2); 747</pre> 748 749<p> 750One can also use <a href="http://www.boost.org/libs/conversion/lexical_cast.htm"><tt>boost::lexical_cast</tt></a>. 751<pre> 752boost::uuids::uuid u1; // initialize uuid 753 754std::string s = boost::lexical_cast<std::string>(u); 755boost::uuids::uuid u2 = boost::lexical_cast<boost::uuids::uuid>(s); 756 757assert(u1 == u2); 758</pre> 759 760<h4><a name="to_string">To String</a></h4> 761<p> 762The functions <tt>to_string</tt> and <tt>to_wstring</tt> are provided as a 763convenience to convert a <b>uuid</b> to a string. They are also likely faster than 764the stream operators or using <a href="http://www.boost.org/libs/conversion/lexical_cast.htm"><tt>boost::lexical_cast</tt></a>. 765<pre> 766boost::uuids::uuid u; // initialize uuid 767 768std::string s1 = to_string(u); 769 770std::wstring s2 = to_wstring(u); 771</pre> 772 773<h3><a name="boost/uuid/uuid_serialize.hpp" href="../../../boost/uuid/uuid_serialize.hpp">boost/uuid/uuid_serialize.hpp</a></h3> 774<h4><a name="Synopsis_serialize">Synopsis</a></h4> 775<pre> 776namespace boost { 777namespace uuids { 778 779BOOST_CLASS_IMPLEMENTATION(boost::uuids::uuid, boost::serialization::primitive_type) 780 781}} // namespace boost::uuids 782</pre> 783 784<h4><a name="Serialization">Serialization</a></h4> 785<p> 786Serialization is accomplished with the <a href="http://www.boost.org/libs/serialization/doc/index.html"> 787Boost Serialization</a> library. A <b>uuid</b> is serialized as a 788<a href="http://www.boost.org/libs/serialization/doc/serialization.html#primitiveoperators"> 789primitive type</a>, thus only the <b>uuid</b> value will be saved to/loaded from an archive. 790<p> 791Include <a href="../../../boost/uuid/uuid_serialize.hpp"><tt>boost/uuid/uuid_serialize.hpp</tt></a> to enable serialization for <b>uuid</b>s. 792 793<h2><A name="Concepts">Concepts</a></h2> 794This section describes all of the concepts defined by the library. 795<h3><a name="Concept:NameHashProvider">NameHashProvider</a></h3> 796A NameHashProvder type is supplied as a template argument to the <tt>basic_name_generator</tt> class. It provides 797the hashing function that the name generator uses to generate a <b>uuid</b>. 798<h4>Requirements</h4> 799In this table, <i>G</i> is a type meeting the requirements of NameHashProvider: 800<p><center><table width="85%" summary="Valid expressions"> 801<thead> 802<tr bgcolor="lightgray"> 803 <th>Expression</th> 804 <th>Semantics, Pre/Post-conditions</th> 805</tr> 806</thead> 807<tbody> 808<tr> 809 <td><tt>typedef G::digest_type</tt></td> 810 <td>A type definition of contiguous raw storage at least 16 bytes in length.</td> 811</tr> 812<tr> 813 <td><tt>void G::process_byte(unsigned char)</tt></td> 814 <td></td> 815</tr> 816<tr> 817 <td><tt>void G::process_bytes(void const*, std::size_t)</tt></td> 818 <td></td> 819</tr> 820<tr> 821 <td><tt>void G::get_digest(typename G::digest_type&)</tt></td> 822 <td>Copies the digest into the supplied parameter. Called once.</td> 823</tr> 824<tr> 825 <td><tt>unsigned char G::get_version() const</tt></td> 826 <td>Returns the RFC 4122 version for the hashing algorithm (4 bits) in 0x07.</td> 827</tr> 828</tbody> 829</table> 830</center></p> 831<h2><a name="Design notes">Design notes</a></h2> 832<p> 833The document, <a href="http://www.itu.int/ITU-T/studygroups/com17/oid/X.667-E.pdf"> 834http://www.itu.int/ITU-T/studygroups/com17/oid/X.667-E.pdf</a>, was used to design 835and implement the <b>boost::uuids::uuid</b> struct. 836 837<p>All functions are re-entrant. Classes are as thread-safe as an int. That is an 838instance can not be shared between threads without proper synchronization. 839 840<h2><a name="History and Acknowledgements">History and Acknowledgements</a></h2> 841<p> 842A number of people on the <a href="http://www.boost.org/">boost.org</a> 843mailing list provided useful comments and greatly helped to shape the library. 844 845<p>Revised November 8, 2017</p> 846 847<hr> 848<p>� Copyright Andy Tompkins, 2006</p> 849<p>� Copyright 2017 James E. King III</p> 850<p> Distributed under the Boost Software 851License, Version 1.0. (See accompanying file <a href="../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy at <a href="https://www.boost.org/LICENSE_1_0.txt"> 852www.boost.org/LICENSE_1_0.txt</a>)</p> 853</body> 854</html> 855