[Bf-blender-cvs] [cf123da640b] master: BLI: Add MutableSpan.copy_from method

Jacques Lucke noreply at git.blender.org
Thu Jul 23 17:57:41 CEST 2020


Commit: cf123da640b0a58284a24675646f66d70a94ae30
Author: Jacques Lucke
Date:   Thu Jul 23 17:54:31 2020 +0200
Branches: master
https://developer.blender.org/rBcf123da640b0a58284a24675646f66d70a94ae30

BLI: Add MutableSpan.copy_from method

===================================================================

M	source/blender/blenlib/BLI_span.hh
M	source/blender/blenlib/tests/BLI_span_test.cc

===================================================================

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<typename T> 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<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.
    */
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



More information about the Bf-blender-cvs mailing list