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