summaryrefslogtreecommitdiff
path: root/AK/Span.h
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2023-04-02 13:00:05 -0400
committerAndreas Kling <kling@serenityos.org>2023-04-03 15:53:49 +0200
commit5c045b693427f35b13b40f586fa63bd1d08e06ed (patch)
tree3212c35c48fa4ccc0f53d1cd313b83cf33d59b55 /AK/Span.h
parentf7960ffbe3b3d686afb0982608818c5341fe057b (diff)
downloadserenity-5c045b693427f35b13b40f586fa63bd1d08e06ed.zip
AK: Add templated Span<u8> and Span<u8 const> constructors for C-arrays
This helper constructor exists on the unspecialized Span<T> class also, and is convenient for e.g. creating Bytes from: u8 buffer[64]; Bytes bytes { buffer };
Diffstat (limited to 'AK/Span.h')
-rw-r--r--AK/Span.h17
1 files changed, 17 insertions, 0 deletions
diff --git a/AK/Span.h b/AK/Span.h
index 4399dcc136..6c77ce04f4 100644
--- a/AK/Span.h
+++ b/AK/Span.h
@@ -64,12 +64,20 @@ public:
, m_size(size)
{
}
+
ALWAYS_INLINE Span(void* values, size_t size)
: m_values(reinterpret_cast<u8*>(values))
, m_size(size)
{
}
+ template<size_t size>
+ ALWAYS_INLINE constexpr Span(u8 (&values)[size])
+ : m_values(values)
+ , m_size(size)
+ {
+ }
+
protected:
u8* m_values { nullptr };
size_t m_size { 0 };
@@ -85,17 +93,26 @@ public:
, m_size(size)
{
}
+
ALWAYS_INLINE Span(void const* values, size_t size)
: m_values(reinterpret_cast<u8 const*>(values))
, m_size(size)
{
}
+
ALWAYS_INLINE Span(char const* values, size_t size)
: m_values(reinterpret_cast<u8 const*>(values))
, m_size(size)
{
}
+ template<size_t size>
+ ALWAYS_INLINE constexpr Span(u8 const (&values)[size])
+ : m_values(values)
+ , m_size(size)
+ {
+ }
+
protected:
u8 const* m_values { nullptr };
size_t m_size { 0 };