[Bf-blender-cvs] [a40b6111283] master: Cleanup: Improve const correctness of ID functions

Hans Goudey noreply at git.blender.org
Tue May 31 17:31:38 CEST 2022


Commit: a40b6111283186b188864cd427ac2ec2970acfef
Author: Hans Goudey
Date:   Tue May 31 17:31:32 2022 +0200
Branches: master
https://developer.blender.org/rBa40b6111283186b188864cd427ac2ec2970acfef

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

===================================================================

M	source/blender/blenkernel/BKE_lib_id.h
M	source/blender/blenkernel/BKE_lib_override.h
M	source/blender/blenkernel/intern/lib_id.c
M	source/blender/blenkernel/intern/lib_override.cc

===================================================================

diff --git a/source/blender/blenkernel/BKE_lib_id.h b/source/blender/blenkernel/BKE_lib_id.h
index 504ab47c1c5..beac608a138 100644
--- a/source/blender/blenkernel/BKE_lib_id.h
+++ b/source/blender/blenkernel/BKE_lib_id.h
@@ -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.
diff --git a/source/blender/blenkernel/BKE_lib_override.h b/source/blender/blenkernel/BKE_lib_override.h
index 38de4bebdbd..86630a6ab44 100644
--- a/source/blender/blenkernel/BKE_lib_override.h
+++ b/source/blender/blenkernel/BKE_lib_override.h
@@ -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
diff --git a/source/blender/blenkernel/intern/lib_id.c b/source/blender/blenkernel/intern/lib_id.c
index 528681d196e..90a4853fd3e 100644
--- a/source/blender/blenkernel/intern/lib_id.c
+++ b/source/blender/blenkernel/intern/lib_id.c
@@ -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));
 }
diff --git a/source/blender/blenkernel/intern/lib_override.cc b/source/blender/blenkernel/intern/lib_override.cc
index fb538eea415..168feebedec 100644
--- a/source/blender/blenkernel/intern/lib_override.cc
+++ b/source/blender/blenkernel/intern/lib_override.cc
@@ -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;



More information about the Bf-blender-cvs mailing list