uuid.html 31 KB

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