Refactor: Core, give the IDWALK_… enum an explicit name

Give the `IDWALK_…` enum an explicit name: `LibraryForeachIDFlag`. This way
the flags are type-safe, and it's known where values come from. This is
much preferred (at least by me) to just having `int flags`.

Uses of `0` have been replaced with `IDWALK_NOP` as that has the same value
and is of the right type.

One invalid use of `IDWALK_CB_NOP` was detected by this change, and is
replaced by `IDWALK_NOP`.

This change might be incomplete; I gave the enum a name, and then fixed
the compiler errors.

No functional changes.

Pull Request: https://projects.blender.org/blender/blender/pulls/131865
This commit is contained in:
Sybren A. Stüvel
2024-12-12 12:43:14 +01:00
parent 203ad8ad4b
commit 8dd1eb4e8f
8 changed files with 36 additions and 32 deletions

View File

@@ -157,7 +157,7 @@ struct LibraryIDLinkCallbackData {
using LibraryIDLinkCallback = int(LibraryIDLinkCallbackData *cb_data);
/* Flags for the foreach function itself. */
enum {
enum LibraryForeachIDFlag {
IDWALK_NOP = 0,
/**
* The callback will never modify the ID pointers it processes.
@@ -229,6 +229,7 @@ enum {
* proper lib_linking and expanding of older files). */
IDWALK_DO_DEPRECATED_POINTERS = (1 << 11),
};
ENUM_OPERATORS(LibraryForeachIDFlag, IDWALK_DO_DEPRECATED_POINTERS);
/**
* Check whether current iteration over ID usages should be stopped or not.
@@ -236,7 +237,7 @@ enum {
*/
bool BKE_lib_query_foreachid_iter_stop(const LibraryForeachIDData *data);
void BKE_lib_query_foreachid_process(LibraryForeachIDData *data, ID **id_pp, int cb_flag);
int BKE_lib_query_foreachid_process_flags_get(const LibraryForeachIDData *data);
LibraryForeachIDFlag BKE_lib_query_foreachid_process_flags_get(const LibraryForeachIDData *data);
Main *BKE_lib_query_foreachid_process_main_get(const LibraryForeachIDData *data);
int BKE_lib_query_foreachid_process_callback_flag_override(LibraryForeachIDData *data,
int cb_flag,
@@ -307,7 +308,7 @@ void BKE_library_foreach_ID_link(Main *bmain,
ID *id,
blender::FunctionRef<LibraryIDLinkCallback> callback,
void *user_data,
int flag);
LibraryForeachIDFlag flag);
/**
* Apply `callback` to all ID usages of the data as defined by `subdata_foreach_id`. Useful to e.g.
@@ -345,7 +346,7 @@ void BKE_library_foreach_subdata_id(
blender::FunctionRef<void(LibraryForeachIDData *data)> subdata_foreach_id,
blender::FunctionRef<LibraryIDLinkCallback> callback,
void *user_data,
const int flag);
const LibraryForeachIDFlag flag);
/**
* Re-usable function, use when replacing ID's.

View File

@@ -256,7 +256,7 @@ static void action_foreach_id(ID *id, LibraryForeachIDData *data)
* NOTE: early-returns by BKE_LIB_FOREACHID_PROCESS_... macros are forbidden in non-readonly
* cases (see #IDWALK_RET_STOP_ITER documentation). */
const int flag = BKE_lib_query_foreachid_process_flags_get(data);
const LibraryForeachIDFlag flag = BKE_lib_query_foreachid_process_flags_get(data);
const bool is_readonly = flag & IDWALK_READONLY;
constexpr int idwalk_flags = IDWALK_CB_NEVER_SELF | IDWALK_CB_LOOPBACK;

View File

@@ -779,7 +779,7 @@ static void reuse_bmain_data_invalid_local_usages_fix(ReuseOldBMainData *reuse_d
nullptr;
BKE_library_foreach_ID_link(
new_bmain, id_iter, reuse_bmain_data_invalid_local_usages_fix_cb, reuse_data, 0);
new_bmain, id_iter, reuse_bmain_data_invalid_local_usages_fix_cb, reuse_data, IDWALK_NOP);
/* Liboverrides who lost their reference should not be liboverrides anymore, but regular IDs.
*/

View File

@@ -44,7 +44,7 @@ struct LibraryForeachIDData {
ID *self_id;
/** Flags controlling the behavior of the 'foreach id' looping code. */
int flag;
LibraryForeachIDFlag flag;
/** Generic flags to be passed to all callback calls for current processed data. */
int cb_flag;
/** Callback flags that are forbidden for all callback calls for current processed data. */
@@ -117,7 +117,7 @@ void BKE_lib_query_foreachid_process(LibraryForeachIDData *data, ID **id_pp, int
}
}
int BKE_lib_query_foreachid_process_flags_get(const LibraryForeachIDData *data)
LibraryForeachIDFlag BKE_lib_query_foreachid_process_flags_get(const LibraryForeachIDData *data)
{
return data->flag;
}
@@ -146,7 +146,7 @@ static bool library_foreach_ID_link(Main *bmain,
ID *id,
blender::FunctionRef<LibraryIDLinkCallback> callback,
void *user_data,
int flag,
LibraryForeachIDFlag flag,
LibraryForeachIDData *inherit_data);
void BKE_lib_query_idpropertiesForeachIDLink_callback(IDProperty *id_prop, void *user_data)
@@ -210,7 +210,7 @@ static bool library_foreach_ID_link(Main *bmain,
ID *id,
blender::FunctionRef<LibraryIDLinkCallback> callback,
void *user_data,
int flag,
LibraryForeachIDFlag flag,
LibraryForeachIDData *inherit_data)
{
LibraryForeachIDData data{};
@@ -415,7 +415,7 @@ void BKE_library_foreach_ID_link(Main *bmain,
ID *id,
blender::FunctionRef<LibraryIDLinkCallback> callback,
void *user_data,
int flag)
const LibraryForeachIDFlag flag)
{
library_foreach_ID_link(bmain, nullptr, id, callback, user_data, flag, nullptr);
}
@@ -438,7 +438,7 @@ void BKE_library_foreach_subdata_id(
blender::FunctionRef<void(LibraryForeachIDData *data)> subdata_foreach_id,
blender::FunctionRef<LibraryIDLinkCallback> callback,
void *user_data,
const int flag)
const LibraryForeachIDFlag flag)
{
BLI_assert((flag & (IDWALK_RECURSE | IDWALK_DO_INTERNAL_RUNTIME_POINTERS |
IDWALK_DO_LIBRARY_POINTER | IDWALK_INCLUDE_UI)) == 0);

View File

@@ -496,17 +496,16 @@ static void libblock_remap_data(
};
const bool include_ui = (remap_flags & ID_REMAP_FORCE_UI_POINTERS) != 0;
const int foreach_id_flags = (((remap_flags & ID_REMAP_FORCE_INTERNAL_RUNTIME_POINTERS) != 0 ?
IDWALK_DO_INTERNAL_RUNTIME_POINTERS :
IDWALK_NOP) |
(include_ui ? IDWALK_INCLUDE_UI : IDWALK_NOP) |
const LibraryForeachIDFlag foreach_id_flags =
(((remap_flags & ID_REMAP_FORCE_INTERNAL_RUNTIME_POINTERS) != 0 ?
IDWALK_DO_INTERNAL_RUNTIME_POINTERS :
IDWALK_NOP) |
(include_ui ? IDWALK_INCLUDE_UI : IDWALK_NOP) |
((remap_flags & ID_REMAP_NO_ORIG_POINTERS_ACCESS) != 0 ?
IDWALK_NO_ORIG_POINTERS_ACCESS :
IDWALK_NOP) |
((remap_flags & ID_REMAP_DO_LIBRARY_POINTERS) != 0 ?
IDWALK_DO_LIBRARY_POINTER :
IDWALK_NOP));
((remap_flags & ID_REMAP_NO_ORIG_POINTERS_ACCESS) != 0 ? IDWALK_NO_ORIG_POINTERS_ACCESS :
IDWALK_NOP) |
((remap_flags & ID_REMAP_DO_LIBRARY_POINTERS) != 0 ? IDWALK_DO_LIBRARY_POINTER :
IDWALK_NOP));
id_remapper.iter(libblock_remap_reset_remapping_status_fn);
@@ -915,7 +914,7 @@ static void libblock_relink_to_newid_prepare_data(Main *bmain,
id->tag &= ~ID_TAG_NEW;
relink_data->ids.append(id);
BKE_library_foreach_ID_link(bmain, id, id_relink_to_newid_looper, relink_data, 0);
BKE_library_foreach_ID_link(bmain, id, id_relink_to_newid_looper, relink_data, IDWALK_NOP);
}
void BKE_libblock_relink_to_newid(Main *bmain, ID *id, const int remap_flag)

View File

@@ -558,8 +558,10 @@ void BKE_main_relations_create(Main *bmain, const short flag)
ID *id;
FOREACH_MAIN_ID_BEGIN (bmain, id) {
const int idwalk_flag = IDWALK_READONLY |
((flag & MAINIDRELATIONS_INCLUDE_UI) != 0 ? IDWALK_INCLUDE_UI : 0);
const LibraryForeachIDFlag idwalk_flag = IDWALK_READONLY |
((flag & MAINIDRELATIONS_INCLUDE_UI) != 0 ?
IDWALK_INCLUDE_UI :
IDWALK_NOP);
/* Ensure all IDs do have an entry, even if they are not connected to any other. */
MainIDRelationsEntry **entry_p;

View File

@@ -3334,8 +3334,10 @@ static void lib_link_all(FileData *fd, Main *bmain)
if ((id->tag & ID_TAG_NEED_LINK) != 0) {
/* Not all original pointer values can be considered as valid.
* Handling of DNA deprecated data should never be needed in undo case. */
const int flag = IDWALK_NO_ORIG_POINTERS_ACCESS | IDWALK_INCLUDE_UI |
((fd->flags & FD_FLAGS_IS_MEMFILE) ? 0 : IDWALK_DO_DEPRECATED_POINTERS);
const LibraryForeachIDFlag flag = IDWALK_NO_ORIG_POINTERS_ACCESS | IDWALK_INCLUDE_UI |
((fd->flags & FD_FLAGS_IS_MEMFILE) ?
IDWALK_NOP :
IDWALK_DO_DEPRECATED_POINTERS);
BKE_library_foreach_ID_link(bmain, id, lib_link_cb, &reader, flag);
after_liblink_id_process(&reader, id);
@@ -4248,10 +4250,10 @@ void BLO_expand_main(void *fdhandle, Main *mainvar, BLOExpandDoitCallback callba
* Expanding should _not_ require processing of UI ID pointers.
* Expanding should never modify ID pointers themselves.
* Handling of DNA deprecated data should never be needed in undo case. */
const int flag = IDWALK_READONLY | IDWALK_NO_ORIG_POINTERS_ACCESS |
((!fd || (fd->flags & FD_FLAGS_IS_MEMFILE)) ?
0 :
IDWALK_DO_DEPRECATED_POINTERS);
const LibraryForeachIDFlag flag = IDWALK_READONLY | IDWALK_NO_ORIG_POINTERS_ACCESS |
((!fd || (fd->flags & FD_FLAGS_IS_MEMFILE)) ?
IDWALK_NOP :
IDWALK_DO_DEPRECATED_POINTERS);
BKE_library_foreach_ID_link(nullptr, id_iter, expand_cb, &expander, flag);
do_it = true;

View File

@@ -247,7 +247,7 @@ static PyObject *bpy_user_map(PyObject * /*self*/, PyObject *args, PyObject *kwd
data_cb.id_curr = id;
BKE_library_foreach_ID_link(
nullptr, id, foreach_libblock_id_user_map_callback, &data_cb, IDWALK_CB_NOP);
nullptr, id, foreach_libblock_id_user_map_callback, &data_cb, IDWALK_NOP);
if (data_cb.py_id_curr) {
Py_DECREF(data_cb.py_id_curr);