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.
This commit is contained in:
Bastien Montagne
2019-02-07 17:16:15 +01:00
parent d2afa51ddc
commit 599561de84
2 changed files with 59 additions and 0 deletions

View File

@@ -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);

View File

@@ -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.
*