add convenience macros to avoid a fptr cast for cxBufferRead and cxBufferWrite

Tue, 07 Jan 2025 00:12:46 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 07 Jan 2025 00:12:46 +0100
changeset 1112
22dc2163fffd
parent 1111
78eeeb950883
child 1113
dce04550fbef

add convenience macros to avoid a fptr cast for cxBufferRead and cxBufferWrite

fixes #545

CHANGELOG file | annotate | diff | comparison | revisions
docs/src/features.md file | annotate | diff | comparison | revisions
src/cx/buffer.h file | annotate | diff | comparison | revisions
src/cx/printf.h file | annotate | diff | comparison | revisions
tests/test_buffer.c file | annotate | diff | comparison | revisions
tests/test_json.c file | annotate | diff | comparison | revisions
tests/test_streams.c file | annotate | diff | comparison | revisions
--- a/CHANGELOG	Mon Jan 06 23:29:41 2025 +0100
+++ b/CHANGELOG	Tue Jan 07 00:12:46 2025 +0100
@@ -14,6 +14,7 @@
  * adds cxBufferAppend()
  * adds cxBufferEnableFlushing() and cxBufferFlush()
  * adds CX_BUFFER_COPY_ON_WRITE and CX_BUFFER_COPY_ON_EXTEND flags
+ * adds cxBufferWriteFunc and cxBufferReadFunc function pointers (for convenience)
  * adds cx_cmp_ptr()
  * adds cx_vcmp_* family of functions
  * adds cx_sprintf() and several more variants
--- a/docs/src/features.md	Mon Jan 06 23:29:41 2025 +0100
+++ b/docs/src/features.md	Tue Jan 07 00:12:46 2025 +0100
@@ -127,7 +127,7 @@
     CxBuffer *content = cxBufferCreate(NULL, 256, pool->allocator, CX_BUFFER_AUTO_EXTEND);
 
     // read the file into the buffer and turn it into a string
-    cx_stream_copy(f, content, (cx_read_func) fread, (cx_write_func) cxBufferWrite);
+    cx_stream_copy(f, content, (cx_read_func) fread, cxBufferWriteFunc);
     fclose(f);
     cxstring contentstr = cx_strn(content->space, content->size);
 
@@ -363,7 +363,7 @@
     cxBufferInit(&fbuf, NULL, 4096, NULL, CX_BUFFER_AUTO_EXTEND);
     cx_stream_copy(inputfile, &fbuf,
                    (cx_read_func) fread,
-                   (cx_write_func) cxBufferWrite);
+                   cxBufferWriteFunc);
     fclose(inputfile);
     
     // ... do something meaningful with the contents ...
--- a/src/cx/buffer.h	Mon Jan 06 23:29:41 2025 +0100
+++ b/src/cx/buffer.h	Tue Jan 07 00:12:46 2025 +0100
@@ -89,6 +89,17 @@
 #define CX_BUFFER_COPY_ON_EXTEND 0x08
 
 /**
+ * Function pointer for cxBufferWrite that is compatible with cx_write_func.
+ * @see cx_write_func
+ */
+#define cxBufferWriteFunc  ((cx_write_func) cxBufferWrite)
+/**
+ * Function pointer for cxBufferRead that is compatible with cx_read_func.
+ * @see cx_read_func
+ */
+#define cxBufferReadFunc  ((cx_read_func) cxBufferRead)
+
+/**
  * Configuration for automatic flushing.
  */
 struct cx_buffer_flush_config_s {
--- a/src/cx/printf.h	Mon Jan 06 23:29:41 2025 +0100
+++ b/src/cx/printf.h	Tue Jan 07 00:12:46 2025 +0100
@@ -185,7 +185,7 @@
  * @see cxBufferWrite()
  */
 #define cx_bprintf(buffer, fmt, ...) cx_fprintf((void*)buffer, \
-    (cx_write_func) cxBufferWrite, fmt, __VA_ARGS__)
+    cxBufferWriteFunc, fmt, __VA_ARGS__)
 
 
 /**
--- a/tests/test_buffer.c	Mon Jan 06 23:29:41 2025 +0100
+++ b/tests/test_buffer.c	Tue Jan 07 00:12:46 2025 +0100
@@ -1084,7 +1084,7 @@
         flush.blksize = 32;
         flush.blkmax = 1;
         flush.target = &target;
-        flush.wfunc = (cx_write_func)cxBufferWrite;
+        flush.wfunc = cxBufferWriteFunc;
         CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush));
         size_t written = cxBufferWrite("foo", 1, 3, &buf);
         CX_TEST_ASSERT(written == 3);
@@ -1117,7 +1117,7 @@
         flush.blksize = 32;
         flush.blkmax = 1;
         flush.target = &target;
-        flush.wfunc = (cx_write_func)cxBufferWrite;
+        flush.wfunc = cxBufferWriteFunc;
         CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush));
         size_t written = cxBufferWrite("foobar", 1, 6, &buf);
         CX_TEST_ASSERT(written == 6);
@@ -1191,7 +1191,7 @@
         flush.blksize = 2;
         flush.blkmax = 2;
         flush.target = &target;
-        flush.wfunc = (cx_write_func)cxBufferWrite;
+        flush.wfunc = cxBufferWriteFunc;
         CX_TEST_ASSERT(0 == cxBufferEnableFlushing(&buf, flush));
         CX_TEST_ASSERT(buf.size == 7);
         buf.pos = 5;
--- a/tests/test_json.c	Mon Jan 06 23:29:41 2025 +0100
+++ b/tests/test_json.c	Tue Jan 07 00:12:46 2025 +0100
@@ -747,7 +747,7 @@
     // write it to a buffer
     CxBuffer buf;
     cxBufferInit(&buf, NULL, 512, NULL, CX_BUFFER_DEFAULT);
-    int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, writer);
+    int result = cxJsonWrite(&buf, obj, cxBufferWriteFunc, writer);
     cxBufferTerminate(&buf); // makes debugging easier
     CX_TEST_ASSERT(result == 0);
 
--- a/tests/test_streams.c	Mon Jan 06 23:29:41 2025 +0100
+++ b/tests/test_streams.c	Tue Jan 07 00:12:46 2025 +0100
@@ -43,23 +43,23 @@
 
     CX_TEST_DO {
         size_t result = cx_stream_bncopy(&source, &target,
-                                         (cx_read_func) cxBufferRead,
-                                         (cx_write_func) cxBufferWrite,
+                                         cxBufferReadFunc,
+                                         cxBufferWriteFunc,
                                          tmp, 4, 0);
         CX_TEST_ASSERT(result == 0);
         CX_TEST_ASSERT(target.size == 0);
 
         result = cx_stream_bncopy(&source, &target,
-                                         (cx_read_func) cxBufferRead,
-                                         (cx_write_func) cxBufferWrite,
+                                         cxBufferReadFunc,
+                                         cxBufferWriteFunc,
                                          tmp, 4, 20);
         CX_TEST_ASSERT(result == 20);
         CX_TEST_ASSERT(target.size == 20);
         CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0);
 
         result = cx_stream_bcopy(&source, &target,
-                                 (cx_read_func) cxBufferRead,
-                                 (cx_write_func) cxBufferWrite,
+                                 cxBufferReadFunc,
+                                 cxBufferWriteFunc,
                                  NULL, 16);
 
         CX_TEST_ASSERT(result == 7);
@@ -82,16 +82,16 @@
 
     CX_TEST_DO {
         size_t result = cx_stream_ncopy(&source, &target,
-                                        (cx_read_func) cxBufferRead,
-                                        (cx_write_func) cxBufferWrite,
+                                        cxBufferReadFunc,
+                                        cxBufferWriteFunc,
                                         20);
         CX_TEST_ASSERT(result == 20);
         CX_TEST_ASSERT(target.size == 20);
         CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0);
 
         result = cx_stream_copy(&source, &target,
-                                (cx_read_func) cxBufferRead,
-                                (cx_write_func) cxBufferWrite);
+                                cxBufferReadFunc,
+                                cxBufferWriteFunc);
 
         CX_TEST_ASSERT(result == 7);
         CX_TEST_ASSERT(target.size == 27);

mercurial