summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Mubashshir <ahmubashshir@gmail.com>2024-06-05 17:23:45 +0600
committerLibravatar Mubashshir <ahmubashshir@gmail.com>2024-06-05 17:23:45 +0600
commit815eca3e8f367a6fed50be59b10316b5028773aa (patch)
treeda6db4241b77bb54f51a4dfdecb7ab6cece20914
parent83cf6de508664535c77e4d6aa8f0695a3fa7cdf2 (diff)
downloadc-obp-example-815eca3e8f367a6fed50be59b10316b5028773aa.tar.gz
c-obp-example-815eca3e8f367a6fed50be59b10316b5028773aa.zip
Add class declaration helper
Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
-rw-r--r--src/cobj.h16
-rw-r--r--src/common.h2
-rw-r--r--src/teams.c9
3 files changed, 26 insertions, 1 deletions
diff --git a/src/cobj.h b/src/cobj.h
index 196649c..fd28746 100644
--- a/src/cobj.h
+++ b/src/cobj.h
@@ -38,6 +38,22 @@
# define DEL(klass, body, ...) \
void klass ## _del(klass self, ## __VA_ARGS__) \
body
+# define NCLASS(klass, data) \
+ typedef struct __##klass##_impl * klass; \
+ struct __##klass##_impl data
+
+# define LIST(klass, field, type, dim, ...) \
+ type _property_ ## field dim; \
+ type* (*get_ ## field)(klass, ## __VA_ARGS__); \
+ bool (*set_ ## field)(klass, ## __VA_ARGS__, type *)
+
+# define ATOM(klass, field, type, ...) \
+ type _property_ ## field; \
+ type (*get_ ## field)(klass, ## __VA_ARGS__);\
+ bool (*set_ ## field)(klass, ## __VA_ARGS__, type)
+
+# define DEFN(klass, name, type, ...) \
+ type (*name)(klass, ## __VA_ARGS__)
#else
# define CLASS(klass) \
typedef struct klass ## _impl_struct * klass
diff --git a/src/common.h b/src/common.h
index 08b8c73..a9a39f2 100644
--- a/src/common.h
+++ b/src/common.h
@@ -13,7 +13,7 @@
(consumer)((ptr), ## __VA_ARGS__); \
strncpy((ptr), "", (size))
-#define array(type, size) ((type *) calloc (sizeof(type), (size)))
+#define array(type, size) ((type *) calloc ((size), sizeof(type)))
#define ref(x) const x * const
#define mut(x) const x *
#define ptr(x) x *
diff --git a/src/teams.c b/src/teams.c
index 33efae3..00c12cd 100644
--- a/src/teams.c
+++ b/src/teams.c
@@ -13,6 +13,15 @@ CLASS(team, {
uint8_t solved;
});
+NCLASS( team_t, {
+ LIST(team_t, name, char, [255]);
+ LIST(team_t, members, char, [3][255], int);
+ LIST(team_t, institution, char, [255]);
+ ATOM(team_t, solved, uint8_t);
+ DEFN(team_t, printf, int);
+ DEFN(team_t, fprintf, int, FILE *);
+});
+
// base functions
NEW(team, {
return (team)malloc(sizeof(TYPE(team)));