[Bf-blender-cvs] [94cf74afbb1] master: Cleanup/refactor: Undosys: Get rid of the magic values for undo direction.

Bastien Montagne noreply at git.blender.org
Thu Feb 4 22:09:49 CET 2021


Commit: 94cf74afbb1329a9ff099e2ebd7f43ed8313f9ec
Author: Bastien Montagne
Date:   Thu Feb 4 22:03:39 2021 +0100
Branches: master
https://developer.blender.org/rB94cf74afbb1329a9ff099e2ebd7f43ed8313f9ec

Cleanup/refactor: Undosys: Get rid of the magic values for undo direction.

Move `eUndoStepDir` to `BKE_undo_system.h` and use its values
everywhere.

Note that this also introduce the `STEP_INVALID` value in that enum.

Finally, kept the matching struct members in some lower-level readfile
code as an `int` to avoid having to include `BKE_undo_system.h` in a lot
of unrelated files.

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

M	source/blender/blenkernel/BKE_blender_undo.h
M	source/blender/blenkernel/BKE_undo_system.h
M	source/blender/blenkernel/intern/blender_undo.c
M	source/blender/blenkernel/intern/blendfile.c
M	source/blender/blenkernel/intern/undo_system.c
M	source/blender/blenloader/BLO_readfile.h
M	source/blender/blenloader/intern/readfile.c
M	source/blender/blenloader/intern/readfile.h
M	source/blender/editors/armature/editarmature_undo.c
M	source/blender/editors/curve/editcurve_undo.c
M	source/blender/editors/curve/editfont_undo.c
M	source/blender/editors/gpencil/gpencil_undo.c
M	source/blender/editors/include/ED_gpencil.h
M	source/blender/editors/lattice/editlattice_undo.c
M	source/blender/editors/mesh/editmesh_undo.c
M	source/blender/editors/metaball/editmball_undo.c
M	source/blender/editors/physics/particle_edit_undo.c
M	source/blender/editors/sculpt_paint/paint_curve_undo.c
M	source/blender/editors/sculpt_paint/sculpt_undo.c
M	source/blender/editors/space_image/image_undo.c
M	source/blender/editors/space_text/text_undo.c
M	source/blender/editors/undo/ed_undo.c
M	source/blender/editors/undo/memfile_undo.c

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

diff --git a/source/blender/blenkernel/BKE_blender_undo.h b/source/blender/blenkernel/BKE_blender_undo.h
index e5ce91df3fb..1febe75b6f2 100644
--- a/source/blender/blenkernel/BKE_blender_undo.h
+++ b/source/blender/blenkernel/BKE_blender_undo.h
@@ -27,12 +27,14 @@ struct Main;
 struct MemFileUndoData;
 struct bContext;
 
+enum eUndoStepDir;
+
 #define BKE_UNDO_STR_MAX 64
 
 struct MemFileUndoData *BKE_memfile_undo_encode(struct Main *bmain,
                                                 struct MemFileUndoData *mfu_prev);
 bool BKE_memfile_undo_decode(struct MemFileUndoData *mfu,
-                             const int undo_direction,
+                             const enum eUndoStepDir undo_direction,
                              const bool use_old_bmain_data,
                              struct bContext *C);
 void BKE_memfile_undo_free(struct MemFileUndoData *mfu);
diff --git a/source/blender/blenkernel/BKE_undo_system.h b/source/blender/blenkernel/BKE_undo_system.h
index 4c3b21482ea..620496864f5 100644
--- a/source/blender/blenkernel/BKE_undo_system.h
+++ b/source/blender/blenkernel/BKE_undo_system.h
@@ -96,6 +96,12 @@ typedef struct UndoStep {
   /* Over alloc 'type->struct_size'. */
 } UndoStep;
 
+typedef enum eUndoStepDir {
+  STEP_REDO = 1,
+  STEP_UNDO = -1,
+  STEP_INVALID = 0,
+} eUndoStepDir;
+
 typedef enum UndoPushReturn {
   UNDO_PUSH_RET_FAILURE = 0,
   UNDO_PUSH_RET_SUCCESS = (1 << 0),
@@ -127,7 +133,7 @@ typedef struct UndoType {
 
   bool (*step_encode)(struct bContext *C, struct Main *bmain, UndoStep *us);
   void (*step_decode)(
-      struct bContext *C, struct Main *bmain, UndoStep *us, int dir, bool is_final);
+      struct bContext *C, struct Main *bmain, UndoStep *us, const eUndoStepDir dir, bool is_final);
 
   /**
    * \note When freeing all steps,
@@ -203,9 +209,9 @@ UndoStep *BKE_undosys_step_find_by_name_with_type(UndoStack *ustack,
 UndoStep *BKE_undosys_step_find_by_type(UndoStack *ustack, const UndoType *ut);
 UndoStep *BKE_undosys_step_find_by_name(UndoStack *ustack, const char *name);
 
-int BKE_undosys_step_calc_direction(const UndoStack *ustack,
-                                    const UndoStep *us_target,
-                                    const UndoStep *us_reference);
+eUndoStepDir BKE_undosys_step_calc_direction(const UndoStack *ustack,
+                                             const UndoStep *us_target,
+                                             const UndoStep *us_reference);
 
 bool BKE_undosys_step_load_data_ex(UndoStack *ustack,
                                    struct bContext *C,
diff --git a/source/blender/blenkernel/intern/blender_undo.c b/source/blender/blenkernel/intern/blender_undo.c
index 9e061b1ac69..d826aaf24e3 100644
--- a/source/blender/blenkernel/intern/blender_undo.c
+++ b/source/blender/blenkernel/intern/blender_undo.c
@@ -48,6 +48,7 @@
 #include "BKE_context.h"
 #include "BKE_global.h"
 #include "BKE_main.h"
+#include "BKE_undo_system.h"
 
 #include "BLO_readfile.h"
 #include "BLO_undofile.h"
@@ -62,7 +63,7 @@
 #define UNDO_DISK 0
 
 bool BKE_memfile_undo_decode(MemFileUndoData *mfu,
-                             const int undo_direction,
+                             const eUndoStepDir undo_direction,
                              const bool use_old_bmain_data,
                              bContext *C)
 {
@@ -80,7 +81,7 @@ bool BKE_memfile_undo_decode(MemFileUndoData *mfu,
   }
   else {
     struct BlendFileReadParams params = {0};
-    params.undo_direction = undo_direction > 0 ? 1 : -1;
+    params.undo_direction = undo_direction;
     if (!use_old_bmain_data) {
       params.skip_flags |= BLO_READ_SKIP_UNDO_OLD_MAIN;
     }
diff --git a/source/blender/blenkernel/intern/blendfile.c b/source/blender/blenkernel/intern/blendfile.c
index 49475c014cd..32710c4fa60 100644
--- a/source/blender/blenkernel/intern/blendfile.c
+++ b/source/blender/blenkernel/intern/blendfile.c
@@ -57,6 +57,7 @@
 #include "BKE_scene.h"
 #include "BKE_screen.h"
 #include "BKE_studiolight.h"
+#include "BKE_undo_system.h"
 #include "BKE_workspace.h"
 
 #include "BLO_readfile.h"
@@ -148,7 +149,7 @@ static void setup_app_data(bContext *C,
     LOAD_UNDO,
   } mode;
 
-  if (params->undo_direction != 0) {
+  if (params->undo_direction != STEP_INVALID) {
     BLI_assert(bfd->curscene != NULL);
     mode = LOAD_UNDO;
   }
diff --git a/source/blender/blenkernel/intern/undo_system.c b/source/blender/blenkernel/intern/undo_system.c
index 643510cf652..07e7d07f1ef 100644
--- a/source/blender/blenkernel/intern/undo_system.c
+++ b/source/blender/blenkernel/intern/undo_system.c
@@ -176,8 +176,12 @@ static bool undosys_step_encode(bContext *C, Main *bmain, UndoStack *ustack, Und
   return ok;
 }
 
-static void undosys_step_decode(
-    bContext *C, Main *bmain, UndoStack *ustack, UndoStep *us, int dir, bool is_final)
+static void undosys_step_decode(bContext *C,
+                                Main *bmain,
+                                UndoStack *ustack,
+                                UndoStep *us,
+                                const eUndoStepDir dir,
+                                bool is_final)
 {
   CLOG_INFO(&LOG, 2, "addr=%p, name='%s', type='%s'", us, us->name, us->type->name);
 
@@ -677,9 +681,9 @@ UndoStep *BKE_undosys_step_find_by_type(UndoStack *ustack, const UndoType *ut)
  *
  * \return -1 for undo, 1 for redo, 0 in case of error.
  */
-int BKE_undosys_step_calc_direction(const UndoStack *ustack,
-                                    const UndoStep *us_target,
-                                    const UndoStep *us_reference)
+eUndoStepDir BKE_undosys_step_calc_direction(const UndoStack *ustack,
+                                             const UndoStep *us_target,
+                                             const UndoStep *us_reference)
 {
   if (us_reference == NULL) {
     us_reference = ustack->step_active;
@@ -694,26 +698,26 @@ int BKE_undosys_step_calc_direction(const UndoStack *ustack,
    *    to the end of the list, rather than its start. */
   /* NOTE: in case target step is the active one, we assume we are in an undo case... */
   if (ELEM(us_target, us_reference, us_reference->prev)) {
-    return -1;
+    return STEP_UNDO;
   }
   if (us_target == us_reference->next) {
-    return 1;
+    return STEP_REDO;
   }
 
   /* Search forward, and then backward. */
   for (UndoStep *us_iter = us_reference->next; us_iter != NULL; us_iter = us_iter->next) {
     if (us_iter == us_target) {
-      return 1;
+      return STEP_REDO;
     }
   }
   for (UndoStep *us_iter = us_reference->prev; us_iter != NULL; us_iter = us_iter->prev) {
     if (us_iter == us_target) {
-      return -1;
+      return STEP_UNDO;
     }
   }
 
   BLI_assert(!"Target undo step not found, this should not happen and may indicate an undo stack corruption");
-  return 0;
+  return STEP_INVALID;
 }
 
 /**
@@ -752,8 +756,8 @@ bool BKE_undosys_step_load_data_ex(UndoStack *ustack,
   }
 
   /* This considers we are in undo case if both `us_target` and `us_reference` are the same. */
-  const int undo_dir = BKE_undosys_step_calc_direction(ustack, us_target, us_reference);
-  BLI_assert(undo_dir != 0);
+  const eUndoStepDir undo_dir = BKE_undosys_step_calc_direction(ustack, us_target, us_reference);
+  BLI_assert(undo_dir != STEP_INVALID);
 
   /* This will be the active step once the undo process is complete.
    *
diff --git a/source/blender/blenloader/BLO_readfile.h b/source/blender/blenloader/BLO_readfile.h
index 7dbe73cbd6a..237294a6017 100644
--- a/source/blender/blenloader/BLO_readfile.h
+++ b/source/blender/blenloader/BLO_readfile.h
@@ -84,8 +84,8 @@ struct BlendFileReadParams {
   uint skip_flags : 3; /* eBLOReadSkip */
   uint is_startup : 1;
 
-  /** Whether we are reading the memfile for an undo (< 0) or a redo (> 0). */
-  int undo_direction : 2;
+  /** Whether we are reading the memfile for an undo or a redo. */
+  int undo_direction; /* eUndoStepDir */
 };
 
 /* skip reading some data-block types (may want to skip screen data too). */
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 2192e9a378f..a63d95c1823 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -94,6 +94,7 @@
 #include "BKE_report.h"
 #include "BKE_scene.h"
 #include "BKE_screen.h"
+#include "BKE_undo_system.h"
 #include "BKE_workspace.h"
 
 #include "DRW_engine.h"
@@ -1294,12 +1295,12 @@ static ssize_t fd_read_from_memfile(FileData *filedata,
       seek += readsize;
       if (r_is_memchunck_identical != NULL) {
         /* `is_identical` of current chunk represents whether it changed compared to previous undo
-         * step. this is fine in redo case (filedata->undo_direction > 0), but not in undo case,
-         * where we need an extra flag defined when saving the next (future) step after the one we
-         * want to restore, as we are supposed to 'come from' that future undo step, and not the
-         * one before current one. */
-        *r_is_memchunck_identical &= filedata->undo_direction > 0 ? chunk->is_identical :
-                                                                    chunk->is_identical_future;
+         * step. this is fine in redo case, but not in undo case, where we need an extra flag
+         * defined when saving the next (future) step after the one we want to restore, as we are
+         * supposed to 'come from' that future undo step, and not the one before current one. */
+        *r_is_memchunck_identical &= filedata->undo_direction == STEP_REDO ?
+                                         chunk->is_identical :
+                                         chunk->is_identical_future;
       }
     } while (totread < size);
 
@@ -2400,11 +2401,12 @@ static int direct_link_id_restore_recalc(const FileData *fd,
 
     /* Tags that were set between the target state and the current state,
      * that we need to perform again. */
-    if (fd->undo_direction < 0) {
+    if (fd->undo_direction == STEP_UNDO) {
       /* Undo: tags from target to the current state. */
       recalc |= id_current->recalc_up_to_undo_push;
     }
     else {
+      BLI_assert(fd->undo_direction == STEP_REDO);
  

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list