UUID.h revision 353358
1//===-- UUID.h --------------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_UTILITY_UUID_H
10#define LLDB_UTILITY_UUID_H
11
12#include <stddef.h>
13#include <stdint.h>
14#include <string>
15#include "llvm/ADT/ArrayRef.h"
16
17namespace llvm {
18  class StringRef;
19}
20
21namespace lldb_private {
22
23  class Stream;
24
25class UUID {
26public:
27  UUID() = default;
28
29  /// Creates a UUID from the data pointed to by the bytes argument. No special
30  /// significance is attached to any of the values.
31  static UUID fromData(const void *bytes, uint32_t num_bytes) {
32    if (bytes)
33      return fromData({reinterpret_cast<const uint8_t *>(bytes), num_bytes});
34    return UUID();
35  }
36
37  /// Creates a uuid from the data pointed to by the bytes argument. No special
38  /// significance is attached to any of the values.
39  static UUID fromData(llvm::ArrayRef<uint8_t> bytes) { return UUID(bytes); }
40
41  /// Creates a UUID from the data pointed to by the bytes argument. Data
42  /// consisting purely of zero bytes is treated as an invalid UUID.
43  static UUID fromOptionalData(const void *bytes, uint32_t num_bytes) {
44    if (bytes)
45      return fromOptionalData(
46          {reinterpret_cast<const uint8_t *>(bytes), num_bytes});
47    return UUID();
48  }
49
50  /// Creates a UUID from the data pointed to by the bytes argument. Data
51  /// consisting purely of zero bytes is treated as an invalid UUID.
52  static UUID fromOptionalData(llvm::ArrayRef<uint8_t> bytes) {
53    if (llvm::all_of(bytes, [](uint8_t b) { return b == 0; }))
54      return UUID();
55    return UUID(bytes);
56  }
57
58  void Clear() { m_bytes.clear(); }
59
60  void Dump(Stream *s) const;
61
62  llvm::ArrayRef<uint8_t> GetBytes() const { return m_bytes; }
63
64  explicit operator bool() const { return IsValid(); }
65  bool IsValid() const { return !m_bytes.empty(); }
66
67  std::string GetAsString(llvm::StringRef separator = "-") const;
68
69  size_t SetFromStringRef(llvm::StringRef str, uint32_t num_uuid_bytes = 16);
70
71  // Same as SetFromStringRef, but if the resultant UUID is all 0 bytes, set the
72  // UUID to invalid.
73  size_t SetFromOptionalStringRef(llvm::StringRef str,
74                                  uint32_t num_uuid_bytes = 16);
75
76  // Decode as many UUID bytes (up to 16) as possible from the C string "cstr"
77  // This is used for auto completion where a partial UUID might have been
78  // typed in. It
79  /// Decode as many UUID bytes (up to 16) as possible from the C
80  /// string \a cstr.
81  ///
82  /// \param[in] cstr
83  ///     A NULL terminate C string that points at a UUID string value
84  ///     (no leading spaces). The string must contain only hex
85  ///     characters and optionally can contain the '-' sepearators.
86  ///
87  /// \param[in] uuid_bytes
88  ///     A buffer of bytes that will contain a full or patially
89  ///     decoded UUID.
90  ///
91  /// \return
92  ///     The original string, with all decoded bytes removed.
93  static llvm::StringRef
94  DecodeUUIDBytesFromString(llvm::StringRef str,
95                            llvm::SmallVectorImpl<uint8_t> &uuid_bytes,
96                            uint32_t num_uuid_bytes = 16);
97
98private:
99  UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes.begin(), bytes.end()) {}
100
101  // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations
102  // for this case.
103  llvm::SmallVector<uint8_t, 20> m_bytes;
104
105  friend bool operator==(const UUID &LHS, const UUID &RHS) {
106    return LHS.m_bytes == RHS.m_bytes;
107  }
108  friend bool operator!=(const UUID &LHS, const UUID &RHS) {
109    return !(LHS == RHS);
110  }
111  friend bool operator<(const UUID &LHS, const UUID &RHS) {
112    return LHS.m_bytes < RHS.m_bytes;
113  }
114  friend bool operator<=(const UUID &LHS, const UUID &RHS) {
115    return !(RHS < LHS);
116  }
117  friend bool operator>(const UUID &LHS, const UUID &RHS) { return RHS < LHS; }
118  friend bool operator>=(const UUID &LHS, const UUID &RHS) {
119    return !(LHS < RHS);
120  }
121};
122} // namespace lldb_private
123
124#endif // LLDB_UTILITY_UUID_H
125