[Bf-blender-cvs] [e59d0263ba8] functions: create ArrayRef from std::vector and std::array

Jacques Lucke noreply at git.blender.org
Wed May 22 17:32:42 CEST 2019


Commit: e59d0263ba8778d6a27ea14f3048537c0255eea9
Author: Jacques Lucke
Date:   Wed May 22 17:11:09 2019 +0200
Branches: functions
https://developer.blender.org/rBe59d0263ba8778d6a27ea14f3048537c0255eea9

create ArrayRef from std::vector and std::array

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

M	source/blender/blenlib/BLI_array_ref.hpp
M	tests/gtests/blenlib/BLI_array_ref_test.cc

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

diff --git a/source/blender/blenlib/BLI_array_ref.hpp b/source/blender/blenlib/BLI_array_ref.hpp
index 9b8e1168c15..cfa0e298186 100644
--- a/source/blender/blenlib/BLI_array_ref.hpp
+++ b/source/blender/blenlib/BLI_array_ref.hpp
@@ -7,6 +7,8 @@
  */
 
 #include "BLI_small_vector.hpp"
+#include <vector>
+#include <array>
 
 namespace BLI {
 
@@ -26,16 +28,28 @@ template<typename T> class ArrayRef {
   {
   }
 
+  ArrayRef(const T *start, uint size) : m_start((T *)start), m_size(size)
+  {
+  }
+
   template<uint N>
-  ArrayRef(const SmallVector<T, N> &vector) : m_start(vector.begin()), m_size(vector.size())
+  ArrayRef(const SmallVector<T, N> &vector) : ArrayRef(vector.begin(), vector.size())
+  {
+  }
+
+  ArrayRef(const SmallVector<T> &vector) : ArrayRef(vector.begin(), vector.size())
+  {
+  }
+
+  ArrayRef(const std::initializer_list<T> &list) : ArrayRef((T *)list.begin(), list.size())
   {
   }
 
-  ArrayRef(const SmallVector<T> &vector) : m_start(vector.begin()), m_size(vector.size())
+  ArrayRef(const std::vector<T> &vector) : ArrayRef(vector.data(), vector.size())
   {
   }
 
-  ArrayRef(const std::initializer_list<T> &list) : m_start((T *)list.begin()), m_size(list.size())
+  template<std::size_t N> ArrayRef(const std::array<T, N> &array) : ArrayRef(array.data(), N)
   {
   }
 
diff --git a/tests/gtests/blenlib/BLI_array_ref_test.cc b/tests/gtests/blenlib/BLI_array_ref_test.cc
index 84d743e5180..c9a764ee9f6 100644
--- a/tests/gtests/blenlib/BLI_array_ref_test.cc
+++ b/tests/gtests/blenlib/BLI_array_ref_test.cc
@@ -127,3 +127,23 @@ TEST(array_ref, FromSingleValue)
   a = 10;
   EXPECT_EQ(a_ref[0], 10);
 }
+
+TEST(array_ref, FromVector)
+{
+  std::vector<int> a = {1, 2, 3, 4};
+  IntArrayRef a_ref(a);
+  EXPECT_EQ(a_ref.size(), 4);
+  EXPECT_EQ(a_ref[0], 1);
+  EXPECT_EQ(a_ref[1], 2);
+  EXPECT_EQ(a_ref[2], 3);
+  EXPECT_EQ(a_ref[3], 4);
+}
+
+TEST(array_ref, FromArray)
+{
+  std::array<int, 2> a = {5, 6};
+  IntArrayRef a_ref(a);
+  EXPECT_EQ(a_ref.size(), 2);
+  EXPECT_EQ(a_ref[0], 5);
+  EXPECT_EQ(a_ref[1], 6);
+}



More information about the Bf-blender-cvs mailing list