Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2020-07-23 18:54:31 +0300
committerJacques Lucke <jacques@blender.org>2020-07-23 18:54:31 +0300
commitcf123da640b0a58284a24675646f66d70a94ae30 (patch)
tree9f9063b6545d48642551bd7410f29b2d0b869a01
parent4619562e2f4c7f7f441212f0438c439a98aa4a71 (diff)
BLI: Add MutableSpan.copy_from method
-rw-r--r--source/blender/blenlib/BLI_span.hh11
-rw-r--r--source/blender/blenlib/tests/BLI_span_test.cc13
2 files changed, 24 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_span.hh b/source/blender/blenlib/BLI_span.hh
index 2d875fe73be..39259e9ee18 100644
--- a/source/blender/blenlib/BLI_span.hh
+++ b/source/blender/blenlib/BLI_span.hh
@@ -609,6 +609,17 @@ template<typename T> class MutableSpan {
}
/**
+ * Copy all values from another span into this span. This invokes undefined behavior when the
+ * destination contains uninitialized data and T is not trivially copy constructible.
+ * The size of both spans is expected to be the same.
+ */
+ void copy_from(Span<T> values)
+ {
+ BLI_assert(size_ == values.size());
+ initialized_copy_n(values.data(), size_, start_);
+ }
+
+ /**
* Returns a new span to the same underlying memory buffer. No conversions are done.
*/
template<typename NewT> MutableSpan<NewT> cast() const
diff --git a/source/blender/blenlib/tests/BLI_span_test.cc b/source/blender/blenlib/tests/BLI_span_test.cc
index d8b68eb6a33..587497624f4 100644
--- a/source/blender/blenlib/tests/BLI_span_test.cc
+++ b/source/blender/blenlib/tests/BLI_span_test.cc
@@ -295,4 +295,17 @@ TEST(span, VoidPointerSpan)
func1({&a, &b, &c});
}
+TEST(span, CopyFrom)
+{
+ std::array<int, 4> src = {5, 6, 7, 8};
+ std::array<int, 4> dst = {1, 2, 3, 4};
+
+ EXPECT_EQ(dst[2], 3);
+ MutableSpan(dst).copy_from(src);
+ EXPECT_EQ(dst[0], 5);
+ EXPECT_EQ(dst[1], 6);
+ EXPECT_EQ(dst[2], 7);
+ EXPECT_EQ(dst[3], 8);
+}
+
} // namespace blender::tests