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 HKS_TEMPLATE_H
17 #define HKS_TEMPLATE_H
18 
19 #include "hks_log.h"
20 
21 #undef HKS_NULL_POINTER
22 
23 #ifdef __cplusplus
24 #define HKS_NULL_POINTER nullptr
25 #else
26 #define HKS_NULL_POINTER NULL
27 #endif
28 
29 #define HKS_IF_NOT_SUCC_LOGE_RETURN(RESULT, ERROR_CODE, LOG_MESSAGE, ...) \
30 if ((RESULT) != HKS_SUCCESS) { \
31     HKS_LOG_E(LOG_MESSAGE, ##__VA_ARGS__); \
32     return (ERROR_CODE); \
33 }
34 
35 #define HKS_IF_NOT_SUCC_LOGE_BREAK(RESULT, LOG_MESSAGE, ...) \
36 if ((RESULT) != HKS_SUCCESS) { \
37     HKS_LOG_E(LOG_MESSAGE, ##__VA_ARGS__); \
38     break; \
39 }
40 
41 #define HKS_IF_NOT_SUCC_BREAK(RESULT, ...) \
42 if ((RESULT) != HKS_SUCCESS) { \
43     break; \
44 }
45 
46 #define HKS_IF_NOT_SUCC_LOGE(RESULT, LOG_MESSAGE, ...) \
47 if ((RESULT) != HKS_SUCCESS) { \
48     HKS_LOG_E(LOG_MESSAGE, ##__VA_ARGS__); \
49 }
50 
51 #define HKS_IF_NOT_SUCC_RETURN(RESULT, ERROR_CODE) \
52 if ((RESULT) != HKS_SUCCESS) { \
53     return (ERROR_CODE); \
54 }
55 
56 #define HKS_IF_NULL_LOGE_RETURN(OBJECT, ERROR_CODE, LOG_MESSAGE, ...) \
57 if ((OBJECT) == HKS_NULL_POINTER) { \
58     HKS_LOG_E(LOG_MESSAGE, ##__VA_ARGS__); \
59     return (ERROR_CODE); \
60 }
61 
62 #define HKS_IF_NULL_LOGE_RETURN_VOID(OBJECT, LOG_MESSAGE, ...) \
63 if ((OBJECT) == HKS_NULL_POINTER) { \
64     HKS_LOG_E(LOG_MESSAGE, ##__VA_ARGS__); \
65     return; \
66 }
67 
68 #define HKS_IF_NULL_LOGE_BREAK(OBJECT, LOG_MESSAGE, ...) \
69 if ((OBJECT) == HKS_NULL_POINTER) { \
70     HKS_LOG_E(LOG_MESSAGE, ##__VA_ARGS__); \
71     break; \
72 }
73 
74 #define HKS_IF_NULL_RETURN(OBJECT, ERROR_CODE) \
75 if ((OBJECT) == HKS_NULL_POINTER) { \
76     return (ERROR_CODE); \
77 }
78 
79 #define HKS_IF_NULL_BREAK(OBJECT) \
80 if ((OBJECT) == HKS_NULL_POINTER) { \
81     break; \
82 }
83 
84 #define HKS_IF_NOT_TRUE_LOGE_RETURN(BOOL_FUNC, ERROR_CODE) \
85 do { \
86     if (!(BOOL_FUNC)) { \
87         HKS_LOG_E("%{public}s failed!", #BOOL_FUNC); \
88         return ERROR_CODE; \
89     } \
90 } while (0)
91 
92 #define HKS_IF_NOT_TRUE_LOGE_RETURN_VOID(BOOL_FUNC) \
93 do { \
94     if (!(BOOL_FUNC)) { \
95         HKS_LOG_E("%{public}s failed!", #BOOL_FUNC); \
96         return; \
97     } \
98 } while (0)
99 #endif /* HKS_TEMPLATE_H */