[dia/zbrown/object-change: 11/16] db: port to DiaObjectChange




commit e76b33cd2a572b3fb06aad8b62a3ef132ec1df3c
Author: Zander Brown <zbrown gnome org>
Date:   Sat Oct 10 22:20:19 2020 +0100

    db: port to DiaObjectChange

 objects/Database/compound.c | 128 +++++++++++++++++++++++++++-----------------
 objects/Database/database.h |  42 ++++++++-------
 objects/Database/table.c    |  91 +++++++++++++++++++------------
 3 files changed, 161 insertions(+), 100 deletions(-)
---
diff --git a/objects/Database/compound.c b/objects/Database/compound.c
index 8dd41455c..ef3cbda71 100644
--- a/objects/Database/compound.c
+++ b/objects/Database/compound.c
@@ -32,9 +32,9 @@
 #include "attributes.h"
 #include "geometry.h"
 #include "propinternals.h"
-#include "dia-object-change-legacy.h"
 
 #include "debug.h"
+#include "database.h"
 
 /* ------------------------------------------------------------------------ */
 
@@ -82,33 +82,39 @@ struct _ArmHandleState {
   ConnectionPoint * connected_to;
 };
 
-struct _CompoundChange {
-  ObjectChange obj_change;
-  Compound * obj;
-  CompoundState * saved_state;
+
+struct _DiaDBCompoundObjectChange {
+  DiaObjectChange  obj_change;
+  Compound        *obj;
+  CompoundState   *saved_state;
 };
 
-struct _MountPointMoveChange {
-  ObjectChange obj_change;
 
-  Compound * obj;
-  Point saved_pos;
+DIA_DEFINE_OBJECT_CHANGE (DiaDBCompoundObjectChange,
+                          dia_db_compound_object_change)
+
+
+struct _DiaDBCompoundMountObjectChange {
+  DiaObjectChange  obj_change;
+  Compound        *obj;
+  Point            saved_pos;
 };
 
+
+DIA_DEFINE_OBJECT_CHANGE (DiaDBCompoundMountObjectChange,
+                          dia_db_compound_mount_object_change)
+
+
 /* ------------------------------------------------------------------------ */
 
 static CompoundState * compound_state_new (Compound *);
 static void compound_state_free (CompoundState *);
 static void compound_state_set (CompoundState *, Compound *);
-static DiaObjectChange *compound_change_new (Compound *, CompoundState *);
-static void compound_change_apply (CompoundChange *, DiaObject *);
-static void compound_change_free (CompoundChange *);
 
+static DiaObjectChange *compound_change_new           (Compound             *,
+                                                       CompoundState        *);
 static DiaObjectChange *mount_point_move_change_new   (Compound             *,
                                                        Point                *);
-static void             mount_point_move_change_apply (MountPointMoveChange *,
-                                                       DiaObject            *);
-static void             mount_point_move_change_free  (MountPointMoveChange *);
 
 static DiaObject * compound_create (Point *, void *, Handle **, Handle **);
 static DiaObject * compound_load (ObjectNode obj_node, int version,DiaContext *ctx);
@@ -309,67 +315,61 @@ compound_state_free (CompoundState * state)
 }
 
 
-static DiaObjectChange *
-compound_change_new (Compound *comp, CompoundState *state)
+static void
+compound_change_apply (DiaDBCompoundObjectChange *change, DiaObject *obj)
 {
-  CompoundChange *change;
-
-  change = g_new (CompoundChange, 1);
+  CompoundState *old_state;
 
-  change->obj_change.apply = (ObjectChangeApplyFunc) compound_change_apply;
-  change->obj_change.revert = (ObjectChangeRevertFunc) compound_change_apply;
-  change->obj_change.free = (ObjectChangeFreeFunc) compound_change_free;
+  old_state = compound_state_new (change->obj);
 
-  change->obj = comp;
-  change->saved_state = state;
+  compound_state_set (change->saved_state, change->obj);
+  compound_state_free (change->saved_state);
 
-  return dia_object_change_legacy_new ((ObjectChange *) change);
+  change->saved_state = old_state;
 }
 
+
 static void
-compound_change_apply (CompoundChange * change, DiaObject * obj)
+dia_db_compound_object_change_apply (DiaObjectChange *self, DiaObject *obj)
 {
-  CompoundState * old_state;
+  compound_change_apply (DIA_DB_COMPOUND_OBJECT_CHANGE (self), obj);
+}
 
-  old_state = compound_state_new (change->obj);
 
-  compound_state_set (change->saved_state, change->obj);
-  compound_state_free (change->saved_state);
-
-  change->saved_state = old_state;
+static void
+dia_db_compound_object_change_revert (DiaObjectChange *self, DiaObject *obj)
+{
+  compound_change_apply (DIA_DB_COMPOUND_OBJECT_CHANGE (self), obj);
 }
 
+
 static void
-compound_change_free (CompoundChange * change)
+dia_db_compound_object_change_free (DiaObjectChange *self)
 {
+  DiaDBCompoundObjectChange *change = DIA_DB_COMPOUND_OBJECT_CHANGE (self);
+
   compound_state_free (change->saved_state);
 }
 
 
 static DiaObjectChange *
-mount_point_move_change_new (Compound * comp, Point * pos)
+compound_change_new (Compound *comp, CompoundState *state)
 {
-  MountPointMoveChange * change;
+  DiaDBCompoundObjectChange *change;
 
-  change = g_new (MountPointMoveChange, 1);
-  change->obj_change.apply =
-    (ObjectChangeApplyFunc) mount_point_move_change_apply;
-  change->obj_change.revert =
-    (ObjectChangeRevertFunc) mount_point_move_change_apply;
-  change->obj_change.free =
-    (ObjectChangeFreeFunc) mount_point_move_change_free;
+  change = dia_object_change_new (DIA_DB_TYPE_COMPOUND_OBJECT_CHANGE);
 
   change->obj = comp;
-  change->saved_pos = *pos;
+  change->saved_state = state;
 
-  return dia_object_change_legacy_new ((ObjectChange *) change);
+  return DIA_OBJECT_CHANGE (change);
 }
 
 
 static void
-mount_point_move_change_apply (MountPointMoveChange * change, DiaObject * obj)
+mount_point_move_change_apply (DiaDBCompoundMountObjectChange *change, DiaObject *obj)
 {
-  Compound * comp = change->obj;
+  Compound *comp = change->obj;
   Point old_pos = comp->handles[0].pos;
 
   comp->handles[0].pos = change->saved_pos;
@@ -381,12 +381,44 @@ mount_point_move_change_apply (MountPointMoveChange * change, DiaObject * obj)
   compound_sanity_check (comp, "After applying mount point move change");
 }
 
+
+static void
+dia_db_compound_mount_object_change_apply (DiaObjectChange *self, DiaObject *obj)
+{
+  mount_point_move_change_apply (DIA_DB_COMPOUND_MOUNT_OBJECT_CHANGE (self),
+                                 obj);
+}
+
+
+static void
+dia_db_compound_mount_object_change_revert (DiaObjectChange *self, DiaObject *obj)
+{
+  mount_point_move_change_apply (DIA_DB_COMPOUND_MOUNT_OBJECT_CHANGE (self),
+                                 obj);
+}
+
+
 static void
-mount_point_move_change_free (MountPointMoveChange * change)
+dia_db_compound_mount_object_change_free (DiaObjectChange *change)
 {
   /* currently there is nothing to be done here */
 }
 
+
+static DiaObjectChange *
+mount_point_move_change_new (Compound *comp, Point *pos)
+{
+  DiaDBCompoundMountObjectChange *change;
+
+  change = dia_object_change_new (DIA_DB_TYPE_COMPOUND_MOUNT_OBJECT_CHANGE);
+
+  change->obj = comp;
+  change->saved_pos = *pos;
+
+  return DIA_OBJECT_CHANGE (change);
+}
+
+
 /* ------------------------------------------------------------------------ */
 
 static DiaObject *
diff --git a/objects/Database/database.h b/objects/Database/database.h
index 66a16dbf0..d7dd4f4a8 100644
--- a/objects/Database/database.h
+++ b/objects/Database/database.h
@@ -16,13 +16,33 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
-#ifndef DATABASE_H
-#define DATABASE_H
+#pragma once
 
 #include "element.h"
 #include "connectionpoint.h"
 #include "orth_conn.h"
-#include "dia-object-change-legacy.h"
+
+G_BEGIN_DECLS
+
+#define DIA_DB_TYPE_TABLE_OBJECT_CHANGE dia_db_table_object_change_get_type ()
+G_DECLARE_FINAL_TYPE (DiaDBTableObjectChange,
+                      dia_db_table_object_change,
+                      DIA_DB, TABLE_OBJECT_CHANGE,
+                      DiaObjectChange)
+
+
+#define DIA_DB_TYPE_COMPOUND_OBJECT_CHANGE dia_db_compound_object_change_get_type ()
+G_DECLARE_FINAL_TYPE (DiaDBCompoundObjectChange,
+                      dia_db_compound_object_change,
+                      DIA_DB, COMPOUND_OBJECT_CHANGE,
+                      DiaObjectChange)
+
+
+#define DIA_DB_TYPE_COMPOUND_MOUNT_OBJECT_CHANGE dia_db_compound_mount_object_change_get_type ()
+G_DECLARE_FINAL_TYPE (DiaDBCompoundMountObjectChange,
+                      dia_db_compound_mount_object_change,
+                      DIA_DB, COMPOUND_MOUNT_OBJECT_CHANGE,
+                      DiaObjectChange)
 
 
 #define IS_NOT_EMPTY(str) (((str) != NULL) && ((str)[0] != '\0'))
@@ -33,7 +53,6 @@ typedef struct _Table Table;
 typedef struct _TableAttribute TableAttribute;
 typedef struct _TableReference TableReference;
 typedef struct _TableState TableState;
-typedef struct _TableChange TableChange;
 typedef struct _Disconnect Disconnect;
 
 struct _Table {
@@ -111,19 +130,6 @@ struct _TableState {
   GList * attributes;
 };
 
-struct _TableChange {
-  ObjectChange obj_change;
-
-  Table * obj;
-
-  GList * added_cp;
-  GList * deleted_cp;
-  GList * disconnected;
-
-  gint applied;
-
-  TableState * saved_state;
-};
 
 struct _TableReference {
   OrthConn orth; /* inheritance */
@@ -152,4 +158,4 @@ struct _TableReference {
   Alignment ep_desc_text_align; /* end-point */
 };
 
-#endif /* DATABASE_H */
+G_END_DECLS
diff --git a/objects/Database/table.c b/objects/Database/table.c
index 514d9bc72..90cc66c89 100644
--- a/objects/Database/table.c
+++ b/objects/Database/table.c
@@ -1511,35 +1511,57 @@ table_state_free (TableState * state)
   g_free (state);
 }
 
+
+struct _DiaDBTableObjectChange {
+  DiaObjectChange obj_change;
+
+  Table *obj;
+
+  GList *added_cp;
+  GList *deleted_cp;
+  GList *disconnected;
+
+  int applied;
+
+  TableState *saved_state;
+};
+
+
+DIA_DEFINE_OBJECT_CHANGE (DiaDBTableObjectChange, dia_db_table_object_change)
+
+
 /**
  * Called to UNDO a change on the table object.
  */
 static void
-table_change_revert (TableChange *change, DiaObject *obj)
+dia_db_table_object_change_revert (DiaObjectChange *self, DiaObject *obj)
 {
+  DiaDBTableObjectChange *change = DIA_DB_TABLE_OBJECT_CHANGE (self);
   TableState *old_state;
   GList *list;
 
-  old_state = table_state_new(change->obj);
+  old_state = table_state_new (change->obj);
 
-  table_state_set(change->saved_state, change->obj);
+  table_state_set (change->saved_state, change->obj);
 
   list = change->disconnected;
   while (list) {
     Disconnect *dis = (Disconnect *)list->data;
 
-    object_connect(dis->other_object, dis->other_handle, dis->cp);
+    object_connect (dis->other_object, dis->other_handle, dis->cp);
 
-    list = g_list_next(list);
+    list = g_list_next (list);
   }
 
   change->saved_state = old_state;
   change->applied = FALSE;
 }
 
+
 static void
-table_change_free (TableChange *change)
+dia_db_table_object_change_free (DiaObjectChange *self)
 {
+  DiaDBTableObjectChange *change = DIA_DB_TABLE_OBJECT_CHANGE (self);
   GList * free_list, * lst;
 
   table_state_free (change->saved_state);
@@ -1549,28 +1571,31 @@ table_change_free (TableChange *change)
     : change->added_cp;
 
   lst = free_list;
-  while (lst)
-    {
-      ConnectionPoint * cp = (ConnectionPoint *) lst->data;
-      g_assert (cp->connected == NULL);
-      object_remove_connections_to (cp);
-      g_clear_pointer (&cp, g_free);
+  while (lst) {
+    ConnectionPoint * cp = (ConnectionPoint *) lst->data;
+    g_assert (cp->connected == NULL);
+    object_remove_connections_to (cp);
+    g_clear_pointer (&cp, g_free);
 
-      lst = g_list_next (lst);
-    }
+    lst = g_list_next (lst);
+  }
   g_list_free (free_list);
 }
 
+
 /**
  * Called to REDO a change on the table object.
  */
 static void
-table_change_apply (TableChange * change, DiaObject * obj)
+dia_db_table_object_change_apply (DiaObjectChange *self, DiaObject *obj)
 {
-  TableState * old_state;
-  GList * lst;
+  DiaDBTableObjectChange *change = DIA_DB_TABLE_OBJECT_CHANGE (self);
+  TableState *old_state;
+  GList *lst;
 
-  g_print ("apply (o: 0x%08x) (c: 0x%08x)\n", GPOINTER_TO_UINT(obj), GPOINTER_TO_UINT(change));
+  g_print ("apply (o: 0x%08x) (c: 0x%08x)\n",
+           GPOINTER_TO_UINT (obj),
+           GPOINTER_TO_UINT (change));
 
   /* first the get the current state for later use */
   old_state = table_state_new (change->obj);
@@ -1578,29 +1603,27 @@ table_change_apply (TableChange * change, DiaObject * obj)
   table_state_set (change->saved_state, change->obj);
 
   lst = change->disconnected;
-  while (lst)
-    {
-      Disconnect * dis = (Disconnect *) lst->data;
-      object_unconnect (dis->other_object, dis->other_handle);
-      lst = g_list_next (lst);
-    }
+  while (lst) {
+    Disconnect * dis = (Disconnect *) lst->data;
+    object_unconnect (dis->other_object, dis->other_handle);
+    lst = g_list_next (lst);
+  }
+
   change->saved_state = old_state;
   change->applied = TRUE;
 }
 
 
 static DiaObjectChange *
-table_change_new (Table * table, TableState * saved_state,
-                  GList * added, GList * deleted,
-                  GList * disconnects)
+table_change_new (Table       *table,
+                  TableState *saved_state,
+                  GList      *added,
+                  GList      *deleted,
+                  GList      *disconnects)
 {
-  TableChange * change;
-
-  change = g_new (TableChange, 1);
+  DiaDBTableObjectChange * change;
 
-  change->obj_change.apply = (ObjectChangeApplyFunc) table_change_apply;
-  change->obj_change.revert = (ObjectChangeRevertFunc) table_change_revert;
-  change->obj_change.free = (ObjectChangeFreeFunc) table_change_free;
+  change = dia_object_change_new (DIA_DB_TYPE_TABLE_OBJECT_CHANGE);
 
   change->obj = table;
   change->added_cp = added;
@@ -1609,5 +1632,5 @@ table_change_new (Table * table, TableState * saved_state,
   change->applied = TRUE;
   change->saved_state = saved_state;
 
-  return dia_object_change_legacy_new ((ObjectChange *) change);
+  return DIA_OBJECT_CHANGE (change);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]