changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619 default tip

Thu, 18 Dec 2025 18:07:29 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 18 Dec 2025 18:07:29 +0100
changeset 1625
89a2d53308e4
parent 1624
aab23807d562

changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619

docs/Writerside/topics/array_list.h.md file | annotate | diff | comparison | revisions
src/cx/array_list.h file | annotate | diff | comparison | revisions
src/json.c file | annotate | diff | comparison | revisions
tests/test_list.c file | annotate | diff | comparison | revisions
--- a/docs/Writerside/topics/array_list.h.md	Thu Dec 18 16:44:11 2025 +0100
+++ b/docs/Writerside/topics/array_list.h.md	Thu Dec 18 18:07:29 2025 +0100
@@ -81,23 +81,23 @@
 ```C
 #include <cx/array_list.h>
 
-int cx_array_add(CxArray array, const void *element);
+int cx_array_add(CxArray array, Any element);
 
 int cx_array_add_array(CxArray array, const void *other, size_t n);
 
-int cx_array_insert(CxArray array, size_t index, const void *element);
+int cx_array_insert(CxArray array, size_t index, Any element);
 
 int cx_array_insert_array(CxArray array,
         size_t index, const void *other, size_t n);
 
 int cx_array_add_a(const CxAllocator* allocator,
-        CxArray array, const void *element);
+        CxArray array, Any element);
 
 int cx_array_add_array(const CxAllocator* allocator,
         CxArray array, const void *other, size_t n);
 
 int cx_array_insert_a(const CxAllocator* allocator,
-        CxArray array, size_t index, const void *element);
+        CxArray array, size_t index, Any element);
 
 int cx_array_insert_array_a(const CxAllocator* allocator,
         CxArray array, size_t index, const void *other, size_t n);
@@ -109,6 +109,10 @@
 When the array capacity is not sufficient, a re-allocation is attempted.
 If the allocation fails, the function returns non-zero.
 
+> Since the "functions" are actually macros, the variants which add or insert one single element
+> also conveniently take the address of the passed argument, so you don't have to do it.
+> This is why the type is specified as `Any` instead of `const void*` here.
+
 > Be careful when using these functions on an array that was initialized with fixed-sized memory.
 > In this case, you MUST make sure that the capacity is sufficient or reallocate the array
 > with `cx_array_copy_to_new()` or `cx_array_copy_to_new_a()` before adding or inserting more elements.
@@ -143,8 +147,7 @@
 ```C
 #include <cx/array_list.h>
 
-int cx_array_insert_sorted(CxArray array,
-        const void *element,
+int cx_array_insert_sorted(CxArray array, Any element,
         cx_compare_func cmp_func);
 
 int cx_array_insert_sorted_array(CxArray array,
@@ -152,15 +155,14 @@
         cx_compare_func cmp_func);
 
 int cx_array_insert_sorted_a(const CxAllocator *allocator,
-        CxArray array, const void *element,
+        CxArray array, Any element,
         cx_compare_func cmp_func);
 
 int cx_array_insert_sorted_array_a(const CxAllocator *allocator,
         CxArray array, const void *sorted_data, size_t n,
         cx_compare_func cmp_func);
 
-int cx_array_insert_sorted_c(CxArray array,
-        const void *element,
+int cx_array_insert_sorted_c(CxArray array, Any element,
         cx_compare_func2 cmp_func, void *context);
 
 int cx_array_insert_sorted_array_c(CxArray array,
@@ -168,7 +170,7 @@
         cx_compare_func2 cmp_func, void *context);
 
 int cx_array_insert_sorted_ca(const CxAllocator *allocator,
-        CxArray array, const void *element,
+        CxArray array, Any element,
         cx_compare_func2 cmp_func, void *context);
 
 int cx_array_insert_sorted_array_ca(const CxAllocator *allocator,
@@ -185,8 +187,7 @@
 ```C
 #include <cx/array_list.h>
 
-int cx_array_insert_unique(CxArray array,
-        const void *element,
+int cx_array_insert_unique(CxArray array, Any element,
         cx_compare_func cmp_func);
 
 int cx_array_insert_unique_array(CxArray array,
@@ -194,15 +195,14 @@
         cx_compare_func cmp_func);
 
 int cx_array_insert_unique_a(const CxAllocator *allocator,
-        CxArray array, const void *element,
+        CxArray array, Any element,
         cx_compare_func cmp_func);
 
 int cx_array_insert_unique_array_a(const CxAllocator *allocator,
         CxArray array, const void *sorted_data, size_t n,
         cx_compare_func cmp_func);
 
-int cx_array_insert_unique_c(CxArray array,
-        const void *element,
+int cx_array_insert_unique_c(CxArray array, Any element,
         cx_compare_func2 cmp_func, void *context);
 
 int cx_array_insert_unique_array_c(CxArray array,
@@ -210,7 +210,7 @@
         cx_compare_func2 cmp_func, void *context);
 
 int cx_array_insert_unique_ca(const CxAllocator *allocator,
-        CxArray array, const void *element,
+        CxArray array, Any element,
         cx_compare_func2 cmp_func, void *context);
 
 int cx_array_insert_unique_array_ca(const CxAllocator *allocator,
--- a/src/cx/array_list.h	Thu Dec 18 16:44:11 2025 +0100
+++ b/src/cx/array_list.h	Thu Dec 18 18:07:29 2025 +0100
@@ -280,12 +280,12 @@
  *
  * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation
  * @param array the name of the array where the element shall be added
- * @param element (@c void*) a pointer to the element that shall be added
+ * @param element the element that shall be added
  * @retval zero success
  * @retval non-zero a re-allocation was necessary but failed
  */
 #define cx_array_add_a(allocator, array, element) \
-        cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (array).size, element, 1)
+        cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (array).size, (void*)&(element), 1)
 
 /**
  * Appends an element to an array.
@@ -308,12 +308,12 @@
  * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation
  * @param array the name of the array where the element shall be inserted
  * @param index (@c size_t) the index where to insert the @p element
- * @param element (@c void*) a pointer to the element that shall be inserted
+ * @param element the element that shall be inserted
  * @retval zero success
  * @retval non-zero a re-allocation was necessary but failed
  */
 #define cx_array_insert_a(allocator, array, index, element) \
-        cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), index, element, 1)
+        cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), index, (void*)&(element), 1)
 
 /**
  * Inserts an element into an array.
@@ -418,13 +418,13 @@
  *
  * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation
  * @param array the name of the array where the elements shall be inserted
- * @param element (@c void*) a pointer to element that shall be inserted
+ * @param element the element that shall be inserted
  * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
  * @retval zero success
  * @retval non-zero a re-allocation was necessary but failed
  */
 #define cx_array_insert_sorted_a(allocator, array, element, cmp_func) \
-        cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), element, 1, cmp_func, true)
+        cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (void*)&(element), 1, cmp_func, true)
 
 /**
  * Inserts an element into a sorted array.
@@ -434,7 +434,7 @@
  * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined.
  *
  * @param array the name of the array where the elements shall be inserted
- * @param element (@c void*) a pointer to element that shall be inserted
+ * @param element the element that shall be inserted
  * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
  * @retval zero success
  * @retval non-zero a re-allocation was necessary but failed
@@ -486,13 +486,13 @@
  *
  * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation
  * @param array the name of the array where the elements shall be inserted
- * @param element (@c void*) a pointer to element that shall be inserted
+ * @param element the element that shall be inserted
  * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
  * @retval zero success
  * @retval non-zero a re-allocation was necessary but failed
  */
 #define cx_array_insert_unique_a(allocator, array, element, cmp_func) \
-        cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), element, 1, cmp_func, false)
+        cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (void*)&(element), 1, cmp_func, false)
 
 /**
  * Inserts an element into a sorted array if it is not already contained.
@@ -502,7 +502,7 @@
  * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined.
  *
  * @param array the name of the array where the elements shall be inserted
- * @param element (@c void*) a pointer to element that shall be inserted
+ * @param element the element that shall be inserted
  * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
  * @retval zero success
  * @retval non-zero a re-allocation was necessary but failed
@@ -575,14 +575,14 @@
  *
  * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation
  * @param array the name of the array where the elements shall be inserted
- * @param element (@c void*) a pointer to element that shall be inserted
+ * @param element the element that shall be inserted
  * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order
  * @param context (@c void*) additional context for the compare function
  * @retval zero success
  * @retval non-zero a re-allocation was necessary but failed
  */
 #define cx_array_insert_sorted_ca(allocator, array, element, cmp_func) \
-        cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), element, 1, cmp_func, context, true)
+        cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (void*)&(element), 1, cmp_func, context, true)
 
 /**
  * Inserts an element into a sorted array.
@@ -592,7 +592,7 @@
  * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined.
  *
  * @param array the name of the array where the elements shall be inserted
- * @param element (@c void*) a pointer to element that shall be inserted
+ * @param element the element that shall be inserted
  * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order
  * @param context (@c void*) additional context for the compare function
  * @retval zero success
@@ -647,14 +647,14 @@
  *
  * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation
  * @param array the name of the array where the elements shall be inserted
- * @param element (@c void*) a pointer to element that shall be inserted
+ * @param element the element that shall be inserted
  * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order
  * @param context (@c void*) additional context for the compare function
  * @retval zero success
  * @retval non-zero a re-allocation was necessary but failed
  */
 #define cx_array_insert_unique_ca(allocator, array, element, cmp_func, context) \
-        cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), element, 1, cmp_func, context, false)
+        cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (void*)&(element), 1, cmp_func, context, false)
 
 /**
  * Inserts an element into a sorted array if it is not already contained.
@@ -664,7 +664,7 @@
  * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined.
  *
  * @param array the name of the array where the elements shall be inserted
- * @param element (@c void*) a pointer to element that shall be inserted
+ * @param element the element that shall be inserted
  * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order
  * @param context (@c void*) additional context for the compare function
  * @retval zero success
--- a/src/json.c	Thu Dec 18 16:44:11 2025 +0100
+++ b/src/json.c	Thu Dec 18 18:07:29 2025 +0100
@@ -485,7 +485,7 @@
         CxJsonValue *parent = json->vbuf.data[json->vbuf.size - 1];
         assert(parent != NULL);
         if (parent->type == CX_JSON_ARRAY) {
-            if (cx_array_add_a(json->allocator, parent->array, &v)) {
+            if (cx_array_add_a(json->allocator, parent->array, v)) {
                 goto create_json_value_exit_error; // LCOV_EXCL_LINE
             }
         } else if (parent->type == CX_JSON_OBJECT) {
--- a/tests/test_list.c	Thu Dec 18 16:44:11 2025 +0100
+++ b/tests/test_list.c	Thu Dec 18 18:07:29 2025 +0100
@@ -49,7 +49,7 @@
     int elem = 8, elem2 = 47;
     int result;
     CX_TEST_DO {
-        result = cx_array_add(arr, &elem);
+        result = cx_array_add(arr, elem);
         CX_TEST_ASSERT(result == 0);
         CX_TEST_ASSERT(arr.data[0] == 2);
         CX_TEST_ASSERT(arr.data[1] == 3);
@@ -60,7 +60,7 @@
         CX_TEST_ASSERT(arr.capacity == 5);
 
         arr.size = 5;
-        result = cx_array_add(arr, &elem2);
+        result = cx_array_add(arr, elem2);
         CX_TEST_ASSERT(result == 0);
         CX_TEST_ASSERT(arr.data[0] == 2);
         CX_TEST_ASSERT(arr.data[1] == 3);
@@ -119,7 +119,7 @@
         CX_TEST_ASSERT(arr.data[2] == 6);
 
         for (int x = 8 ; x <= 18 ; x+=2) {
-            cx_array_add(arr, &x);
+            cx_array_add(arr, x);
         }
 
         CX_TEST_ASSERT(arr.size == 9);
@@ -154,19 +154,19 @@
     cx_array_init(array, 4);
 
     CX_TEST_DO {
-        CX_TEST_ASSERT(0 == cx_array_insert_sorted(array, &d1, cx_cmp_int));
+        CX_TEST_ASSERT(0 == cx_array_insert_sorted(array, d1, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 1);
         CX_TEST_ASSERT(array.capacity == 4);
-        CX_TEST_ASSERT(0 == cx_array_insert_sorted(array, &d2, cx_cmp_int));
+        CX_TEST_ASSERT(0 == cx_array_insert_sorted(array, d2, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 2);
         CX_TEST_ASSERT(array.capacity == 4);
-        CX_TEST_ASSERT(0 == cx_array_insert_sorted(array, &d3, cx_cmp_int));
+        CX_TEST_ASSERT(0 == cx_array_insert_sorted(array, d3, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 3);
         CX_TEST_ASSERT(array.capacity == 4);
-        CX_TEST_ASSERT(0 == cx_array_insert_sorted(array, &d4, cx_cmp_int));
+        CX_TEST_ASSERT(0 == cx_array_insert_sorted(array, d4, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 4);
         CX_TEST_ASSERT(array.capacity == 4);
-        CX_TEST_ASSERT(0 == cx_array_insert_sorted(array, &d5, cx_cmp_int));
+        CX_TEST_ASSERT(0 == cx_array_insert_sorted(array, d5, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 5);
         CX_TEST_ASSERT(array.capacity >= 5);
         CX_TEST_ASSERT(0 == cx_array_insert_sorted_array(array, d6a, 6, cx_cmp_int));
@@ -175,10 +175,10 @@
         CX_TEST_ASSERT(0 == cx_array_insert_sorted_array(array, d7a, 6, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 17);
         CX_TEST_ASSERT(array.capacity >= 17);
-        CX_TEST_ASSERT(0 == cx_array_insert_sorted(array, &d8, cx_cmp_int));
+        CX_TEST_ASSERT(0 == cx_array_insert_sorted(array, d8, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 18);
         CX_TEST_ASSERT(array.capacity >= 18);
-        CX_TEST_ASSERT(0 == cx_array_insert_sorted(array, &d9, cx_cmp_int));
+        CX_TEST_ASSERT(0 == cx_array_insert_sorted(array, d9, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 19);
         CX_TEST_ASSERT(array.capacity >= 19);
         CX_TEST_ASSERT(0 == cx_array_insert_sorted_array(array, d10a, 3, cx_cmp_int));
@@ -217,19 +217,19 @@
     cx_array_init(array, 4);
 
     CX_TEST_DO {
-        CX_TEST_ASSERT(0 == cx_array_insert_unique(array, &d1, cx_cmp_int));
+        CX_TEST_ASSERT(0 == cx_array_insert_unique(array, d1, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 1);
         CX_TEST_ASSERT(array.capacity == 4);
-        CX_TEST_ASSERT(0 == cx_array_insert_unique(array, &d2, cx_cmp_int));
+        CX_TEST_ASSERT(0 == cx_array_insert_unique(array, d2, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 2);
         CX_TEST_ASSERT(array.capacity == 4);
-        CX_TEST_ASSERT(0 == cx_array_insert_unique(array, &d3, cx_cmp_int));
+        CX_TEST_ASSERT(0 == cx_array_insert_unique(array, d3, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 3);
         CX_TEST_ASSERT(array.capacity == 4);
-        CX_TEST_ASSERT(0 == cx_array_insert_unique(array, &d4, cx_cmp_int));
+        CX_TEST_ASSERT(0 == cx_array_insert_unique(array, d4, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 4);
         CX_TEST_ASSERT(array.capacity == 4);
-        CX_TEST_ASSERT(0 == cx_array_insert_unique(array, &d5, cx_cmp_int));
+        CX_TEST_ASSERT(0 == cx_array_insert_unique(array, d5, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 5);
         CX_TEST_ASSERT(array.capacity >= 5);
         CX_TEST_ASSERT(0 == cx_array_insert_unique_array(array, d6a, 6, cx_cmp_int));
@@ -238,10 +238,10 @@
         CX_TEST_ASSERT(0 == cx_array_insert_unique_array(array, d7a, 6, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 17);
         CX_TEST_ASSERT(array.capacity >= 17);
-        CX_TEST_ASSERT(0 == cx_array_insert_unique(array, &d8, cx_cmp_int));
+        CX_TEST_ASSERT(0 == cx_array_insert_unique(array, d8, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 18);
         CX_TEST_ASSERT(array.capacity >= 18);
-        CX_TEST_ASSERT(0 == cx_array_insert_unique(array, &d9, cx_cmp_int));
+        CX_TEST_ASSERT(0 == cx_array_insert_unique(array, d9, cx_cmp_int));
         CX_TEST_ASSERT(array.size == 18);
         CX_TEST_ASSERT(array.capacity >= 18);
         CX_TEST_ASSERT(0 == cx_array_insert_unique_array(array, d10a, 3, cx_cmp_int));

mercurial