[Bf-blender-cvs] [f7206f99105] functions: cleanup iterating over AttributeRefs

Jacques Lucke noreply at git.blender.org
Tue Sep 3 16:23:11 CEST 2019


Commit: f7206f99105b7497782a9a5146c7150813e3b34c
Author: Jacques Lucke
Date:   Tue Sep 3 13:50:22 2019 +0200
Branches: functions
https://developer.blender.org/rBf7206f99105b7497782a9a5146c7150813e3b34c

cleanup iterating over AttributeRefs

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

M	source/blender/simulations/bparticles/action_interface.hpp
M	source/blender/simulations/bparticles/attributes.cpp
M	source/blender/simulations/bparticles/attributes.hpp
M	source/blender/simulations/bparticles/particle_function_builder.cpp

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

diff --git a/source/blender/simulations/bparticles/action_interface.hpp b/source/blender/simulations/bparticles/action_interface.hpp
index 3a74cad2170..5d35b39cf99 100644
--- a/source/blender/simulations/bparticles/action_interface.hpp
+++ b/source/blender/simulations/bparticles/action_interface.hpp
@@ -118,8 +118,7 @@ inline void Action::execute_from_emitter(AttributesRefGroup &new_particles,
                                                    *emitter_action_context;
 
   uint offset = 0;
-  for (uint i = 0; i < new_particles.range_amount(); i++) {
-    AttributesRef attributes = new_particles.segment(i);
+  for (AttributesRef attributes : new_particles) {
     uint range_size = attributes.size();
 
     used_emitter_context.update(Range<uint>(offset, offset + range_size));
@@ -178,8 +177,7 @@ inline void Action::execute_for_new_particles(AttributesRefGroup &new_particles,
   /* Use empty action context, until there a better solution is implemented. */
   EmptyActionContext empty_context;
 
-  for (uint i = 0; i < new_particles.range_amount(); i++) {
-    AttributesRef attributes = new_particles.segment(i);
+  for (AttributesRef attributes : new_particles) {
     uint range_size = attributes.size();
 
     AttributesRef offsets(info, buffers, range_size);
@@ -205,8 +203,7 @@ inline void Action::execute_for_new_particles(AttributesRefGroup &new_particles,
 
   EmptyActionContext empty_context;
 
-  for (uint i = 0; i < new_particles.range_amount(); i++) {
-    AttributesRef attributes = new_particles.segment(i);
+  for (AttributesRef attributes : new_particles) {
     uint range_size = attributes.size();
 
     AttributesRef offsets(info, buffers, range_size);
diff --git a/source/blender/simulations/bparticles/attributes.cpp b/source/blender/simulations/bparticles/attributes.cpp
index b60f175e14b..d8ddc3dd88a 100644
--- a/source/blender/simulations/bparticles/attributes.cpp
+++ b/source/blender/simulations/bparticles/attributes.cpp
@@ -53,18 +53,17 @@ void AttributesRefGroup::set_elements(uint index, void *data)
 
   void *remaining_data = data;
 
-  for (uint i = 0; i < this->range_amount(); i++) {
-    AttributesRef attributes = this->segment(i);
+  for (AttributesRef attributes : *this) {
     void *dst = attributes.get_ptr(index);
 
-    uint range_size = m_ranges[i].size();
-    for (uint pindex = 0; pindex < range_size; pindex++) {
+    uint size = attributes.size();
+    for (uint pindex = 0; pindex < size; pindex++) {
       memcpy(POINTER_OFFSET(dst, element_size * pindex),
              POINTER_OFFSET(remaining_data, element_size * pindex),
              element_size);
     }
 
-    remaining_data = POINTER_OFFSET(remaining_data, range_size * element_size);
+    remaining_data = POINTER_OFFSET(remaining_data, size * element_size);
   }
 }
 
@@ -82,12 +81,10 @@ void AttributesRefGroup::set_repeated_elements(uint index,
   uint element_size = size_of_attribute_type(type);
 
   uint offset = 0;
-  for (uint i = 0; i < this->range_amount(); i++) {
-    AttributesRef attributes = this->segment(i);
+  for (AttributesRef attributes : *this) {
     void *dst = attributes.get_ptr(index);
 
-    uint range_size = m_ranges[i].size();
-    for (uint pindex = 0; pindex < range_size; pindex++) {
+    for (uint pindex = 0; pindex < attributes.size(); pindex++) {
       memcpy(POINTER_OFFSET(dst, element_size * pindex),
              POINTER_OFFSET(data, element_size * offset),
              element_size);
@@ -104,12 +101,10 @@ void AttributesRefGroup::fill_elements(uint index, void *value)
   AttributeType type = m_attributes_info->type_of(index);
   uint element_size = size_of_attribute_type(type);
 
-  for (uint i = 0; i < this->range_amount(); i++) {
-    AttributesRef attributes = this->segment(i);
+  for (AttributesRef attributes : *this) {
     void *dst = attributes.get_ptr(index);
 
-    uint range_size = m_ranges[i].size();
-    for (uint pindex = 0; pindex < range_size; pindex++) {
+    for (uint pindex = 0; pindex < attributes.size(); pindex++) {
       memcpy(POINTER_OFFSET(dst, element_size * pindex), value, element_size);
     }
   }
diff --git a/source/blender/simulations/bparticles/attributes.hpp b/source/blender/simulations/bparticles/attributes.hpp
index a171bb99e5b..451dd5f5c73 100644
--- a/source/blender/simulations/bparticles/attributes.hpp
+++ b/source/blender/simulations/bparticles/attributes.hpp
@@ -443,24 +443,49 @@ class AttributesRefGroup {
     this->fill<T>(index, value);
   }
 
-  AttributesRef segment(uint i)
-  {
-    return AttributesRef(*m_attributes_info, m_buffers[i], m_ranges[i]);
-  }
-
   AttributesInfo &attributes_info()
   {
     return *m_attributes_info;
   }
 
-  uint range_amount() const
+  class Iterator {
+   private:
+    AttributesRefGroup *m_group;
+    uint m_current;
+
+   public:
+    Iterator(AttributesRefGroup *group, uint current) : m_group(group), m_current(current)
+    {
+    }
+
+    Iterator &operator++()
+    {
+      m_current++;
+      return *this;
+    }
+
+    AttributesRef operator*()
+    {
+      return AttributesRef(*m_group->m_attributes_info,
+                           m_group->m_buffers[m_current],
+                           m_group->m_ranges[m_current]);
+    }
+
+    friend bool operator!=(const Iterator &a, const Iterator &b)
+    {
+      BLI_assert(a.m_group == b.m_group);
+      return a.m_current != b.m_current;
+    }
+  };
+
+  Iterator begin()
   {
-    return m_buffers.size();
+    return Iterator(this, 0);
   }
 
-  Range<uint> range(uint i) const
+  Iterator end()
   {
-    return m_ranges[i];
+    return Iterator(this, m_buffers.size());
   }
 
  private:
diff --git a/source/blender/simulations/bparticles/particle_function_builder.cpp b/source/blender/simulations/bparticles/particle_function_builder.cpp
index b35c943332e..b8b3a426fc9 100644
--- a/source/blender/simulations/bparticles/particle_function_builder.cpp
+++ b/source/blender/simulations/bparticles/particle_function_builder.cpp
@@ -62,6 +62,7 @@ static AttributeType attribute_type_from_socket_type(FN::Type *type)
   }
   else {
     BLI_assert(false);
+    return AttributeType::Byte;
   }
 }



More information about the Bf-blender-cvs mailing list