1c29fa5a6Sopenharmony_ci/*
2c29fa5a6Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at
6c29fa5a6Sopenharmony_ci *
7c29fa5a6Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8c29fa5a6Sopenharmony_ci *
9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and
13c29fa5a6Sopenharmony_ci * limitations under the License.
14c29fa5a6Sopenharmony_ci */
15c29fa5a6Sopenharmony_ci
16c29fa5a6Sopenharmony_ci#ifndef UTIL_EX_H
17c29fa5a6Sopenharmony_ci#define UTIL_EX_H
18c29fa5a6Sopenharmony_ci
19c29fa5a6Sopenharmony_ci#include <ctime>
20c29fa5a6Sopenharmony_ci#include <map>
21c29fa5a6Sopenharmony_ci#include <string>
22c29fa5a6Sopenharmony_ci#include <type_traits>
23c29fa5a6Sopenharmony_ci#include <vector>
24c29fa5a6Sopenharmony_ci
25c29fa5a6Sopenharmony_ci#include "securec.h"
26c29fa5a6Sopenharmony_ci
27c29fa5a6Sopenharmony_ci#include "define_multimodal.h"
28c29fa5a6Sopenharmony_ci#include "mmi_log.h"
29c29fa5a6Sopenharmony_ci#include "struct_multimodal.h"
30c29fa5a6Sopenharmony_ci#include "util.h"
31c29fa5a6Sopenharmony_ci
32c29fa5a6Sopenharmony_ci#undef MMI_LOG_TAG
33c29fa5a6Sopenharmony_ci#define MMI_LOG_TAG "UtilEx"
34c29fa5a6Sopenharmony_ci
35c29fa5a6Sopenharmony_cinamespace OHOS {
36c29fa5a6Sopenharmony_cinamespace MMI {
37c29fa5a6Sopenharmony_citemplate<class ...Ts>
38c29fa5a6Sopenharmony_ciint32_t mprintf(int32_t fd, const char* fmt, Ts... args)
39c29fa5a6Sopenharmony_ci{
40c29fa5a6Sopenharmony_ci    if (fmt == nullptr) {
41c29fa5a6Sopenharmony_ci        return RET_ERR;
42c29fa5a6Sopenharmony_ci    }
43c29fa5a6Sopenharmony_ci
44c29fa5a6Sopenharmony_ci    static constexpr size_t bufSize = 1024 * 10;
45c29fa5a6Sopenharmony_ci    char buf[bufSize] = {};
46c29fa5a6Sopenharmony_ci    int32_t ret = snprintf_s(buf, bufSize, bufSize - 1, fmt, args...);
47c29fa5a6Sopenharmony_ci    if (ret == -1) {
48c29fa5a6Sopenharmony_ci        return ret;
49c29fa5a6Sopenharmony_ci    }
50c29fa5a6Sopenharmony_ci
51c29fa5a6Sopenharmony_ci    if (fd < 0) {
52c29fa5a6Sopenharmony_ci        ret = printf("%s\n", buf);
53c29fa5a6Sopenharmony_ci    } else if (fd == 0) {
54c29fa5a6Sopenharmony_ci        MMI_HILOGF("%{public}s", buf);
55c29fa5a6Sopenharmony_ci    } else {
56c29fa5a6Sopenharmony_ci        ret = dprintf(fd, "%s\n", buf);
57c29fa5a6Sopenharmony_ci    }
58c29fa5a6Sopenharmony_ci    return ret;
59c29fa5a6Sopenharmony_ci}
60c29fa5a6Sopenharmony_ci
61c29fa5a6Sopenharmony_citemplate <class Enum>
62c29fa5a6Sopenharmony_ciconstexpr auto EnumUnderlyingValue(Enum const e) -> typename std::underlying_type<Enum>::type
63c29fa5a6Sopenharmony_ci{
64c29fa5a6Sopenharmony_ci    static_assert(std::is_enum<Enum>::value, "input value is not of enum class nor enum");
65c29fa5a6Sopenharmony_ci    return static_cast<typename std::underlying_type<Enum>::type>(e);
66c29fa5a6Sopenharmony_ci}
67c29fa5a6Sopenharmony_ci
68c29fa5a6Sopenharmony_citemplate <class Enum, class T>
69c29fa5a6Sopenharmony_ciEnum EnumAdd(Enum const e, T val)
70c29fa5a6Sopenharmony_ci{
71c29fa5a6Sopenharmony_ci    static_assert(std::is_enum<Enum>::value, "input value is not of enum class nor enum");
72c29fa5a6Sopenharmony_ci    auto a = EnumUnderlyingValue(e);
73c29fa5a6Sopenharmony_ci    return static_cast<Enum>(a + val);
74c29fa5a6Sopenharmony_ci}
75c29fa5a6Sopenharmony_ci} // namespace MMI
76c29fa5a6Sopenharmony_ci} // namespace OHOS
77c29fa5a6Sopenharmony_ci#endif // UTIL_EX_H
78