1 //===-- UUID.h --------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLDB_UTILITY_UUID_H
11 #define LLDB_UTILITY_UUID_H
16 #include "llvm/ADT/ArrayRef.h"
22 namespace lldb_private {
30 /// Creates a UUID from the data pointed to by the bytes argument. No special
31 /// significance is attached to any of the values.
32 static UUID fromData(const void *bytes, uint32_t num_bytes) {
34 return fromData({reinterpret_cast<const uint8_t *>(bytes), num_bytes});
38 /// Creates a uuid from the data pointed to by the bytes argument. No special
39 /// significance is attached to any of the values.
40 static UUID fromData(llvm::ArrayRef<uint8_t> bytes) { return UUID(bytes); }
42 /// Creates a UUID from the data pointed to by the bytes argument. Data
43 /// consisting purely of zero bytes is treated as an invalid UUID.
44 static UUID fromOptionalData(const void *bytes, uint32_t num_bytes) {
46 return fromOptionalData(
47 {reinterpret_cast<const uint8_t *>(bytes), num_bytes});
51 /// Creates a UUID from the data pointed to by the bytes argument. Data
52 /// consisting purely of zero bytes is treated as an invalid UUID.
53 static UUID fromOptionalData(llvm::ArrayRef<uint8_t> bytes) {
54 if (llvm::all_of(bytes, [](uint8_t b) { return b == 0; }))
59 void Clear() { m_bytes.clear(); }
61 void Dump(Stream *s) const;
63 llvm::ArrayRef<uint8_t> GetBytes() const { return m_bytes; }
65 explicit operator bool() const { return IsValid(); }
66 bool IsValid() const { return !m_bytes.empty(); }
68 std::string GetAsString(llvm::StringRef separator = "-") const;
70 size_t SetFromStringRef(llvm::StringRef str, uint32_t num_uuid_bytes = 16);
72 // Decode as many UUID bytes (up to 16) as possible from the C string "cstr"
73 // This is used for auto completion where a partial UUID might have been
75 //------------------------------------------------------------------
76 /// Decode as many UUID bytes (up to 16) as possible from the C
80 /// A NULL terminate C string that points at a UUID string value
81 /// (no leading spaces). The string must contain only hex
82 /// characters and optionally can contain the '-' sepearators.
84 /// @param[in] uuid_bytes
85 /// A buffer of bytes that will contain a full or patially
89 /// The original string, with all decoded bytes removed.
90 //------------------------------------------------------------------
91 static llvm::StringRef
92 DecodeUUIDBytesFromString(llvm::StringRef str,
93 llvm::SmallVectorImpl<uint8_t> &uuid_bytes,
94 uint32_t num_uuid_bytes = 16);
97 UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes.begin(), bytes.end()) {}
99 // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations
101 llvm::SmallVector<uint8_t, 20> m_bytes;
103 friend bool operator==(const UUID &LHS, const UUID &RHS) {
104 return LHS.m_bytes == RHS.m_bytes;
106 friend bool operator!=(const UUID &LHS, const UUID &RHS) {
107 return !(LHS == RHS);
109 friend bool operator<(const UUID &LHS, const UUID &RHS) {
110 return LHS.m_bytes < RHS.m_bytes;
112 friend bool operator<=(const UUID &LHS, const UUID &RHS) {
115 friend bool operator>(const UUID &LHS, const UUID &RHS) { return RHS < LHS; }
116 friend bool operator>=(const UUID &LHS, const UUID &RHS) {
120 } // namespace lldb_private
122 #endif // LLDB_UTILITY_UUID_H