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" |
| 9 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 10 | |
| 11 | namespace aos { |
| 12 | |
| 13 | // Class to generate and hold a UUID. |
| 14 | class UUID { |
| 15 | public: |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 16 | // Size of a UUID both as a string and the raw data. |
| 17 | static constexpr size_t kStringSize = 36; |
| 18 | static constexpr size_t kDataSize = 16; |
| 19 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 20 | // Returns a randomly generated UUID. This is known as a UUID4. |
| 21 | static UUID Random(); |
| 22 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 23 | // Returns a uuid with all '0's. |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 24 | static UUID Zero(); |
| 25 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 26 | // Converts a string UUID of the form 00000000-0000-0000-0000-000000000000 to |
| 27 | // a UUID. |
| 28 | static UUID FromString(std::string_view string); |
| 29 | static UUID FromString(const flatbuffers::String *string); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 30 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 31 | // Converts a 16 byte vector (128 bits) to a UUID. This requires no |
| 32 | // transformation. |
| 33 | static UUID FromVector(const flatbuffers::Vector<uint8_t> *data); |
| 34 | |
| 35 | // Returns the boot UUID for the current linux computer. |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 36 | static UUID BootUUID(); |
| 37 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 38 | // Default constructor which builds an uninitialized UUID. Use one of the |
| 39 | // static methods if you want something more useful. |
| 40 | UUID() {} |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 41 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 42 | // Packs this UUID into a flatbuffer as a string. |
| 43 | flatbuffers::Offset<flatbuffers::String> PackString( |
| 44 | flatbuffers::FlatBufferBuilder *fbb) const; |
| 45 | // Copies this UUID as a string into the memory pointed by result. Result |
| 46 | // must be at least kStringSize long. |
| 47 | void CopyTo(char *result) const; |
| 48 | // Returns this UUID as a string. |
| 49 | std::string ToString() const; |
| 50 | |
| 51 | // Packs the UUID bytes directly into a vector. |
| 52 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> PackVector( |
| 53 | flatbuffers::FlatBufferBuilder *fbb) const; |
| 54 | |
| 55 | // Returns the underlying UUID data. |
| 56 | absl::Span<const uint8_t> span() const { |
| 57 | return absl::Span<const uint8_t>(data_.data(), data_.size()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 60 | bool operator==(const UUID &other) const { return other.span() == span(); } |
| 61 | bool operator!=(const UUID &other) const { return other.span() != span(); } |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 62 | |
| 63 | private: |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 64 | friend std::ostream &operator<<(std::ostream &os, const UUID &uuid); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 65 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 66 | // Encoded storage for the data. |
| 67 | std::array<uint8_t, kDataSize> data_; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 70 | std::ostream &operator<<(std::ostream &os, const UUID &uuid); |
| 71 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 72 | } // namespace aos |
| 73 | |
| 74 | #endif // AOS_EVENTS_LOGGING_UUID_H_ |