1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef DEFINE_MULTIMODAL_H
17 #define DEFINE_MULTIMODAL_H
18 
19 #include "mmi_log.h"
20 
21 namespace OHOS {
22 namespace MMI {
23 #ifndef RET_OK
24     #define RET_OK (0)
25 #endif
26 
27 #ifndef RET_ERR
28     #define RET_ERR (-1)
29 #endif
30 
31 inline constexpr int32_t INVALID_FD { -1 };
32 inline constexpr int32_t INVALID_PID { -1 };
33 
34 #ifndef LINEINFO
35 #define LINEINFO __FILE__, __LINE__
36 #endif
37 
38 #ifdef DEBUG_CODE_TEST
39 #define CHKPL(cond, ...) \
40     do { \
41         if ((cond) == nullptr) { \
42             MMI_HILOGW("%{public}s, (%{public}d), CHKPL(%{public}s) is null, do nothing", \
43                 __FILE__, __LINE__, #cond); \
44         } \
45     } while (0)
46 
47 #define CHKPV(cond) \
48     do { \
49         if ((cond) == nullptr) { \
50             MMI_HILOGE("%{public}s, (%{public}d), CHKPV(%{public}s) is null", \
51                 __FILE__, __LINE__, #cond); \
52             return; \
53         } \
54     } while (0)
55 
56 #define CHKPF(cond) \
57     do { \
58         if ((cond) == nullptr) { \
59             MMI_HILOGE("%{public}s, (%{public}d), CHKPF(%{public}s) is null", \
60                 __FILE__, __LINE__, #cond); \
61             return false; \
62         } \
63     } while (0)
64 
65 #define CHKPS(cond) \
66     do { \
67         if ((cond) == nullptr) { \
68             MMI_HILOGE("%{public}s, (%{public}d), CHKPS(%{public}s) is null", \
69                 __FILE__, __LINE__, #cond); \
70             return ""; \
71         } \
72     } while (0)
73 
74 #define CHKPC(cond) \
75     { \
76         if ((cond) == nullptr) { \
77             MMI_HILOGW("%{public}s, (%{public}d), CHKPC(%{public}s) is null, skip then continue", \
78                 __FILE__, __LINE__, #cond); \
79             continue; \
80         } \
81     }
82 
83 #define CHKPB(cond) \
84     { \
85         if ((cond) == nullptr) { \
86             MMI_HILOGW("%{public}s, (%{public}d), CHKPB(%{public}s) is null, skip then break", \
87                 __FILE__, __LINE__, #cond); \
88             break; \
89         } \
90     }
91 
92 #define CHKPR(cond, r) \
93     do { \
94         if ((cond) == nullptr) { \
95             MMI_HILOGE("%{public}s, (%{public}d), CHKPR(%{public}s) is null, return value is %{public}d", \
96                 __FILE__, __LINE__, #cond, r); \
97             return r; \
98         } \
99     } while (0)
100 
101 #define CHKPP(cond) \
102     do { \
103         if ((cond) == nullptr) { \
104             MMI_HILOGE("%{public}s, (%{public}d), CHKPP(%{public}s) is null, return value is null", \
105                 __FILE__, __LINE__, #cond); \
106             return nullptr; \
107         } \
108     } while (0)
109 
110 #define CHKPO(cond) \
111     do { \
112         if ((cond) == nullptr) { \
113             MMI_HILOGW("%{public}s, (%{public}d), CHKPO(%{public}s) is null, skip then continue", \
114                 __FILE__, __LINE__, #cond); \
115             return {}; \
116         } \
117     } while (0)
118 
119 #define CK(cond, ec) \
120     do { \
121         if (!(cond)) { \
122             MMI_HILOGE("%{public}s, (%{public}d), CK(%{public}s), errCode:%{public}d", \
123                 __FILE__, __LINE__, #cond, ec); \
124         } \
125     } while (0)
126 
127 #define CHK_PID_AND_TID() \
128     do { \
129         MMI_HILOGD("%{public}s, (%{public}d), pid:%{public}d threadId:%{public}" PRIu64, \
130             __FILE__, __LINE__, GetPid(), GetThisThreadId()); \
131     } while (0)
132 
133 #else // DEBUG_CODE_TEST
134 #define CHKPL(cond) \
135     do { \
136         if ((cond) == nullptr) { \
137             MMI_HILOGW("CHKPL(%{public}s) is null, do nothing", #cond); \
138         } \
139     } while (0)
140 
141 #define CHKPV(cond) \
142     do { \
143         if ((cond) == nullptr) { \
144             MMI_HILOGE("CHKPV(%{public}s) is null", #cond); \
145             return; \
146         } \
147     } while (0)
148 
149 #define CHK_INVALID_RV(cond, desc) \
150     do { \
151         if ((cond) < 0) { \
152             MMI_HILOGE("(%{public}s less than 0, desc:%{public}s)", #cond, std::string(desc).c_str()); \
153             return; \
154         } \
155     } while (0)
156 
157 #define CHKPF(cond) \
158     do { \
159         if ((cond) == nullptr) { \
160             MMI_HILOGE("CHKPF(%{public}s) is null", #cond); \
161             return false; \
162         } \
163     } while (0)
164 
165 #define CHECKSIZE(arg0, arg1) \
166     do { \
167         if ((arg0) > (arg1)) { \
168             MMI_HILOGE("arg0 value is out of arg1 size"); \
169             return false; \
170         } \
171     } while (0)
172 
173 #define CHKPS(cond) \
174     do { \
175         if ((cond) == nullptr) { \
176             MMI_HILOGE("CHKPS(%{public}s) is null", #cond); \
177             return ""; \
178         } \
179     } while (0)
180 
181 #define CHKPC(cond) \
182     { \
183         if ((cond) == nullptr) { \
184             MMI_HILOGW("CHKPC(%{public}s) is null, skip then continue", #cond); \
185             continue; \
186         } \
187     }
188 
189 #define CHKPB(cond) \
190     { \
191         if ((cond) == nullptr) { \
192             MMI_HILOGW("CHKPB(%{public}s) is null, skip then break", #cond); \
193             break; \
194         } \
195     }
196 
197 #define CHKPR(cond, r) \
198     do { \
199         if ((cond) == nullptr) { \
200             MMI_HILOGE("CHKPR(%{public}s) is null, return value is %{public}d", #cond, r); \
201             return r; \
202         } \
203     } while (0)
204 
205 #define CHKFR(cond, r, desc) \
206     do { \
207         if (!(cond)) { \
208             MMI_HILOGE("CHKFR(%{public}s) is false, hint is %{public}s", #cond, desc); \
209             return r; \
210         } \
211     } while (0)
212 
213 #define CHK_KEY_ITEM(keyItem) \
214     do { \
215         if (!(keyItem)) { \
216             MMI_HILOGE("The keyItem is nullopt"); \
217             return false; \
218         } \
219     } while (0)
220 
221 #define CHKPRV(cond, msg) \
222     do { \
223         if ((cond) == nullptr) { \
224             MMI_HILOGE("CHKPRV(%{public}s) is null, msg %{public}s", #cond, msg); \
225             return; \
226         } \
227     } while (0)
228 
229 #define CHKNOKRV(cond, msg) \
230     do { \
231         if ((cond) != RET_OK) { \
232             MMI_HILOGE("CHKNOKRV(%{public}s) is not RET_OK, hint is %{public}s", #cond, msg); \
233             return; \
234         } \
235     } while (0)
236 
237 #define CHKFRV(cond, msg) \
238     do { \
239         if (!(cond)) { \
240             MMI_HILOGE("CHKFRV(%{public}s) is null, hint is %{public}s", #cond, msg); \
241             return; \
242         } \
243     } while (0)
244 
245 #define CHKPP(cond) \
246     do { \
247         if ((cond) == nullptr) { \
248             MMI_HILOGE("CHKPP(%{public}s) is null, return value is null", #cond); \
249             return nullptr; \
250         } \
251     } while (0)
252 
253 #define CHKPO(cond) \
254     do { \
255         if ((cond) == nullptr) { \
256             MMI_HILOGW("%{public}s, (%{public}d), CHKPO(%{public}s) is null, return object is null", \
257                 __FILE__, __LINE__, #cond); \
258             return {}; \
259         } \
260     } while (0)
261 
262 #define CK(cond, ec) \
263     do { \
264         if (!(cond)) { \
265             MMI_HILOGE("CK(%{public}s), errCode:%{public}d", #cond, ec); \
266         } \
267     } while (0)
268 
269 #define CHK_PID_AND_TID() \
270     do { \
271         MMI_HILOGD("pid:%{public}d threadId:%{public}" PRIu64, GetPid(), GetThisThreadId()); \
272     } while (0)
273 
274 #endif
275 
276 #define DEFRET_1(data, value, ...) (value)
277 #define DEFRET(...) DEFRET_1(__VA_ARGS__, false)
278 
279 #define WRITEBOOL(parcel, data, ...) \
280     do { \
281         if (!(parcel).WriteBool(data)) { \
282             MMI_HILOGE("WriteBool "#data" failed"); \
283             return DEFRET(false, ##__VA_ARGS__); \
284         } \
285     } while (0)
286 
287 #define WRITEINT32(parcel, data, ...) \
288     do { \
289         if (!(parcel).WriteInt32(data)) { \
290             MMI_HILOGE("WriteInt32 "#data" failed"); \
291             return DEFRET(false, ##__VA_ARGS__); \
292         } \
293     } while (0)
294 
295 #define WRITEINT64(parcel, data, ...) \
296     do { \
297         if (!(parcel).WriteInt64(data)) { \
298             MMI_HILOGE("WriteInt64 "#data" failed"); \
299             return DEFRET(false, ##__VA_ARGS__); \
300         } \
301     } while (0)
302 
303 #define WRITEUINT8(parcel, data, ...) \
304     do { \
305         if (!(parcel).WriteUint8(data)) { \
306             MMI_HILOGE("WriteUint8 "#data" failed"); \
307             return DEFRET(false, ##__VA_ARGS__); \
308         } \
309     } while (0)
310 
311 #define WRITEUINT32(parcel, data, ...) \
312     do { \
313         if (!(parcel).WriteUint32(data)) { \
314             MMI_HILOGE("WriteUint32 "#data" failed"); \
315             return DEFRET(false, ##__VA_ARGS__); \
316         } \
317     } while (0)
318 
319 #define WRITEUINT64(parcel, data, ...) \
320     do { \
321         if (!(parcel).WriteUint64(data)) { \
322             MMI_HILOGE("WriteUint64 "#data" failed"); \
323             return DEFRET(false, ##__VA_ARGS__); \
324         } \
325     } while (0)
326 
327 #define WRITEDOUBLE(parcel, data, ...) \
328     do { \
329         if (!(parcel).WriteDouble(data)) { \
330             MMI_HILOGE("WriteDouble "#data" failed"); \
331             return DEFRET(false, ##__VA_ARGS__); \
332         } \
333     } while (0)
334 
335 #define WRITEFLOAT(parcel, data, ...) \
336     do { \
337         if (!(parcel).WriteFloat(data)) { \
338             MMI_HILOGE("WriteFloat "#data" failed"); \
339             return DEFRET(false, ##__VA_ARGS__); \
340         } \
341     } while (0)
342 
343 #define WRITESTRING(parcel, data, ...) \
344     do { \
345         if (!(parcel).WriteString(data)) { \
346             MMI_HILOGE("WriteString "#data" failed"); \
347             return DEFRET(false, ##__VA_ARGS__); \
348         } \
349     } while (0)
350 
351 #define WRITEBUFFER(parcel, data, length, ...) \
352     do { \
353         if (!(parcel).WriteBuffer((data), length)) { \
354             MMI_HILOGE("WriteBuffer "#data" failed"); \
355             return DEFRET(false, ##__VA_ARGS__); \
356         } \
357     } while (0)
358 
359 #define WRITEREMOTEOBJECT(parcel, data, ...) \
360     do { \
361         if (!(parcel).WriteRemoteObject(data)) { \
362             MMI_HILOGE("WriteRemoteObject "#data" failed"); \
363             return DEFRET(false, ##__VA_ARGS__); \
364         } \
365     } while (0)
366 
367 #define READBOOL(parcel, data, ...) \
368     do { \
369         if (!(parcel).ReadBool(data)) { \
370             MMI_HILOGE("ReadBool "#data" failed"); \
371             return DEFRET(false, ##__VA_ARGS__); \
372         } \
373     } while (0)
374 
375 #define READINT32(parcel, data, ...) \
376     do { \
377         if (!(parcel).ReadInt32(data)) { \
378             MMI_HILOGE("ReadInt32 "#data" failed"); \
379             return DEFRET(false, ##__VA_ARGS__); \
380         } \
381     } while (0)
382 
383 #define READINT64(parcel, data, ...) \
384     do { \
385         if (!(parcel).ReadInt64(data)) { \
386             MMI_HILOGE("ReadInt64 "#data" failed"); \
387             return DEFRET(false, ##__VA_ARGS__); \
388         } \
389     } while (0)
390 
391 #define READUINT8(parcel, data, ...) \
392     do { \
393         if (!(parcel).ReadUint8(data)) { \
394             MMI_HILOGE("ReadUint8 "#data" failed"); \
395             return DEFRET(false, ##__VA_ARGS__); \
396         } \
397     } while (0)
398 
399 #define READUINT32(parcel, data, ...) \
400     do { \
401         if (!(parcel).ReadUint32(data)) { \
402             MMI_HILOGE("ReadUint32 "#data" failed"); \
403             return DEFRET(false, ##__VA_ARGS__); \
404         } \
405     } while (0)
406 
407 #define READUINT64(parcel, data, ...) \
408     do { \
409         if (!(parcel).ReadUint64(data)) { \
410             MMI_HILOGE("ReadUint64 "#data" failed"); \
411             return DEFRET(false, ##__VA_ARGS__); \
412         } \
413     } while (0)
414 
415 #define READDOUBLE(parcel, data, ...) \
416     do { \
417         if (!(parcel).ReadDouble(data)) { \
418             MMI_HILOGE("ReadDouble "#data" failed"); \
419             return DEFRET(false, ##__VA_ARGS__); \
420         } \
421     } while (0)
422 
423 #define READFLOAT(parcel, data, ...) \
424     do { \
425         if (!(parcel).ReadFloat(data)) { \
426             MMI_HILOGE("ReadFloat "#data" failed"); \
427             return DEFRET(false, ##__VA_ARGS__); \
428         } \
429     } while (0)
430 
431 #define READSTRING(parcel, data, ...) \
432     do { \
433         if (!(parcel).ReadString(data)) { \
434             MMI_HILOGE("ReadString "#data" failed"); \
435             return DEFRET(false, ##__VA_ARGS__); \
436         } \
437     } while (0)
438 } // namespace MMI
439 } // namespace OHOS
440 #endif // DEFINE_MULTIMODAL_H
441