summaryrefslogtreecommitdiff
path: root/libs/binder/Utils.h
diff options
context:
space:
mode:
Diffstat (limited to 'libs/binder/Utils.h')
-rw-r--r--libs/binder/Utils.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/libs/binder/Utils.h b/libs/binder/Utils.h
index 7dcb70e6fd..37c1262c9b 100644
--- a/libs/binder/Utils.h
+++ b/libs/binder/Utils.h
@@ -60,6 +60,17 @@ struct Span {
size = offset;
return rest;
}
+
+ // Returns nullopt if the byte size of `this` isn't evenly divisible by sizeof(U).
+ template <typename U>
+ std::optional<Span<U>> reinterpret() const {
+ // Only allow casting from bytes for simplicity.
+ static_assert(std::is_same_v<std::remove_const_t<T>, uint8_t>);
+ if (size % sizeof(U) != 0) {
+ return std::nullopt;
+ }
+ return Span<U>{reinterpret_cast<U*>(data), size / sizeof(U)};
+ }
};
} // namespace android