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 UTIL_EX_H
17 #define UTIL_EX_H
18
19 #include "securec.h"
20
21 #include "util.h"
22
23 #undef MMI_LOG_TAG
24 #define MMI_LOG_TAG "UtilEx"
25
26 namespace OHOS {
27 namespace MMI {
28 template<class ...Ts>
mprintf(int32_t fd,const char * fmt,Ts...args)29 int32_t mprintf(int32_t fd, const char* fmt, Ts... args)
30 {
31 if (fmt == nullptr) {
32 return RET_ERR;
33 }
34
35 static constexpr size_t bufSize = 1024 * 10;
36 char buf[bufSize] = {};
37 int32_t ret = snprintf_s(buf, bufSize, bufSize - 1, fmt, args...);
38 if (ret == -1) {
39 return ret;
40 }
41
42 if (fd < 0) {
43 ret = printf("%s\n", buf);
44 } else if (fd == 0) {
45 MMI_HILOGF("%{public}s", buf);
46 } else {
47 ret = dprintf(fd, "%s\n", buf);
48 }
49 return ret;
50 }
51
52 template <class Enum>
53 constexpr auto EnumUnderlyingValue(Enum const e) -> typename std::underlying_type<Enum>::type
54 {
55 static_assert(std::is_enum<Enum>::value, "input value is not of enum class nor enum");
56 return static_cast<typename std::underlying_type<Enum>::type>(e);
57 }
58
59 template <class Enum, class T>
EnumAdd(Enum const e,T val)60 Enum EnumAdd(Enum const e, T val)
61 {
62 static_assert(std::is_enum<Enum>::value, "input value is not of enum class nor enum");
63 auto a = EnumUnderlyingValue(e);
64 return static_cast<Enum>(a + val);
65 }
66 } // namespace MMI
67 } // namespace OHOS
68 #endif // UTIL_EX_H
69