Store UUIDs as 16 bytes of data
This makes them much more efficient to write over shared memory to solve
the boot UUID logging problem when we add them.
Change-Id: Idf361d6b096bfa52cbc98f555c90bf1f6b90d3e0
diff --git a/aos/events/logging/uuid.h b/aos/events/logging/uuid.h
index d7ec33a..1379369 100644
--- a/aos/events/logging/uuid.h
+++ b/aos/events/logging/uuid.h
@@ -2,45 +2,77 @@
#define AOS_EVENTS_LOGGING_UUID_H_
#include <array>
-#include <random>
-#include <string_view>
+#include <ostream>
+#include <string>
+
+#include "absl/types/span.h"
+#include "flatbuffers/flatbuffers.h"
namespace aos {
// Class to generate and hold a UUID.
class UUID {
public:
+ // Size of a UUID both as a string and the raw data.
+ static constexpr size_t kStringSize = 36;
+ static constexpr size_t kDataSize = 16;
+
// Returns a randomly generated UUID. This is known as a UUID4.
static UUID Random();
- // Returns a uuid with all '0' characters.
+ // Returns a uuid with all '0's.
static UUID Zero();
- static UUID FromString(std::string_view);
+ // Converts a string UUID of the form 00000000-0000-0000-0000-000000000000 to
+ // a UUID.
+ static UUID FromString(std::string_view string);
+ static UUID FromString(const flatbuffers::String *string);
+ // Converts a 16 byte vector (128 bits) to a UUID. This requires no
+ // transformation.
+ static UUID FromVector(const flatbuffers::Vector<uint8_t> *data);
+
+ // Returns the boot UUID for the current linux computer.
static UUID BootUUID();
- // Size of a UUID.
- static constexpr size_t kSize = 36;
+ // Default constructor which builds an uninitialized UUID. Use one of the
+ // static methods if you want something more useful.
+ UUID() {}
- std::string_view string_view() const {
- return std::string_view(data_.data(), data_.size());
+ // Packs this UUID into a flatbuffer as a string.
+ flatbuffers::Offset<flatbuffers::String> PackString(
+ flatbuffers::FlatBufferBuilder *fbb) const;
+ // Copies this UUID as a string into the memory pointed by result. Result
+ // must be at least kStringSize long.
+ void CopyTo(char *result) const;
+ // Returns this UUID as a string.
+ std::string ToString() const;
+
+ // Packs the UUID bytes directly into a vector.
+ flatbuffers::Offset<flatbuffers::Vector<uint8_t>> PackVector(
+ flatbuffers::FlatBufferBuilder *fbb) const;
+
+ // Returns the underlying UUID data.
+ absl::Span<const uint8_t> span() const {
+ return absl::Span<const uint8_t>(data_.data(), data_.size());
}
bool operator==(const UUID &other) const {
- return other.string_view() == string_view();
+ return other.span() == span();
}
bool operator!=(const UUID &other) const {
- return other.string_view() != string_view();
+ return other.span() != span();
}
private:
- UUID() {}
+ friend std::ostream &operator<<(std::ostream &os, const UUID &uuid);
- // Fixed size storage for the data. Non-null terminated.
- std::array<char, kSize> data_;
+ // Encoded storage for the data.
+ std::array<uint8_t, kDataSize> data_;
};
+std::ostream &operator<<(std::ostream &os, const UUID &uuid);
+
} // namespace aos
#endif // AOS_EVENTS_LOGGING_UUID_H_