From e0567eadbd751c37e4418bb91d89837d94e2c050 Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Tue, 26 Mar 2024 13:36:30 -0400 Subject: [PATCH] Cleanup: Use FunctionRef for IDProperty iteration callback Avoid the need for a seaprate user_data argument. --- source/blender/blenkernel/BKE_idprop.hh | 13 +++---- source/blender/blenkernel/intern/armature.cc | 22 +++++------- source/blender/blenkernel/intern/fcurve.cc | 8 ++--- source/blender/blenkernel/intern/idprop.cc | 9 +++-- source/blender/blenkernel/intern/lib_query.cc | 7 ++-- source/blender/blenkernel/intern/node.cc | 14 ++++---- .../blenkernel/intern/node_tree_interface.cc | 8 ++--- source/blender/blenkernel/intern/object.cc | 8 ++--- source/blender/blenkernel/intern/scene.cc | 20 +++++------ .../intern/builder/deg_builder_nodes.cc | 11 ++---- .../intern/builder/deg_builder_relations.cc | 11 ++---- .../editors/geometry/node_group_operator.cc | 24 +++++-------- source/blender/modifiers/intern/MOD_nodes.cc | 34 +++++-------------- 13 files changed, 66 insertions(+), 123 deletions(-) diff --git a/source/blender/blenkernel/BKE_idprop.hh b/source/blender/blenkernel/BKE_idprop.hh index f3f05361d76..4d15d3eff94 100644 --- a/source/blender/blenkernel/BKE_idprop.hh +++ b/source/blender/blenkernel/BKE_idprop.hh @@ -11,6 +11,7 @@ #include #include "BLI_compiler_attrs.h" +#include "BLI_function_ref.hh" #include "BLI_span.hh" #include "BLI_string_ref.hh" #include "BLI_sys_types.h" @@ -299,11 +300,6 @@ float IDP_coerce_to_float_or_zero(const IDProperty *prop); */ double IDP_coerce_to_double_or_zero(const IDProperty *prop); -/** - * Call a callback for each #IDproperty in the hierarchy under given root one (included). - */ -using IDPForeachPropertyCallback = void (*)(IDProperty *id_property, void *user_data); - /** * Loop through all ID properties in hierarchy of given \a id_property_root included. * @@ -314,8 +310,7 @@ using IDPForeachPropertyCallback = void (*)(IDProperty *id_property, void *user_ */ void IDP_foreach_property(IDProperty *id_property_root, int type_filter, - IDPForeachPropertyCallback callback, - void *user_data); + blender::FunctionRef callback); /* Format IDProperty as strings */ char *IDP_reprN(const IDProperty *prop, uint *r_len); @@ -324,8 +319,8 @@ void IDP_repr_fn(const IDProperty *prop, void *user_data); void IDP_print(const IDProperty *prop); -void IDP_BlendWrite(struct BlendWriter *writer, const IDProperty *prop); -void IDP_BlendReadData_impl(struct BlendDataReader *reader, +void IDP_BlendWrite(BlendWriter *writer, const IDProperty *prop); +void IDP_BlendReadData_impl(BlendDataReader *reader, IDProperty **prop, const char *caller_func_id); #define IDP_BlendDataRead(reader, prop) IDP_BlendReadData_impl(reader, prop, __func__) diff --git a/source/blender/blenkernel/intern/armature.cc b/source/blender/blenkernel/intern/armature.cc index dd7947a9d43..ceb26cf7766 100644 --- a/source/blender/blenkernel/intern/armature.cc +++ b/source/blender/blenkernel/intern/armature.cc @@ -227,9 +227,9 @@ static void armature_free_data(ID *id) static void armature_foreach_id_bone(Bone *bone, LibraryForeachIDData *data) { BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL( - data, - IDP_foreach_property( - bone->prop, IDP_TYPE_FILTER_ID, BKE_lib_query_idpropertiesForeachIDLink_callback, data)); + data, IDP_foreach_property(bone->prop, IDP_TYPE_FILTER_ID, [&](IDProperty *prop) { + BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data); + })); LISTBASE_FOREACH (Bone *, curbone, &bone->childbase) { BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL(data, armature_foreach_id_bone(curbone, data)); @@ -239,21 +239,17 @@ static void armature_foreach_id_bone(Bone *bone, LibraryForeachIDData *data) static void armature_foreach_id_editbone(EditBone *edit_bone, LibraryForeachIDData *data) { BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL( - data, - IDP_foreach_property(edit_bone->prop, - IDP_TYPE_FILTER_ID, - BKE_lib_query_idpropertiesForeachIDLink_callback, - data)); + data, IDP_foreach_property(edit_bone->prop, IDP_TYPE_FILTER_ID, [&](IDProperty *prop) { + BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data); + })); } static void armature_foreach_id_bone_collection(BoneCollection *bcoll, LibraryForeachIDData *data) { BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL( - data, - IDP_foreach_property(bcoll->prop, - IDP_TYPE_FILTER_ID, - BKE_lib_query_idpropertiesForeachIDLink_callback, - data)); + data, IDP_foreach_property(bcoll->prop, IDP_TYPE_FILTER_ID, [&](IDProperty *prop) { + BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data); + })); } static void armature_foreach_id(ID *id, LibraryForeachIDData *data) diff --git a/source/blender/blenkernel/intern/fcurve.cc b/source/blender/blenkernel/intern/fcurve.cc index a0caba34cc3..913682516ca 100644 --- a/source/blender/blenkernel/intern/fcurve.cc +++ b/source/blender/blenkernel/intern/fcurve.cc @@ -184,11 +184,9 @@ void BKE_fmodifiers_foreach_id(ListBase *fmodifiers, LibraryForeachIDData *data) BKE_LIB_FOREACHID_PROCESS_IDSUPER(data, fcm_py->script, IDWALK_CB_NOP); BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL( - data, - IDP_foreach_property(fcm_py->prop, - IDP_TYPE_FILTER_ID, - BKE_lib_query_idpropertiesForeachIDLink_callback, - data)); + data, IDP_foreach_property(fcm_py->prop, IDP_TYPE_FILTER_ID, [&](IDProperty *prop) { + BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data); + })); break; } default: diff --git a/source/blender/blenkernel/intern/idprop.cc b/source/blender/blenkernel/intern/idprop.cc index 9e0d5f9f9b4..c46ad2ebcef 100644 --- a/source/blender/blenkernel/intern/idprop.cc +++ b/source/blender/blenkernel/intern/idprop.cc @@ -1235,29 +1235,28 @@ void IDP_Reset(IDProperty *prop, const IDProperty *reference) void IDP_foreach_property(IDProperty *id_property_root, const int type_filter, - IDPForeachPropertyCallback callback, - void *user_data) + const blender::FunctionRef callback) { if (!id_property_root) { return; } if (type_filter == 0 || (1 << id_property_root->type) & type_filter) { - callback(id_property_root, user_data); + callback(id_property_root); } /* Recursive call into container types of ID properties. */ switch (id_property_root->type) { case IDP_GROUP: { LISTBASE_FOREACH (IDProperty *, loop, &id_property_root->data.group) { - IDP_foreach_property(loop, type_filter, callback, user_data); + IDP_foreach_property(loop, type_filter, callback); } break; } case IDP_IDPARRAY: { IDProperty *loop = static_cast(IDP_Array(id_property_root)); for (int i = 0; i < id_property_root->len; i++) { - IDP_foreach_property(&loop[i], type_filter, callback, user_data); + IDP_foreach_property(&loop[i], type_filter, callback); } break; } diff --git a/source/blender/blenkernel/intern/lib_query.cc b/source/blender/blenkernel/intern/lib_query.cc index 8a083ab02ad..c452c7c46c0 100644 --- a/source/blender/blenkernel/intern/lib_query.cc +++ b/source/blender/blenkernel/intern/lib_query.cc @@ -344,10 +344,9 @@ static bool library_foreach_ID_link(Main *bmain, } } - IDP_foreach_property(id->properties, - IDP_TYPE_FILTER_ID, - BKE_lib_query_idpropertiesForeachIDLink_callback, - &data); + IDP_foreach_property(id->properties, IDP_TYPE_FILTER_ID, [&](IDProperty *prop) { + BKE_lib_query_idpropertiesForeachIDLink_callback(prop, &data); + }); if (BKE_lib_query_foreachid_iter_stop(&data)) { library_foreach_ID_data_cleanup(&data); return false; diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc index 20066dcfb63..a6476374667 100644 --- a/source/blender/blenkernel/intern/node.cc +++ b/source/blender/blenkernel/intern/node.cc @@ -319,9 +319,9 @@ static void ntree_free_data(ID *id) static void library_foreach_node_socket(LibraryForeachIDData *data, bNodeSocket *sock) { BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL( - data, - IDP_foreach_property( - sock->prop, IDP_TYPE_FILTER_ID, BKE_lib_query_idpropertiesForeachIDLink_callback, data)); + data, IDP_foreach_property(sock->prop, IDP_TYPE_FILTER_ID, [&](IDProperty *prop) { + BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data); + })); switch (eNodeSocketDatatype(sock->type)) { case SOCK_OBJECT: { @@ -383,11 +383,9 @@ static void node_foreach_id(ID *id, LibraryForeachIDData *data) BKE_LIB_FOREACHID_PROCESS_ID(data, node->id, IDWALK_CB_USER); BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL( - data, - IDP_foreach_property(node->prop, - IDP_TYPE_FILTER_ID, - BKE_lib_query_idpropertiesForeachIDLink_callback, - data)); + data, IDP_foreach_property(node->prop, IDP_TYPE_FILTER_ID, [&](IDProperty *prop) { + BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data); + })); LISTBASE_FOREACH (bNodeSocket *, sock, &node->inputs) { BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL(data, library_foreach_node_socket(data, sock)); } diff --git a/source/blender/blenkernel/intern/node_tree_interface.cc b/source/blender/blenkernel/intern/node_tree_interface.cc index 23fa75b188b..d32f8623713 100644 --- a/source/blender/blenkernel/intern/node_tree_interface.cc +++ b/source/blender/blenkernel/intern/node_tree_interface.cc @@ -607,11 +607,9 @@ static void item_foreach_id(LibraryForeachIDData *data, bNodeTreeInterfaceItem & bNodeTreeInterfaceSocket &socket = reinterpret_cast(item); BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL( - data, - IDP_foreach_property(socket.properties, - IDP_TYPE_FILTER_ID, - BKE_lib_query_idpropertiesForeachIDLink_callback, - data)); + data, IDP_foreach_property(socket.properties, IDP_TYPE_FILTER_ID, [&](IDProperty *prop) { + BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data); + })); socket_types::socket_data_foreach_id(data, socket); break; diff --git a/source/blender/blenkernel/intern/object.cc b/source/blender/blenkernel/intern/object.cc index db02945ad0b..bb148f668c6 100644 --- a/source/blender/blenkernel/intern/object.cc +++ b/source/blender/blenkernel/intern/object.cc @@ -436,11 +436,9 @@ static void object_foreach_id(ID *id, LibraryForeachIDData *data) if (object->pose) { LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) { BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL( - data, - IDP_foreach_property(pchan->prop, - IDP_TYPE_FILTER_ID, - BKE_lib_query_idpropertiesForeachIDLink_callback, - data)); + data, IDP_foreach_property(pchan->prop, IDP_TYPE_FILTER_ID, [&](IDProperty *prop) { + BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data); + })); BKE_LIB_FOREACHID_PROCESS_IDSUPER(data, pchan->custom, IDWALK_CB_USER); BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL( diff --git a/source/blender/blenkernel/intern/scene.cc b/source/blender/blenkernel/intern/scene.cc index eec4e7d9599..50a1046c184 100644 --- a/source/blender/blenkernel/intern/scene.cc +++ b/source/blender/blenkernel/intern/scene.cc @@ -829,8 +829,9 @@ static bool seq_foreach_member_id_cb(Sequence *seq, void *user_data) FOREACHID_PROCESS_IDSUPER(data, seq->clip, IDWALK_CB_USER); FOREACHID_PROCESS_IDSUPER(data, seq->mask, IDWALK_CB_USER); FOREACHID_PROCESS_IDSUPER(data, seq->sound, IDWALK_CB_USER); - IDP_foreach_property( - seq->prop, IDP_TYPE_FILTER_ID, BKE_lib_query_idpropertiesForeachIDLink_callback, data); + IDP_foreach_property(seq->prop, IDP_TYPE_FILTER_ID, [&](IDProperty *prop) { + BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data); + }); LISTBASE_FOREACH (SequenceModifierData *, smd, &seq->modifiers) { FOREACHID_PROCESS_IDSUPER(data, smd->mask_id, IDWALK_CB_USER); } @@ -885,10 +886,9 @@ static void scene_foreach_id(ID *id, LibraryForeachIDData *data) BKE_LIB_FOREACHID_PROCESS_IDSUPER(data, view_layer->world_override, IDWALK_CB_USER); BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL( data, - IDP_foreach_property(view_layer->id_properties, - IDP_TYPE_FILTER_ID, - BKE_lib_query_idpropertiesForeachIDLink_callback, - data)); + IDP_foreach_property(view_layer->id_properties, IDP_TYPE_FILTER_ID, [&](IDProperty *prop) { + BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data); + })); BKE_view_layer_synced_ensure(scene, view_layer); LISTBASE_FOREACH (Base *, base, BKE_view_layer_object_bases_get(view_layer)) { @@ -914,11 +914,9 @@ static void scene_foreach_id(ID *id, LibraryForeachIDData *data) LISTBASE_FOREACH (TimeMarker *, marker, &scene->markers) { BKE_LIB_FOREACHID_PROCESS_IDSUPER(data, marker->camera, IDWALK_CB_NOP); BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL( - data, - IDP_foreach_property(marker->prop, - IDP_TYPE_FILTER_ID, - BKE_lib_query_idpropertiesForeachIDLink_callback, - data)); + data, IDP_foreach_property(marker->prop, IDP_TYPE_FILTER_ID, [&](IDProperty *prop) { + BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data); + })); } ToolSettings *toolsett = scene->toolsettings; diff --git a/source/blender/depsgraph/intern/builder/deg_builder_nodes.cc b/source/blender/depsgraph/intern/builder/deg_builder_nodes.cc index d4d9a04679b..dac6d6b8b45 100644 --- a/source/blender/depsgraph/intern/builder/deg_builder_nodes.cc +++ b/source/blender/depsgraph/intern/builder/deg_builder_nodes.cc @@ -672,16 +672,11 @@ void DepsgraphNodeBuilder::build_generic_id(ID *id) build_parameters(id); } -static void build_idproperties_callback(IDProperty *id_property, void *user_data) -{ - DepsgraphNodeBuilder *builder = reinterpret_cast(user_data); - BLI_assert(id_property->type == IDP_ID); - builder->build_id(reinterpret_cast(id_property->data.pointer)); -} - void DepsgraphNodeBuilder::build_idproperties(IDProperty *id_property) { - IDP_foreach_property(id_property, IDP_TYPE_FILTER_ID, build_idproperties_callback, this); + IDP_foreach_property(id_property, IDP_TYPE_FILTER_ID, [&](IDProperty *id_property) { + this->build_id(static_cast(id_property->data.pointer)); + }); } void DepsgraphNodeBuilder::build_collection(LayerCollection *from_layer_collection, diff --git a/source/blender/depsgraph/intern/builder/deg_builder_relations.cc b/source/blender/depsgraph/intern/builder/deg_builder_relations.cc index b0fdb50ae52..d5db506179c 100644 --- a/source/blender/depsgraph/intern/builder/deg_builder_relations.cc +++ b/source/blender/depsgraph/intern/builder/deg_builder_relations.cc @@ -632,16 +632,11 @@ void DepsgraphRelationBuilder::build_generic_id(ID *id) build_parameters(id); } -static void build_idproperties_callback(IDProperty *id_property, void *user_data) -{ - DepsgraphRelationBuilder *builder = reinterpret_cast(user_data); - BLI_assert(id_property->type == IDP_ID); - builder->build_id(reinterpret_cast(id_property->data.pointer)); -} - void DepsgraphRelationBuilder::build_idproperties(IDProperty *id_property) { - IDP_foreach_property(id_property, IDP_TYPE_FILTER_ID, build_idproperties_callback, this); + IDP_foreach_property(id_property, IDP_TYPE_FILTER_ID, [&](IDProperty *id_property) { + this->build_id(static_cast(id_property->data.pointer)); + }); } void DepsgraphRelationBuilder::build_collection(LayerCollection *from_layer_collection, diff --git a/source/blender/editors/geometry/node_group_operator.cc b/source/blender/editors/geometry/node_group_operator.cc index 3bbf073ed98..d5f15adb170 100644 --- a/source/blender/editors/geometry/node_group_operator.cc +++ b/source/blender/editors/geometry/node_group_operator.cc @@ -265,14 +265,11 @@ static Depsgraph *build_depsgraph_from_indirect_ids(Main &bmain, needs_own_transform_relation, needs_scene_camera_relation); IDP_foreach_property( - &const_cast(properties), - IDP_TYPE_FILTER_ID, - [](IDProperty *property, void *user_data) { + &const_cast(properties), IDP_TYPE_FILTER_ID, [&](IDProperty *property) { if (ID *id = IDP_Id(property)) { - static_cast *>(user_data)->add(id); + ids_for_relations.add(id); } - }, - &ids_for_relations); + }); Vector ids; ids.append(&node_tree_orig.id); @@ -289,16 +286,11 @@ static IDProperty *replace_inputs_evaluated_data_blocks(const IDProperty &op_pro { /* We just create a temporary copy, so don't adjust data-block user count. */ IDProperty *properties = IDP_CopyProperty_ex(&op_properties, LIB_ID_CREATE_NO_USER_REFCOUNT); - IDP_foreach_property( - properties, - IDP_TYPE_FILTER_ID, - [](IDProperty *property, void *user_data) { - if (ID *id = IDP_Id(property)) { - Depsgraph *depsgraph = static_cast(user_data); - property->data.pointer = DEG_get_evaluated_id(depsgraph, id); - } - }, - &const_cast(depsgraph)); + IDP_foreach_property(properties, IDP_TYPE_FILTER_ID, [&](IDProperty *property) { + if (ID *id = IDP_Id(property)) { + property->data.pointer = DEG_get_evaluated_id(&depsgraph, id); + } + }); return properties; } diff --git a/source/blender/modifiers/intern/MOD_nodes.cc b/source/blender/modifiers/intern/MOD_nodes.cc index b18b6602294..bc904ff8b41 100644 --- a/source/blender/modifiers/intern/MOD_nodes.cc +++ b/source/blender/modifiers/intern/MOD_nodes.cc @@ -118,17 +118,11 @@ static void init_data(ModifierData *md) static void find_used_ids_from_settings(const NodesModifierSettings &settings, Set &ids) { - IDP_foreach_property( - settings.properties, - IDP_TYPE_FILTER_ID, - [](IDProperty *property, void *user_data) { - Set *ids = (Set *)user_data; - ID *id = IDP_Id(property); - if (id != nullptr) { - ids->add(id); - } - }, - &ids); + IDP_foreach_property(settings.properties, IDP_TYPE_FILTER_ID, [&](IDProperty *property) { + if (ID *id = IDP_Id(property)) { + ids.add(id); + } + }); } /* We don't know exactly what attributes from the other object we will need. */ @@ -268,21 +262,9 @@ static void foreach_ID_link(ModifierData *md, Object *ob, IDWalkFunc walk, void NodesModifierData *nmd = reinterpret_cast(md); walk(user_data, ob, (ID **)&nmd->node_group, IDWALK_CB_USER); - struct ForeachSettingData { - IDWalkFunc walk; - void *user_data; - Object *ob; - } settings = {walk, user_data, ob}; - - IDP_foreach_property( - nmd->settings.properties, - IDP_TYPE_FILTER_ID, - [](IDProperty *id_prop, void *user_data) { - ForeachSettingData *settings = (ForeachSettingData *)user_data; - settings->walk( - settings->user_data, settings->ob, (ID **)&id_prop->data.pointer, IDWALK_CB_USER); - }, - &settings); + IDP_foreach_property(nmd->settings.properties, IDP_TYPE_FILTER_ID, [&](IDProperty *id_prop) { + walk(user_data, ob, (ID **)&id_prop->data.pointer, IDWALK_CB_USER); + }); for (NodesModifierBake &bake : MutableSpan(nmd->bakes, nmd->bakes_num)) { for (NodesModifierDataBlock &data_block : MutableSpan(bake.data_blocks, bake.data_blocks_num))