remove single-member structs

Fri, 01 Nov 2024 17:24:51 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 01 Nov 2024 17:24:51 +0100
changeset 965
dfdfedbe2c86
parent 964
3860f509fcbe
child 966
1aa7ec3e46e7

remove single-member structs

relates to #431

src/cx/json.h file | annotate | diff | comparison | revisions
src/json.c file | annotate | diff | comparison | revisions
--- a/src/cx/json.h	Fri Nov 01 16:21:06 2024 +0100
+++ b/src/cx/json.h	Fri Nov 01 17:24:51 2024 +0100
@@ -69,7 +69,7 @@
     CX_JSON_LITERAL
 };
 
-enum cx_json_literal_type {
+enum cx_json_literal {
     CX_JSON_NULL,
     CX_JSON_TRUE,
     CX_JSON_FALSE
@@ -88,7 +88,6 @@
 
 typedef enum cx_json_token_type CxJsonTokenType;
 typedef enum cx_json_value_type CxJsonValueType;
-typedef enum cx_json_literal_type CxJsonLiteralType;
 typedef enum cx_json_reader_type CxJsonReaderType;
 
 typedef struct cx_json_s CxJson;
@@ -99,9 +98,9 @@
 typedef struct cx_json_array_s CxJsonArray;
 typedef struct cx_json_object_s CxJsonObject;
 typedef struct cx_mutstr_s CxJsonString;
-typedef struct cx_json_integer_s CxJsonInteger;
-typedef struct cx_json_number_s CxJsonNumber;
-typedef struct cx_json_literal_s CxJsonLiteral;
+typedef int64_t CxJsonInteger;
+typedef double CxJsonNumber;
+typedef enum cx_json_literal CxJsonLiteral;
 
 typedef struct cx_json_obj_value_s CxJsonObjValue;
 
@@ -163,20 +162,6 @@
     CxJsonValue *value;
 };
 
-// TODO: remove single member structs
-
-struct cx_json_integer_s {
-    int64_t value;
-};
-
-struct cx_json_number_s {
-    double value;
-};
-
-struct cx_json_literal_s {
-    CxJsonLiteralType literal;
-};
-
 struct cx_json_value_s {
     CxJsonValueType type;
     union {
@@ -200,7 +185,6 @@
 __attribute__((__nonnull__))
 void cxJsonFill(CxJson *json, const char *buf, size_t len);
 
-// TODO: discuss if it is intentional that cxJsonNext() will usually parse an entire file in one go
 __attribute__((__nonnull__))
 int cxJsonNext(CxJson *json, CxJsonValue **value);
 
@@ -239,22 +223,22 @@
 
 __attribute__((__nonnull__))
 static inline bool cxJsonIsBool(CxJsonValue *value) {
-    return cxJsonIsLiteral(value) && value->value.literal.literal != CX_JSON_NULL;
+    return cxJsonIsLiteral(value) && value->value.literal != CX_JSON_NULL;
 }
 
 __attribute__((__nonnull__))
 static inline bool cxJsonIsTrue(CxJsonValue *value) {
-    return cxJsonIsLiteral(value) && value->value.literal.literal == CX_JSON_TRUE;
+    return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_TRUE;
 }
 
 __attribute__((__nonnull__))
 static inline bool cxJsonIsFalse(CxJsonValue *value) {
-    return cxJsonIsLiteral(value) && value->value.literal.literal == CX_JSON_FALSE;
+    return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_FALSE;
 }
 
 __attribute__((__nonnull__))
 static inline bool cxJsonIsNull(CxJsonValue *value) {
-    return cxJsonIsLiteral(value) && value->value.literal.literal == CX_JSON_NULL;
+    return cxJsonIsLiteral(value) && value->value.literal == CX_JSON_NULL;
 }
 
 __attribute__((__nonnull__))
@@ -265,17 +249,17 @@
 
 __attribute__((__nonnull__))
 static inline double cxJsonAsDouble(CxJsonValue *value) {
-    return value->value.number.value;
+    return value->value.number;
 }
 
 __attribute__((__nonnull__))
 static inline int64_t cxJsonAsInteger(CxJsonValue *value) {
-    return value->value.integer.value;
+    return value->value.integer;
 }
 
 __attribute__((__nonnull__))
 static inline bool cxJsonAsBool(CxJsonValue *value) {
-    return value->value.literal.literal == CX_JSON_TRUE;
+    return value->value.literal == CX_JSON_TRUE;
 }
 
 __attribute__((__nonnull__))
--- a/src/json.c	Fri Nov 01 16:21:06 2024 +0100
+++ b/src/json.c	Fri Nov 01 17:24:51 2024 +0100
@@ -541,7 +541,7 @@
     return ret;
 }
 
-static CxJsonLiteralType json_reader_literal(CxJson *p) {
+static CxJsonLiteral json_reader_literal(CxJson *p) {
     const char *l = p->reader_token.content;
     size_t token_len = p->reader_token.length;
     if (token_len == 4 && !memcmp(l, "true", 4)) {
@@ -746,17 +746,17 @@
                 }
                 case CX_JSON_READER_INTEGER: {
                     v->type = CX_JSON_INTEGER;
-                    v->value.integer.value = p->value_int;
+                    v->value.integer = p->value_int;
                     break;
                 }
                 case CX_JSON_READER_NUMBER: {
                     v->type = CX_JSON_NUMBER;
-                    v->value.number.value = p->value_double;
+                    v->value.number = p->value_double;
                     break;
                 }
                 case CX_JSON_READER_LITERAL: {
                     v->type = CX_JSON_LITERAL;
-                    v->value.literal.literal = json_reader_literal(p);
+                    v->value.literal = json_reader_literal(p);
                     break;
                 }
             }

mercurial