From cf123da640b0a58284a24675646f66d70a94ae30 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Thu, 23 Jul 2020 17:54:31 +0200 Subject: BLI: Add MutableSpan.copy_from method --- source/blender/blenlib/BLI_span.hh | 11 +++++++++++ source/blender/blenlib/tests/BLI_span_test.cc | 13 +++++++++++++ 2 files changed, 24 insertions(+) (limited to 'source/blender') 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 @@ -608,6 +608,17 @@ template class MutableSpan { return counter; } + /** + * 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 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. */ 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 src = {5, 6, 7, 8}; + std::array 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 -- cgit v1.2.3