xref: /freebsd/contrib/llvm-project/lldb/include/lldb/Utility/UUID.h (revision 9e5787d2284e187abb5b654d924394a65772e004)
1 //===-- UUID.h --------------------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_UTILITY_UUID_H
10 #define LLDB_UTILITY_UUID_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/StringRef.h"
14 #include <stddef.h>
15 #include <stdint.h>
16 #include <string>
17 
18 namespace lldb_private {
19 
20   class Stream;
21 
22 class UUID {
23 public:
24   UUID() = default;
25 
26   /// Creates a UUID from the data pointed to by the bytes argument. No special
27   /// significance is attached to any of the values.
28   static UUID fromData(const void *bytes, uint32_t num_bytes) {
29     if (bytes)
30       return fromData({reinterpret_cast<const uint8_t *>(bytes), num_bytes});
31     return UUID();
32   }
33 
34   /// Creates a uuid from the data pointed to by the bytes argument. No special
35   /// significance is attached to any of the values.
36   static UUID fromData(llvm::ArrayRef<uint8_t> bytes) { return UUID(bytes); }
37 
38   /// Creates a UUID from the data pointed to by the bytes argument. Data
39   /// consisting purely of zero bytes is treated as an invalid UUID.
40   static UUID fromOptionalData(const void *bytes, uint32_t num_bytes) {
41     if (bytes)
42       return fromOptionalData(
43           {reinterpret_cast<const uint8_t *>(bytes), num_bytes});
44     return UUID();
45   }
46 
47   /// Creates a UUID from the data pointed to by the bytes argument. Data
48   /// consisting purely of zero bytes is treated as an invalid UUID.
49   static UUID fromOptionalData(llvm::ArrayRef<uint8_t> bytes) {
50     if (llvm::all_of(bytes, [](uint8_t b) { return b == 0; }))
51       return UUID();
52     return UUID(bytes);
53   }
54 
55   void Clear() { m_bytes.clear(); }
56 
57   void Dump(Stream *s) const;
58 
59   llvm::ArrayRef<uint8_t> GetBytes() const { return m_bytes; }
60 
61   explicit operator bool() const { return IsValid(); }
62   bool IsValid() const { return !m_bytes.empty(); }
63 
64   std::string GetAsString(llvm::StringRef separator = "-") const;
65 
66   bool SetFromStringRef(llvm::StringRef str);
67 
68   // Same as SetFromStringRef, but if the resultant UUID is all 0 bytes, set the
69   // UUID to invalid.
70   bool SetFromOptionalStringRef(llvm::StringRef str);
71 
72   /// Decode as many UUID bytes as possible from the C string \a cstr.
73   ///
74   /// \param[in] str
75   ///     An llvm::StringRef that points at a UUID string value (no leading
76   ///     spaces). The string must contain only hex characters and optionally
77   ///     can contain the '-' sepearators.
78   ///
79   /// \param[in] uuid_bytes
80   ///     A buffer of bytes that will contain a full or partially decoded UUID.
81   ///
82   /// \return
83   ///     The original string, with all decoded bytes removed.
84   static llvm::StringRef
85   DecodeUUIDBytesFromString(llvm::StringRef str,
86                             llvm::SmallVectorImpl<uint8_t> &uuid_bytes);
87 
88 private:
89   UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes.begin(), bytes.end()) {}
90 
91   // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations
92   // for this case.
93   llvm::SmallVector<uint8_t, 20> m_bytes;
94 
95   friend bool operator==(const UUID &LHS, const UUID &RHS) {
96     return LHS.m_bytes == RHS.m_bytes;
97   }
98   friend bool operator!=(const UUID &LHS, const UUID &RHS) {
99     return !(LHS == RHS);
100   }
101   friend bool operator<(const UUID &LHS, const UUID &RHS) {
102     return LHS.m_bytes < RHS.m_bytes;
103   }
104   friend bool operator<=(const UUID &LHS, const UUID &RHS) {
105     return !(RHS < LHS);
106   }
107   friend bool operator>(const UUID &LHS, const UUID &RHS) { return RHS < LHS; }
108   friend bool operator>=(const UUID &LHS, const UUID &RHS) {
109     return !(LHS < RHS);
110   }
111 };
112 } // namespace lldb_private
113 
114 #endif // LLDB_UTILITY_UUID_H
115