1/* 2 * Copyright (C) 2017 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#include <tuple> 18 19namespace utility { 20 21namespace detail { 22// Helper class to generate the HIDL synchronous callback 23template <class... ResultStore> 24class ReturnIn { 25 public: 26 // Provide to the constructor the variables where the output parameters must be copied 27 // TODO: take pointers to match google output parameter style ? 28 ReturnIn(ResultStore&... ts) : results(ts...) {} 29 // Synchronous callback 30 template <class... Results> 31 void operator() (Results&&...results) { 32 set(std::forward<Results>(results)...); 33 } 34 private: 35 // Recursively set all output parameters 36 template <class Head, class... Tail> 37 void set(Head&& head, Tail&&... tail) { 38 std::get<sizeof...(ResultStore) - sizeof...(Tail) - 1>(results) 39 = std::forward<Head>(head); 40 set(tail...); 41 } 42 // Trivial case 43 void set() {} 44 45 // All variables to set are stored here 46 std::tuple<ResultStore&...> results; 47}; 48} // namespace detail 49 50// Generate the HIDL synchronous callback with a copy policy 51// Input: the variables (lvalue reference) where to save the return values 52// Output: the callback to provide to a HIDL call with a synchronous callback 53// The output parameters *will be copied* do not use this function if you have 54// a zero copy policy 55template <class... ResultStore> 56detail::ReturnIn<ResultStore...> returnIn(ResultStore&... ts) { return {ts...};} 57 58} 59