[Bf-blender-cvs] [fda6430d6af] functions: improve const correctness in many places

Jacques Lucke noreply at git.blender.org
Wed Aug 21 17:39:06 CEST 2019


Commit: fda6430d6af754ebbea7a07424642d5c70f57b5b
Author: Jacques Lucke
Date:   Wed Aug 21 17:38:54 2019 +0200
Branches: functions
https://developer.blender.org/rBfda6430d6af754ebbea7a07424642d5c70f57b5b

improve const correctness in many places

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

M	source/blender/blenkernel/BKE_node_tree.hpp
M	source/blender/blenlib/BLI_array.hpp
M	source/blender/blenlib/BLI_array_ref.hpp
M	source/blender/blenlib/BLI_map.hpp
M	source/blender/blenlib/BLI_monotonic_allocator.hpp
M	source/blender/blenlib/BLI_multi_map.hpp
M	source/blender/blenlib/BLI_refcount.hpp
M	source/blender/blenlib/BLI_set_vector.hpp
M	source/blender/blenlib/BLI_stack.hpp
M	source/blender/blenlib/BLI_task.hpp
M	source/blender/blenlib/BLI_vector.hpp
M	source/blender/functions/backends/cpp/list.hpp
M	source/blender/functions/backends/llvm/build_ir_body.hpp
M	source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp
M	source/blender/functions/core/data_graph.hpp
M	source/blender/functions/core/data_graph_builder.cpp
M	source/blender/functions/core/function.hpp
M	source/blender/functions/frontends/data_flow_nodes/graph_generation.hpp
M	source/blender/functions/frontends/data_flow_nodes/unlinked_input_groupers.cpp
M	source/blender/functions/frontends/data_flow_nodes/unlinked_input_inserters.cpp
M	source/blender/functions/frontends/data_flow_nodes/unlinked_input_inserters.hpp
M	source/blender/functions/functions/array_execution.cpp
M	source/blender/functions/functions/array_execution.hpp
M	source/blender/functions/functions/auto_vectorization.cpp
M	source/blender/functions/functions/object_input.cpp
M	source/blender/simulations/bparticles/attributes.hpp
M	source/blender/simulations/bparticles/c_wrapper.cpp
M	source/blender/simulations/bparticles/force_interface.hpp
M	source/blender/simulations/bparticles/forces.cpp
M	source/blender/simulations/bparticles/integrator.cpp
M	source/blender/simulations/bparticles/integrator.hpp
M	source/blender/simulations/bparticles/particle_allocator.cpp
M	source/blender/simulations/bparticles/particle_function_builder.cpp
M	source/blender/simulations/bparticles/particle_set.hpp
M	source/blender/simulations/bparticles/particles_container.cpp
M	source/blender/simulations/bparticles/particles_container.hpp
M	source/blender/simulations/bparticles/simulate.cpp
M	source/blender/simulations/bparticles/step_description_interfaces.hpp
M	tests/gtests/blenlib/BLI_array_ref_test.cc

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

diff --git a/source/blender/blenkernel/BKE_node_tree.hpp b/source/blender/blenkernel/BKE_node_tree.hpp
index 3892d31e480..e13da37887b 100644
--- a/source/blender/blenkernel/BKE_node_tree.hpp
+++ b/source/blender/blenkernel/BKE_node_tree.hpp
@@ -19,6 +19,7 @@ using BLI::IntrusiveListBaseWrapper;
 using BLI::Map;
 using BLI::MonotonicAllocator;
 using BLI::MultiMap;
+using BLI::MutableArrayRef;
 using BLI::StringRef;
 using BLI::StringRefNull;
 using BLI::Vector;
@@ -94,8 +95,8 @@ class VirtualNode {
   VirtualNodeTree *m_backlink;
   bNodeTree *m_btree;
   bNode *m_bnode;
-  ArrayRef<VirtualSocket *> m_inputs;
-  ArrayRef<VirtualSocket *> m_outputs;
+  MutableArrayRef<VirtualSocket *> m_inputs;
+  MutableArrayRef<VirtualSocket *> m_outputs;
 
  public:
   ArrayRef<VirtualSocket *> inputs()
@@ -160,8 +161,8 @@ class VirtualSocket {
   bNodeSocket *m_bsocket;
   uint m_id;
 
-  ArrayRef<VirtualSocket *> m_direct_links;
-  ArrayRef<VirtualSocket *> m_links;
+  MutableArrayRef<VirtualSocket *> m_direct_links;
+  MutableArrayRef<VirtualSocket *> m_links;
 
  public:
   bool is_input() const
diff --git a/source/blender/blenlib/BLI_array.hpp b/source/blender/blenlib/BLI_array.hpp
index 2e299de76dd..4bd9b0e900f 100644
--- a/source/blender/blenlib/BLI_array.hpp
+++ b/source/blender/blenlib/BLI_array.hpp
@@ -90,6 +90,11 @@ template<typename T, typename Allocator = GuardedAllocator> class Array {
     return ArrayRef<T>(m_data, m_size);
   }
 
+  operator MutableArrayRef<T>()
+  {
+    return MutableArrayRef<T>(m_data, m_size);
+  }
+
   T &operator[](uint index)
   {
     BLI_assert(index < m_size);
@@ -103,12 +108,12 @@ template<typename T, typename Allocator = GuardedAllocator> class Array {
 
   void fill(const T &value)
   {
-    ArrayRef<T>(*this).fill(value);
+    MutableArrayRef<T>(*this).fill(value);
   }
 
   void fill_indices(ArrayRef<uint> indices, const T &value)
   {
-    ArrayRef<T>(*this).fill_indices(indices, value);
+    MutableArrayRef<T>(*this).fill_indices(indices, value);
   }
 
   T *begin()
diff --git a/source/blender/blenlib/BLI_array_ref.hpp b/source/blender/blenlib/BLI_array_ref.hpp
index 5ac73ca124b..fd8f6468f7b 100644
--- a/source/blender/blenlib/BLI_array_ref.hpp
+++ b/source/blender/blenlib/BLI_array_ref.hpp
@@ -40,7 +40,7 @@ namespace BLI {
 
 template<typename T> class ArrayRef {
  private:
-  T *m_start = nullptr;
+  const T *m_start = nullptr;
   uint m_size = 0;
 
  public:
@@ -50,15 +50,11 @@ template<typename T> class ArrayRef {
    */
   ArrayRef() = default;
 
-  ArrayRef(T *start, uint size) : m_start(start), m_size(size)
+  ArrayRef(const T *start, uint size) : m_start(start), m_size(size)
   {
   }
 
-  ArrayRef(const T *start, uint size) : m_start((T *)start), m_size(size)
-  {
-  }
-
-  ArrayRef(const std::initializer_list<T> &list) : ArrayRef((T *)list.begin(), list.size())
+  ArrayRef(const std::initializer_list<T> &list) : ArrayRef(list.begin(), list.size())
   {
   }
 
@@ -116,62 +112,35 @@ template<typename T> class ArrayRef {
     return this->slice(this->size() - n, n);
   }
 
-  /**
-   * Replace all elements in the referenced array with the given value.
-   */
-  void fill(const T &element)
-  {
-    std::fill_n(m_start, m_size, element);
-  }
-
-  /**
-   * Replace a subset of all elements with the given value.
-   */
-  void fill_indices(ArrayRef<uint> indices, const T &element)
-  {
-    for (uint i : indices) {
-      m_start[i] = element;
-    }
-  }
-
-  /**
-   * Copy the values from another array into the references array.
-   */
-  void copy_from(const T *ptr)
-  {
-    BLI::copy_n(ptr, m_size, m_start);
-  }
-
-  void copy_from(ArrayRef<T> other)
-  {
-    BLI_assert(this->size() == other.size());
-    this->copy_from(other.begin());
-  }
-
   /**
    * Copy the values in this array to another array.
    */
-  void copy_to(T *ptr)
+  void copy_to(T *ptr) const
   {
     BLI::copy_n(m_start, m_size, ptr);
   }
 
-  T *begin() const
+  const T *begin() const
   {
     return m_start;
   }
 
-  T *end() const
+  const T *end() const
   {
     return m_start + m_size;
   }
 
-  T &operator[](uint index) const
+  const T &operator[](uint index) const
   {
     BLI_assert(index < m_size);
     return m_start[index];
   }
 
+  const T *data() const
+  {
+    return m_start;
+  }
+
   /**
    * Return the number of elements in the referenced array.
    */
@@ -192,9 +161,9 @@ template<typename T> class ArrayRef {
    * Does a linear search to see of the value is in the array.
    * Return true if it is, otherwise false.
    */
-  bool contains(const T &value)
+  bool contains(const T &value) const
   {
-    for (T &element : *this) {
+    for (const T &element : *this) {
       if (element == value) {
         return true;
       }
@@ -206,7 +175,7 @@ template<typename T> class ArrayRef {
    * Does a constant time check to see if the pointer is within the referenced array.
    * Return true if it is, otherwise false.
    */
-  bool contains_ptr(const T *ptr)
+  bool contains_ptr(const T *ptr) const
   {
     return (this->begin() <= ptr) && (ptr < this->end());
   }
@@ -215,10 +184,10 @@ template<typename T> class ArrayRef {
    * Does a linear search to count how often the value is in the array.
    * Returns the number of occurences.
    */
-  uint count(const T &value)
+  uint count(const T &value) const
   {
     uint counter = 0;
-    for (T &element : *this) {
+    for (const T &element : *this) {
       if (element == value) {
         counter++;
       }
@@ -230,7 +199,7 @@ template<typename T> class ArrayRef {
    * Return a reference to the first element in the array.
    * Asserts when the array is empty.
    */
-  T &first()
+  const T &first() const
   {
     BLI_assert(m_size > 0);
     return m_start[0];
@@ -240,7 +209,7 @@ template<typename T> class ArrayRef {
    * Return a reference to the last elemeent in the array.
    * Asserts when the array is empty.
    */
-  T &last()
+  const T &last() const
   {
     BLI_assert(m_size > 0);
     return m_start[m_size - 1];
@@ -283,6 +252,141 @@ template<typename T> class ArrayRef {
   }
 };
 
+template<typename T> class MutableArrayRef {
+ private:
+  T *m_start;
+  uint m_size;
+
+ public:
+  MutableArrayRef() = default;
+
+  MutableArrayRef(T *start, uint size) : m_start(start), m_size(size)
+  {
+  }
+
+  MutableArrayRef(std::initializer_list<T> &list) : MutableArrayRef(list.begin(), list.size())
+  {
+  }
+
+  MutableArrayRef(std::vector<T> &vector) : MutableArrayRef(vector.data(), vector.size())
+  {
+  }
+
+  template<std::size_t N>
+  MutableArrayRef(std::array<T, N> &array) : MutableArrayRef(array.data(), N)
+  {
+  }
+
+  operator ArrayRef<T>()
+  {
+    return ArrayRef<T>(this->data(), this->size());
+  }
+
+  T *data() const
+  {
+    return m_start;
+  }
+
+  uint size() const
+  {
+    return m_size;
+  }
+
+  /**
+   * Replace all elements in the referenced array with the given value.
+   */
+  void fill(const T &element)
+  {
+    std::fill_n(m_start, m_size, element);
+  }
+
+  /**
+   * Replace a subset of all elements with the given value.
+   */
+  void fill_indices(ArrayRef<uint> indices, const T &element)
+  {
+    for (uint i : indices) {
+      m_start[i] = element;
+    }
+  }
+
+  /**
+   * Copy the values from another array into the references array.
+   */
+  void copy_from(const T *ptr)
+  {
+    BLI::copy_n(ptr, m_size, m_start);
+  }
+
+  void copy_from(ArrayRef<T> other)
+  {
+    BLI_assert(this->size() == other.size());
+    this->copy_from(other.begin());
+  }
+
+  T *begin() const
+  {
+    return m_start;
+  }
+
+  T *end() const
+  {
+    return m_start + m_size;
+  }
+
+  T &operator[](uint index) const
+  {
+    BLI_assert(index < this->size());
+    return m_start[index];
+  }
+
+  /**
+   * Return a continuous part of the array.
+   * This will assert when the slice is out of bounds.
+   */
+  MutableArrayRef slice(uint start, uint length) const
+  {
+    BLI_assert(start + length <= this->size());
+    return MutableArrayRef(m_start + start, length);
+  }
+
+  /**
+   * Return a new MutableArrayRef with n elements removed from the beginning.
+   */
+  MutableArrayRef drop_front(uint n = 1) const
+  {
+    BLI_assert(n <= this->size());
+    return this->slice(n, this->size() - n);
+  }
+
+  /**
+   * Return a new MutableArrayRef with n elements removed from the beginning.
+   */
+  MutableArrayRef drop_back(uint n = 1) const
+  {
+    BLI_assert(n <= this->size());
+    return this->slice(0, this->size() - n);
+  }
+
+  /**
+   * Return a new MutableArrayRef that only contains the first n elements.
+   */
+  MutableArrayRef take_front(uint n) const
+  {
+    BLI_assert(n <= this->size());
+    return this->slice(0, n);
+  }
+
+  /**
+   * Return a new MutableArrayRef that only contains the last n elements.
+   */
+  MutableArrayRef take_back(uint n) const
+  {
+    BLI_assert(n <= this->size());
+    return this->slice(this->size() - n, n);
+  }
+};
+
 /**
  * Shorthand to make use of automatic template parameter deduction.
  */
diff --git a/source/blender/blenlib/BLI_map.hpp b/source/blender/blenlib/BLI_map.hpp
index bafb10f965f..6d30a87f6f0 100644
--- a/source/blender/blenlib/BLI_map.hpp
+++ b/source/blender/blenlib/BLI_map.hpp
@@ -265,7 +265,7 @@ template<typename KeyT, typename ValueT, typename Allocator = GuardedAllocator>
         key, [&value]() { return value; }, [&value](ValueT &old_value) { old_value = value; });
   }
 
-  ValueT *lookup_ptr(const KeyT &key) const
+  const ValueT *lookup_ptr(const KeyT &key) const
   {
     ITER_SLOTS_BEGIN (key, m_array, const, item, offset) {
       if (item.is_empty(offset)) {
@@ -278,11 +278,23 @@ template<typename KeyT, typename ValueT, typename Allocator = GuardedAllocator>
     ITER_SLOTS_END(offset);
   }
 
-  ValueT &lookup(const KeyT &key) const
+  const ValueT &lookup(const KeyT &key) const
   {
     return *this->lookup_ptr(key);
   }
 
+  ValueT *lookup_ptr(const KeyT &key)
+  {
+    const Map *const_this = this;
+    return const_cast<ValueT *>(const_this->lookup_ptr(key));
+  }
+
+  ValueT &lookup(const KeyT &key)
+  {
+    const Map *const_this = this;
+    return const_cast<ValueT &>(const_this->lookup(key));
+  }
+
   ValueT lookup_default

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list