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