1 //===-- UUID.h --------------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_UTILITY_UUID_H
10 #define LLDB_UTILITY_UUID_H
15 #include "llvm/ADT/ArrayRef.h"
21 namespace lldb_private {
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) {
33 return fromData({reinterpret_cast<const uint8_t *>(bytes), num_bytes});
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); }
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) {
45 return fromOptionalData(
46 {reinterpret_cast<const uint8_t *>(bytes), num_bytes});
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; }))
58 void Clear() { m_bytes.clear(); }
60 void Dump(Stream *s) const;
62 llvm::ArrayRef<uint8_t> GetBytes() const { return m_bytes; }
64 explicit operator bool() const { return IsValid(); }
65 bool IsValid() const { return !m_bytes.empty(); }
67 std::string GetAsString(llvm::StringRef separator = "-") const;
69 size_t SetFromStringRef(llvm::StringRef str, uint32_t num_uuid_bytes = 16);
71 // Same as SetFromStringRef, but if the resultant UUID is all 0 bytes, set the
73 size_t SetFromOptionalStringRef(llvm::StringRef str,
74 uint32_t num_uuid_bytes = 16);
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
79 /// Decode as many UUID bytes (up to 16) as possible from the C
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.
87 /// \param[in] uuid_bytes
88 /// A buffer of bytes that will contain a full or patially
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);
99 UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes.begin(), bytes.end()) {}
101 // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations
103 llvm::SmallVector<uint8_t, 20> m_bytes;
105 friend bool operator==(const UUID &LHS, const UUID &RHS) {
106 return LHS.m_bytes == RHS.m_bytes;
108 friend bool operator!=(const UUID &LHS, const UUID &RHS) {
109 return !(LHS == RHS);
111 friend bool operator<(const UUID &LHS, const UUID &RHS) {
112 return LHS.m_bytes < RHS.m_bytes;
114 friend bool operator<=(const UUID &LHS, const UUID &RHS) {
117 friend bool operator>(const UUID &LHS, const UUID &RHS) { return RHS < LHS; }
118 friend bool operator>=(const UUID &LHS, const UUID &RHS) {
122 } // namespace lldb_private
124 #endif // LLDB_UTILITY_UUID_H