Cleanup: replace Main ID's foreach functions by macros.
[blender.git] / source / blender / blenkernel / BKE_main.h
index a96c1399979a1aaa44c8315ed705ad278826597e..08b6d47877e6113602c6bba3a217d620ed28123a 100644 (file)
@@ -140,14 +140,39 @@ void BKE_main_relations_free(struct Main *bmain);
 struct GSet *BKE_main_gset_create(struct Main *bmain, struct GSet *gset);
 
 /* *** 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);
+
+#define FOREACH_MAIN_LISTBASE_ID_BEGIN(_lb, _id)                          \
+       for (_id = _lb->first; _id != NULL; _id = _id->next) {                \
+
+#define FOREACH_MAIN_LISTBASE_ID_END                                      \
+       } ((void)0)
+
+
+#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)                                \
+       {                                                                     \
+               ListBase *_lbarray[MAX_LIBARRAY];                                 \
+               int i = set_listbasepointers(_bmain, _lbarray);                   \
+               while (i--) {                                                     \
+                       FOREACH_MAIN_LISTBASE_ID_BEGIN(_lbarray[i], _id)              \
+
+#define FOREACH_MAIN_ID_END                                               \
+                       FOREACH_MAIN_LISTBASE_ID_END;                                 \
+               }                                                                 \
+       } ((void)0)
+
+/** \param _do_break A boolean, to allow breaking iteration (only used to break by type,
+ *                   you must also use an explicit `break;` operation if you want to
+ *                   immediately break from inner by-ID loop).
+ */
+#define FOREACH_MAIN_ID_BREAKABLE_BEGIN(_bmain, _id, _do_break)           \
+       {                                                                     \
+               ListBase *_lbarray[MAX_LIBARRAY];                                 \
+               int i = set_listbasepointers(_bmain, _lbarray);                   \
+               while (i-- && !_do_break) {                                       \
+                       FOREACH_MAIN_LISTBASE_ID_BEGIN(_lbarray[i], _id)              \
+
+#define FOREACH_MAIN_ID_BREAKABLE_END FOREACH_MAIN_ID_END
+
 
 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);