summaryrefslogtreecommitdiff
path: root/src/main/c/common/assert_is.h
blob: 316bf02cdeae462743ab228984a2fd9313b6cb02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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);
}