1 | // |
---|
2 | // UUID.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/UUID.h#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: UUID |
---|
8 | // Module: UUID |
---|
9 | // |
---|
10 | // Definition of the UUID class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_UUID_INCLUDED |
---|
40 | #define Foundation_UUID_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | |
---|
45 | |
---|
46 | namespace Poco { |
---|
47 | |
---|
48 | |
---|
49 | class Foundation_API UUID |
---|
50 | /// A UUID is an identifier that is unique across both space and time, |
---|
51 | /// with respect to the space of all UUIDs. Since a UUID is a fixed |
---|
52 | /// size and contains a time field, it is possible for values to |
---|
53 | /// rollover (around A.D. 3400, depending on the specific algorithm |
---|
54 | /// used). A UUID can be used for multiple purposes, from tagging |
---|
55 | /// objects with an extremely short lifetime, to reliably identifying |
---|
56 | /// very persistent objects across a network. |
---|
57 | /// This class implements a Universal Unique Identifier, |
---|
58 | /// as specified in Appendix A of the DCE 1.1 Remote Procedure |
---|
59 | /// Call Specification (http://www.opengroup.org/onlinepubs/9629399/), |
---|
60 | /// RFC 2518 (WebDAV), section 6.4.1 and the UUIDs and GUIDs internet |
---|
61 | /// draft by Leach/Salz from February, 1998 |
---|
62 | /// (http://ftp.ics.uci.edu/pub/ietf/webdav/uuid-guid/draft-leach-uuids-guids-01.txt) |
---|
63 | /// and also |
---|
64 | /// http://www.ietf.org/internet-drafts/draft-mealling-uuid-urn-03.txt |
---|
65 | { |
---|
66 | public: |
---|
67 | enum Version |
---|
68 | { |
---|
69 | UUID_TIME_BASED = 0x01, |
---|
70 | UUID_DCE_UID = 0x02, |
---|
71 | UUID_NAME_BASED = 0x03, |
---|
72 | UUID_RANDOM = 0x04 |
---|
73 | }; |
---|
74 | |
---|
75 | UUID(); |
---|
76 | /// Creates a nil (all zero) UUID. |
---|
77 | |
---|
78 | UUID(const UUID& uuid); |
---|
79 | /// Copy constructor. |
---|
80 | |
---|
81 | explicit UUID(const std::string& uuid); |
---|
82 | /// Parses the UUID from a string. |
---|
83 | |
---|
84 | explicit UUID(const char* uuid); |
---|
85 | /// Parses the UUID from a string. |
---|
86 | |
---|
87 | ~UUID(); |
---|
88 | /// Destroys the UUID. |
---|
89 | |
---|
90 | UUID& operator = (const UUID& uuid); |
---|
91 | /// Assignment operator. |
---|
92 | |
---|
93 | void swap(UUID& uuid); |
---|
94 | /// Swaps the UUID with another one. |
---|
95 | |
---|
96 | void parse(const std::string& uuid); |
---|
97 | /// Parses the UUID from its string representation. |
---|
98 | |
---|
99 | std::string toString() const; |
---|
100 | /// Returns a string representation of the UUID consisting |
---|
101 | /// of groups of hexadecimal digits separated by hyphens. |
---|
102 | |
---|
103 | void copyFrom(const char* buffer); |
---|
104 | /// Copies the UUID (16 bytes) from a buffer or byte array. |
---|
105 | /// The UUID fields are expected to be |
---|
106 | /// stored in network byte order. |
---|
107 | /// The buffer need not be aligned. |
---|
108 | |
---|
109 | void copyTo(char* buffer) const; |
---|
110 | /// Copies the UUID to the buffer. The fields |
---|
111 | /// are in network byte order. |
---|
112 | /// The buffer need not be aligned. |
---|
113 | /// There must have room for at least 16 bytes. |
---|
114 | |
---|
115 | Version version() const; |
---|
116 | /// Returns the version of the UUID. |
---|
117 | |
---|
118 | int variant() const; |
---|
119 | /// Returns the variant number of the UUID: |
---|
120 | /// - 0 reserved for NCS backward compatibility |
---|
121 | /// - 2 the Leach-Salz variant (used by this class) |
---|
122 | /// - 6 reserved, Microsoft Corporation backward compatibility |
---|
123 | /// - 7 reserved for future definition |
---|
124 | |
---|
125 | bool operator == (const UUID& uuid) const; |
---|
126 | bool operator != (const UUID& uuid) const; |
---|
127 | bool operator < (const UUID& uuid) const; |
---|
128 | bool operator <= (const UUID& uuid) const; |
---|
129 | bool operator > (const UUID& uuid) const; |
---|
130 | bool operator >= (const UUID& uuid) const; |
---|
131 | |
---|
132 | bool isNil() const; |
---|
133 | /// Returns true iff the UUID is nil (in other words, |
---|
134 | /// consists of all zeros). |
---|
135 | |
---|
136 | static const UUID& nil(); |
---|
137 | /// Returns a nil UUID. |
---|
138 | |
---|
139 | static const UUID& dns(); |
---|
140 | /// Returns the namespace identifier for the DNS namespace. |
---|
141 | |
---|
142 | static const UUID& uri(); |
---|
143 | /// Returns the namespace identifier for the URI (former URL) namespace. |
---|
144 | |
---|
145 | static const UUID& oid(); |
---|
146 | /// Returns the namespace identifier for the OID namespace. |
---|
147 | |
---|
148 | static const UUID& x500(); |
---|
149 | /// Returns the namespace identifier for the X500 namespace. |
---|
150 | |
---|
151 | protected: |
---|
152 | UUID(UInt32 timeLow, UInt32 timeMid, UInt32 timeHiAndVersion, UInt16 clockSeq, UInt8 node[]); |
---|
153 | UUID(const char* bytes, Version version); |
---|
154 | int compare(const UUID& uuid) const; |
---|
155 | static void appendHex(std::string& str, UInt8 n); |
---|
156 | static void appendHex(std::string& str, UInt16 n); |
---|
157 | static void appendHex(std::string& str, UInt32 n); |
---|
158 | static UInt8 nibble(char hex); |
---|
159 | void fromNetwork(); |
---|
160 | void toNetwork(); |
---|
161 | |
---|
162 | private: |
---|
163 | UInt32 _timeLow; |
---|
164 | UInt16 _timeMid; |
---|
165 | UInt16 _timeHiAndVersion; |
---|
166 | UInt16 _clockSeq; |
---|
167 | UInt8 _node[6]; |
---|
168 | |
---|
169 | friend class UUIDGenerator; |
---|
170 | }; |
---|
171 | |
---|
172 | |
---|
173 | // |
---|
174 | // inlines |
---|
175 | // |
---|
176 | inline bool UUID::operator == (const UUID& uuid) const |
---|
177 | { |
---|
178 | return compare(uuid) == 0; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | inline bool UUID::operator != (const UUID& uuid) const |
---|
183 | { |
---|
184 | return compare(uuid) != 0; |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | inline bool UUID::operator < (const UUID& uuid) const |
---|
189 | { |
---|
190 | return compare(uuid) < 0; |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | inline bool UUID::operator <= (const UUID& uuid) const |
---|
195 | { |
---|
196 | return compare(uuid) <= 0; |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | inline bool UUID::operator > (const UUID& uuid) const |
---|
201 | { |
---|
202 | return compare(uuid) > 0; |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | inline bool UUID::operator >= (const UUID& uuid) const |
---|
207 | { |
---|
208 | return compare(uuid) >= 0; |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | inline UUID::Version UUID::version() const |
---|
213 | { |
---|
214 | return Version(_timeHiAndVersion >> 12); |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | inline bool UUID::isNil() const |
---|
219 | { |
---|
220 | return compare(nil()) == 0; |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | inline void swap(UUID& u1, UUID& u2) |
---|
225 | { |
---|
226 | u1.swap(u2); |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | } // namespace Poco |
---|
231 | |
---|
232 | |
---|
233 | #endif // Foundation_UUID_INCLUDED |
---|