summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Mubashshir <ahmubashshir@gmail.com>2023-05-23 18:31:53 +0600
committerLibravatar Mubashshir <ahmubashshir@gmail.com>2023-05-23 18:31:53 +0600
commit09133381cad0a1dec37595867eadf9c6858ce8ee (patch)
treea7755e42183c08729e07e2dd2f5ff8054511bd6e
parent413e443613499a53cc4aa1f35664c289ffbde04b (diff)
downloadc-obp-example-09133381cad0a1dec37595867eadf9c6858ce8ee.tar.gz
c-obp-example-09133381cad0a1dec37595867eadf9c6858ce8ee.zip
Clean up main header file
Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
-rw-r--r--src/main.c1
-rw-r--r--src/teams-hell.h50
-rw-r--r--src/teams.c71
-rw-r--r--src/teams.h27
4 files changed, 88 insertions, 61 deletions
diff --git a/src/main.c b/src/main.c
index 2cc35fa..afa1eeb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,6 +3,7 @@
#include <stdlib.h>
#include "teams.h"
+
#define getline(x, y) \
while(((x)[strcspn((x), "\r\n")] = 0) == 0 && strncmp((x), "", (y)) == 0) \
fgets((x), (y), stdin)
diff --git a/src/teams-hell.h b/src/teams-hell.h
new file mode 100644
index 0000000..72fbdbd
--- /dev/null
+++ b/src/teams-hell.h
@@ -0,0 +1,50 @@
+#if defined(__TEAMS_H__) || defined(__TEAMS_PRIVATE_UNDEF__)
+
+# ifndef __TEAMS_HELL_H__
+# define __TEAMS_HELL_H__
+# 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 del(klass, ...) klass##_del(__VA_ARGS__)
+# define fun(klass, fun, ...) klass##_##fun(__VA_ARGS__)
+# endif /* __TEAMS_HELL_H__ */
+
+# if defined(__TEAMS_PRIVATE_UNDEF__)
+# undef GETTER
+# undef SETTER
+# undef DEFINE
+# undef NEW
+# undef DEL
+# undef __TEAMS_PRIVATE_UNDEF__
+# undef __TEAMS_PRIVATE_H__
+# elif defined(__IMPL_TEAMS_INTERNAL)
+# define UNUSED(arg) while(0 && (arg))
+# define GETTER(klass, field, struct, type, body, ...) \
+ type klass##_get_##field(struct self, ## __VA_ARGS__) \
+ body
+# define SETTER(klass, field, struct, type, body, ...) \
+ bool klass##_set_##field(struct self, ## __VA_ARGS__, type value) \
+ body
+# define DEFINE(klass, name, struct, type, body, ...) \
+ type klass##_##name(struct self, ## __VA_ARGS__) \
+ body
+# define NEW(klass, struct, body, ...) \
+ struct klass ## _new(__VA_ARGS__) \
+ body
+# define DEL(klass, struct, body, ...) \
+ void klass ## _del(struct self, ## __VA_ARGS__) \
+ body
+# elif defined(__TEAMS_PRIVATE_H__)
+# 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 NEW(klass, struct, ...) struct klass ## _new(__VA_ARGS__)
+# define DEL(klass, struct, ...) void klass ## _del(struct self, ## __VA_ARGS__)
+# endif
+
+#endif
diff --git a/src/teams.c b/src/teams.c
index bfee347..f75f161 100644
--- a/src/teams.c
+++ b/src/teams.c
@@ -2,8 +2,9 @@
#include <stdlib.h>
#include <string.h>
-#define __IMPL_TEAMS_INTERNAL
#include "teams.h"
+#define __IMPL_TEAMS_INTERNAL
+#include "teams-hell.h"
typedef struct team_impl_struct {
char name[255];
@@ -11,74 +12,65 @@ typedef struct team_impl_struct {
char institution[255];
uint8_t solved;
} TEAM_impl;
+
// base functions
-TEAM team_new()
-{
+NEW(team, TEAM, {
return (TEAM_impl *)malloc(sizeof(TEAM_impl));
-}
-void team_del(TEAM self)
-{
+})
+
+DEL(team, TEAM, {
if(self) free(self);
-}
+})
// Setter definitions
// @set char[]: TEAM.name -> bool
-SETTER(team, name, TEAM, char *)
-{
+SETTER(team, name, TEAM, char *, {
return !(strncpy(self->name, value, 255) == NULL);
-}
+})
// @set char[]: TEAM.institution -> bool
-SETTER(team, institution, TEAM, char *)
-{
+SETTER(team, institution, TEAM, char *, {
return !(strncpy(self->institution, value, 255) == NULL);
-}
+})
// @set char[]: TEAM.member_name, int: id -> bool
-SETTER(team, member_name, TEAM, char *, int id)
-{
+SETTER(team, member_name, TEAM, char *, {
return (id > 0 && id < 4 && !(strncpy(self->members[id-1], value, 255) == NULL));
-}
+}, int id)
+
// @set int: TEAM.solved -> bool
-SETTER(team, solved, TEAM, uint8_t)
-{
+SETTER(team, solved, TEAM, uint8_t, {
if (value <= 10)
self->solved = value;
return (value <= 10);
-}
+})
// Getter definitions
// @get TEAM.name -> char[]
-GETTER(team, name, TEAM, char *)
-{
+GETTER(team, name, TEAM, char *, {
return self->name;
-}
+})
// @get TEAM.institution -> char[]
-GETTER(team, institution, TEAM, char *)
-{
+GETTER(team, institution, TEAM, char *, {
return self->institution;
-}
+})
// @get TEAM.solved -> int
-GETTER(team, solved, TEAM, uint8_t)
-{
+GETTER(team, solved, TEAM, uint8_t, {
return self->solved;
-}
+})
// @get TEAM.member_name, id -> char[]
-GETTER(team, member_name, TEAM, char *, int id)
-{
+GETTER(team, member_name, TEAM, char *, {
return (id > 0 && id < 4)? self->members[id - 1] : "(incorrect id)";
-}
+}, int id)
-DEFINE(team, printf, TEAM, int)
-{
+DEFINE(team, printf, TEAM, int, {
return fun(team, fprintf, self, stdout);
-}
+})
-DEFINE(team, fprintf, TEAM, int, FILE *file)
-{
+DEFINE(team, fprintf, TEAM, int, {
return fprintf(file,
"Team name : %s\n"
"Institution: %s\n"
@@ -92,10 +84,9 @@ DEFINE(team, fprintf, TEAM, int, FILE *file)
self->members[0],
self->members[1],
self->members[2]);
-}
+}, FILE *file)
-DEFINE(team, find_champion, TEAM, TEAM, TEAM *teams, unsigned num)
-{
+DEFINE(team, find_champion, TEAM, TEAM, {
UNUSED(self);
TEAM champion = teams[num - 1];
@@ -103,4 +94,4 @@ DEFINE(team, find_champion, TEAM, TEAM, TEAM *teams, unsigned num)
if ( teams[num]->solved > champion->solved )
champion = teams[num];
return champion;
-}
+}, TEAM *teams, unsigned num)
diff --git a/src/teams.h b/src/teams.h
index 74a4159..435ac4a 100644
--- a/src/teams.h
+++ b/src/teams.h
@@ -1,26 +1,15 @@
#ifndef __TEAMS_H__
# define __TEAMS_H__
+# define __TEAMS_PRIVATE_H__
# include <stdint.h>
# include <stdbool.h>
-# 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__)
+# include "teams-hell.h"
// Type definition
typedef struct team_impl_struct * TEAM;
// base functions
-TEAM team_new();
-void team_del(TEAM team);
+NEW(team, TEAM);
+DEL(team, TEAM);
// field getters
SETTER(team, name, TEAM, char *);
@@ -39,10 +28,6 @@ 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
+# define __TEAMS_PRIVATE_UNDEF__
+# include "teams-hell.h"
#endif /* __TEAMS_H__ */