summaryrefslogtreecommitdiff
path: root/src/teams.c
diff options
context:
space:
mode:
authorLibravatar Mubashshir <ahmubashshir@gmail.com>2025-05-19 14:08:11 +0600
committerLibravatar Mubashshir <ahmubashshir@gmail.com>2025-05-19 14:08:11 +0600
commitff1ba1e770b4205c2cc367ff513325e5f22c73e8 (patch)
tree8bef720b23626dc8945d6da9897020766ec61274 /src/teams.c
parent815eca3e8f367a6fed50be59b10316b5028773aa (diff)
downloadc-obp-example-ff1ba1e770b4205c2cc367ff513325e5f22c73e8.tar.gz
c-obp-example-ff1ba1e770b4205c2cc367ff513325e5f22c73e8.zip
Code cleanup and reuse
Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
Diffstat (limited to 'src/teams.c')
-rw-r--r--src/teams.c79
1 files changed, 38 insertions, 41 deletions
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);
+})