UUID.h revision 341825
1//===-- UUID.h --------------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLDB_UTILITY_UUID_H
11#define LLDB_UTILITY_UUID_H
12
13// C Includes
14// C++ Includes
15#include <stddef.h>
16#include <stdint.h>
17#include <string>
18#include "llvm/ADT/ArrayRef.h"
19
20namespace llvm {
21  class StringRef;
22}
23
24namespace lldb_private {
25
26  class Stream;
27
28class UUID {
29public:
30  UUID() = default;
31
32  /// Creates a UUID from the data pointed to by the bytes argument. No special
33  /// significance is attached to any of the values.
34  static UUID fromData(const void *bytes, uint32_t num_bytes) {
35    if (bytes)
36      return fromData({reinterpret_cast<const uint8_t *>(bytes), num_bytes});
37    return UUID();
38  }
39
40  /// Creates a uuid from the data pointed to by the bytes argument. No special
41  /// significance is attached to any of the values.
42  static UUID fromData(llvm::ArrayRef<uint8_t> bytes) { return UUID(bytes); }
43
44  /// Creates a UUID from the data pointed to by the bytes argument. Data
45  /// consisting purely of zero bytes is treated as an invalid UUID.
46  static UUID fromOptionalData(const void *bytes, uint32_t num_bytes) {
47    if (bytes)
48      return fromOptionalData(
49          {reinterpret_cast<const uint8_t *>(bytes), num_bytes});
50    return UUID();
51  }
52
53  /// Creates a UUID from the data pointed to by the bytes argument. Data
54  /// consisting purely of zero bytes is treated as an invalid UUID.
55  static UUID fromOptionalData(llvm::ArrayRef<uint8_t> bytes) {
56    if (llvm::all_of(bytes, [](uint8_t b) { return b == 0; }))
57      return UUID();
58    return UUID(bytes);
59  }
60
61  void Clear() { m_bytes.clear(); }
62
63  void Dump(Stream *s) const;
64
65  llvm::ArrayRef<uint8_t> GetBytes() const { return m_bytes; }
66
67  explicit operator bool() const { return IsValid(); }
68  bool IsValid() const { return !m_bytes.empty(); }
69
70  std::string GetAsString(llvm::StringRef separator = "-") const;
71
72  size_t SetFromStringRef(llvm::StringRef str, uint32_t num_uuid_bytes = 16);
73
74  // Decode as many UUID bytes (up to 16) as possible from the C string "cstr"
75  // This is used for auto completion where a partial UUID might have been
76  // typed in. It
77  //------------------------------------------------------------------
78  /// Decode as many UUID bytes (up to 16) as possible from the C
79  /// string \a cstr.
80  ///
81  /// @param[in] cstr
82  ///     A NULL terminate C string that points at a UUID string value
83  ///     (no leading spaces). The string must contain only hex
84  ///     characters and optionally can contain the '-' sepearators.
85  ///
86  /// @param[in] uuid_bytes
87  ///     A buffer of bytes that will contain a full or patially
88  ///     decoded UUID.
89  ///
90  /// @return
91  ///     The original string, with all decoded bytes removed.
92  //------------------------------------------------------------------
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