blob: 3f7fc50319d770208b7e6106e284fbf58dae673d [file] [log] [blame]
Austin Schuh64fab802020-09-09 22:47:47 -07001#ifndef AOS_EVENTS_LOGGING_UUID_H_
2#define AOS_EVENTS_LOGGING_UUID_H_
3
4#include <array>
Austin Schuh5e2bfb82021-03-13 22:46:55 -08005#include <ostream>
6#include <string>
7
8#include "absl/types/span.h"
9#include "flatbuffers/flatbuffers.h"
Austin Schuh64fab802020-09-09 22:47:47 -070010
11namespace aos {
12
13// Class to generate and hold a UUID.
14class UUID {
15 public:
Austin Schuh5e2bfb82021-03-13 22:46:55 -080016 // 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 Schuh64fab802020-09-09 22:47:47 -070020 // Returns a randomly generated UUID. This is known as a UUID4.
21 static UUID Random();
22
Austin Schuh5e2bfb82021-03-13 22:46:55 -080023 // Returns a uuid with all '0's.
Brian Silverman1f345222020-09-24 21:14:48 -070024 static UUID Zero();
25
Austin Schuh5e2bfb82021-03-13 22:46:55 -080026 // 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 Schuh20ac95d2020-12-05 17:24:19 -080030
Austin Schuh5e2bfb82021-03-13 22:46:55 -080031 // 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 Schuh20ac95d2020-12-05 17:24:19 -080036 static UUID BootUUID();
37
Austin Schuh5e2bfb82021-03-13 22:46:55 -080038 // Default constructor which builds an uninitialized UUID. Use one of the
39 // static methods if you want something more useful.
40 UUID() {}
Austin Schuh20ac95d2020-12-05 17:24:19 -080041
Austin Schuh5e2bfb82021-03-13 22:46:55 -080042 // 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 Schuh64fab802020-09-09 22:47:47 -070058 }
59
Austin Schuh4385b142021-03-14 21:31:13 -070060 bool operator==(const UUID &other) const { return other.span() == span(); }
Austin Schuh58646e22021-08-23 23:51:46 -070061 bool operator<(const UUID &other) const { return other.span() < span(); }
Austin Schuh4385b142021-03-14 21:31:13 -070062 bool operator!=(const UUID &other) const { return other.span() != span(); }
Austin Schuh64fab802020-09-09 22:47:47 -070063
64 private:
Austin Schuh5e2bfb82021-03-13 22:46:55 -080065 friend std::ostream &operator<<(std::ostream &os, const UUID &uuid);
Austin Schuh64fab802020-09-09 22:47:47 -070066
Austin Schuh5e2bfb82021-03-13 22:46:55 -080067 // Encoded storage for the data.
68 std::array<uint8_t, kDataSize> data_;
Austin Schuh64fab802020-09-09 22:47:47 -070069};
70
Austin Schuh5e2bfb82021-03-13 22:46:55 -080071std::ostream &operator<<(std::ostream &os, const UUID &uuid);
72
Austin Schuh64fab802020-09-09 22:47:47 -070073} // namespace aos
74
75#endif // AOS_EVENTS_LOGGING_UUID_H_