Home | History | Annotate | Download | only in threads
      1 /*
      2  * Copyright (C) 2016 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 #ifndef CUTTLEFISH_COMMON_COMMON_LIBS_THREADS_THUNKERS_H_
     17 #define CUTTLEFISH_COMMON_COMMON_LIBS_THREADS_THUNKERS_H_
     18 
     19 namespace cvd {
     20 namespace internal {
     21 
     22 template <typename HalType, typename F>
     23 struct ThunkerImpl;
     24 
     25 template <typename HalType, typename Impl, typename R, typename... Args>
     26 struct ThunkerImpl<HalType, R (Impl::*)(Args...)> {
     27   template <R (Impl::*MemFn)(Args...)>
     28   static R call(HalType* in, Args... args) {
     29     return (reinterpret_cast<Impl*>(in)->*MemFn)(args...);
     30   }
     31 };
     32 
     33 template <typename HalType, typename Impl, typename R, typename... Args>
     34 struct ThunkerImpl<HalType, R (Impl::*)(Args...) const> {
     35   template <R (Impl::*MemFn)(Args...) const>
     36   static R call(const HalType* in, Args... args) {
     37     return (reinterpret_cast<const Impl*>(in)->*MemFn)(args...);
     38   }
     39 };
     40 
     41 template <typename HalType, auto MemFunc>
     42 struct Thunker {
     43   static constexpr auto call =
     44       ThunkerImpl<HalType, decltype(MemFunc)>::template call<MemFunc>;
     45 };
     46 
     47 }  // namespace internal
     48 
     49 template <typename HalType, auto MemFunc>
     50 constexpr auto thunk = internal::Thunker<HalType, MemFunc>::call;
     51 
     52 }  // namespace cvd
     53 
     54 #endif
     55