Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/generic/include/boost/uuid/name_generator.hpp @ 16:2665513ce2d3
Add boost headers
author | Chris Cannam |
---|---|
date | Tue, 05 Aug 2014 11:11:38 +0100 |
parents | |
children | c530137014c0 |
comparison
equal
deleted
inserted
replaced
15:663ca0da4350 | 16:2665513ce2d3 |
---|---|
1 // Boost name_generator.hpp header file ----------------------------------------------// | |
2 | |
3 // Copyright 2010 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 #ifndef BOOST_UUID_NAME_GENERATOR_HPP | |
9 #define BOOST_UUID_NAME_GENERATOR_HPP | |
10 | |
11 #include <boost/uuid/uuid.hpp> | |
12 #include <boost/uuid/sha1.hpp> | |
13 #include <boost/assert.hpp> | |
14 #include <string> | |
15 #include <cstring> // for strlen, wcslen | |
16 | |
17 #ifdef BOOST_NO_STDC_NAMESPACE | |
18 namespace std { | |
19 using ::strlen; | |
20 using ::wcslen; | |
21 } //namespace std | |
22 #endif //BOOST_NO_STDC_NAMESPACE | |
23 | |
24 namespace boost { | |
25 namespace uuids { | |
26 | |
27 // generate a name-based uuid | |
28 // TODO: add in common namesspace uuids | |
29 class name_generator { | |
30 public: | |
31 typedef uuid result_type; | |
32 | |
33 explicit name_generator(uuid const& namespace_uuid) | |
34 : namespace_uuid(namespace_uuid) | |
35 {} | |
36 | |
37 uuid operator()(const char* name) { | |
38 reset(); | |
39 process_characters(name, std::strlen(name)); | |
40 return sha_to_uuid(); | |
41 } | |
42 | |
43 uuid operator()(const wchar_t* name) { | |
44 reset(); | |
45 process_characters(name, std::wcslen(name)); | |
46 return sha_to_uuid(); | |
47 } | |
48 | |
49 template <typename ch, typename char_traits, typename alloc> | |
50 uuid operator()(std::basic_string<ch, char_traits, alloc> const& name) { | |
51 reset(); | |
52 process_characters(name.c_str(), name.length()); | |
53 return sha_to_uuid(); | |
54 } | |
55 | |
56 uuid operator()(void const* buffer, std::size_t byte_count) { | |
57 reset(); | |
58 sha.process_bytes(buffer, byte_count); | |
59 return sha_to_uuid(); | |
60 }; | |
61 | |
62 private: | |
63 // we convert all characters to uint32_t so that each | |
64 // character is 4 bytes reguardless of sizeof(char) or | |
65 // sizeof(wchar_t). We want the name string on any | |
66 // platform / compiler to generate the same uuid | |
67 // except for char | |
68 template <typename char_type> | |
69 void process_characters(char_type const*const characters, size_t count) { | |
70 BOOST_ASSERT(sizeof(uint32_t) >= sizeof(char_type)); | |
71 | |
72 for (size_t i=0; i<count; i++) { | |
73 uint32_t c = characters[i]; | |
74 sha.process_byte( (c >> 0) & 0xFF ); | |
75 sha.process_byte( (c >> 8) & 0xFF ); | |
76 sha.process_byte( (c >> 16) & 0xFF ); | |
77 sha.process_byte( (c >> 24) & 0xFF ); | |
78 } | |
79 } | |
80 | |
81 void process_characters(char const*const characters, size_t count) { | |
82 sha.process_bytes(characters, count); | |
83 } | |
84 | |
85 void reset() | |
86 { | |
87 sha.reset(); | |
88 sha.process_bytes(namespace_uuid.begin(), namespace_uuid.size()); | |
89 } | |
90 | |
91 uuid sha_to_uuid() | |
92 { | |
93 unsigned int digest[5]; | |
94 | |
95 sha.get_digest(digest); | |
96 | |
97 uuid u; | |
98 for (int i=0; i<4; ++i) { | |
99 *(u.begin() + i*4+0) = ((digest[i] >> 24) & 0xFF); | |
100 *(u.begin() + i*4+1) = ((digest[i] >> 16) & 0xFF); | |
101 *(u.begin() + i*4+2) = ((digest[i] >> 8) & 0xFF); | |
102 *(u.begin() + i*4+3) = ((digest[i] >> 0) & 0xFF); | |
103 } | |
104 | |
105 // set variant | |
106 // must be 0b10xxxxxx | |
107 *(u.begin()+8) &= 0xBF; | |
108 *(u.begin()+8) |= 0x80; | |
109 | |
110 // set version | |
111 // must be 0b0101xxxx | |
112 *(u.begin()+6) &= 0x5F; //0b01011111 | |
113 *(u.begin()+6) |= 0x50; //0b01010000 | |
114 | |
115 return u; | |
116 } | |
117 | |
118 private: | |
119 uuid namespace_uuid; | |
120 detail::sha1 sha; | |
121 }; | |
122 | |
123 }} // namespace boost::uuids | |
124 | |
125 #endif // BOOST_UUID_NAME_GENERATOR_HPP |