Add a Span constructor to SizePrefixedFlatbufferVector
This lets us copy without duplicating the copy code all over the place.
Change-Id: I17e8746ad1186c833a82167fee06efa4c0ffad42
Signed-off-by: Austin Schuh <austin.schuh@bluerivertech.com>
diff --git a/aos/events/logging/logfile_utils.cc b/aos/events/logging/logfile_utils.cc
index f32b337..5f0e372 100644
--- a/aos/events/logging/logfile_utils.cc
+++ b/aos/events/logging/logfile_utils.cc
@@ -411,10 +411,7 @@
}
// And copy the config so we have it forever, removing the size prefix.
- ResizeableBuffer data;
- data.resize(config_data.size());
- memcpy(data.data(), config_data.begin(), data.size());
- SizePrefixedFlatbufferVector<LogFileHeader> result(std::move(data));
+ SizePrefixedFlatbufferVector<LogFileHeader> result(config_data);
if (!result.Verify()) {
return std::nullopt;
}
@@ -435,10 +432,7 @@
}
// And copy the config so we have it forever, removing the size prefix.
- ResizeableBuffer data;
- data.resize(data_span.size());
- memcpy(data.data(), data_span.begin(), data.size());
- SizePrefixedFlatbufferVector<MessageHeader> result(std::move(data));
+ SizePrefixedFlatbufferVector<MessageHeader> result(data_span);
if (!result.Verify()) {
return std::nullopt;
}
@@ -480,10 +474,7 @@
return std::nullopt;
}
- ResizeableBuffer result_buffer;
- result_buffer.resize(msg_data.size());
- memcpy(result_buffer.data(), msg_data.begin(), result_buffer.size());
- SizePrefixedFlatbufferVector<MessageHeader> result(std::move(result_buffer));
+ SizePrefixedFlatbufferVector<MessageHeader> result(msg_data);
const monotonic_clock::time_point timestamp = monotonic_clock::time_point(
chrono::nanoseconds(result.message().monotonic_sent_time()));
diff --git a/aos/flatbuffers.h b/aos/flatbuffers.h
index 5bef204..90742c3 100644
--- a/aos/flatbuffers.h
+++ b/aos/flatbuffers.h
@@ -413,9 +413,13 @@
: data_(std::move(data)) {}
// Builds a Flatbuffer by copying the data from the other flatbuffer.
- SizePrefixedFlatbufferVector(const SizePrefixedFlatbuffer<T> &other) {
- data_.resize(other.span().size());
- memcpy(data_.data(), other.span().data(), data_.size());
+ SizePrefixedFlatbufferVector(const SizePrefixedFlatbuffer<T> &other)
+ : SizePrefixedFlatbufferVector(other.span()) {}
+
+ // Builds a flatbuffer by copying the data from the provided span.
+ SizePrefixedFlatbufferVector(const absl::Span<const uint8_t> span) {
+ data_.resize(span.size());
+ memcpy(data_.data(), span.data(), data_.size());
}
// Copy constructor.