summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Mubashshir <ahmubashshir@gmail.com>2023-10-28 20:05:49 +0600
committerLibravatar Mubashshir <ahmubashshir@gmail.com>2023-10-28 20:05:49 +0600
commit7455c3d0bda6ab60d3f67fafd0ce04428143bcbe (patch)
tree4a139278995f0602469b10223aaa448ae0a967f9
parentc47100c331e1fcc4353b6ed83a1d3485c9331eaf (diff)
downloadc-obp-example-7455c3d0bda6ab60d3f67fafd0ce04428143bcbe.tar.gz
c-obp-example-7455c3d0bda6ab60d3f67fafd0ce04428143bcbe.zip
Use better names for reference helpers
Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
-rw-r--r--src/common.h5
-rw-r--r--src/main.c9
-rw-r--r--src/teams.c18
-rw-r--r--src/teams.h16
4 files changed, 25 insertions, 23 deletions
diff --git a/src/common.h b/src/common.h
index b0f9e7f..08b8c73 100644
--- a/src/common.h
+++ b/src/common.h
@@ -14,7 +14,8 @@
strncpy((ptr), "", (size))
#define array(type, size) ((type *) calloc (sizeof(type), (size)))
-#define ref(x) x * const
-#define mut(x) x *
+#define ref(x) const x * const
+#define mut(x) const x *
+#define ptr(x) x *
#endif /* __COMMON_H__ */
diff --git a/src/main.c b/src/main.c
index 9edf6a6..a42b5a6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -5,9 +5,10 @@
#include "teams.h"
#include "common.h"
-// ref(type) references content is always const
-// mut(type) references content is mutable
-void process(const ref(char) line, unsigned idx, team obj)
+// ref(type) -> const ref to const data
+// mut(type) -> const ref to mut data
+// ptr(type) -> mut ref to mut data
+void process(ref(char) line, unsigned idx, team obj)
{
unsigned s;
switch(idx) {
@@ -35,7 +36,7 @@ int main()
{
unsigned num = 0, i = 0;
char line[255] = "";
- team *teams;
+ ptr(team) teams;
getline(line, 255, stdin);
consume(line, 255, sscanf, "%u", &num);
diff --git a/src/teams.c b/src/teams.c
index 9b6d37b..a3bf0a2 100644
--- a/src/teams.c
+++ b/src/teams.c
@@ -15,7 +15,7 @@ typedef struct team_impl_struct {
// base functions
NEW(team, {
- return (TEAM_impl *)malloc(sizeof(TEAM_impl));
+ return (ptr(TEAM_impl))malloc(sizeof(TEAM_impl));
})
DEL(team, {
@@ -24,17 +24,17 @@ DEL(team, {
// Setter definitions
// @set char[]: TEAM.name -> bool
-SETTER(team, name, const ref(char), {
+SETTER(team, name, ref(char), {
return !(strncpy(self->name, value, 255) == NULL);
})
// @set char[]: TEAM.institution -> bool
-SETTER(team, institution, const ref(char), {
+SETTER(team, institution, ref(char), {
return !(strncpy(self->institution, value, 255) == NULL);
})
// @set char[]: TEAM.member_name, int: id -> bool
-SETTER(team, member_name, const ref(char), {
+SETTER(team, member_name, ref(char), {
return (id > 0 && id < 4 && !(strncpy(self->members[id-1], value, 255) == NULL));
}, int id)
@@ -47,12 +47,12 @@ SETTER(team, solved, uint8_t, {
// Getter definitions
// @get TEAM.name -> char[]
-GETTER(team, name, char *, {
+GETTER(team, name, ptr(char), {
return self->name;
})
// @get TEAM.institution -> char[]
-GETTER(team, institution, char *, {
+GETTER(team, institution, ptr(char), {
return self->institution;
})
@@ -62,7 +62,7 @@ GETTER(team, solved, uint8_t, {
})
// @get TEAM.member_name, id -> char[]
-GETTER(team, member_name, char *, {
+GETTER(team, member_name, ptr(char), {
return (id > 0 && id < 4)? self->members[id - 1] : "(incorrect id)";
}, int id)
@@ -84,7 +84,7 @@ DEFINE(team, fprintf, int, {
self->members[0],
self->members[1],
self->members[2]);
-}, FILE *file)
+}, ptr(FILE) file)
DEFINE(team, find_champion, team, {
UNUSED(self);
@@ -94,4 +94,4 @@ DEFINE(team, find_champion, team, {
if ( teams[num]->solved > champion->solved )
champion = teams[num];
return champion;
-}, team *teams, unsigned num)
+}, ptr(team) teams, unsigned num)
diff --git a/src/teams.h b/src/teams.h
index 342ce95..2fc1be3 100644
--- a/src/teams.h
+++ b/src/teams.h
@@ -13,21 +13,21 @@ NEW(team);
DEL(team);
// field getters
-SETTER(team, name, const ref(char));
-SETTER(team, institution, const ref(char));
-SETTER(team, member_name, const ref(char), int);
+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, char *);
-GETTER(team, institution, char *);
-GETTER(team, member_name, char *, int);
+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, FILE *);
-DEFINE(team, find_champion, team, team *, unsigned);
+DEFINE(team, fprintf, int, ptr(FILE));
+DEFINE(team, find_champion, team, ptr(team), unsigned);
# include "cobj.h"
#endif /* __TEAMS_H__ */