1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci
16fb299fa2Sopenharmony_ci#ifndef TRAITS_UTIL_H
17fb299fa2Sopenharmony_ci#define TRAITS_UTIL_H
18fb299fa2Sopenharmony_ci
19fb299fa2Sopenharmony_ci#include <optional>
20fb299fa2Sopenharmony_ci#include <string>
21fb299fa2Sopenharmony_ci#include <vector>
22fb299fa2Sopenharmony_ci
23fb299fa2Sopenharmony_cinamespace Updater {
24fb299fa2Sopenharmony_cinamespace Detail {
25fb299fa2Sopenharmony_citemplate<typename T>
26fb299fa2Sopenharmony_ciinline constexpr bool G_IS_NUM = std::is_integral_v<T> && !std::is_same_v<T, bool>;
27fb299fa2Sopenharmony_ci
28fb299fa2Sopenharmony_citemplate<typename T>
29fb299fa2Sopenharmony_ciinline constexpr bool G_IS_BOOL = std::is_same_v<bool, T>;
30fb299fa2Sopenharmony_ci
31fb299fa2Sopenharmony_citemplate<typename T>
32fb299fa2Sopenharmony_ciinline constexpr bool G_IS_STR = (std::is_same_v<char *, std::decay_t<T>> ||
33fb299fa2Sopenharmony_ci    std::is_same_v<const char *, std::decay_t<T>> || std::is_same_v<std::string, T>);
34fb299fa2Sopenharmony_ci
35fb299fa2Sopenharmony_citemplate<typename T>
36fb299fa2Sopenharmony_ciinline constexpr bool G_IS_PRINTABLE = (G_IS_NUM<T> || G_IS_BOOL<T> || G_IS_STR<T>);
37fb299fa2Sopenharmony_ci
38fb299fa2Sopenharmony_citemplate<typename T>
39fb299fa2Sopenharmony_ciinline constexpr bool G_IS_BASE_TYPE = (G_IS_NUM<T> || G_IS_BOOL<T> || G_IS_STR<T>);
40fb299fa2Sopenharmony_ci
41fb299fa2Sopenharmony_citemplate<typename T>
42fb299fa2Sopenharmony_cistruct IsVector : std::false_type {};
43fb299fa2Sopenharmony_ci
44fb299fa2Sopenharmony_citemplate<typename T>
45fb299fa2Sopenharmony_cistruct IsVector<std::vector<T>> : std::true_type {};
46fb299fa2Sopenharmony_ci
47fb299fa2Sopenharmony_citemplate<typename T>
48fb299fa2Sopenharmony_ciconstexpr bool G_IS_VECTOR = IsVector<T>::value;
49fb299fa2Sopenharmony_ci
50fb299fa2Sopenharmony_citemplate<bool b, typename T>
51fb299fa2Sopenharmony_ciusing isMatch = typename std::enable_if_t<b, T>;
52fb299fa2Sopenharmony_ci
53fb299fa2Sopenharmony_citemplate<typename T>
54fb299fa2Sopenharmony_ciusing RemoveCvRef = std::remove_cv_t<std::remove_reference_t<T>>;
55fb299fa2Sopenharmony_ci
56fb299fa2Sopenharmony_citemplate<typename T>
57fb299fa2Sopenharmony_cistruct StandardTypeHelper {
58fb299fa2Sopenharmony_ci    static_assert(G_IS_BASE_TYPE<T>);
59fb299fa2Sopenharmony_ci    using type = std::conditional_t<G_IS_NUM<T>, int, std::conditional_t<G_IS_STR<T>, std::string, bool>>;
60fb299fa2Sopenharmony_ci};
61fb299fa2Sopenharmony_ci
62fb299fa2Sopenharmony_citemplate<typename T>
63fb299fa2Sopenharmony_ciusing StandardType = typename StandardTypeHelper<T>::type;
64fb299fa2Sopenharmony_ci
65fb299fa2Sopenharmony_citemplate<typename T>
66fb299fa2Sopenharmony_ciusing OptStandardType = std::optional<StandardType<RemoveCvRef<T>>>;
67fb299fa2Sopenharmony_ci
68fb299fa2Sopenharmony_citemplate<std::size_t idx, typename...T>
69fb299fa2Sopenharmony_ciinline auto &Get(T &&...t)
70fb299fa2Sopenharmony_ci{
71fb299fa2Sopenharmony_ci    return std::get<idx>(std::forward_as_tuple(t...));
72fb299fa2Sopenharmony_ci}
73fb299fa2Sopenharmony_ci}
74fb299fa2Sopenharmony_ci}
75fb299fa2Sopenharmony_ci#endif
76