xref: /base/update/updater/utils/json/traits_util.h (revision fb299fa2)
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
23namespace Updater {
24namespace Detail {
25template<typename T>
26inline constexpr bool G_IS_NUM = std::is_integral_v<T> && !std::is_same_v<T, bool>;
27
28template<typename T>
29inline constexpr bool G_IS_BOOL = std::is_same_v<bool, T>;
30
31template<typename T>
32inline 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
35template<typename T>
36inline constexpr bool G_IS_PRINTABLE = (G_IS_NUM<T> || G_IS_BOOL<T> || G_IS_STR<T>);
37
38template<typename T>
39inline constexpr bool G_IS_BASE_TYPE = (G_IS_NUM<T> || G_IS_BOOL<T> || G_IS_STR<T>);
40
41template<typename T>
42struct IsVector : std::false_type {};
43
44template<typename T>
45struct IsVector<std::vector<T>> : std::true_type {};
46
47template<typename T>
48constexpr bool G_IS_VECTOR = IsVector<T>::value;
49
50template<bool b, typename T>
51using isMatch = typename std::enable_if_t<b, T>;
52
53template<typename T>
54using RemoveCvRef = std::remove_cv_t<std::remove_reference_t<T>>;
55
56template<typename T>
57struct 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
62template<typename T>
63using StandardType = typename StandardTypeHelper<T>::type;
64
65template<typename T>
66using OptStandardType = std::optional<StandardType<RemoveCvRef<T>>>;
67
68template<std::size_t idx, typename...T>
69inline auto &Get(T &&...t)
70{
71    return std::get<idx>(std::forward_as_tuple(t...));
72}
73}
74}
75#endif
76