UUID.h revision 353358
1317027Sdim//===-- UUID.h --------------------------------------------------*- C++ -*-===// 2317027Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6317027Sdim// 7317027Sdim//===----------------------------------------------------------------------===// 8317027Sdim 9317027Sdim#ifndef LLDB_UTILITY_UUID_H 10317027Sdim#define LLDB_UTILITY_UUID_H 11317027Sdim 12317027Sdim#include <stddef.h> 13317027Sdim#include <stdint.h> 14317027Sdim#include <string> 15341825Sdim#include "llvm/ADT/ArrayRef.h" 16317027Sdim 17317027Sdimnamespace llvm { 18317027Sdim class StringRef; 19317027Sdim} 20317027Sdim 21317027Sdimnamespace lldb_private { 22317027Sdim 23317027Sdim class Stream; 24317027Sdim 25317027Sdimclass UUID { 26317027Sdimpublic: 27341825Sdim UUID() = default; 28317027Sdim 29341825Sdim /// Creates a UUID from the data pointed to by the bytes argument. No special 30341825Sdim /// significance is attached to any of the values. 31341825Sdim static UUID fromData(const void *bytes, uint32_t num_bytes) { 32341825Sdim if (bytes) 33341825Sdim return fromData({reinterpret_cast<const uint8_t *>(bytes), num_bytes}); 34341825Sdim return UUID(); 35341825Sdim } 36317027Sdim 37341825Sdim /// Creates a uuid from the data pointed to by the bytes argument. No special 38341825Sdim /// significance is attached to any of the values. 39341825Sdim static UUID fromData(llvm::ArrayRef<uint8_t> bytes) { return UUID(bytes); } 40317027Sdim 41341825Sdim /// Creates a UUID from the data pointed to by the bytes argument. Data 42341825Sdim /// consisting purely of zero bytes is treated as an invalid UUID. 43341825Sdim static UUID fromOptionalData(const void *bytes, uint32_t num_bytes) { 44341825Sdim if (bytes) 45341825Sdim return fromOptionalData( 46341825Sdim {reinterpret_cast<const uint8_t *>(bytes), num_bytes}); 47341825Sdim return UUID(); 48341825Sdim } 49317027Sdim 50341825Sdim /// Creates a UUID from the data pointed to by the bytes argument. Data 51341825Sdim /// consisting purely of zero bytes is treated as an invalid UUID. 52341825Sdim static UUID fromOptionalData(llvm::ArrayRef<uint8_t> bytes) { 53341825Sdim if (llvm::all_of(bytes, [](uint8_t b) { return b == 0; })) 54341825Sdim return UUID(); 55341825Sdim return UUID(bytes); 56341825Sdim } 57317027Sdim 58341825Sdim void Clear() { m_bytes.clear(); } 59341825Sdim 60317027Sdim void Dump(Stream *s) const; 61317027Sdim 62341825Sdim llvm::ArrayRef<uint8_t> GetBytes() const { return m_bytes; } 63317027Sdim 64341825Sdim explicit operator bool() const { return IsValid(); } 65341825Sdim bool IsValid() const { return !m_bytes.empty(); } 66317027Sdim 67341825Sdim std::string GetAsString(llvm::StringRef separator = "-") const; 68317027Sdim 69318384Sdim size_t SetFromStringRef(llvm::StringRef str, uint32_t num_uuid_bytes = 16); 70353358Sdim 71353358Sdim // Same as SetFromStringRef, but if the resultant UUID is all 0 bytes, set the 72353358Sdim // UUID to invalid. 73353358Sdim size_t SetFromOptionalStringRef(llvm::StringRef str, 74353358Sdim uint32_t num_uuid_bytes = 16); 75317027Sdim 76317027Sdim // Decode as many UUID bytes (up to 16) as possible from the C string "cstr" 77317027Sdim // This is used for auto completion where a partial UUID might have been 78317027Sdim // typed in. It 79317027Sdim /// Decode as many UUID bytes (up to 16) as possible from the C 80317027Sdim /// string \a cstr. 81317027Sdim /// 82353358Sdim /// \param[in] cstr 83317027Sdim /// A NULL terminate C string that points at a UUID string value 84317027Sdim /// (no leading spaces). The string must contain only hex 85317027Sdim /// characters and optionally can contain the '-' sepearators. 86317027Sdim /// 87353358Sdim /// \param[in] uuid_bytes 88317027Sdim /// A buffer of bytes that will contain a full or patially 89317027Sdim /// decoded UUID. 90317027Sdim /// 91353358Sdim /// \return 92317027Sdim /// The original string, with all decoded bytes removed. 93317027Sdim static llvm::StringRef 94341825Sdim DecodeUUIDBytesFromString(llvm::StringRef str, 95341825Sdim llvm::SmallVectorImpl<uint8_t> &uuid_bytes, 96317027Sdim uint32_t num_uuid_bytes = 16); 97317027Sdim 98341825Sdimprivate: 99341825Sdim UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes.begin(), bytes.end()) {} 100317027Sdim 101341825Sdim // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations 102341825Sdim // for this case. 103341825Sdim llvm::SmallVector<uint8_t, 20> m_bytes; 104317027Sdim 105341825Sdim friend bool operator==(const UUID &LHS, const UUID &RHS) { 106341825Sdim return LHS.m_bytes == RHS.m_bytes; 107341825Sdim } 108341825Sdim friend bool operator!=(const UUID &LHS, const UUID &RHS) { 109341825Sdim return !(LHS == RHS); 110341825Sdim } 111341825Sdim friend bool operator<(const UUID &LHS, const UUID &RHS) { 112341825Sdim return LHS.m_bytes < RHS.m_bytes; 113341825Sdim } 114341825Sdim friend bool operator<=(const UUID &LHS, const UUID &RHS) { 115341825Sdim return !(RHS < LHS); 116341825Sdim } 117341825Sdim friend bool operator>(const UUID &LHS, const UUID &RHS) { return RHS < LHS; } 118341825Sdim friend bool operator>=(const UUID &LHS, const UUID &RHS) { 119341825Sdim return !(LHS < RHS); 120341825Sdim } 121341825Sdim}; 122317027Sdim} // namespace lldb_private 123317027Sdim 124317027Sdim#endif // LLDB_UTILITY_UUID_H 125