1 /* 2 * Copyright (C) 2019 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 17 #pragma once 18 19 namespace android::hardware::automotive::hidl_utils { 20 21 /** 22 * Helper functor to fetch results from multi-return HIDL calls. 23 * It's meant to be used in place of _hidl_cb callbacks. 24 * 25 * Please note extracting these return variables outside of the callback scope requires making 26 * a copy of each return variable. This may be costly for frequently called HIDL methods with 27 * non-negligible return object size. Please be cautious about performance when using this. 28 * 29 * Example usage: 30 * Result result; 31 * sp<ISomeInterface> iface; 32 * hidlObject->someMethod(arg1, arg2, hidl_utils::fill(&result, &iface)).assertOk(); 33 * // use result and iface 34 */ 35 template <typename... T> 36 struct fill : public std::function<void(const T&...)> { 37 /** 38 * Create _hidl_cb functor that copies the call arguments to specified pointers. 39 * 40 * \param args... Targets to copy the call arguments to 41 */ fillfill42 fill(T*... args) : mTargets(args...) {} 43 operatorfill44 void operator()(const T&... args) { copy<0, T...>(args...); } 45 46 private: 47 std::tuple<T*...> mTargets; 48 49 template <int Pos, typename First> copyfill50 inline void copy(const First& first) { 51 *std::get<Pos>(mTargets) = first; 52 } 53 54 template <int Pos, typename First, typename... Rest> copyfill55 inline void copy(const First& first, const Rest&... rest) { 56 *std::get<Pos>(mTargets) = first; 57 copy<Pos + 1, Rest...>(rest...); 58 } 59 }; 60 61 } // namespace android::hardware::automotive::hidl_utils 62