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> |
| 5 | #include <random> |
| 6 | #include <string_view> |
| 7 | |
| 8 | namespace aos { |
| 9 | |
| 10 | // Class to generate and hold a UUID. |
| 11 | class UUID { |
| 12 | public: |
| 13 | // Returns a randomly generated UUID. This is known as a UUID4. |
| 14 | static UUID Random(); |
| 15 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 16 | // Returns a uuid with all '0' characters. |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 17 | static UUID Zero(); |
| 18 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 19 | static UUID FromString(std::string_view); |
| 20 | |
| 21 | static UUID BootUUID(); |
| 22 | |
| 23 | // Size of a UUID. |
| 24 | static constexpr size_t kSize = 36; |
| 25 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 26 | std::string_view string_view() const { |
| 27 | return std::string_view(data_.data(), data_.size()); |
| 28 | } |
| 29 | |
| 30 | bool operator==(const UUID &other) const { |
| 31 | return other.string_view() == string_view(); |
| 32 | } |
| 33 | bool operator!=(const UUID &other) const { |
| 34 | return other.string_view() != string_view(); |
| 35 | } |
| 36 | |
| 37 | private: |
| 38 | UUID() {} |
| 39 | |
| 40 | // Fixed size storage for the data. Non-null terminated. |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 41 | std::array<char, kSize> data_; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | } // namespace aos |
| 45 | |
| 46 | #endif // AOS_EVENTS_LOGGING_UUID_H_ |