summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Mubashshir <ahmubashshir@gmail.com>2023-05-23 17:19:44 +0600
committerLibravatar Mubashshir <ahmubashshir@gmail.com>2023-05-23 17:19:44 +0600
commit413e443613499a53cc4aa1f35664c289ffbde04b (patch)
tree37deab8142fd80ab9ef17bb970d2beef5e53796d
parent5942f6125ea85bc78fc3df7b2f72b41260f4b4a2 (diff)
downloadc-obp-example-413e443613499a53cc4aa1f35664c289ffbde04b.tar.gz
c-obp-example-413e443613499a53cc4aa1f35664c289ffbde04b.zip
Add new operators
Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
-rw-r--r--Makefile4
-rw-r--r--src/main.c13
-rw-r--r--src/teams.c64
-rw-r--r--src/teams.h41
4 files changed, 73 insertions, 49 deletions
diff --git a/Makefile b/Makefile
index 4fb7a1c..bffc51c 100644
--- a/Makefile
+++ b/Makefile
@@ -47,7 +47,7 @@ endif
all: obj/$(BINARY)
obj/$(BINARY): $(<<objects>>)
- $(<<) "LINK\t" $(^:obj/%=%)
+ $(<<) " LD\t"-o $(@) $(LDFLAGS)"\t"$(^:obj/%=%)
@$(CC) $(^) -o $(@) $(LDFLAGS)
clean: clean-objs clean-tests
@@ -62,7 +62,7 @@ clean-objs:
test: $(<<results>>)
obj/%.o: src/%.c
- $(<<) " CC\t" $(<:src/%=%)
+ $(<<) " CC\t"$(CFLAGS)"\t"$(<:src/%=%)
@mkdir -p $(shell dirname $(@))
@$(CC) -c $(<) -o $(@) $(CFLAGS)
diff --git a/src/main.c b/src/main.c
index faf76f7..2cc35fa 100644
--- a/src/main.c
+++ b/src/main.c
@@ -24,25 +24,25 @@ int main()
}
for(i = 0; i < num; i++) {
- teams[i] = team_new();
+ teams[i] = new(team);
unsigned idx = 0, s = 0;
while (idx ++ < 6) {
getline(line, 255);
switch(idx) {
case 1:
- team_set(teams[i], name, line);
+ set(team, teams[i], name, line);
break;
case 2:
- team_set(teams[i], institution, line);
+ set(team, teams[i], institution, line);
break;
case 3:
sscanf(line, "%u", &s);
- team_set(teams[i], solved, s);
+ set(team, teams[i], solved, s);
break;
case 4: // falls through
case 5: // falls through
case 6:
- team_set(teams[i], member_name, idx - 3, line);
+ set(team, teams[i], member_name, idx - 3, line);
break;
default:
break;
@@ -50,6 +50,7 @@ int main()
strncpy(line, "", 255);
}
}
- team_printf(team_find_champion(teams, num));
+
+ fun(team, printf, fun(team, find_champion, NULL, teams, num));
return 0;
}
diff --git a/src/teams.c b/src/teams.c
index f9a3fd0..bfee347 100644
--- a/src/teams.c
+++ b/src/teams.c
@@ -16,62 +16,68 @@ TEAM team_new()
{
return (TEAM_impl *)malloc(sizeof(TEAM_impl));
}
-void team_del(TEAM team)
+void team_del(TEAM self)
{
- if(team) free(team);
+ if(self) free(self);
}
// Setter definitions
// @set char[]: TEAM.name -> bool
-SETTER(char*, name)
+SETTER(team, name, TEAM, char *)
{
- return !(strncpy(team->name, name, 255) == NULL);
+ return !(strncpy(self->name, value, 255) == NULL);
}
+
// @set char[]: TEAM.institution -> bool
-SETTER(char*, institution)
+SETTER(team, institution, TEAM, char *)
{
- return !(strncpy(team->institution, institution, 255) == NULL);
+ return !(strncpy(self->institution, value, 255) == NULL);
}
+
// @set char[]: TEAM.member_name, int: id -> bool
-SETTER(char*, member_name, int id)
+SETTER(team, member_name, TEAM, char *, int id)
{
- return (id > 0 && id < 4 && !(strncpy(team->members[id-1], member_name, 255) == NULL));
+ return (id > 0 && id < 4 && !(strncpy(self->members[id-1], value, 255) == NULL));
}
// @set int: TEAM.solved -> bool
-SETTER(uint8_t, solved)
+SETTER(team, solved, TEAM, uint8_t)
{
- if (solved <= 10)
- team->solved = solved;
- return (solved <= 10);
+ if (value <= 10)
+ self->solved = value;
+ return (value <= 10);
}
// Getter definitions
// @get TEAM.name -> char[]
-GETTER(char*, name)
+GETTER(team, name, TEAM, char *)
{
- return team->name;
+ return self->name;
}
+
// @get TEAM.institution -> char[]
-GETTER(char*, institution)
+GETTER(team, institution, TEAM, char *)
{
- return team->institution;
+ return self->institution;
}
+
// @get TEAM.solved -> int
-GETTER(uint8_t, solved)
+GETTER(team, solved, TEAM, uint8_t)
{
- return team->solved;
+ return self->solved;
}
+
// @get TEAM.member_name, id -> char[]
-GETTER(char*, member_name, int id)
+GETTER(team, member_name, TEAM, char *, int id)
{
- return (id > 0 && id < 4)? team->members[id - 1] : "(incorrect id)";
+ return (id > 0 && id < 4)? self->members[id - 1] : "(incorrect id)";
}
-int team_printf(TEAM team)
+DEFINE(team, printf, TEAM, int)
{
- return team_fprintf(stdout, team);
+ return fun(team, fprintf, self, stdout);
}
-int team_fprintf(FILE *file, TEAM team)
+
+DEFINE(team, fprintf, TEAM, int, FILE *file)
{
return fprintf(file,
"Team name : %s\n"
@@ -80,12 +86,18 @@ int team_fprintf(FILE *file, TEAM team)
" Member 1: %s\n"
" Member 2: %s\n"
" Member 3: %s\n",
- team->name, team->institution, team->solved,
- team->members[0], team->members[1], team->members[2]);
+ self->name,
+ self->institution,
+ self->solved,
+ self->members[0],
+ self->members[1],
+ self->members[2]);
}
-TEAM team_find_champion(TEAM teams[], unsigned num)
+DEFINE(team, find_champion, TEAM, TEAM, TEAM *teams, unsigned num)
{
+ UNUSED(self);
+
TEAM champion = teams[num - 1];
while(num --)
if ( teams[num]->solved > champion->solved )
diff --git a/src/teams.h b/src/teams.h
index 5672fda..74a4159 100644
--- a/src/teams.h
+++ b/src/teams.h
@@ -2,10 +2,19 @@
# define __TEAMS_H__
# include <stdint.h>
# include <stdbool.h>
-# define GETTER(type, field, ...) type team_get_##field(TEAM team, ## __VA_ARGS__)
-# define SETTER(type, field, ...) bool team_set_##field(TEAM team, ## __VA_ARGS__, type field)
-# define team_get(team, field, ...) team_get_##field(team, ## __VA_ARGS__)
-# define team_set(team, field, ...) team_set_##field(team, ## __VA_ARGS__)
+# define GETTER(klass, field, struct, type, ...) \
+ type klass##_get_##field(struct self, ## __VA_ARGS__)
+# define SETTER(klass, field, struct, type, ...) \
+ bool klass##_set_##field(struct self, ## __VA_ARGS__, type value)
+# define DEFINE(klass, name, struct, type, ...) \
+ type klass##_##name(struct self, ## __VA_ARGS__)
+# define UNUSED(arg) while(0 && (arg))
+# define get(klass, instance, field, ...) \
+ klass##_get_##field(instance, ## __VA_ARGS__)
+# define set(klass, instance, field, ...) \
+ klass##_set_##field(instance, ## __VA_ARGS__)
+# define new(klass, ...) klass##_new(__VA_ARGS__)
+# define fun(klass, fun, ...) klass##_##fun(__VA_ARGS__)
// Type definition
typedef struct team_impl_struct * TEAM;
@@ -14,24 +23,26 @@ TEAM team_new();
void team_del(TEAM team);
// field getters
-SETTER(char *, name);
-SETTER(char *, institution);
-SETTER(char *, member_name, int);
-SETTER(uint8_t, solved);
+SETTER(team, name, TEAM, char *);
+SETTER(team, institution, TEAM, char *);
+SETTER(team, member_name, TEAM, char *, int);
+SETTER(team, solved, TEAM, uint8_t);
// field setters
-GETTER(char *, name);
-GETTER(char *, member_name, int);
-GETTER(char *, institution);
-GETTER(uint8_t, solved);
+GETTER(team, name, TEAM, char *);
+GETTER(team, institution, TEAM, char *);
+GETTER(team, member_name, TEAM, char *, int);
+GETTER(team, solved, TEAM, uint8_t);
// helper functions
-int team_printf(TEAM);
-int team_fprintf(FILE *, TEAM);
-TEAM team_find_champion(TEAM teams[], unsigned num);
+DEFINE(team, printf, TEAM, int);
+DEFINE(team, fprintf, TEAM, int, FILE *);
+DEFINE(team, find_champion, TEAM, TEAM, TEAM *, unsigned num);
# ifndef __IMPL_TEAMS_INTERNAL
# undef GETTER
# undef SETTER
+# undef DEFINE
+# undef UNUSED
# endif
#endif /* __TEAMS_H__ */