From 4073b907cc317ed9319f18704694c06661d8d187 Mon Sep 17 00:00:00 2001 From: Gabriel Wang Date: Sat, 27 Jul 2024 02:07:23 +0100 Subject: [PATCH] use __typeof__ to simplify with and foreach --- example/example.uvoptx | 35 +---------------------------------- example/main.c | 4 ++-- perf_counter.h | 10 ++++++++++ 3 files changed, 13 insertions(+), 36 deletions(-) diff --git a/example/example.uvoptx b/example/example.uvoptx index 90f7d42..15b1e38 100644 --- a/example/example.uvoptx +++ b/example/example.uvoptx @@ -153,40 +153,7 @@ (105=150,186,829,544,0)(106=-1,-1,-1,-1,0)(107=180,218,520,403,0) - - - 0 - 0 - 151 - 1 -
16362
- 0 - 0 - 0 - 0 - 0 - 1 - <1>.\main.c - - \\example\main.c\151 -
- - 1 - 0 - 158 - 1 -
0
- 0 - 0 - 0 - 0 - 0 - 0 - <1>.\main.c - - -
-
+ 0 diff --git a/example/main.c b/example/main.c index fa6ce30..24ee20f 100644 --- a/example/main.c +++ b/example/main.c @@ -142,7 +142,7 @@ int main (void) /*! demo of __cycleof__() operation */ __cycleof__() { - foreach(example_lv0_t, s_tItem, ptItem) { + foreach(s_tItem) { printf("Processing item with ID = %d\r\n", _->chID); } } @@ -164,7 +164,7 @@ int main (void) _->hwB = 2; _->chC = 3; - with(example_lv1_t, &pitem->tLV1) { + with(&pitem->tLV1) { _->wLV1A = 4; _->hwLV1B = 5; _->chLV1C = 6; diff --git a/perf_counter.h b/perf_counter.h index 2aeb933..507e528 100644 --- a/perf_counter.h +++ b/perf_counter.h @@ -257,6 +257,9 @@ extern "C" { #undef __with3 #undef with +#define __with1(__addr) \ + using(__typeof__(*__addr) *_=(__addr)) + #define __with2(__type, __addr) \ using(__type *_=(__addr)) #define __with3(__type, __addr, __item) \ @@ -278,6 +281,13 @@ extern "C" { #undef foreach3 #undef foreach +#define foreach1(__array) \ + using(__typeof__(__array[0]) *_ = __array) \ + for ( uint_fast32_t SAFE_NAME(count) = dimof(__array); \ + SAFE_NAME(count) > 0; \ + _++, SAFE_NAME(count)-- \ + ) + #define foreach2(__type, __array) \ using(__type *_ = __array) \ for ( uint_fast32_t SAFE_NAME(count) = dimof(__array); \