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