UUID.h revision 344779
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#include <stddef.h> 14#include <stdint.h> 15#include <string> 16#include "llvm/ADT/ArrayRef.h" 17 18namespace llvm { 19 class StringRef; 20} 21 22namespace lldb_private { 23 24 class Stream; 25 26class UUID { 27public: 28 UUID() = default; 29 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) { 33 if (bytes) 34 return fromData({reinterpret_cast<const uint8_t *>(bytes), num_bytes}); 35 return UUID(); 36 } 37 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); } 41 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) { 45 if (bytes) 46 return fromOptionalData( 47 {reinterpret_cast<const uint8_t *>(bytes), num_bytes}); 48 return UUID(); 49 } 50 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; })) 55 return UUID(); 56 return UUID(bytes); 57 } 58 59 void Clear() { m_bytes.clear(); } 60 61 void Dump(Stream *s) const; 62 63 llvm::ArrayRef<uint8_t> GetBytes() const { return m_bytes; } 64 65 explicit operator bool() const { return IsValid(); } 66 bool IsValid() const { return !m_bytes.empty(); } 67 68 std::string GetAsString(llvm::StringRef separator = "-") const; 69 70 size_t SetFromStringRef(llvm::StringRef str, uint32_t num_uuid_bytes = 16); 71 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 74 // typed in. It 75 //------------------------------------------------------------------ 76 /// Decode as many UUID bytes (up to 16) as possible from the C 77 /// string \a cstr. 78 /// 79 /// @param[in] cstr 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. 83 /// 84 /// @param[in] uuid_bytes 85 /// A buffer of bytes that will contain a full or patially 86 /// decoded UUID. 87 /// 88 /// @return 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); 95 96private: 97 UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes.begin(), bytes.end()) {} 98 99 // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations 100 // for this case. 101 llvm::SmallVector<uint8_t, 20> m_bytes; 102 103 friend bool operator==(const UUID &LHS, const UUID &RHS) { 104 return LHS.m_bytes == RHS.m_bytes; 105 } 106 friend bool operator!=(const UUID &LHS, const UUID &RHS) { 107 return !(LHS == RHS); 108 } 109 friend bool operator<(const UUID &LHS, const UUID &RHS) { 110 return LHS.m_bytes < RHS.m_bytes; 111 } 112 friend bool operator<=(const UUID &LHS, const UUID &RHS) { 113 return !(RHS < LHS); 114 } 115 friend bool operator>(const UUID &LHS, const UUID &RHS) { return RHS < LHS; } 116 friend bool operator>=(const UUID &LHS, const UUID &RHS) { 117 return !(LHS < RHS); 118 } 119}; 120} // namespace lldb_private 121 122#endif // LLDB_UTILITY_UUID_H 123