Coverage Report

Created: 2026-07-25 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/poco/Foundation/include/Poco/UUID.h
Line
Count
Source
1
//
2
// UUID.h
3
//
4
// Library: Foundation
5
// Package: UUID
6
// Module:  UUID
7
//
8
// Definition of the UUID class.
9
//
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11
// and Contributors.
12
//
13
// SPDX-License-Identifier: BSL-1.0
14
//
15
16
17
#ifndef Foundation_UUID_INCLUDED
18
#define Foundation_UUID_INCLUDED
19
20
21
#include "Poco/Foundation.h"
22
23
24
namespace Poco {
25
26
27
class Foundation_API UUID
28
  /// A UUID is an identifier that is unique across both space and time,
29
  /// with respect to the space of all UUIDs. Since a UUID is a fixed
30
  /// size and contains a time field, it is possible for values to
31
  /// rollover (around A.D. 3400, depending on the specific algorithm
32
  /// used). A UUID can be used for multiple purposes, from tagging
33
  /// objects with an extremely short lifetime, to reliably identifying
34
  /// very persistent objects across a network.
35
  ///
36
  /// This class implements a Universal Unique Identifier,
37
  /// as specified in Appendix A of the DCE 1.1 Remote Procedure
38
  /// Call Specification (http://www.opengroup.org/onlinepubs/9629399/),
39
  /// RFC 2518 (WebDAV), section 6.4.1 and the UUIDs and GUIDs internet
40
  /// draft by Leach/Salz from February, 1998
41
  /// (http://www.ics.uci.edu/~ejw/authoring/uuid-guid/draft-leach-uuids-guids-01.txt)
42
  /// and also http://tools.ietf.org/html/draft-mealling-uuid-urn-05
43
  ///
44
  /// Version 6 and 7 UUIDs are based on RFC 9562.
45
  ///
46
  /// To generate a UUID, see UUIDGenerator.
47
{
48
public:
49
  enum Version
50
  {
51
    UUID_TIME_BASED      = 0x01,
52
    UUID_DCE_UID         = 0x02,
53
    UUID_NAME_BASED      = 0x03,
54
    UUID_RANDOM          = 0x04,
55
    UUID_NAME_BASED_SHA1 = 0x05,
56
    UUID_TIME_BASED_V6   = 0x06,
57
    UUID_TIME_BASED_V7   = 0x07
58
  };
59
60
  UUID();
61
    /// Creates a nil (all zero) UUID.
62
63
  UUID(const UUID& uuid);
64
    /// Copy constructor.
65
66
  explicit UUID(const std::string& uuid);
67
    /// Parses the UUID from a string.
68
69
  explicit UUID(const char* uuid);
70
    /// Parses the UUID from a string.
71
72
  ~UUID();
73
    /// Destroys the UUID.
74
75
  UUID& operator = (const UUID& uuid);
76
    /// Assignment operator.
77
78
  void swap(UUID& uuid) noexcept;
79
    /// Swaps the UUID with another one.
80
81
  void parse(const std::string& uuid);
82
    /// Parses the UUID from its string representation.
83
84
  bool tryParse(const std::string& uuid);
85
    /// Tries to interpret the given string as an UUID.
86
    /// If the UUID is syntactically valid, assigns the
87
    /// members and returns true. Otherwise leaves the
88
    /// object unchanged and returns false.
89
90
  std::string toString() const;
91
    /// Returns a string representation of the UUID consisting
92
    /// of groups of hexadecimal digits separated by hyphens.
93
94
  void copyFrom(const char* buffer);
95
    /// Copies the UUID (16 bytes) from a buffer or byte array.
96
    /// The UUID fields are expected to be
97
    /// stored in network byte order.
98
    /// The buffer need not be aligned.
99
100
  void copyTo(char* buffer) const;
101
    /// Copies the UUID to the buffer. The fields
102
    /// are in network byte order.
103
    /// The buffer need not be aligned.
104
    /// There must have room for at least 16 bytes.
105
106
  Version version() const;
107
    /// Returns the version of the UUID.
108
109
  int variant() const;
110
    /// Returns the variant number of the UUID:
111
    ///   - 0 reserved for NCS backward compatibility
112
    ///   - 2 the Leach-Salz variant (used by this class)
113
    ///   - 6 reserved, Microsoft Corporation backward compatibility
114
    ///   - 7 reserved for future definition
115
116
  bool operator == (const UUID& uuid) const;
117
  bool operator != (const UUID& uuid) const;
118
  bool operator <  (const UUID& uuid) const;
119
  bool operator <= (const UUID& uuid) const;
120
  bool operator >  (const UUID& uuid) const;
121
  bool operator >= (const UUID& uuid) const;
122
123
  bool isNull() const;
124
    /// Returns true iff the UUID is nil (in other words,
125
    /// consists of all zeros).
126
127
  static const UUID& null();
128
    /// Returns a null/nil UUID.
129
130
  static const UUID& dns();
131
    /// Returns the namespace identifier for the DNS namespace.
132
133
  static const UUID& uri();
134
    /// Returns the namespace identifier for the URI (former URL) namespace.
135
136
  static const UUID& oid();
137
    /// Returns the namespace identifier for the OID namespace.
138
139
  static const UUID& x500();
140
    /// Returns the namespace identifier for the X500 namespace.
141
142
protected:
143
  UUID(UInt32 timeLow, UInt16 timeMid, UInt16 timeHiAndVersion, UInt16 clockSeq, UInt8 node[]);
144
  UUID(const char* bytes, Version version);
145
  int compare(const UUID& uuid) const;
146
  static void appendHex(std::string& str, UInt8 n);
147
  static void appendHex(std::string& str, UInt16 n);
148
  static void appendHex(std::string& str, UInt32 n);
149
  static Int16 nibble(char hex);
150
  void fromNetwork();
151
  void toNetwork();
152
153
private:
154
  UInt32 _timeLow;
155
  UInt16 _timeMid;
156
  UInt16 _timeHiAndVersion;
157
  UInt16 _clockSeq;
158
  UInt8  _node[6];
159
160
  friend class UUIDGenerator;
161
};
162
163
164
//
165
// inlines
166
//
167
inline bool UUID::operator == (const UUID& uuid) const
168
0
{
169
0
  return compare(uuid) == 0;
170
0
}
171
172
173
inline bool UUID::operator != (const UUID& uuid) const
174
0
{
175
0
  return compare(uuid) != 0;
176
0
}
177
178
179
inline bool UUID::operator < (const UUID& uuid) const
180
0
{
181
0
  return compare(uuid) < 0;
182
0
}
183
184
185
inline bool UUID::operator <= (const UUID& uuid) const
186
0
{
187
0
  return compare(uuid) <= 0;
188
0
}
189
190
191
inline bool UUID::operator > (const UUID& uuid) const
192
0
{
193
0
  return compare(uuid) > 0;
194
0
}
195
196
197
inline bool UUID::operator >= (const UUID& uuid) const
198
0
{
199
0
  return compare(uuid) >= 0;
200
0
}
201
202
203
inline UUID::Version UUID::version() const
204
0
{
205
0
  return Version(_timeHiAndVersion >> 12);
206
0
}
207
208
209
inline bool UUID::isNull() const
210
0
{
211
0
  return compare(null()) == 0;
212
0
}
213
214
215
inline void swap(UUID& u1, UUID& u2) noexcept
216
0
{
217
0
  u1.swap(u2);
218
0
}
219
220
221
} // namespace Poco
222
223
224
#endif // Foundation_UUID_INCLUDED