summaryrefslogtreecommitdiff
path: root/asl
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2024-11-04 13:19:11 +0100
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2024-12-20 15:35:58 +0100
commit5682cb422c39eea6b4d5d54c2f31260785dc256f (patch)
treeebadbbb0a5af47f309766ccf63474f097c08743f /asl
parent6726a96f0cf3c230e9caa2abd40fcfbf03fe73a4 (diff)
Start work on span
Diffstat (limited to 'asl')
-rw-r--r--asl/BUILD.bazel2
-rw-r--r--asl/span.hpp44
-rw-r--r--asl/tests/span_tests.cpp28
3 files changed, 74 insertions, 0 deletions
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<is_object T, int64_t kSize = dynamic_size>
+class span
+{
+ static constexpr bool kIsDynamic = kSize < 0;
+
+ using SizeType = select_t<kIsDynamic, int64_t, empty>;
+
+ 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<T>; }
+
+ 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<asl::span<int>>);
+static_assert(asl::trivially_destructible<asl::span<HasDestructor>>);
+
+static_assert(asl::trivially_copyable<asl::span<int>>);
+static_assert(asl::trivially_copyable<asl::span<NonCopyConstructible>>);
+
+static_assert(asl::size_of<asl::span<int>> == asl::size_of<void*> * 2);
+static_assert(asl::size_of<asl::span<int, 2>> == asl::size_of<void*>);
+
+ASL_TEST(empty_dynamic)
+{
+ asl::span<int> 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<int, 0> s;
+ ASL_TEST_EXPECT(s.size() == 0);
+ ASL_TEST_EXPECT(s.size_bytes() == 0);
+ ASL_TEST_EXPECT(s.is_empty());
+}