[Bf-blender-cvs] [4adbf49d83e] functions-experimental-refactor: improve ArrayRef

Jacques Lucke noreply at git.blender.org
Tue Oct 22 12:51:58 CEST 2019


Commit: 4adbf49d83ea13a27553e7018d6997a45da23023
Author: Jacques Lucke
Date:   Tue Oct 22 11:47:00 2019 +0200
Branches: functions-experimental-refactor
https://developer.blender.org/rB4adbf49d83ea13a27553e7018d6997a45da23023

improve ArrayRef

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

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

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

diff --git a/source/blender/blenlib/BLI_array_ref.h b/source/blender/blenlib/BLI_array_ref.h
index 29e523875a0..0455ed0a7e6 100644
--- a/source/blender/blenlib/BLI_array_ref.h
+++ b/source/blender/blenlib/BLI_array_ref.h
@@ -78,6 +78,17 @@ template<typename T> class ArrayRef {
   {
   }
 
+  /**
+   * Construct ArrayRef<const T *> from ArrayRef<T *>.
+   */
+  template<typename U>
+  ArrayRef(
+      ArrayRef<U *> array,
+      typename std::enable_if<std::is_convertible<U *const *, T const *>::value>::type * = nullptr)
+      : ArrayRef(array.begin(), array.size())
+  {
+  }
+
   /**
    * Return a continuous part of the array.
    * Asserts that the slice stays within the array.
@@ -321,7 +332,7 @@ template<typename T> class MutableArrayRef {
   {
   }
 
-  operator ArrayRef<T>()
+  operator ArrayRef<T>() const
   {
     return ArrayRef<T>(m_start, m_size);
   }
diff --git a/tests/gtests/blenlib/BLI_array_ref_test.cc b/tests/gtests/blenlib/BLI_array_ref_test.cc
index 4507d9e6e84..d202ca66148 100644
--- a/tests/gtests/blenlib/BLI_array_ref_test.cc
+++ b/tests/gtests/blenlib/BLI_array_ref_test.cc
@@ -2,7 +2,8 @@
 #include "BLI_array_ref.h"
 #include "BLI_vector.h"
 
-using BLI::IndexRange;
+using namespace BLI;
+
 using IntVector = BLI::Vector<int>;
 using IntArrayRef = BLI::ArrayRef<int>;
 using MutableIntArrayRef = BLI::MutableArrayRef<int>;
@@ -17,6 +18,15 @@ TEST(array_ref, FromSmallVector)
   EXPECT_EQ(a_ref[2], 3);
 }
 
+TEST(array_ref, AddConstToPointer)
+{
+  int a = 0;
+  std::vector<int *> vec = {&a};
+  ArrayRef<int *> ref = vec;
+  ArrayRef<const int *> const_ref = ref;
+  EXPECT_EQ(const_ref.size(), 1);
+}
+
 TEST(array_ref, IsReferencing)
 {
   int array[] = {3, 5, 8};



More information about the Bf-blender-cvs mailing list