blob: e3439a75d792f5a58a65aa5ef11aad27f5e93f43 [file] [log] [blame]
Tomasz Wasilczyk6301e8f2021-10-18 16:53:40 -07001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
18#include <hidl/HidlSupport.h>
19
Tomasz Wasilczykd2e74592021-11-02 12:14:52 -070020#include <type_traits>
21#include <variant>
22
Tomasz Wasilczyk6301e8f2021-10-18 16:53:40 -070023namespace android::hardware::radio::compat {
24
25/**
26 * Converts hidl_vec<T> HIDL list to std::vector<T> AIDL list.
27 *
28 * To convert values, the template uses toAidl functions for a given type T, assuming it's defined.
29 *
30 * \param inp vector to convert
31 */
32template <typename T>
33auto toAidl(const hidl_vec<T>& inp) {
34 std::vector<decltype(toAidl(T{}))> out(inp.size());
35 for (size_t i = 0; i < inp.size(); i++) {
36 out[i] = toAidl(inp[i]);
37 }
38 return out;
39}
40
41/**
42 * Converts std::vector<T> AIDL list to hidl_vec<T> HIDL list.
43 *
44 * To convert values, the template uses toHidl functions for a given type T, assuming it's defined.
45 *
46 * \param inp vector to convert
47 */
48template <typename T>
49auto toHidl(const std::vector<T>& inp) {
50 hidl_vec<decltype(toHidl(T{}))> out(inp.size());
51 for (size_t i = 0; i < inp.size(); i++) {
52 out[i] = toHidl(inp[i]);
53 }
54 return out;
55}
56
Tomasz Wasilczykd2e74592021-11-02 12:14:52 -070057/**
58 * Converts T=OptionalX HIDL value to std::optional<X> AIDL value.
59 *
60 * To convert values, the template uses toAidl functions for a given type T.value.
61 */
62template <typename T>
63std::optional<decltype(toAidl(T{}.value()))> toAidl(const T& opt) {
64 if (opt.getDiscriminator() == T::hidl_discriminator::noinit) return std::nullopt;
65 return toAidl(opt.value());
66}
67
68/**
69 * Converts T=OptionalX HIDL value to std::variant<bool, X> AIDL value.
70 *
71 * For some reason, not every OptionalX gets generated into a std::optional<X>.
72 */
73template <typename T>
74std::variant<bool, decltype(toAidl(T{}.value()))> toAidlVariant(const T& opt) {
75 if (opt.getDiscriminator() == T::hidl_discriminator::noinit) return false;
76 return toAidl(opt.value());
77}
78
79/**
80 * Converts std::optional<X> AIDL value to T=OptionalX HIDL value.
81 *
82 * X is inferred from toAidl(T.value) declaration. Please note that toAidl(T.value) doesn't have to
83 * be implemented if it's not needed for anything else than giving this hint to type system.
84 *
85 * To convert values, the template uses toHidl functions for a given type T, assuming it's defined.
86 *
87 * \param opt value to convert
88 */
89template <typename T>
90T toHidl(const std::optional<decltype(toAidl(T{}.value()))>& opt) {
91 T hidl;
92 if (opt.has_value()) hidl.value(toHidl(*opt));
93 return hidl;
94}
95
96/**
97 * Converts U AIDL bitfield value to HIDL T bitfield value.
98 *
99 * \param val value to convert
100 */
101template <typename T, typename U>
102hidl_bitfield<T> toHidlBitfield(U val) {
103 return static_cast<int>(val);
104}
105
Tomasz Wasilczyk6301e8f2021-10-18 16:53:40 -0700106} // namespace android::hardware::radio::compat