summaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2023-08-08 18:56:09 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2023-08-08 19:41:04 +0000
commitab9003889950efeb741199d3f7eba0d12c568ba4 (patch)
tree873e007e9caf45aad31b54f3b361b0862b585fc5 /vespalib
parent9b2f725bf7e92cacf73e1ef9c60e82e20990f9c8 (diff)
Add support for creating ConstArrayRef from std::array
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/src/tests/arrayref/arrayref_test.cpp20
-rw-r--r--vespalib/src/vespa/vespalib/util/arrayref.h5
2 files changed, 25 insertions, 0 deletions
diff --git a/vespalib/src/tests/arrayref/arrayref_test.cpp b/vespalib/src/tests/arrayref/arrayref_test.cpp
index bd8646b2f99..8c41d38b292 100644
--- a/vespalib/src/tests/arrayref/arrayref_test.cpp
+++ b/vespalib/src/tests/arrayref/arrayref_test.cpp
@@ -53,4 +53,24 @@ TEST("require that references can be unconstified") {
EXPECT_EQUAL(data[1], 5);
}
+TEST("require that std::array references can be constified") {
+ std::array<int,3> data({1,2,3});
+ const ArrayRef<int> array_ref(data);
+ ConstArrayRef<int> const_ref(array_ref);
+ EXPECT_EQUAL(const_ref.size(), 3u);
+ EXPECT_EQUAL(const_ref.end() - const_ref.begin(), 3);
+ EXPECT_EQUAL(const_ref[2], 3);
+}
+
+TEST("require that references can be unconstified") {
+ std::array<int, 3> data({1,2,3});
+ const ConstArrayRef<int> const_ref(data);
+ ArrayRef<int> array_ref = unconstify(const_ref);
+ EXPECT_EQUAL(array_ref.size(), 3u);
+ EXPECT_EQUAL(array_ref.end() - array_ref.begin(), 3);
+ EXPECT_EQUAL(array_ref[1], 2);
+ array_ref[1] = 5;
+ EXPECT_EQUAL(data[1], 5);
+}
+
TEST_MAIN() { TEST_RUN_ALL(); }
diff --git a/vespalib/src/vespa/vespalib/util/arrayref.h b/vespalib/src/vespa/vespalib/util/arrayref.h
index 319947e4cd9..9057f56fac0 100644
--- a/vespalib/src/vespa/vespalib/util/arrayref.h
+++ b/vespalib/src/vespa/vespalib/util/arrayref.h
@@ -3,6 +3,7 @@
#include <cstddef>
#include <vector>
+#include <array>
namespace vespalib {
@@ -17,6 +18,8 @@ public:
constexpr ArrayRef(T * v, size_t sz) noexcept : _v(v), _sz(sz) { }
template<typename A=std::allocator<T>>
ArrayRef(std::vector<T, A> & v) noexcept : _v(v.data()), _sz(v.size()) { }
+ template<size_t SZ>
+ ArrayRef(std::array<T, SZ> & v) noexcept : _v(v.data()), _sz(SZ) { }
T & operator [] (size_t i) noexcept { return _v[i]; }
const T & operator [] (size_t i) const noexcept { return _v[i]; }
T * data() noexcept { return _v; }
@@ -36,6 +39,8 @@ public:
constexpr ConstArrayRef(const T *v, size_t sz) noexcept : _v(v), _sz(sz) { }
template<typename A=std::allocator<T>>
ConstArrayRef(const std::vector<T, A> & v) noexcept : _v(v.data()), _sz(v.size()) { }
+ template<size_t SZ>
+ ConstArrayRef(const std::array<T, SZ> & v) noexcept : _v(v.data()), _sz(SZ) { }
ConstArrayRef(const ArrayRef<T> & v) noexcept : _v(v.data()), _sz(v.size()) { }
constexpr ConstArrayRef() noexcept : _v(nullptr), _sz(0) {}
const T & operator [] (size_t i) const noexcept { return _v[i]; }