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 STATS_COMMON_H
17 #define STATS_COMMON_H
18
19 #include <cstdint>
20 #include <type_traits>
21
22 #include "stats_errors.h"
23 #include "stats_log.h"
24
25 namespace OHOS {
26 namespace PowerMgr {
27 #define STATS_RETURN_IF_WITH_RET(cond, retval) if (cond) { return (retval); }
28 #define STATS_RETURN_IF(cond) if (cond) { return; }
29 #define STATS_RETURN_IF_WITH_LOG(loglabel, cond, loginfo) \
30 do { \
31 if (cond) { \
32 STATS_HILOGE(loglabel, ""#loginfo""); \
33 return; \
34 } \
35 } while (0) \
36
37 #define STATS_READ_PARCEL_NO_RET(loglabel, parcel, type, out) \
38 do { \
39 if (!(parcel).Read##type(out)) { \
40 STATS_HILOGE(loglabel, "Read "#out" failed"); \
41 return; \
42 } \
43 } while (0) \
44
45 #define STATS_WRITE_PARCEL_NO_RET(loglabel, parcel, type, data) \
46 do { \
47 if (!(parcel).Write##type(data)) { \
48 STATS_HILOGE(loglabel, "Write "#data" failed"); \
49 return; \
50 } \
51 } while (0) \
52
53 #define STATS_READ_PARCEL_WITH_RET(loglabel, parcel, type, out, retval) \
54 do { \
55 if (!(parcel).Read##type(out)) { \
56 STATS_HILOGE(loglabel, "Read "#out" failed"); \
57 return (retval); \
58 } \
59 } while (0) \
60
61 #define STATS_WRITE_PARCEL_WITH_RET(loglabel, parcel, type, data, retval) \
62 do { \
63 if (!(parcel).Write##type(data)) { \
64 STATS_HILOGE(loglabel, "Write "#data" failed"); \
65 return (retval); \
66 } \
67 } while (0)
68
69 template<typename E>
StatsToUnderlying(E e)70 constexpr auto StatsToUnderlying(E e) noexcept
71 {
72 return static_cast<std::underlying_type_t<E>>(e);
73 }
74 } // namespace PowerMgr
75 } // namespace OHOS
76
77 #endif // STATS_COMMON_H
78