[Bf-blender-cvs] [06c2bb97970] undo-experiments-swap-reread-datablocks: undoexp: BKE_id_swap: add 'full' swapping.

Bastien Montagne noreply at git.blender.org
Tue Jan 28 21:26:07 CET 2020


Commit: 06c2bb979709bec07ecf07231b00ddc7a95c705c
Author: Bastien Montagne
Date:   Tue Jan 28 21:06:50 2020 +0100
Branches: undo-experiments-swap-reread-datablocks
https://developer.blender.org/rB06c2bb979709bec07ecf07231b00ddc7a95c705c

undoexp: BKE_id_swap: add 'full' swapping.

Add a new func to do a full swapping of two ID data, including all of
the ID strcut content itself.

Also make both idswap functions resilient to a NULL given bmain pointer
(they simply do not do internal remapping of pointers to itself then).

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

M	source/blender/blenkernel/BKE_library.h
M	source/blender/blenkernel/intern/library.c

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

diff --git a/source/blender/blenkernel/BKE_library.h b/source/blender/blenkernel/BKE_library.h
index c41cd50eba5..fa4c9f2ee8d 100644
--- a/source/blender/blenkernel/BKE_library.h
+++ b/source/blender/blenkernel/BKE_library.h
@@ -198,7 +198,10 @@ bool id_single_user(struct bContext *C,
 bool BKE_id_copy_is_allowed(const struct ID *id);
 bool BKE_id_copy(struct Main *bmain, const struct ID *id, struct ID **newid);
 bool BKE_id_copy_ex(struct Main *bmain, const struct ID *id, struct ID **r_newid, const int flag);
+
 void BKE_id_swap(struct Main *bmain, struct ID *id_a, struct ID *id_b);
+void BKE_id_full_swap(struct Main *bmain, struct ID *id_a, struct ID *id_b);
+
 void id_sort_by_name(struct ListBase *lb, struct ID *id, struct ID *id_sorting_hint);
 void BKE_id_expand_local(struct Main *bmain, struct ID *id);
 void BKE_id_copy_ensure_local(struct Main *bmain, const struct ID *old_id, struct ID *new_id);
diff --git a/source/blender/blenkernel/intern/library.c b/source/blender/blenkernel/intern/library.c
index 49faeb7aa0d..28374956ab7 100644
--- a/source/blender/blenkernel/intern/library.c
+++ b/source/blender/blenkernel/intern/library.c
@@ -803,7 +803,7 @@ bool BKE_id_copy(Main *bmain, const ID *id, ID **newid)
  * Does a mere memory swap over the whole IDs data (including type-specific memory).
  * \note Most internal ID data itself is not swapped (only IDProperties are).
  */
-void BKE_id_swap(Main *bmain, ID *id_a, ID *id_b)
+static void id_swap(Main *bmain, ID *id_a, ID *id_b, const bool do_full_id)
 {
   BLI_assert(GS(id_a->name) == GS(id_b->name));
 
@@ -857,17 +857,45 @@ void BKE_id_swap(Main *bmain, ID *id_a, ID *id_b)
 
 #undef CASE_SWAP
 
-  /* Restore original ID's internal data. */
-  *id_a = id_a_back;
-  *id_b = id_b_back;
+  if (!do_full_id) {
+    /* Restore original ID's internal data. */
+    *id_a = id_a_back;
+    *id_b = id_b_back;
+
+    /* Exception: IDProperties. */
+    id_a->properties = id_b_back.properties;
+    id_b->properties = id_a_back.properties;
+  }
 
-  /* Exception: IDProperties. */
-  id_a->properties = id_b_back.properties;
-  id_b->properties = id_a_back.properties;
+  if (bmain != NULL) {
+    /* Swap will have broken internal references to itself, restore them. */
+    BKE_libblock_relink_ex(bmain, id_a, id_b, id_a, ID_REMAP_SKIP_NEVER_NULL_USAGE);
+    BKE_libblock_relink_ex(bmain, id_b, id_a, id_b, ID_REMAP_SKIP_NEVER_NULL_USAGE);
+  }
+}
 
-  /* Swap will have broken internal references to itself, restore them. */
-  BKE_libblock_relink_ex(bmain, id_a, id_b, id_a, ID_REMAP_SKIP_NEVER_NULL_USAGE);
-  BKE_libblock_relink_ex(bmain, id_b, id_a, id_b, ID_REMAP_SKIP_NEVER_NULL_USAGE);
+/**
+ * Does a mere memory swap over the whole IDs data (including type-specific memory).
+ * \note Most internal ID data itself is not swapped (only IDProperties are).
+ *
+ * \param bmain May be NULL, in which case there will be no remapping of internal pointers to
+ * itself.
+ */
+void BKE_id_swap(Main *bmain, ID *id_a, ID *id_b)
+{
+  id_swap(bmain, id_a, id_b, false);
+}
+
+/**
+ * Does a mere memory swap over the whole IDs data (including type-specific memory).
+ * \note All internal ID data itself is also swapped.
+ *
+ * \param bmain May be NULL, in which case there will be no remapping of internal pointers to
+ * itself.
+ */
+void BKE_id_full_swap(Main *bmain, ID *id_a, ID *id_b)
+{
+  id_swap(bmain, id_a, id_b, true);
 }
 
 /** Does *not* set ID->newid pointer. */



More information about the Bf-blender-cvs mailing list