Cleanup: Improve const correctness of ID functions

These functions don't change their inputs, so they can be const,
which is a bit more intuitive and clean to use for callers.

Differential Revision: https://developer.blender.org/D14943
This commit is contained in:
Hans Goudey 2022-05-31 17:31:32 +02:00
parent 908dfd5e0d
commit a40b611128
4 changed files with 26 additions and 13 deletions

View File

@ -603,7 +603,7 @@ bool BKE_id_can_be_asset(const struct ID *id);
* we should either cache that status info also in virtual override IDs, or address the
* long-standing TODO of getting an efficient 'owner_id' access for all embedded ID types.
*/
bool BKE_id_is_editable(struct Main *bmain, struct ID *id);
bool BKE_id_is_editable(const struct Main *bmain, const struct ID *id);
/**
* Returns ordered list of data-blocks for display in the UI.

View File

@ -62,12 +62,12 @@ void BKE_lib_override_library_free(struct IDOverrideLibrary **override, bool do_
/**
* Check if given ID has some override rules that actually indicate the user edited it.
*/
bool BKE_lib_override_library_is_user_edited(struct ID *id);
bool BKE_lib_override_library_is_user_edited(const struct ID *id);
/**
* Check if given ID is a system override.
*/
bool BKE_lib_override_library_is_system_defined(struct Main *bmain, struct ID *id);
bool BKE_lib_override_library_is_system_defined(const struct Main *bmain, const struct ID *id);
/**
* Check if given ID is a leaf in its liboverride hierarchy (i.e. if it does not use any other

View File

@ -2161,7 +2161,7 @@ bool BKE_id_can_be_asset(const ID *id)
BKE_idtype_idcode_is_linkable(GS(id->name));
}
bool BKE_id_is_editable(Main *bmain, ID *id)
bool BKE_id_is_editable(const Main *bmain, const ID *id)
{
return !(ID_IS_LINKED(id) || BKE_lib_override_library_is_system_defined(bmain, id));
}

View File

@ -88,7 +88,9 @@ BLI_INLINE void lib_override_object_posemode_transfer(ID *id_dst, ID *id_src)
}
/** Get override data for a given ID. Needed because of our beloved shape keys snowflake. */
BLI_INLINE IDOverrideLibrary *lib_override_get(Main *bmain, ID *id, ID **r_owner_id)
BLI_INLINE const IDOverrideLibrary *lib_override_get(const Main *bmain,
const ID *id,
const ID **r_owner_id)
{
if (r_owner_id != nullptr) {
*r_owner_id = id;
@ -96,7 +98,9 @@ BLI_INLINE IDOverrideLibrary *lib_override_get(Main *bmain, ID *id, ID **r_owner
if (id->flag & LIB_EMBEDDED_DATA_LIB_OVERRIDE) {
const IDTypeInfo *id_type = BKE_idtype_get_info_from_id(id);
if (id_type->owner_get != nullptr) {
ID *owner_id = id_type->owner_get(bmain, id);
/* The #IDTypeInfo::owner_get callback should not modify the arguments, so casting away const
* is okay. */
const ID *owner_id = id_type->owner_get(const_cast<Main *>(bmain), const_cast<ID *>(id));
if (r_owner_id != nullptr) {
*r_owner_id = owner_id;
}
@ -107,6 +111,15 @@ BLI_INLINE IDOverrideLibrary *lib_override_get(Main *bmain, ID *id, ID **r_owner
return id->override_library;
}
BLI_INLINE IDOverrideLibrary *lib_override_get(Main *bmain, ID *id, ID **r_owner_id)
{
/* Reuse the implementation of the const access function, which does not change the arguments.
* Add const explicitly to make it clear to the compiler to avoid just calling this function. */
return const_cast<IDOverrideLibrary *>(lib_override_get(const_cast<const Main *>(bmain),
const_cast<const ID *>(id),
const_cast<const ID **>(r_owner_id)));
}
IDOverrideLibrary *BKE_lib_override_library_init(ID *local_id, ID *reference_id)
{
/* If reference_id is nullptr, we are creating an override template for purely local data.
@ -267,7 +280,7 @@ static ID *lib_override_library_create_from(Main *bmain,
/* TODO: This could be simplified by storing a flag in #IDOverrideLibrary
* during the diffing process? */
bool BKE_lib_override_library_is_user_edited(ID *id)
bool BKE_lib_override_library_is_user_edited(const ID *id)
{
if (!ID_IS_OVERRIDE_LIBRARY(id)) {
@ -281,8 +294,8 @@ bool BKE_lib_override_library_is_user_edited(ID *id)
return false;
}
LISTBASE_FOREACH (IDOverrideLibraryProperty *, op, &id->override_library->properties) {
LISTBASE_FOREACH (IDOverrideLibraryPropertyOperation *, opop, &op->operations) {
LISTBASE_FOREACH (const IDOverrideLibraryProperty *, op, &id->override_library->properties) {
LISTBASE_FOREACH (const IDOverrideLibraryPropertyOperation *, opop, &op->operations) {
if ((opop->flag & IDOVERRIDE_LIBRARY_FLAG_IDPOINTER_MATCH_REFERENCE) != 0) {
continue;
}
@ -297,11 +310,11 @@ bool BKE_lib_override_library_is_user_edited(ID *id)
return false;
}
bool BKE_lib_override_library_is_system_defined(Main *bmain, ID *id)
bool BKE_lib_override_library_is_system_defined(const Main *bmain, const ID *id)
{
if (ID_IS_OVERRIDE_LIBRARY(id)) {
ID *override_owner_id;
const ID *override_owner_id;
lib_override_get(bmain, id, &override_owner_id);
return (override_owner_id->override_library->flag & IDOVERRIDE_LIBRARY_FLAG_SYSTEM_DEFINED) !=
0;
@ -963,8 +976,8 @@ static void lib_override_overrides_group_tag_recursive(LibOverrideGroupTagData *
continue;
}
Library *reference_lib = lib_override_get(bmain, id_owner, nullptr)->reference->lib;
ID *to_id_reference = lib_override_get(bmain, to_id, nullptr)->reference;
const Library *reference_lib = lib_override_get(bmain, id_owner, nullptr)->reference->lib;
const ID *to_id_reference = lib_override_get(bmain, to_id, nullptr)->reference;
if (to_id_reference->lib != reference_lib) {
/* We do not override data-blocks from other libraries, nor do we process them. */
continue;