commit e0bba32dbe43a728bc7b3f33024149b1ec393f3d
parent b119ae6bb2caf40d84abaf30ed6036a17ed156c7
Author: Remy Noulin <loader2x@gmail.com>
Date: Mon, 29 Jul 2019 15:46:49 +0200
add rangeM macro (range for macros)
release/libsheepy.h | 13 +++++++++++--
src/libsheepy.h | 13 +++++++++++--
2 files changed, 22 insertions(+), 4 deletions(-)
Diffstat:
2 files changed, 22 insertions(+), 4 deletions(-)
diff --git a/release/libsheepy.h b/release/libsheepy.h
@@ -2358,6 +2358,15 @@ int listPrintS(char **list);
for (size_t index = 0 ; index < UNIQVAR(maxCnt) ; index++)
/**
+ * range macro loop
+ * same as range, but doesnt define UNIQVAR(maxCnt) so the maxCount parameter is evaluated mutiple times (one time per loop)
+ * rangeM is to be used in macros where all the code is on one line since range uses UNIQVAR which appends the line number to maxCnt
+ * and variable name collision happen (for example: range() { range(){} }).
+ */
+#define rangeM(index, maxCount) \
+ for (size_t index = 0 ; index < (size_t)(maxCount) ; index++)
+
+/**
* infinity loop
* increase the index infinitly
*/
@@ -2395,7 +2404,7 @@ int listPrintS(char **list);
* u32 array[20];
* arange(i, array) {}
*/
-#define arange(index, array) range(index, ARRAY_SIZE(array))
+#define arange(index, array) rangeM(index, ARRAY_SIZE(array))
/**
* loop on the elements of C static array of any type from highest index down to 0
@@ -3392,7 +3401,7 @@ void **iListDel(void ***list, intmax_t start, intmax_t end);
* sliceAt(vec, i) = 0;
* }
*/
-#define forEachSc(name, index) range(index, (name)->count)
+#define forEachSc(name, index) rangeM(index, (name)->count)
/**
* insert an element at index
diff --git a/src/libsheepy.h b/src/libsheepy.h
@@ -2358,6 +2358,15 @@ int listPrintS(char **list);
for (size_t index = 0 ; index < UNIQVAR(maxCnt) ; index++)
/**
+ * range macro loop
+ * same as range, but doesnt define UNIQVAR(maxCnt) so the maxCount parameter is evaluated mutiple times (one time per loop)
+ * rangeM is to be used in macros where all the code is on one line since range uses UNIQVAR which appends the line number to maxCnt
+ * and variable name collision happen (for example: range() { range(){} }).
+ */
+#define rangeM(index, maxCount) \
+ for (size_t index = 0 ; index < (size_t)(maxCount) ; index++)
+
+/**
* infinity loop
* increase the index infinitly
*/
@@ -2395,7 +2404,7 @@ int listPrintS(char **list);
* u32 array[20];
* arange(i, array) {}
*/
-#define arange(index, array) range(index, ARRAY_SIZE(array))
+#define arange(index, array) rangeM(index, ARRAY_SIZE(array))
/**
* loop on the elements of C static array of any type from highest index down to 0
@@ -3392,7 +3401,7 @@ void **iListDel(void ***list, intmax_t start, intmax_t end);
* sliceAt(vec, i) = 0;
* }
*/
-#define forEachSc(name, index) range(index, (name)->count)
+#define forEachSc(name, index) rangeM(index, (name)->count)
/**
* insert an element at index