1 #ifndef ANDROID_PDX_RPC_FUNCTION_TRAITS_H_
2 #define ANDROID_PDX_RPC_FUNCTION_TRAITS_H_
3 
4 #include <type_traits>
5 
6 #include <pdx/rpc/type_operators.h>
7 
8 namespace android {
9 namespace pdx {
10 namespace rpc {
11 
12 // Utility type to capture return and argument types of a function signature.
13 // Examples:
14 //     typedef SignatureType<int(int)> SignatureType;
15 //     using SignatureType = SignatureType<int(int)>;
16 template <typename T>
17 using SignatureType = T;
18 
19 // Utility class to extract return and argument types from function types.
20 // Provides nested types for return value, arguments, and full signature. Also
21 // provides accessor types for individual arguments, argument-arity, and type
22 // substitution.
23 template <typename T>
24 struct FunctionTraits;
25 
26 template <typename Return_, typename... Args_>
27 struct FunctionTraits<Return_(Args_...)> {
28   using Return = Return_;
29   using Args = std::tuple<Args_...>;
30   using Signature = SignatureType<Return_(Args_...)>;
31 
32   enum : std::size_t { Arity = sizeof...(Args_) };
33 
34   template <std::size_t Index>
35   using Arg = typename std::tuple_element<Index, Args>::type;
36 
37   template <typename... Params>
38   using RewriteArgs =
39       SignatureType<Return_(ConditionalRewrite<Args_, Params>...)>;
40 
41   template <typename ReturnType, typename... Params>
42   using RewriteSignature =
43       SignatureType<ConditionalRewrite<Return_, ReturnType>(
44           ConditionalRewrite<Args_, Params>...)>;
45 
46   template <template <typename> class Wrapper, typename ReturnType,
47             typename... Params>
48   using RewriteSignatureWrapReturn =
49       SignatureType<Wrapper<ConditionalRewrite<Return_, ReturnType>>(
50           ConditionalRewrite<Args_, Params>...)>;
51 
52   template <typename ReturnType>
53   using RewriteReturn =
54       SignatureType<ConditionalRewrite<Return_, ReturnType>(Args_...)>;
55 };
56 
57 }  // namespace rpc
58 }  // namespace pdx
59 }  // namespace android
60 
61 #endif  //  ANDROID_PDX_RPC_FUNCTION_TRAITS_H_
62