Reworked compatibility computation.
[gedcom-parse.git] / gom / func_template.h
index a21424d1b425b1847d3f2123b556da5a0758671d..769debb2a96e84c350da8c118a6e0067885bd7dc 100644 (file)
@@ -29,8 +29,9 @@
 #define DESTROYFUNC(STRUCTTYPE)  destroy_ ## STRUCTTYPE ## _record
 #define GETXREFFUNC(STRUCTTYPE)  gom_get_ ## STRUCTTYPE ## _by_xref
 #define CLEANFUNC(STRUCTTYPE)    STRUCTTYPE ## _cleanup
-#define ADDFUNC(STRUCTTYPE)      gom_add_ ## STRUCTTYPE
-#define SUB_ADDFUNC(STRUCTTYPE)  gom_add_ ## STRUCTTYPE
+#define ADDFUNC(STRUCTTYPE)      gom_new_ ## STRUCTTYPE
+#define SUB_SETFUNC(STRUCTTYPE)  gom_set_new_ ## STRUCTTYPE
+#define UNREFALLFUNC(STRUCTTYPE) STRUCTTYPE ## _unref_all
 #define DELETEFUNC(STRUCTTYPE)   gom_delete_ ## STRUCTTYPE
 #define SUB_DELETEFUNC(STRUCTTYPE) gom_delete_ ## STRUCTTYPE
 #define ADDFUNC2(T1,T2)          T1 ## _add_ ## T2
@@ -48,6 +49,9 @@
 #define DECLARE_CLEANFUNC(STRUCTTYPE)                                         \
   void CLEANFUNC(STRUCTTYPE)(struct STRUCTTYPE* obj)
 
+#define DECLARE_UNREFALLFUNC(STRUCTTYPE)                                      \
+  void UNREFALLFUNC(STRUCTTYPE)(struct STRUCTTYPE* obj)
+
 #define DECLARE_ADDFUNC2(STRUCTTYPE,T2)                                       \
   void ADDFUNC2(STRUCTTYPE,T2)(Gom_ctxt ctxt, struct T2* obj)
 
       (FIRSTVAL)->previous = VAL->previous;                                   \
   }
 
+#define MOVE_CHAIN_ELT(STRUCTTYPE, DIR, FIRSTVAL, VAL)                        \
+  {                                                                           \
+    struct STRUCTTYPE *first, *second;                                        \
+    if (DIR == MOVE_UP) {                                                     \
+      first  = VAL->previous;                                                 \
+      second = VAL;                                                           \
+    }                                                                         \
+    else {                                                                    \
+      first  = VAL;                                                           \
+      second = VAL->next;                                                     \
+    }                                                                         \
+    if (second && (second != FIRSTVAL)) {                                     \
+      if (first != FIRSTVAL)                                                  \
+       first->previous->next  = second;                                      \
+      else                                                                    \
+       FIRSTVAL               = second;                                      \
+                                                                              \
+      if (second->next)                                                       \
+       second->next->previous = first;                                       \
+      else                                                                    \
+       (FIRSTVAL)->previous   = first;                                       \
+                                                                              \
+      first->next            = second->next;                                  \
+      second->next           = first;                                         \
+                                                                              \
+      second->previous       = first->previous;                               \
+      first->previous        = second;                                        \
+    }                                                                         \
+    else {                                                                    \
+      gom_move_error(#STRUCTTYPE);                                            \
+    }                                                                         \
+  }
+
 #define MAKE_CHAIN_ELT(STRUCTTYPE, FIRSTVAL, VAL)                             \
   {                                                                           \
     VAL = (struct STRUCTTYPE*) malloc(sizeof(struct STRUCTTYPE));             \
   }
 
 #define DEFINE_DESTROYFUNC(STRUCTTYPE,FIRSTVAL)                               \
-  void CLEANFUNC(STRUCTTYPE)(struct STRUCTTYPE* obj);                         \
+  DECLARE_CLEANFUNC(STRUCTTYPE);                                              \
   void DESTROYFUNC(STRUCTTYPE)(struct STRUCTTYPE* obj) {                      \
     if (obj) {                                                                \
       CLEANFUNC(STRUCTTYPE)(obj);                                             \
     return obj;                                                               \
   }
 
-#define DEFINE_SUB_ADDFUNC(STRUCTTYPE)                                        \
-  struct STRUCTTYPE *SUB_ADDFUNC(STRUCTTYPE)(struct STRUCTTYPE** addto)       \
+#define DEFINE_SUB_SETFUNC(STRUCTTYPE)                                        \
+  struct STRUCTTYPE *SUB_SETFUNC(STRUCTTYPE)(struct STRUCTTYPE** addto)       \
   {                                                                           \
     struct STRUCTTYPE *obj = NULL;                                            \
     if (addto && ! *addto) {                                                  \
     return obj;                                                               \
   }
 
-/* TODO: Check whether there are still xrefs linked in */
 #define DEFINE_DELETEFUNC(STRUCTTYPE)                                         \
+  DECLARE_UNREFALLFUNC(STRUCTTYPE);                                           \
   int DELETEFUNC(STRUCTTYPE)(struct STRUCTTYPE* obj)                          \
   {                                                                           \
     int result = 1;                                                           \
     if (obj) {                                                                \
       result = gedcom_delete_xref(obj->xrefstr);                              \
-      if (result == 0)                                                        \
+      if (result == 0) {                                                      \
+        UNREFALLFUNC(STRUCTTYPE)(obj);                                        \
        DESTROYFUNC(STRUCTTYPE)(obj);                                         \
+      }                                                                       \
     }                                                                         \
     return result;                                                            \
   }
   {                                                                           \
     int result = 1;                                                           \
     if (obj && *obj) {                                                        \
+      UNREFALLFUNC(STRUCTTYPE)(*obj);                                         \
       CLEANFUNC(STRUCTTYPE)(*obj);                                            \
       SAFE_FREE(*obj);                                                        \
       result = 0;                                                             \