summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Fankhauser hiddenalpha.ch2024-04-08 21:29:11 +0200
committerAndreas Fankhauser hiddenalpha.ch2024-04-08 21:29:11 +0200
commitb55041beb9c8d2fc60d8657f6d38f665b3418004 (patch)
tree31cbf7af7229ceaa17b626283d8f2f2c601007f7
parente8d61dd86577f75e5b8442c8d0de850f762bcb76 (diff)
downloadUnspecifiedGarbage-b55041beb9c8d2fc60d8657f6d38f665b3418004.zip
UnspecifiedGarbage-b55041beb9c8d2fc60d8657f6d38f665b3418004.tar.gz
Add assert_is_tYPE as a template.
-rw-r--r--src/main/c/common/assert_is.h39
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..b6e3132
--- /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);
+}
+