2e54e475f786999478beda66d404213a2ad1bac5
[invirt/third/libt4.git] / rpc / marshall_wrap.h
1 #ifndef marshall_wrap_h
2 #define marshall_wrap_h
3
4 #include "marshall.h"
5
6 typedef std::function<rpc_protocol::status(unmarshall &&, marshall &)> handler;
7
8 //
9 // Automatic marshalling wrappers for RPC handlers
10 //
11
12 // PAI 2013/09/19
13 // C++11 does neither of these two things for us:
14 // 1) Declare variables using a parameter pack expansion, like so
15 //      Args... args;
16 // 2) Call a function with a tuple of the arguments it expects
17 //
18 // We implement an 'invoke' function for functions of the RPC handler
19 // signature, i.e. int(R & r, const Args...)
20 //
21 // One thing we need in order to accomplish this is a way to cause the compiler
22 // to specialize 'invoke' with a parameter pack containing a list of indices
23 // for the elements of the tuple.  This will allow us to call the underlying
24 // function with the exploded contents of the tuple.  The empty type
25 // tuple_indices<size_t...> accomplishes this.  It will be passed in to
26 // 'invoke' as a parameter which will be ignored, but its type will force the
27 // compiler to specialize 'invoke' appropriately.
28
29 // This class encapsulates the default response to runtime unmarshalling
30 // failures.  The templated wrappers below may optionally use a different
31 // class.
32
33 struct VerifyOnFailure {
34     static inline int unmarshall_args_failure() {
35         VERIFY(0);
36     }
37 };
38
39 // Here's the implementation of 'invoke'.  It could be more general, but this
40 // meets our needs.
41
42 // One for function pointers...
43
44 template <class F, class R, class RV, class args_type, size_t... Indices>
45 typename enable_if<!std::is_member_function_pointer<F>::value, RV>::type inline 
46 invoke(RV, F f, void *, R & r, args_type & t, tuple_indices<Indices...>) {
47     return f(r, std::get<Indices>(t)...);
48 }
49
50 // And one for pointers to member functions...
51
52 template <class F, class C, class RV, class R, class args_type, size_t... Indices>
53 typename enable_if<std::is_member_function_pointer<F>::value, RV>::type inline 
54 invoke(RV, F f, C *c, R & r, args_type & t, tuple_indices<Indices...>) {
55     return (c->*f)(r, std::get<Indices>(t)...);
56 }
57
58 // The class marshalled_func_imp uses partial template specialization to
59 // implement the ::wrap static function.  ::wrap takes a function pointer or a
60 // pointer to a member function and returns a handler * object which
61 // unmarshalls arguments, verifies successful unmarshalling, calls the supplied
62 // function, and marshalls the response.
63
64 template <class Functor, class Instance, class Signature,
65           class ErrorHandler=VerifyOnFailure> struct marshalled_func_imp;
66
67 // Here we specialize on the Signature template parameter to obtain the list of
68 // argument types.  Note that we do not assume that the Functor parameter has
69 // the same pattern as Signature; this allows us to ignore the distinctions
70 // between various types of callable objects at this level of abstraction.
71
72 template <class F, class C, class ErrorHandler, class R, class RV, class... Args>
73 struct marshalled_func_imp<F, C, RV(R &, Args...), ErrorHandler> {
74     static inline handler *wrap(F f, C *c=nullptr) {
75         // This type definition represents storage for f's unmarshalled
76         // arguments.  decay is (most notably) stripping off const
77         // qualifiers.
78         using ArgsStorage = tuple<typename std::decay<Args>::type...>;
79         // Allocate a handler (i.e. function) to hold the lambda
80         // which will unmarshall RPCs and call f.
81         return new handler([=](unmarshall && u, marshall & m) -> RV {
82             // Unmarshall each argument with the correct type and store the
83             // result in a tuple.
84             ArgsStorage t{u._grab<typename std::decay<Args>::type>()...};
85             // Verify successful unmarshalling of the entire input stream.
86             if (!u.okdone())
87                 return (RV)ErrorHandler::unmarshall_args_failure();
88             // Allocate space for the RPC response -- will be passed into the
89             // function as an lvalue reference.
90             R r;
91             // Perform the invocation.  Note that TUPLE_INDICES calls the
92             // default constructor of an empty struct with template parameters
93             // running from 0 up to (# args) - 1.
94             RV b = invoke(RV(), f, c, r, t, TUPLE_INDICES(Args));
95             // Marshall the response.
96             m << r;
97             // Make like a tree.
98             return b;
99         });
100     }
101 };
102
103 // More partial template specialization shenanigans to reduce the number of
104 // parameters which must be provided explicitly and to support a few common
105 // callable types.  C++11 doesn't allow partial function template
106 // specialization, so we use classes (structs).
107
108 template <class Functor, class ErrorHandler=VerifyOnFailure,
109     class Signature=Functor> struct marshalled_func;
110
111 template <class F, class ErrorHandler, class RV, class... Args>
112 struct marshalled_func<F, ErrorHandler, RV(*)(Args...)> :
113     public marshalled_func_imp<F, void, RV(Args...), ErrorHandler> {};
114
115 template <class F, class ErrorHandler, class RV, class C, class... Args>
116 struct marshalled_func<F, ErrorHandler, RV(C::*)(Args...)> :
117     public marshalled_func_imp<F, C, RV(Args...), ErrorHandler> {};
118
119 template <class F, class ErrorHandler, class Signature>
120 struct marshalled_func<F, ErrorHandler, std::function<Signature>> :
121     public marshalled_func_imp<F, void, Signature, ErrorHandler> {};
122
123 #endif