[Bf-blender-cvs] [599561de841] master: BKE_main: add utils to loop over whole IDs of a given Main database.

Bastien Montagne noreply at git.blender.org
Thu Feb 7 20:47:22 CET 2019


Commit: 599561de841b6f6f2c0ef6a5a4afa80cf3c51918
Author: Bastien Montagne
Date:   Thu Feb 7 17:16:15 2019 +0100
Branches: master
https://developer.blender.org/rB599561de841b6f6f2c0ef6a5a4afa80cf3c51918

BKE_main: add utils to loop over whole IDs of a given Main database.

We are currently having the same boiler plate code in tens of places
accross our code, we can as well have a utils to do that.

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

M	source/blender/blenkernel/BKE_main.h
M	source/blender/blenkernel/intern/main.c

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

diff --git a/source/blender/blenkernel/BKE_main.h b/source/blender/blenkernel/BKE_main.h
index 129ddf67468..872cdc2bcd3 100644
--- a/source/blender/blenkernel/BKE_main.h
+++ b/source/blender/blenkernel/BKE_main.h
@@ -136,6 +136,16 @@ void BKE_main_unlock(struct Main *bmain);
 void BKE_main_relations_create(struct Main *bmain);
 void BKE_main_relations_free(struct Main *bmain);
 
+/* *** Generic utils to loop over whole Main database. *** */
+/** \return false to stop iteration, true to keep going. */
+typedef bool (*MainForeachIDCallback) (struct Main *bmain, struct ID *id, void *user_data);
+bool BKE_main_listbase_foreach_id(
+        struct Main *bmain, struct ListBase *lb,
+        MainForeachIDCallback callback, void *user_data);
+bool BKE_main_foreach_id(
+        struct Main *bmain, const bool reverse_type_order,
+        MainForeachIDCallback callback, void *user_data);
+
 struct BlendThumbnail *BKE_main_thumbnail_from_imbuf(struct Main *bmain, struct ImBuf *img);
 struct ImBuf *BKE_main_thumbnail_to_imbuf(struct Main *bmain, struct BlendThumbnail *data);
 void BKE_main_thumbnail_create(struct Main *bmain);
diff --git a/source/blender/blenkernel/intern/main.c b/source/blender/blenkernel/intern/main.c
index 327c1cda165..2d3988ed8f5 100644
--- a/source/blender/blenkernel/intern/main.c
+++ b/source/blender/blenkernel/intern/main.c
@@ -211,6 +211,55 @@ void BKE_main_relations_free(Main *bmain)
 	}
 }
 
+/**
+ * Call given callback over every IDs of given \a lb listbase (assumed to be part of given \a bmain).
+ *
+ * \return false if the iteration was iterrupted by the callback.
+ *
+ * \warning \a callback may affect the ID, but DO NOT change the listbase or Main database (add/remove/reorder its IDs).
+ */
+bool BKE_main_listbase_foreach_id(
+        Main *bmain, ListBase *lb,
+        MainForeachIDCallback callback, void *user_data)
+{
+	bool keep_looping = true;
+	for (ID *id = lb->first; id; id = id->next) {
+		if (!(keep_looping = callback(bmain, id, user_data))) {
+			return keep_looping;
+		}
+	}
+	return keep_looping;
+}
+
+/**
+ * Call given callback over every IDs of given \a bmain Main database.
+ *
+ * \param reverse_type_order Allow to reverse order in which ID *types* are handled
+ *                           (i.e. does not reverse the order in which IDs themselves are handled
+ *                           whithin a give listbase).
+ * \return false if the iteration was iterrupted by the callback.
+ *
+ * \warning \a callback may affect the ID, but DO NOT change the Main database (add/remove/reorder its IDs).
+ */
+bool BKE_main_foreach_id(
+        Main *bmain, const bool reverse_type_order,
+        MainForeachIDCallback callback, void *user_data)
+{
+	ListBase *lbarray[MAX_LIBARRAY];
+	const int nbr_types = set_listbasepointers(bmain, lbarray);
+
+	bool keep_looping = true;
+	for (int i = reverse_type_order ? nbr_types - 1 : 0;
+	     reverse_type_order ? i >= 0 : i < nbr_types;
+	     reverse_type_order ? i-- : i++)
+	{
+		if (!(keep_looping = BKE_main_listbase_foreach_id(bmain, lbarray[i], callback, user_data))) {
+			return keep_looping;
+		}
+	}
+	return keep_looping;
+}
+
 /**
  * Generates a raw .blend file thumbnail data from given image.
  *



More information about the Bf-blender-cvs mailing list