diff options
author | Andreas Fankhauser hiddenalpha.ch | 2024-05-12 12:55:07 +0200 |
---|---|---|
committer | Andreas Fankhauser hiddenalpha.ch | 2024-05-12 12:55:07 +0200 |
commit | 5cc4c2b95df9bf6998d92496ca0e0064314fba39 (patch) | |
tree | ac7d979f81a86703508ff23e2694a6b4d6bfe4d5 /src/main/c/common/assert_is.h | |
parent | 1dc9f8c9db499a326ceee6cb3b48878ee17ff9fa (diff) | |
parent | 95d934e8e3918832c03f05b2fc32cb5d5272cb83 (diff) | |
download | UnspecifiedGarbage-5cc4c2b95df9bf6998d92496ca0e0064314fba39.zip UnspecifiedGarbage-5cc4c2b95df9bf6998d92496ca0e0064314fba39.tar.gz |
Merge remote-tracking branch 'ha-public/master' into wip-LatitudeReSetup-20230620wip-LatitudeReSetup-20230620
Diffstat (limited to 'src/main/c/common/assert_is.h')
-rw-r--r-- | src/main/c/common/assert_is.h | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/main/c/common/assert_is.h b/src/main/c/common/assert_is.h new file mode 100644 index 0000000..316bf02 --- /dev/null +++ b/src/main/c/common/assert_is.h @@ -0,0 +1,39 @@ + +#if !NDEBUG +#define TPL_assert_is(T, PRED) static inline T*assert_is_##T(void*p,\ +const char*f,int l){if(p==NULL){fprintf(stderr,"assert(" STR_QUOT(T)\ +" != NULL) %s:%d\n",f,l);abort();}T*obj=p;if(!(PRED)){fprintf(stderr,\ +"ssert(type is \""STR_QUOT(T)"\") %s:%d\n",f,l);abort();}return p; } +#else +#define TPL_assert_is(T, PRED) static inline T*assert_is_##T(void*p,\ +const char*f,int l){return p;} +#endif + + + +/* Example usage: */ + +/* add some magic to your struct under check */ +typedef struct Person Person; +struct Person { + char tYPE[sizeof"Hi, I'm a Person"]; +}; + +/* instantiate a checker */ +TPL_assert_is(Person, !strcmp(obj->tYPE, "Hi, I'm a Person")) +#define assert_is_Person(p) assert_is_Person(p, __FILE__, __LINE__) + +/* make sure magic is initialized (ALSO MAKE SURE TO PROPERLY INVALIDATE + * IT IN DTOR!)*/ +static void someCaller( void ){ + Person p = {0}; + strcpy(p.tYPE, "Hi, I'm a Person"); + void *ptr = p; /*whops compiler cannot help us any longer*/ + someCallee(ptr); +} + +/* verify you reall got a Person*/ +static void someCallee( void*shouldBeAPerson ){ + Person *p = assert_is_Person(shouldBeAPerson); +} + |