diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2019-02-07 19:16:15 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2019-02-07 22:46:41 +0300 |
commit | 599561de841b6f6f2c0ef6a5a4afa80cf3c51918 (patch) | |
tree | 342d46ea6ef2960f1b78a6e446ec799de4d20d8d /source/blender | |
parent | d2afa51ddc68c8a923c9535ba7678ebe468e2220 (diff) |
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.
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenkernel/BKE_main.h | 10 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/main.c | 49 |
2 files changed, 59 insertions, 0 deletions
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 @@ -212,6 +212,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. * * \param bmain: If not NULL, also store generated data in this Main. |