• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 COMMON_NATIVE_INCLUDE_CJSON_CHECK_H
17 #define COMMON_NATIVE_INCLUDE_CJSON_CHECK_H
18 #include "edm_log.h"
19 #include "cJSON.h"
20 
21 namespace OHOS {
22 namespace EDM {
23 #define CJSON_CREATE_OBJECT_AND_CHECK(obj, ret)  \
24     do {                                         \
25         (obj) = cJSON_CreateObject();            \
26         if ((obj) == nullptr) {                  \
27             EDMLOGE("cJSON_CreateObject error"); \
28             return (ret);                        \
29         }                                        \
30     } while (0)
31 
32 #define CJSON_CREATE_OBJECT_AND_CHECK_AND_CLEAR(obj, ret, root)  \
33     do {                                                         \
34         (obj) = cJSON_CreateObject();                            \
35         if ((obj) == nullptr) {                                  \
36             EDMLOGE("cJSON_CreateObject error");                 \
37             cJSON_Delete((root));                                \
38             return (ret);                                        \
39         }                                                        \
40     } while (0)
41 
42 #define CJSON_CREATE_ARRAY_AND_CHECK(obj, ret)  \
43     do {                                        \
44         (obj) = cJSON_CreateArray();            \
45         if ((obj) == nullptr) {                 \
46             EDMLOGE("cJSON_CreateArray error"); \
47             return (ret);                       \
48         }                                       \
49     } while (0)
50 
51 #define CJSON_CREATE_ARRAY_AND_CHECK_VOID(obj)       \
52     do {                                             \
53         (obj) = cJSON_CreateArray();                 \
54         if ((obj) == nullptr) {                      \
55             EDMLOGE("cJSON_CreateArray error");      \
56             return;                                  \
57         }                                            \
58     } while (0)
59 
60 #define CJSON_ADD_ITEM_TO_ARRAY_AND_CHECK_AND_CLEAR(obj, array, ret)  \
61     do {                                                              \
62         if (!cJSON_AddItemToArray((array), (obj))) {                  \
63             EDMLOGE("cJSON_AddItemToArray error");                    \
64             cJSON_Delete((obj));                                      \
65             cJSON_Delete((array));                                    \
66             return ret;                                               \
67         }                                                             \
68     } while (0)
69 
70 #define CJSON_ADD_ITEM_TO_ARRAY_AND_CHECK_AND_CLEAR_VOID(obj, array)  \
71     do {                                                              \
72         if (!cJSON_AddItemToArray((array), (obj))) {                  \
73             EDMLOGE("cJSON_AddItemToArray error");                    \
74             cJSON_Delete((obj));                                      \
75             cJSON_Delete((array));                                    \
76             return;                                                   \
77         }                                                             \
78     } while (0)
79 } // namespace EDM
80 } // namespace OHOS
81 #endif // COMMON_NATIVE_INCLUDE_CJSON_CHECK_H
82