• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 JS_COMMON_H
17 #define JS_COMMON_H
18 #include "hilog/log.h"
19 namespace OHOS::ObjectStore {
20 #define CHECK_EQUAL_WITH_RETURN_NULL(status, value)                   \
21     {                                                                 \
22         if (status != value) {                                        \
23             LOG_ERROR("error! %{public}d %{public}d", status, value); \
24             return nullptr;                                           \
25         }                                                             \
26     }
27 
28 #define CHECK_EQUAL_WITH_RETURN_VOID(status, value)                   \
29     {                                                                 \
30         if (status != value) {                                        \
31             LOG_ERROR("error! %{public}d %{public}d", status, value); \
32             return;                                                   \
33         }                                                             \
34     }
35 
36 #define CHECK_EQUAL_WITH_RETURN_FALSE(status, value)                  \
37     {                                                                 \
38         if (status != value) {                                        \
39             LOG_ERROR("error! %{public}d %{public}d", status, value); \
40             return false;                                             \
41         }                                                             \
42     }
43 
44 #define ASSERT_MATCH_ELSE_RETURN_VOID(condition)        \
45     {                                                   \
46         if (!(condition)) {                             \
47             LOG_ERROR("error! %{public}s", #condition); \
48             return;                                     \
49         }                                               \
50     }
51 
52 #define ASSERT_MATCH_ELSE_RETURN_NULL(condition)        \
53     {                                                   \
54         if (!(condition)) {                             \
55             LOG_ERROR("error! %{public}s", #condition); \
56             return nullptr;                             \
57         }                                               \
58     }
59 
60 #define ASSERT_MATCH_ELSE_GOTO_ERROR(condition)         \
61     {                                                   \
62         if (!(condition)) {                             \
63             LOG_ERROR("error! %{public}s", #condition); \
64             goto ERROR;                                 \
65         }                                               \
66     }
67 
68 #define CHECK_API_VALID(assertion)                                                                           \
69     do {                                                                                                     \
70         if (!(assertion)) {                                                                                  \
71             std::shared_ptr<DeviceNotSupportedError> apiError = std::make_shared<DeviceNotSupportedError>(); \
72             ctxt->SetError(apiError);                                                                        \
73             ctxt->status = napi_generic_failure;                                                             \
74             return;                                                                                          \
75         }                                                                                                    \
76     } while (0)
77 
78 #define CHECK_VALID(assertion, msg)                                                  \
79     do {                                                                             \
80         if (!(assertion)) {                                                          \
81             std::shared_ptr<InnerError> innerError = std::make_shared<InnerError>(); \
82             ctxt->SetError(innerError);                                              \
83             ctxt->status = napi_generic_failure;                                     \
84             ctxt->message = msg;                                                     \
85             return;                                                                  \
86         }                                                                            \
87     } while (0)
88 } // namespace OHOS::ObjectStore
89 static const char *CHANGE = "change";
90 static const char *STATUS = "status";
91 #endif // JS_COMMON_H
92