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