/* * Copyright (C) 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include namespace android::hardware::radio::compat { /** * Converts hidl_vec HIDL list to std::vector AIDL list. * * To convert values, the template uses toAidl functions for a given type T, assuming it's defined. * * \param inp vector to convert */ template auto toAidl(const hidl_vec& inp) { std::vector out(inp.size()); for (size_t i = 0; i < inp.size(); i++) { out[i] = toAidl(inp[i]); } return out; } /** * Converts std::vector AIDL list to hidl_vec HIDL list. * * To convert values, the template uses toHidl functions for a given type T, assuming it's defined. * * \param inp vector to convert */ template auto toHidl(const std::vector& inp) { hidl_vec out(inp.size()); for (size_t i = 0; i < inp.size(); i++) { out[i] = toHidl(inp[i]); } return out; } /** * Converts hidl_array HIDL list to std::vector AIDL list. * * To convert values, the template uses toAidl functions for a given type T, assuming it's defined. * * \param inp array to convert */ template auto toAidl(const hidl_array& inp) { std::vector out(N); for (size_t i = 0; i < N; i++) { out[i] = toAidl(inp[i]); } return out; } /** * Converts T=OptionalX HIDL value to std::optional AIDL value. * * To convert values, the template uses toAidl functions for a given type T.value. */ template std::optional toAidl(const T& opt) { if (opt.getDiscriminator() == T::hidl_discriminator::noinit) return std::nullopt; return toAidl(opt.value()); } /** * Converts T=OptionalX HIDL value to std::variant AIDL value. * * For some reason, not every OptionalX gets generated into a std::optional. */ template std::variant toAidlVariant(const T& opt) { if (opt.getDiscriminator() == T::hidl_discriminator::noinit) return false; return toAidl(opt.value()); } /** * Converts std::optional AIDL value to T=OptionalX HIDL value. * * X is inferred from toAidl(T.value) declaration. Please note that toAidl(T.value) doesn't have to * be implemented if it's not needed for anything else than giving this hint to type system. * * To convert values, the template uses toHidl functions for a given type T, assuming it's defined. * * \param opt value to convert */ template T toHidl(const std::optional& opt) { T hidl; if (opt.has_value()) hidl.value(toHidl(*opt)); return hidl; } /** * Converts U AIDL bitfield value to HIDL T bitfield value. * * \param val value to convert */ template hidl_bitfield toHidlBitfield(U val) { return static_cast(val); } } // namespace android::hardware::radio::compat