CppCommon  1.0.4.1
C++ Common Library
uuid.inl
Go to the documentation of this file.
1 
9 namespace CppCommon {
10 
12 namespace Internals {
13 
14 inline constexpr uint8_t unhex(char ch)
15 {
16  if ((ch >= '0') && (ch <= '9'))
17  return ch - '0';
18  else if ((ch >= 'a') && (ch <= 'f'))
19  return 10 + ch - 'a';
20  else if ((ch >= 'A') && (ch <= 'F'))
21  return 10 + ch - 'A';
22  else
23  throwex DomainException("Invalid UUID character");
24 }
25 
26 } // namespace Internals
28 
29 inline constexpr UUID::UUID(const char* uuid, size_t size)
30 {
31  char v1 = 0;
32  char v2 = 0;
33  bool pack = false;
34  size_t index = 0;
35 
36  // Parse UUID string
37  for (size_t i = 0; i < size; ++i)
38  {
39  char ch = uuid[i];
40  if ((ch == '-') || (ch == '{') || (ch == '}'))
41  continue;
42 
43  if (pack)
44  {
45  v2 = ch;
46  pack = false;
47  uint8_t ui1 = Internals::unhex(v1);
48  uint8_t ui2 = Internals::unhex(v2);
49  if ((ui1 > 15) || (ui2 > 15))
50  throwex ArgumentException("Invalid UUID literal");
51  _data[index++] = ui1 * 16 + ui2;
52  if (index >= 16)
53  break;
54  }
55  else
56  {
57  v1 = ch;
58  pack = true;
59  }
60  }
61 
62  // Fill remaining data with zeros
63  for (; index < 16; ++index)
64  _data[index++] = 0;
65 }
66 
67 inline void UUID::swap(UUID& uuid) noexcept
68 {
69  using std::swap;
70  swap(_data, uuid._data);
71 }
72 
73 inline void swap(UUID& uuid1, UUID& uuid2) noexcept
74 {
75  uuid1.swap(uuid2);
76 }
77 
78 } // namespace CppCommon
79 
80 #if defined(FMT_VERSION)
81 template <>
82 struct fmt::formatter<CppCommon::UUID> : formatter<std::string_view>
83 {
84  template <typename FormatContext>
85  auto format(const CppCommon::UUID& value, FormatContext& ctx) const
86  {
87  return formatter<string_view>::format(value.string(), ctx);
88  }
89 };
90 #endif
91 
93 template <>
94 struct std::hash<CppCommon::UUID>
95 {
96  typedef CppCommon::UUID argument_type;
97  typedef size_t result_type;
98 
99  result_type operator() (const argument_type& value) const
100  {
101  result_type result = 17;
102  std::hash<uint8_t> hasher;
103  for (size_t i = 0; i < value.data().size(); ++i)
104  result = result * 31 + hasher(value.data()[i]);
105  return result;
106  }
107 };
Argument exception.
Definition: exceptions.h:79
Universally unique identifier (UUID)
Definition: uuid.h:36
std::string string() const
Get string from the current UUID in format "00000000-0000-0000-0000-000000000000".
Definition: uuid.cpp:22
void swap(UUID &uuid) noexcept
Swap two instances.
Definition: uuid.inl:67
constexpr UUID()
Default constructor.
Definition: uuid.h:39
#define throwex
Throw extended exception macro.
Definition: exceptions.h:23
C++ Common project definitions.
Definition: token_bucket.h:15
std::string format(fmt::format_string< T... > pattern, T &&... args)
Format string.
Definition: format.inl:12
void swap(FileCache &cache1, FileCache &cache2) noexcept
Definition: filecache.inl:23
void swap(UUID &uuid1, UUID &uuid2) noexcept
Definition: uuid.inl:73