1 /*
2  * Copyright (c) 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 TRAITS_UTIL_H
17 #define TRAITS_UTIL_H
18 
19 #include <optional>
20 #include <string>
21 #include <vector>
22 
23 namespace Updater {
24 namespace Detail {
25 template<typename T>
26 inline constexpr bool G_IS_NUM = std::is_integral_v<T> && !std::is_same_v<T, bool>;
27 
28 template<typename T>
29 inline constexpr bool G_IS_BOOL = std::is_same_v<bool, T>;
30 
31 template<typename T>
32 inline constexpr bool G_IS_STR = (std::is_same_v<char *, std::decay_t<T>> ||
33     std::is_same_v<const char *, std::decay_t<T>> || std::is_same_v<std::string, T>);
34 
35 template<typename T>
36 inline constexpr bool G_IS_PRINTABLE = (G_IS_NUM<T> || G_IS_BOOL<T> || G_IS_STR<T>);
37 
38 template<typename T>
39 inline constexpr bool G_IS_BASE_TYPE = (G_IS_NUM<T> || G_IS_BOOL<T> || G_IS_STR<T>);
40 
41 template<typename T>
42 struct IsVector : std::false_type {};
43 
44 template<typename T>
45 struct IsVector<std::vector<T>> : std::true_type {};
46 
47 template<typename T>
48 constexpr bool G_IS_VECTOR = IsVector<T>::value;
49 
50 template<bool b, typename T>
51 using isMatch = typename std::enable_if_t<b, T>;
52 
53 template<typename T>
54 using RemoveCvRef = std::remove_cv_t<std::remove_reference_t<T>>;
55 
56 template<typename T>
57 struct StandardTypeHelper {
58     static_assert(G_IS_BASE_TYPE<T>);
59     using type = std::conditional_t<G_IS_NUM<T>, int, std::conditional_t<G_IS_STR<T>, std::string, bool>>;
60 };
61 
62 template<typename T>
63 using StandardType = typename StandardTypeHelper<T>::type;
64 
65 template<typename T>
66 using OptStandardType = std::optional<StandardType<RemoveCvRef<T>>>;
67 
68 template<std::size_t idx, typename...T>
69 inline auto &Get(T &&...t)
70 {
71     return std::get<idx>(std::forward_as_tuple(t...));
72 }
73 }
74 }
75 #endif
76