1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
From f0f43a3ad6ee46d6e6378325fc03b1adadd35c41 Mon Sep 17 00:00:00 2001
From: Michael Forney <mforney@mforney.org>
Date: Mon, 1 Jul 2019 23:13:26 -0700
Subject: [PATCH] Use static inline functions instead of macros
---
include/libwapcaplet/libwapcaplet.h | 102 +++++++++++-----------------
1 file changed, 39 insertions(+), 63 deletions(-)
diff --git a/include/libwapcaplet/libwapcaplet.h b/include/libwapcaplet/libwapcaplet.h
index 57e2e48..96dd7aa 100644
--- a/include/libwapcaplet/libwapcaplet.h
+++ b/include/libwapcaplet/libwapcaplet.h
@@ -133,17 +133,21 @@ extern lwc_error lwc_string_tolower(lwc_string *str, lwc_string **ret);
* @note Use this if copying the string and intending both sides to retain
* ownership.
*/
-#if defined(STMTEXPR)
-#define lwc_string_ref(str) ({lwc_string *__lwc_s = (str); assert(__lwc_s != NULL); __lwc_s->refcnt++; __lwc_s;})
-#else
-static inline lwc_string *
-lwc_string_ref(lwc_string *str)
+static inline lwc_string *lwc_string_ref(lwc_string *str)
{
assert(str != NULL);
str->refcnt++;
return str;
}
-#endif
+
+/**
+ * Destroy an unreffed lwc_string.
+ *
+ * This destroys an lwc_string whose reference count indicates that it should be.
+ *
+ * @param str The string to unref.
+ */
+extern void lwc_string_destroy(lwc_string *str);
/**
* Release a reference on an lwc_string.
@@ -156,23 +160,14 @@ lwc_string_ref(lwc_string *str)
* freed. (Ref count of 1 where string is its own insensitve match
* will also result in the string being freed.)
*/
-#define lwc_string_unref(str) { \
- lwc_string *__lwc_s = (str); \
- assert(__lwc_s != NULL); \
- __lwc_s->refcnt--; \
- if ((__lwc_s->refcnt == 0) || \
- ((__lwc_s->refcnt == 1) && (__lwc_s->insensitive == __lwc_s))) \
- lwc_string_destroy(__lwc_s); \
- }
-
-/**
- * Destroy an unreffed lwc_string.
- *
- * This destroys an lwc_string whose reference count indicates that it should be.
- *
- * @param str The string to unref.
- */
-extern void lwc_string_destroy(lwc_string *str);
+static inline void lwc_string_unref(lwc_string *str)
+{
+ assert(str != NULL);
+ str->refcnt--;
+ if ((str->refcnt == 0) ||
+ ((str->refcnt == 1) && (str->insensitive == str)))
+ lwc_string_destroy(str);
+}
/**
* Check if two interned strings are equal.
@@ -183,8 +178,12 @@ extern void lwc_string_destroy(lwc_string *str);
* @return Result of operation, if not ok then value pointed to
* by \a ret will not be valid.
*/
-#define lwc_string_isequal(str1, str2, ret) \
- ((*(ret) = ((str1) == (str2))), lwc_error_ok)
+static inline lwc_error lwc_string_isequal(
+ lwc_string *str1, lwc_string *str2, bool *ret)
+{
+ *ret = (str1 == str2);
+ return lwc_error_ok;
+}
/**
* Intern a caseless copy of the passed string.
@@ -200,7 +199,6 @@ extern void lwc_string_destroy(lwc_string *str);
extern lwc_error
lwc__intern_caseless_string(lwc_string *str);
-#if defined(STMTEXPR)
/**
* Check if two interned strings are case-insensitively equal.
*
@@ -210,33 +208,6 @@ lwc__intern_caseless_string(lwc_string *str);
* @return Result of operation, if not ok then value pointed to by \a ret will
* not be valid.
*/
-#define lwc_string_caseless_isequal(_str1,_str2,_ret) ({ \
- lwc_error __lwc_err = lwc_error_ok; \
- lwc_string *__lwc_str1 = (_str1); \
- lwc_string *__lwc_str2 = (_str2); \
- bool *__lwc_ret = (_ret); \
- \
- if (__lwc_str1->insensitive == NULL) { \
- __lwc_err = lwc__intern_caseless_string(__lwc_str1); \
- } \
- if (__lwc_err == lwc_error_ok && __lwc_str2->insensitive == NULL) { \
- __lwc_err = lwc__intern_caseless_string(__lwc_str2); \
- } \
- if (__lwc_err == lwc_error_ok) \
- *__lwc_ret = (__lwc_str1->insensitive == __lwc_str2->insensitive); \
- __lwc_err; \
- })
-
-#else
-/**
- * Check if two interned strings are case-insensitively equal.
- *
- * @param str1 The first string in the comparison.
- * @param str2 The second string in the comparison.
- * @param ret A pointer to a boolean to be filled out with the result.
- * @return Result of operation, if not ok then value pointed to by \a ret will
- * not be valid.
- */
static inline lwc_error
lwc_string_caseless_isequal(lwc_string *str1, lwc_string *str2, bool *ret)
{
@@ -251,13 +222,6 @@ lwc_string_caseless_isequal(lwc_string *str1, lwc_string *str2, bool *ret)
*ret = (str1->insensitive == str2->insensitive);
return err;
}
-#endif
-
-#if defined(STMTEXPR)
-#define lwc__assert_and_expr(str, expr) ({assert(str != NULL); expr;})
-#else
-#define lwc__assert_and_expr(str, expr) (expr)
-#endif
/**
* Retrieve the data pointer for an interned string.
@@ -271,7 +235,11 @@ lwc_string_caseless_isequal(lwc_string *str1, lwc_string *str2, bool *ret)
* in future. Any code relying on it currently should be
* modified to use ::lwc_string_length if possible.
*/
-#define lwc_string_data(str) lwc__assert_and_expr(str, (const char *)((str)+1))
+static inline const char *lwc_string_data(lwc_string *str)
+{
+ assert(str != NULL);
+ return (const char *)(str + 1);
+}
/**
* Retrieve the data length for an interned string.
@@ -279,7 +247,11 @@ lwc_string_caseless_isequal(lwc_string *str1, lwc_string *str2, bool *ret)
* @param str The string to retrieve the length of.
* @return The length of \a str.
*/
-#define lwc_string_length(str) lwc__assert_and_expr(str, (str)->len)
+static inline size_t lwc_string_length(lwc_string *str)
+{
+ assert(str != NULL);
+ return str->len;
+}
/**
* Retrieve (or compute if unavailable) a hash value for the content of the string.
@@ -293,7 +265,11 @@ lwc_string_caseless_isequal(lwc_string *str1, lwc_string *str2, bool *ret)
* to be stable between invocations of the program. Never use the hash
* value as a way to directly identify the value of the string.
*/
-#define lwc_string_hash_value(str) lwc__assert_and_expr(str, (str)->hash)
+static inline lwc_hash lwc_string_hash_value(lwc_string *str)
+{
+ assert(str != NULL);
+ return str->hash;
+}
/**
* Retrieve a hash value for the caseless content of the string.
--
2.26.2
|