Skip to content
Snippets Groups Projects
Commit d8ba7215 authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Remove internal macros, constify better, etc.

parent 837ec3fe
No related branches found
No related tags found
No related merge requests found
Pipeline #5462 passed
......@@ -43,7 +43,7 @@ struct type_str_values type_str[] = {
{"uint32_t", "uint32"},
{"NI_err", "NI_err"},
{"bool", "bool"},
{"const char *", "const_char_ptr"},
{"char *", "char_ptr"},
{"enum NewIfc_object", "obj_enum"},
};
......@@ -157,7 +157,7 @@ attribute_functions(size_t i, FILE *c_code, const char *alias)
case attr_impl_object:
if (!attributes[i].read_only) {
fprintf(c_code, "NI_err\n"
"NI_set_%s(NewIfcObj obj, %s value) {\n"
"NI_set_%s(NewIfcObj obj, const %s value) {\n"
" NI_err ret;\n"
" if (obj == NULL)\n"
" return NewIfc_error_invalid_arg;\n"
......@@ -197,7 +197,7 @@ attribute_functions(size_t i, FILE *c_code, const char *alias)
case attr_impl_global:
if (!attributes[i].read_only) {
fprintf(c_code, "NI_err\n"
"NI_set_%s(NewIfcObj obj, %s value) {\n"
"NI_set_%s(NewIfcObj obj, const %s value) {\n"
" NI_err ret;\n"
" if (obj == NULL)\n"
" return NewIfc_error_invalid_arg;\n"
......@@ -268,7 +268,7 @@ attribute_functions(size_t i, FILE *c_code, const char *alias)
}
if (!attributes[i].no_event) {
fprintf(c_code, "NI_err\n"
"NI_add_%s_handler(NewIfcObj obj, NI_err (*handler)(NewIfcObj obj, %s newval, void *cbdata), void *cbdata)\n"
"NI_add_%s_handler(NewIfcObj obj, NI_err (*handler)(NewIfcObj obj, const %s newval, void *cbdata), void *cbdata)\n"
"{\n"
" NI_err ret;\n"
" if (obj == NULL || handler == NULL)\n"
......@@ -362,20 +362,20 @@ main(int argc, char **argv)
nitems = sizeof(attributes) / sizeof(attributes[0]);
for (i = 0; i < nitems; i++) {
if (!attributes[i].read_only) {
fprintf(header, "NI_err NI_set_%s(NewIfcObj obj, %s value);\n", attributes[i].name, type_str[attributes[i].type].type);
fprintf(header, "NI_err NI_set_%s(NewIfcObj obj, const %s value);\n", attributes[i].name, type_str[attributes[i].type].type);
}
if (!attributes[i].no_event)
fprintf(header, "NI_err NI_add_%s_handler(NewIfcObj obj, NI_err (*handler)(NewIfcObj obj, %s newval, void *cbdata), void *cbdata);\n", attributes[i].name, type_str[attributes[i].type].type);
fprintf(header, "NI_err NI_add_%s_handler(NewIfcObj obj, NI_err (*handler)(NewIfcObj obj, const %s newval, void *cbdata), void *cbdata);\n", attributes[i].name, type_str[attributes[i].type].type);
fprintf(header, "NI_err NI_get_%s(NewIfcObj obj, %s* value);\n", attributes[i].name, type_str[attributes[i].type].type);
}
nitems = sizeof(aliases) / sizeof(aliases[0]);
for (i = 0; i < nitems; i++) {
size_t a = find_attribute(aliases[i].attribute_name);
if (!attributes[a].read_only) {
fprintf(header, "NI_err NI_set_%s(NewIfcObj obj, %s value);\n", aliases[i].alias_name, type_str[attributes[a].type].type);
fprintf(header, "NI_err NI_set_%s(NewIfcObj obj, const %s value);\n", aliases[i].alias_name, type_str[attributes[a].type].type);
}
if (!attributes[i].no_event)
fprintf(header, "NI_err NI_add_%s_handler(NewIfcObj obj, NI_err (*handler)(NewIfcObj obj, %s newval, void *cbdata), void *cbdata);\n", aliases[i].alias_name, type_str[attributes[a].type].type);
fprintf(header, "NI_err NI_add_%s_handler(NewIfcObj obj, NI_err (*handler)(NewIfcObj obj, const %s newval, void *cbdata), void *cbdata);\n", aliases[i].alias_name, type_str[attributes[a].type].type);
fprintf(header, "NI_err NI_get_%s(NewIfcObj obj, %s* value);\n", aliases[i].alias_name, type_str[attributes[a].type].type);
}
fputs("\n#endif\n", header);
......@@ -413,7 +413,7 @@ main(int argc, char **argv)
" union {\n", internal_header);
nitems = sizeof(type_str) / sizeof(type_str[0]);
for (i = 0; i < nitems; i++) {
fprintf(internal_header, " NI_err (*on_%s_change)(NewIfcObj obj, %s newval, void *cbdata);\n", type_str[i].var_name, type_str[i].type);
fprintf(internal_header, " NI_err (*on_%s_change)(NewIfcObj obj, const %s newval, void *cbdata);\n", type_str[i].var_name, type_str[i].type);
}
nitems = sizeof(extra_handlers) / sizeof(extra_handlers[0]);
for (i = 0; i < nitems; i++) {
......@@ -490,7 +490,6 @@ main(int argc, char **argv)
fputs("#include \"newifc.h\"\n", c_code);
fputs("#include \"newifc_internal.h\"\n", c_code);
fputs("#include \"internal_macros.h\"\n", c_code);
fputs("\n", c_code);
nitems = sizeof(objtypes) / sizeof(objtypes[0]);
......@@ -570,7 +569,7 @@ main(int argc, char **argv)
nitems = sizeof(type_str) / sizeof(type_str[0]);
for (i = 0; i < nitems; i++) {
fprintf(c_code, "NI_err\n"
"call_%s_change_handlers(NewIfcObj obj, enum NewIfc_attribute type, %s newval)\n"
"call_%s_change_handlers(NewIfcObj obj, enum NewIfc_attribute type, const %s newval)\n"
"{\n"
" if (obj->handlers == NULL)\n"
" return NewIfc_error_none;\n"
......
#ifndef INTERNAL_MACROS_H
#define INTERNAL_MACROS_H
#define SET_VARS \
va_list ap; \
char *buf; \
#define SET_BOOL(st, field) do { \
st->field = va_arg(ap, int); \
} while(0)
#define SET_UINT32_T(st, field) do { \
st->field = va_arg(ap, uint32_t); \
} while(0);
#define SET_UINT16_T(st, field) do { \
st->field = va_arg(ap, uint16_t); \
} while(0);
#define SET_STRING(st, field) do { \
buf = strdup(va_arg(ap, const char *)); \
if (buf == NULL) { \
ret = NewIfc_error_allocation_failure; \
break; \
} \
free(st->field); \
st->field = buf; \
} while(0)
#define GET_VARS \
va_list ap; \
#define GET_BOOL(st, field) *(va_arg(ap, bool *)) = st->field
#define GET_UINT32_T(st, field) *(va_arg(ap, uint32_t *)) = st->field;
#define GET_UINT16_T(st, field) *(va_arg(ap, uint16_t *)) = st->field;
#define GET_STRING(st, field) *(va_arg(ap, char **)) = st->field;
#endif
......@@ -11,8 +11,6 @@
#include "newifc.h"
#include "newifc_internal.h"
#include "internal_macros.h"
#ifdef BUILD_TESTS
#include "CuTest.h"
#endif
......@@ -27,12 +25,19 @@ label_set(NewIfcObj obj, int attr, ...)
{
struct label *l = (struct label *)obj;
NI_err ret = NewIfc_error_none;
SET_VARS;
va_list ap;
char *buf;
va_start(ap, attr);
switch (attr) {
case NewIfc_text:
SET_STRING(l, text);
buf = strdup(va_arg(ap, const char *));
if (buf == NULL) {
ret = NewIfc_error_allocation_failure;
break;
}
free(l->text);
l->text = buf;
break;
default:
ret = NewIfc_error_not_implemented;
......@@ -48,12 +53,12 @@ label_get(NewIfcObj obj, int attr, ...)
{
struct label *l = (struct label *)obj;
NI_err ret = NewIfc_error_none;
GET_VARS;
va_list ap;
va_start(ap, attr);
switch (attr) {
case NewIfc_text:
GET_STRING(l, text);
*(va_arg(ap, char **)) = l->text;
break;
default:
ret = NewIfc_error_not_implemented;
......@@ -134,7 +139,7 @@ NewIFC_label(NewIfcObj parent, NewIfcObj *newobj)
parent->bottomchild = parent->topchild = (NewIfcObj)*newl;
(*newl)->api.root = parent->root;
(*newl)->api.parent = parent;
(*newl)->api.width = 0;
(*newl)->api.width = parent->child_width;
(*newl)->api.height = 1;
(*newl)->api.min_width = 0;
(*newl)->api.min_height = 1;
......@@ -145,7 +150,6 @@ NewIFC_label(NewIfcObj parent, NewIfcObj *newobj)
void test_label(CuTest *ct)
{
bool b;
char *s;
NewIfcObj obj;
NewIfcObj robj;
......@@ -160,7 +164,7 @@ void test_label(CuTest *ct)
CuAssertPtrNotNull(ct, obj->set);
CuAssertPtrNotNull(ct, obj->copy);
CuAssertPtrNotNull(ct, obj->do_render);
CuAssertTrue(ct, obj->width == 0);
CuAssertTrue(ct, obj->width == 80);
CuAssertTrue(ct, obj->height == 1);
CuAssertTrue(ct, obj->min_width == 0);
CuAssertTrue(ct, obj->min_height == 1);
......
......@@ -11,8 +11,6 @@
#include "newifc.h"
#include "newifc_internal.h"
#include "internal_macros.h"
#ifdef BUILD_TESTS
#include "CuTest.h"
#endif
......@@ -36,7 +34,7 @@ rw_set(NewIfcObj obj, int attr, ...)
{
struct root_window *rw = (struct root_window *)obj;
NI_err ret = NewIfc_error_none;
SET_VARS;
va_list ap;
va_start(ap, attr);
switch (attr) {
......@@ -88,7 +86,7 @@ rw_get(NewIfcObj obj, int attr, ...)
{
struct root_window *rw = (struct root_window *)obj;
NI_err ret = NewIfc_error_none;
GET_VARS;
va_list ap;
int lkret;
va_start(ap, attr);
......@@ -139,6 +137,17 @@ rw_copy(NewIfcObj old, NewIfcObj *newobj)
return NewIfc_error_allocation_failure;
}
memcpy(*newrw, old, sizeof(struct root_window));
(*newrw)->mtx = pthread_mutex_initializer_np(true);
(*newrw)->locks = 0;
size_t cells = (*newrw)->api.width;
cells *= (*newrw)->api.height;
(*newrw)->display = malloc(sizeof(struct vmem_cell) * cells);
if ((*newrw)->display == NULL) {
free(*newrw);
*newrw = NULL;
return NewIfc_error_allocation_failure;
}
memcpy((*newrw)->display, oldrw->display, sizeof(struct vmem_cell) * cells);
return NewIfc_error_none;
}
......@@ -295,9 +304,7 @@ NewIFC_root_window(NewIfcObj parent, NewIfcObj *newobj)
void test_root_window(CuTest *ct)
{
bool b;
char *s;
NewIfcObj obj;
static const char *new_title = "New Title";
CuAssertTrue(ct, NewIFC_root_window(NULL, &obj) == NewIfc_error_none);
CuAssertPtrNotNull(ct, obj);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment