From 5682cb422c39eea6b4d5d54c2f31260785dc256f Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Mon, 4 Nov 2024 13:19:11 +0100 Subject: Start work on span --- asl/BUILD.bazel | 2 ++ asl/span.hpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ asl/tests/span_tests.cpp | 28 ++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 asl/span.hpp create mode 100644 asl/tests/span_tests.cpp (limited to 'asl') diff --git a/asl/BUILD.bazel b/asl/BUILD.bazel index f0a9c38..d8810dd 100644 --- a/asl/BUILD.bazel +++ b/asl/BUILD.bazel @@ -14,6 +14,7 @@ cc_library( "meta.hpp", "option.hpp", "print.hpp", + "span.hpp", "utility.hpp", ], srcs = [ @@ -40,5 +41,6 @@ cc_library( "maybe_uninit", "meta", "option", + "span", "utility", ]] diff --git a/asl/span.hpp b/asl/span.hpp new file mode 100644 index 0000000..f3d4036 --- /dev/null +++ b/asl/span.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include "asl/meta.hpp" +#include "asl/annotations.hpp" +#include "asl/layout.hpp" + +namespace asl +{ + +static constexpr int64_t dynamic_size = -1; + +template +class span +{ + static constexpr bool kIsDynamic = kSize < 0; + + using SizeType = select_t; + + T* m_data{}; + ASL_NO_UNIQUE_ADDRESS SizeType m_size{}; + +public: + constexpr span() = default; + + constexpr span(const span&) = default; + constexpr span(span&&) = default; + + constexpr span& operator=(const span&) = default; + constexpr span& operator=(span&&) = default; + + ~span() = default; + + constexpr int64_t size() const + { + if constexpr (kIsDynamic) { return m_size; } + else { return kSize; } + } + + constexpr int64_t size_bytes() const { return size() * size_of; } + + constexpr bool is_empty() const { return size() == 0; } +}; + +} // namespace asl diff --git a/asl/tests/span_tests.cpp b/asl/tests/span_tests.cpp new file mode 100644 index 0000000..276d50a --- /dev/null +++ b/asl/tests/span_tests.cpp @@ -0,0 +1,28 @@ +#include "asl/span.hpp" +#include "asl/testing/testing.hpp" +#include "asl/tests/test_types.hpp" + +static_assert(asl::trivially_destructible>); +static_assert(asl::trivially_destructible>); + +static_assert(asl::trivially_copyable>); +static_assert(asl::trivially_copyable>); + +static_assert(asl::size_of> == asl::size_of * 2); +static_assert(asl::size_of> == asl::size_of); + +ASL_TEST(empty_dynamic) +{ + asl::span s; + ASL_TEST_EXPECT(s.size() == 0); + ASL_TEST_EXPECT(s.size_bytes() == 0); + ASL_TEST_EXPECT(s.is_empty()); +} + +ASL_TEST(empty_static) +{ + asl::span s; + ASL_TEST_EXPECT(s.size() == 0); + ASL_TEST_EXPECT(s.size_bytes() == 0); + ASL_TEST_EXPECT(s.is_empty()); +} -- cgit