1 #ifndef ANDROID_PDX_RPC_REMOTE_METHOD_TYPE_H_
2 #define ANDROID_PDX_RPC_REMOTE_METHOD_TYPE_H_
3 
4 #include <cstddef>
5 #include <tuple>
6 #include <type_traits>
7 
8 #include <pdx/rpc/enumeration.h>
9 #include <pdx/rpc/function_traits.h>
10 
11 namespace android {
12 namespace pdx {
13 namespace rpc {
14 
15 // Utility class binding a remote method opcode to its function signature.
16 // Describes the interface between RPC clients and services for a single method.
17 template <int Opcode_, typename Signature_>
18 struct RemoteMethodType {
19   typedef FunctionTraits<Signature_> Traits;
20 
21   enum : int { Opcode = Opcode_ };
22 
23   typedef typename Traits::Signature Signature;
24   typedef typename Traits::Return Return;
25   typedef typename Traits::Args Args;
26 
27   template <typename... Params>
28   using RewriteArgs = typename Traits::template RewriteArgs<Params...>;
29 
30   template <typename ReturnType, typename... Params>
31   using RewriteSignature =
32       typename Traits::template RewriteSignature<ReturnType, Params...>;
33 
34   template <template <typename> class Wrapper, typename ReturnType,
35             typename... Params>
36   using RewriteSignatureWrapReturn =
37       typename Traits::template RewriteSignatureWrapReturn<Wrapper, ReturnType,
38                                                            Params...>;
39 
40   template <typename ReturnType>
41   using RewriteReturn = typename Traits::template RewriteReturn<ReturnType>;
42 };
43 
44 // Utility class representing a set of related RemoteMethodTypes. Describes the
45 // interface between RPC clients and services as a set of methods.
46 template <typename... MethodTypes>
47 struct RemoteAPI {
48   typedef std::tuple<MethodTypes...> Methods;
49   enum : std::size_t { Length = sizeof...(MethodTypes) };
50 
51   template <std::size_t Index>
52   using Method = typename std::tuple_element<Index, Methods>::type;
53 
54   template <typename MethodType>
MethodIndexRemoteAPI55   static constexpr std::size_t MethodIndex() {
56     return ElementForType<MethodType, MethodTypes...>::Index;
57   }
58 };
59 
60 // Macro to simplify defining remote method signatures. Remote method signatures
61 // are specified by defining a RemoteMethodType for each remote method.
62 #define PDX_REMOTE_METHOD(name, opcode, ... /*signature*/) \
63   using name = ::android::pdx::rpc::RemoteMethodType<opcode, __VA_ARGS__>
64 
65 // Macro to simplify defining a set of remote method signatures.
66 #define PDX_REMOTE_API(name, ... /*methods*/) \
67   using name = ::android::pdx::rpc::RemoteAPI<__VA_ARGS__>
68 
69 }  // namespace rpc
70 }  // namespace pdx
71 }  // namespace android
72 
73 #endif  // ANDROID_PDX_RPC_REMOTE_METHOD_TYPE_H_
74