ucx/cx/collection.h

branch
ucx-3.1
changeset 816
839fefbdedc7
parent 775
e5909dff0dbf
--- a/ucx/cx/collection.h	Sat Apr 20 13:01:58 2024 +0200
+++ b/ucx/cx/collection.h	Thu May 23 22:35:45 2024 +0200
@@ -30,7 +30,6 @@
  * \brief Common definitions for various collection implementations.
  * \author Mike Becker
  * \author Olaf Wintermann
- * \version 3.0
  * \copyright 2-Clause BSD License
  */
 
@@ -39,6 +38,7 @@
 
 #include "allocator.h"
 #include "iterator.h"
+#include "compare.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -50,56 +50,73 @@
 #define CX_STORE_POINTERS 0
 
 /**
- * A comparator function comparing two collection elements.
+ * Base attributes of a collection.
  */
-typedef int(*cx_compare_func)(
-        void const *left,
-        void const *right
-);
+struct cx_collection_s {
+    /**
+     * The allocator to use.
+     */
+    CxAllocator const *allocator;
+    /**
+     * The comparator function for the elements.
+     */
+    cx_compare_func cmpfunc;
+    /**
+     * The size of each element.
+     */
+    size_t elem_size;
+    /**
+     * The number of currently stored elements.
+     */
+    size_t size;
+    /**
+     * An optional simple destructor for the collection's elements.
+     *
+     * @attention Read the documentation of the particular collection implementation
+     * whether this destructor shall only destroy the contents or also free the memory.
+     */
+    cx_destructor_func simple_destructor;
+    /**
+     * An optional advanced destructor for the collection's elements.
+     *
+     * @attention Read the documentation of the particular collection implementation
+     * whether this destructor shall only destroy the contents or also free the memory.
+     */
+    cx_destructor_func2 advanced_destructor;
+    /**
+     * The pointer to additional data that is passed to the advanced destructor.
+     */
+    void *destructor_data;
+    /**
+     * Indicates if this list is supposed to store pointers
+     * instead of copies of the actual objects.
+     */
+    bool store_pointer;
+};
 
 /**
  * Use this macro to declare common members for a collection structure.
  */
-#define CX_COLLECTION_MEMBERS \
-    /** \
-     * The allocator to use. \
-     */ \
-    CxAllocator const *allocator; \
-    /** \
-     * The comparator function for the elements. \
-     */ \
-    cx_compare_func cmpfunc; \
-    /** \
-     * The size of each element. \
-     */ \
-    size_t item_size; \
-    /** \
-     * The number of currently stored elements. \
-     */ \
-    size_t size; \
-    /** \
-     * An optional simple destructor for the collection's elements. \
-     * \
-     * @attention Read the documentation of the particular collection implementation \
-     * whether this destructor shall only destroy the contents or also free the memory. \
-     */ \
-    cx_destructor_func simple_destructor; \
-    /** \
-     * An optional advanced destructor for the collection's elements. \
-     * \
-     * @attention Read the documentation of the particular collection implementation \
-     * whether this destructor shall only destroy the contents or also free the memory. \
-     */ \
-    cx_destructor_func2 advanced_destructor; \
-    /** \
-     * The pointer to additional data that is passed to the advanced destructor. \
-     */ \
-    void *destructor_data; \
-    /** \
-     * Indicates if this instance of a collection is supposed to store pointers \
-     * instead of copies of the actual objects. \
-     */ \
-    bool store_pointer;
+#define CX_COLLECTION_BASE struct cx_collection_s collection
+
+/**
+ * Sets a simple destructor function for this collection.
+ *
+ * @param c the collection
+ * @param destr the destructor function
+ */
+#define cxDefineDestructor(c, destr) \
+    (c)->collection.simple_destructor = (cx_destructor_func) destr
+
+/**
+ * Sets a simple destructor function for this collection.
+ *
+ * @param c the collection
+ * @param destr the destructor function
+ */
+#define cxDefineAdvancedDestructor(c, destr, data) \
+    (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \
+    (c)->collection.destructor_data = data
 
 /**
  * Invokes the simple destructor function for a specific element.
@@ -111,7 +128,7 @@
  * @param e the element
  */
 #define cx_invoke_simple_destructor(c, e) \
-    (c)->simple_destructor((c)->store_pointer ? (*((void **) (e))) : (e))
+    (c)->collection.simple_destructor((c)->collection.store_pointer ? (*((void **) (e))) : (e))
 
 /**
  * Invokes the advanced destructor function for a specific element.
@@ -123,8 +140,8 @@
  * @param e the element
  */
 #define cx_invoke_advanced_destructor(c, e) \
-    (c)->advanced_destructor((c)->destructor_data, \
-    (c)->store_pointer ? (*((void **) (e))) : (e))
+    (c)->collection.advanced_destructor((c)->collection.destructor_data, \
+    (c)->collection.store_pointer ? (*((void **) (e))) : (e))
 
 
 /**
@@ -137,8 +154,8 @@
  * @param e the element
  */
 #define cx_invoke_destructor(c, e) \
-    if ((c)->simple_destructor) cx_invoke_simple_destructor(c,e); \
-    if ((c)->advanced_destructor) cx_invoke_advanced_destructor(c,e)
+    if ((c)->collection.simple_destructor) cx_invoke_simple_destructor(c,e); \
+    if ((c)->collection.advanced_destructor) cx_invoke_advanced_destructor(c,e)
 
 #ifdef __cplusplus
 } // extern "C"

mercurial