Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGING_UUID_H_ |
| 2 | #define AOS_EVENTS_LOGGING_UUID_H_ |
| 3 | |
| 4 | #include <array> |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 5 | #include <ostream> |
| 6 | #include <string> |
| 7 | |
| 8 | #include "absl/types/span.h" |
Austin Schuh | 81d0fe4 | 2022-08-17 16:29:23 -0700 | [diff] [blame^] | 9 | #include "aos/thread_local.h" |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 10 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 11 | |
| 12 | namespace aos { |
| 13 | |
| 14 | // Class to generate and hold a UUID. |
| 15 | class UUID { |
| 16 | public: |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 17 | // Size of a UUID both as a string and the raw data. |
| 18 | static constexpr size_t kStringSize = 36; |
| 19 | static constexpr size_t kDataSize = 16; |
| 20 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 21 | // Returns a randomly generated UUID. This is known as a UUID4. |
| 22 | static UUID Random(); |
| 23 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 24 | // Returns a uuid with all '0's. |
Austin Schuh | 81d0fe4 | 2022-08-17 16:29:23 -0700 | [diff] [blame^] | 25 | static constexpr UUID Zero() { |
| 26 | UUID result; |
| 27 | std::memset(result.data_.data(), 0, result.data_.size()); |
| 28 | return result; |
| 29 | } |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 30 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 31 | // Converts a string UUID of the form 00000000-0000-0000-0000-000000000000 to |
| 32 | // a UUID. |
| 33 | static UUID FromString(std::string_view string); |
| 34 | static UUID FromString(const flatbuffers::String *string); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 35 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 36 | // Converts a 16 byte vector (128 bits) to a UUID. This requires no |
| 37 | // transformation. |
| 38 | static UUID FromVector(const flatbuffers::Vector<uint8_t> *data); |
| 39 | |
| 40 | // Returns the boot UUID for the current linux computer. |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 41 | static UUID BootUUID(); |
| 42 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 43 | // Default constructor which builds an uninitialized UUID. Use one of the |
| 44 | // static methods if you want something more useful. |
Austin Schuh | 81d0fe4 | 2022-08-17 16:29:23 -0700 | [diff] [blame^] | 45 | constexpr UUID() : data_() {} |
| 46 | constexpr UUID(const UUID &uuid) : data_(uuid.data_) {} |
| 47 | |
| 48 | void operator=(const UUID &other) { |
| 49 | memcpy(data_.data(), other.data_.data(), kDataSize); |
| 50 | } |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 51 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 52 | // Packs this UUID into a flatbuffer as a string. |
| 53 | flatbuffers::Offset<flatbuffers::String> PackString( |
| 54 | flatbuffers::FlatBufferBuilder *fbb) const; |
| 55 | // Copies this UUID as a string into the memory pointed by result. Result |
| 56 | // must be at least kStringSize long. |
| 57 | void CopyTo(char *result) const; |
| 58 | // Returns this UUID as a string. |
| 59 | std::string ToString() const; |
| 60 | |
| 61 | // Packs the UUID bytes directly into a vector. |
| 62 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> PackVector( |
| 63 | flatbuffers::FlatBufferBuilder *fbb) const; |
| 64 | |
Austin Schuh | 81d0fe4 | 2022-08-17 16:29:23 -0700 | [diff] [blame^] | 65 | // Returns a human-readable string representing this UUID. |
| 66 | // |
| 67 | // This is done without any memory allocation, which means it's returned in a |
| 68 | // thread-local buffer. |
| 69 | // |
| 70 | // Be careful using this. It's mostly useful for low-level tracing of UUIDs |
| 71 | // through the system. |
| 72 | const char *thread_local_string() const { |
| 73 | AOS_THREAD_LOCAL char buffer[kStringSize + 1]; |
| 74 | CopyTo(buffer); |
| 75 | return buffer; |
| 76 | } |
| 77 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 78 | // Returns the underlying UUID data. |
| 79 | absl::Span<const uint8_t> span() const { |
| 80 | return absl::Span<const uint8_t>(data_.data(), data_.size()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 83 | bool operator==(const UUID &other) const { return other.span() == span(); } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 84 | bool operator<(const UUID &other) const { return other.span() < span(); } |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 85 | bool operator!=(const UUID &other) const { return other.span() != span(); } |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 86 | |
| 87 | private: |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 88 | friend std::ostream &operator<<(std::ostream &os, const UUID &uuid); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 89 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 90 | // Encoded storage for the data. |
| 91 | std::array<uint8_t, kDataSize> data_; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 94 | std::ostream &operator<<(std::ostream &os, const UUID &uuid); |
| 95 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 96 | } // namespace aos |
| 97 | |
| 98 | #endif // AOS_EVENTS_LOGGING_UUID_H_ |