hxt_class_macro.h 3.87 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1
2
/*
 * Marblesbag - Copyright (C) <2010-2018>
3
4
 * <Universite catholique de Louvain (UCL), Belgium>
 * <Universite de Montpellier, France>
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * 	
 * List of the contributors to the development of SLIM: see AUTHORS file.
 * Description and complete License: see LICENSE file.
 * 	
 * This program (Marblesbag) is free software: 
 * you can redistribute it and/or modify it under the terms of the GNU General 
 * Public License as published by the Free Software Foundation, either version
 * 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program (see COPYING file).  If not, 
 * see <http://www.gnu.org/licenses/>.
 */

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

#define FE_0(WHAT, _)
#define FE_1(WHAT, X, _) WHAT(X,1) 
#define FE_2(WHAT, X, ...) WHAT(X,2)FE_1(WHAT, __VA_ARGS__)
#define FE_3(WHAT, X, ...) WHAT(X,3)FE_2(WHAT, __VA_ARGS__)
#define FE_4(WHAT, X, ...) WHAT(X,4)FE_3(WHAT, __VA_ARGS__)
#define FE_5(WHAT, X, ...) WHAT(X,5)FE_4(WHAT, __VA_ARGS__)
#define FE_6(WHAT, X, ...) WHAT(X,6)FE_5(WHAT, __VA_ARGS__)
#define FE_7(WHAT, X, ...) WHAT(X,7)FE_6(WHAT, __VA_ARGS__)
#define FE_8(WHAT, X, ...) WHAT(X,8)FE_7(WHAT, __VA_ARGS__)
#define FE_9(WHAT, X, ...) WHAT(X,9)FE_8(WHAT, __VA_ARGS__)

#define GET_MACRO(_0_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,NAME,...) NAME 
#define FOR_EACH(action,...) \
  GET_MACRO(__VA_ARGS__,FE_9,FE_8,FE_7,FE_6,FE_5,FE_4,FE_3,FE_2,FE_1,FE_0)(action,__VA_ARGS__)
#define ARG_AND_NAME(X, I) ,X _##I
#define NAME_ONLY(X, I) , _##I
#define ARG_ONLY(X, I) ,X
#define ARGS_AND_NAMES(...) FOR_EACH(ARG_AND_NAME,__VA_ARGS__)
#define NAMES_ONLY(...) FOR_EACH(NAME_ONLY,__VA_ARGS__)
#define ARGS_ONLY(...) FOR_EACH(ARG_ONLY,__VA_ARGS__)

#define HXT_FUNCTION_MEMBER_ASSIGN(derivedType,member, ...) \
{\
  HXTStatus(*castcheck)(HXT##derivedType* ARGS_ONLY(__VA_ARGS__)) = hxt##derivedType##member;\
  c->member = (HXTStatus(*)(void* ARGS_ONLY(__VA_ARGS__)))castcheck;\
}

#define HXT_FUNCTION_MEMBER_DECLARE(derivedType, member, ...) \
  HXTStatus(*member)(void* ARGS_ONLY(__VA_ARGS__))

#define HXT_FUNCTION_MEMBER_IMPLEMENT(derivedType, member, ...) \
HXTStatus hxt##derivedType##member(HXT##derivedType *c ARGS_AND_NAMES(__VA_ARGS__)) { \
  return c->c->member(c->data NAMES_ONLY(__VA_ARGS__)); \
}

#define HXT_DECLARE_DERIVED_CLASS(baseType, derivedType) \
static HXT##baseType##Class *hxt##derivedType##Class;\
HXTStatus hxt##derivedType##Register() \
{ \
  HXT_CHECK(hxtMalloc(&hxt##derivedType##Class, sizeof(HXT##baseType##Class))); \
  HXT##baseType##Class *c = hxt##derivedType##Class; \
  HXT##baseType##_MEMBERS(HXT_FUNCTION_MEMBER_ASSIGN, derivedType) \
  HXTStatus(*castcheck)(HXT##derivedType**) = hxt##derivedType##Delete; \
  c->Delete =  (HXTStatus(*)(void**))castcheck; \
  return HXT_STATUS_OK;\
} \
\
HXTStatus hxt##baseType##Get##derivedType(HXT##baseType *b, HXT##derivedType **d) { \
  if (b->c == hxt##derivedType##Class) { \
    *d = b->data; \
  } \
  else { \
    *d = NULL; \
  } \
  return HXT_STATUS_OK; \
}

#define HXT_DECLARE_INTERFACE(baseType) \
typedef struct { \
  HXT##baseType##_MEMBERS(HXT_FUNCTION_MEMBER_DECLARE,) \
  HXTStatus (*Delete)(void **); \
} HXT##baseType##Class; \
struct HXT##baseType##Struct { \
  HXT##baseType##Class *c;\
  void *data; \
}; \
HXT##baseType##_MEMBERS(HXT_FUNCTION_MEMBER_IMPLEMENT,baseType) \
HXTStatus hxt##baseType##Delete(HXT##baseType **pp) {\
  HXT_CHECK((*pp)->c->Delete(&(*pp)->data));\
  HXT_CHECK(hxtFree(pp));\
  return HXT_STATUS_OK;\
} \
HXTStatus hxt##baseType##Create(HXT##baseType **pp, HXT##baseType##Class *c, void *data) {\
  HXT_CHECK(hxtMalloc(pp, sizeof(HXT##baseType))); \
  (*pp)->c = c; \
  (*pp)->data = data; \
  return HXT_STATUS_OK;\
}