diff options
author | 2025-05-19 14:08:11 +0600 | |
---|---|---|
committer | 2025-05-19 14:08:11 +0600 | |
commit | ff1ba1e770b4205c2cc367ff513325e5f22c73e8 (patch) | |
tree | 8bef720b23626dc8945d6da9897020766ec61274 | |
parent | 815eca3e8f367a6fed50be59b10316b5028773aa (diff) | |
download | c-obp-example-ff1ba1e770b4205c2cc367ff513325e5f22c73e8.tar.gz c-obp-example-ff1ba1e770b4205c2cc367ff513325e5f22c73e8.zip |
Code cleanup and reuse
Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
-rw-r--r-- | src/cobj.h | 50 | ||||
-rw-r--r-- | src/common.h | 2 | ||||
-rw-r--r-- | src/main.c | 12 | ||||
-rw-r--r-- | src/teams.c | 79 | ||||
-rw-r--r-- | src/teams.h | 32 |
5 files changed, 82 insertions, 93 deletions
@@ -1,12 +1,13 @@ #ifndef __COBJ_IMPLS_H__ # define __COBJ_IMPLS_H__ -# define get(klass, instance, field, ...) \ - klass##_get_##field(instance, ## __VA_ARGS__) -# define set(klass, instance, field, ...) \ - klass##_set_##field(instance, ## __VA_ARGS__) +# define get(instance, field, ...) \ + (instance)->get_##field(instance, ## __VA_ARGS__) +# define set(instance, field, ...) \ + (instance)->set_##field(instance, ## __VA_ARGS__) +# define call(instance, fun, ...) \ + (instance)->fun(instance, ## __VA_ARGS__) # define new(klass, ...) klass##_new(__VA_ARGS__) # define del(klass, ...) klass##_del(__VA_ARGS__) -# define ask(klass, fun, ...) klass##_##fun(__VA_ARGS__) #endif /* __COBJ_IMPLS_H__ */ #if defined(__USE_C_OBJSYS__) @@ -19,10 +20,8 @@ # undef __USE_C_OBJSYS__ #elif defined(__COBJ_PRIV_IMPLS_H__) # define UNUSED(arg) while(0 && (arg)) -# define CLASS(klass, data) \ - typedef struct klass##_impl_struct data __ ## klass ## _impl # define TYPE(klass) \ - __ ## klass ## _impl + struct __ ## klass ## _impl # define GETTER(klass, field, type, body, ...) \ type klass##_get_##field(klass self, ## __VA_ARGS__) \ body @@ -38,32 +37,39 @@ # define DEL(klass, body, ...) \ void klass ## _del(klass self, ## __VA_ARGS__) \ body -# define NCLASS(klass, data) \ + +# define SELF(name) \ + self->_property_ ## name +# define GET(obj, name) \ + (obj)->_property_ ## name + +#else +# define CLASS(klass, data, ...) \ typedef struct __##klass##_impl * klass; \ + klass klass ## _new(__VA_ARGS__); \ + void klass ## _delete(); \ 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 *) + bool (*set_ ## field)(klass, ## __VA_ARGS__, const type * const) # 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, ...) \ +# define FUNC(klass, name, type, ...) \ type (*name)(klass, ## __VA_ARGS__) -#else -# define CLASS(klass) \ - typedef struct klass ## _impl_struct * klass -# define GETTER(klass, field, type, ...) \ - type klass##_get_##field(klass self, ## __VA_ARGS__) -# define SETTER(klass, field, type, ...) \ - bool klass##_set_##field(klass self, ## __VA_ARGS__, type value) -# define DEFINE(klass, name, type, ...) \ - type klass##_##name(klass self, ## __VA_ARGS__) -# define NEW(klass, ...) klass klass ## _new(__VA_ARGS__) -# define DEL(klass, ...) void klass ## _del(klass self, ## __VA_ARGS__) + +# define PROPERTY(klass, name) \ + (self->get_ ## name) = (& klass ## _ ## get_ ## name), \ + (self->set_ ## name) = (& klass ## _ ## set_ ## name) + +# define CALLABLE(klass, name) \ + (self->name) = (& klass ## _ ## name) + +# define DEL() void (*del)(klass self) # define __USE_C_OBJSYS__ #endif diff --git a/src/common.h b/src/common.h index a9a39f2..1d1a088 100644 --- a/src/common.h +++ b/src/common.h @@ -17,5 +17,5 @@ #define ref(x) const x * const #define mut(x) const x * #define ptr(x) x * - +#define dref(x) *x #endif /* __COMMON_H__ */ @@ -13,19 +13,19 @@ void process(ref(char) line, unsigned idx, team obj) unsigned s; switch(idx) { case 1: - set(team, obj, name, line); + set(obj, name, line); break; case 2: - set(team, obj, institution, line); + set(obj, institution, line); break; case 3: sscanf(line, "%u", &s); - set(team, obj, solved, s); + set(obj, solved, s); break; case 4: // falls through case 5: // falls through case 6: - set(team, obj, member_name, idx - 3, line); + set(obj, member_name, idx - 3, line); break; default: break; @@ -56,8 +56,8 @@ int main() } } - team champion = ask(team, find_champion, NULL, teams, num); - ask(team, printf, champion); + team champion = call(dref(teams), find_champion, teams, num); + call(champion, printf); while(num --) del(team, teams[num]); free(teams); diff --git a/src/teams.c b/src/teams.c index 00c12cd..46c566c 100644 --- a/src/teams.c +++ b/src/teams.c @@ -6,78 +6,54 @@ #define __COBJ_PRIV_IMPLS_H__ #include "cobj.h" -CLASS(team, { - char name[255]; - char members[3][255]; - char institution[255]; - 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))); -}) - -DEL(team, { - if(self) free(self); -}) // Setter definitions // @set char[]: TEAM.name -> bool SETTER(team, name, ref(char), { - return !(strncpy(self->name, value, 255) == NULL); + return !(strncpy(SELF(name), value, 255) == NULL); }) // @set char[]: TEAM.institution -> bool SETTER(team, institution, ref(char), { - return !(strncpy(self->institution, value, 255) == NULL); + return !(strncpy(SELF(institution), value, 255) == NULL); }) // @set char[]: TEAM.member_name, int: id -> bool SETTER(team, member_name, ref(char), { - return (id > 0 && id < 4 && !(strncpy(self->members[id-1], value, 255) == NULL)); + return (id > 0 && id < 4 && !(strncpy(SELF(member_name)[id-1], value, 255) == NULL)); }, int id) // @set int: TEAM.solved -> bool SETTER(team, solved, uint8_t, { if (value <= 10) - self->solved = value; + SELF(solved) = value; return (value <= 10); }) // Getter definitions // @get TEAM.name -> char[] GETTER(team, name, ptr(char), { - return self->name; + return SELF(name); }) // @get TEAM.institution -> char[] GETTER(team, institution, ptr(char), { - return self->institution; + return SELF(institution); }) // @get TEAM.solved -> int GETTER(team, solved, uint8_t, { - return self->solved; + return SELF(solved); }) static char* const _id_error = (char *)"(incorrect id)"; // C++ fix // @get TEAM.member_name, id -> char[] GETTER(team, member_name, ptr(char), { - return (id > 0 && id < 4)? self->members[id - 1] : _id_error; + return (id > 0 && id < 4)? SELF(member_name)[id - 1] : _id_error; }, int id) DEFINE(team, printf, int, { - return ask(team, fprintf, self, stdout); + return call(self, fprintf, stdout); }) DEFINE(team, fprintf, int, { @@ -88,12 +64,12 @@ DEFINE(team, fprintf, int, { " Member 1: %s\n" " Member 2: %s\n" " Member 3: %s\n", - self->name, - self->institution, - self->solved, - self->members[0], - self->members[1], - self->members[2]); + SELF(name), + SELF(institution), + SELF(solved), + SELF(member_name)[0], + SELF(member_name)[1], + SELF(member_name)[2]); }, ptr(FILE) file) DEFINE(team, find_champion, team, { @@ -101,7 +77,28 @@ DEFINE(team, find_champion, team, { team champion = teams[num - 1]; while(num --) - if ( teams[num]->solved > champion->solved ) + if (GET(teams[num], solved) > GET(champion, solved) ) champion = teams[num]; return champion; -}, ptr(team) teams, unsigned num) +}, ptr(team) teams, size_t num) + + +// base functions +NEW(team, { + team self = (team)malloc(sizeof(TYPE(team))); + if (self) + { + PROPERTY(team, name); + PROPERTY(team, member_name); + PROPERTY(team, institution); + PROPERTY(team, solved); + CALLABLE(team, printf); + CALLABLE(team, fprintf); + CALLABLE(team, find_champion); + } + return self; +}) + +DEL(team, { + if(self) free(self); +}) diff --git a/src/teams.h b/src/teams.h index 15aad82..c65bad3 100644 --- a/src/teams.h +++ b/src/teams.h @@ -6,29 +6,15 @@ # include "common.h" // Type definition -CLASS(team); - -// base functions -NEW(team); -DEL(team); - -// field getters -SETTER(team, name, ref(char)); -SETTER(team, institution, ref(char)); -SETTER(team, member_name, ref(char), int); -SETTER(team, solved, uint8_t); - -// field setters -GETTER(team, name, ptr(char)); -GETTER(team, institution, ptr(char)); -GETTER(team, member_name, ptr(char), int); -GETTER(team, solved, uint8_t); - -// helper functions -DEFINE(team, printf, int); -DEFINE(team, fprintf, int, ptr(FILE)); -DEFINE(team, find_champion, team, ptr(team), unsigned); - +CLASS( team, { + LIST(team, name, char, [255]); + LIST(team, member_name, char, [3][255], int); + LIST(team, institution, char, [255]); + ATOM(team, solved, uint8_t); + FUNC(team, printf, int); + FUNC(team, fprintf, int, FILE *); + FUNC(team, find_champion, team, ptr(team), size_t); +}); # include "cobj.h" #endif /* __TEAMS_H__ */ /* vim: ts=8 */ |