summaryrefslogtreecommitdiff
path: root/json.h
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2009-08-27 12:33:38 +0000
committerEric S. Raymond <esr@thyrsus.com>2009-08-27 12:33:38 +0000
commitccc3c5d85f1fabc99e3d4acd43fa5b9da8602cdf (patch)
tree8176a0cedcfba11f745c6de2d22ec4876eb06b3e /json.h
parent2e4cec8f9ff75c03bc5adf55ef3dbf4441c58d37 (diff)
downloadgpsd-ccc3c5d85f1fabc99e3d4acd43fa5b9da8602cdf.tar.gz
Create a convenience macro for JSON parser template declarations.
Diffstat (limited to 'json.h')
-rw-r--r--json.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/json.h b/json.h
index 54aedf4e..b732a39f 100644
--- a/json.h
+++ b/json.h
@@ -76,4 +76,24 @@ const char *json_error_string(int);
#define JSON_ERR_BADENUM 17 /* invalid flag token */
#define JSON_ERR_NOPARSTR 18 /* can't support strings in aparalle. arrays */
+/*
+ * Use the following macros to declare template initializers for structobject
+ * arrays. Writing the equivalents out by hand is error-prone.
+ *
+ * STRUCTOBJECT takes a type t (one of the enumerated scalar field types
+ * above), a structure name s, and a fieldname f in s.
+ *
+ * STRUCTARRAY takes the name of a structure array, a pointer to a an
+ * initializer defining the subobject type, and the address of an integer to
+ * store the length in.
+ */
+#define STRUCTOBJECT(t, s, f) .type = t, .addr.offset = offsetof(s, f)
+#define STRUCTARRAY(a, e, n) .type = array, \
+ .addr.array.element_type = structobject, \
+ .addr.array.arr.objects.subtype = e, \
+ .addr.array.arr.objects.base = (char*)a, \
+ .addr.array.arr.objects.stride = sizeof(a[0]), \
+ .addr.array.count = n, \
+ .addr.array.maxlen = NITEMS(a)
+
/* json.h ends here */