1 /*
2  * Copyright (c) 2021 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 POWER_COMMON_H
17 #define POWER_COMMON_H
18 
19 #include <cstdint>
20 #include <type_traits>
21 
22 #include "battery_log.h"
23 #include "power_mgr_errors.h"
24 
25 namespace OHOS {
26 namespace PowerMgr {
27 #define RETURN_IF_WITH_RET(cond, retval) if (cond) {return (retval);}
28 #define RETURN_IF(cond) if (cond) {return;}
29 #define RETURN_IF_WITH_LOG(cond, loginfo)                                                   \
30     do {                                                                                \
31         if (cond) {                                                                     \
32             BATTERY_HILOGW(COMP_FWK, #loginfo);                                        \
33             return;                                                                     \
34         }                                                                               \
35     } while (0)                                                                         \
36 
37 #define RETURN_IF_READ_PARCEL_FAILED_NO_RET(parcel, type, out)                                  \
38     do {                                                                                \
39         if (!(parcel).Read##type(out)) {                                                \
40             BATTERY_HILOGW(COMP_FWK, "read "#out" failed");                             \
41             return;                                                                     \
42         }                                                                               \
43     } while (0)                                                                         \
44 
45 #define RETURN_IF_WRITE_PARCEL_FAILED_NO_RET(parcel, type, data)                                \
46     do {                                                                                \
47         if (!(parcel).Write##type(data)) {                                              \
48             BATTERY_HILOGW(COMP_FWK, "write "#data" failed");                           \
49             return;                                                                     \
50         }                                                                               \
51     } while (0)                                                                         \
52 
53 #define RETURN_IF_READ_PARCEL_FAILED_WITH_RET(parcel, type, out, retval)                        \
54     do {                                                                               \
55         if (!(parcel).Read##type(out)) {                                               \
56             BATTERY_HILOGW(COMP_FWK, "read "#out" failed");                            \
57             return (retval);                                                           \
58         }                                                                              \
59     } while (0)                                                                        \
60 
61 #define RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(parcel, type, data, retval)                      \
62     do {                                                                               \
63         if (!(parcel).Write##type(data)) {                                             \
64             BATTERY_HILOGW(COMP_FWK, "write "#data" failed");                          \
65             return (retval);                                                           \
66         }                                                                              \
67     } while (0)
68 
69 template<typename E>
ToUnderlying(E e)70 constexpr auto ToUnderlying(E e) noexcept
71 {
72     return static_cast<std::underlying_type_t<E>>(e);
73 }
74 } // namespace PowerMgr
75 } // namespace OHOS
76 
77 #endif // POWER_COMMON_H
78