04f5268e3a06dcbb20d8ba6b0ae91d56ecccbd8e
[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 // This class encapsulates the default response to runtime unmarshalling
22 // failures.  The templated wrappers below may optionally use a different
23 // class.
24
25 struct VerifyOnFailure {
26     static inline int unmarshall_args_failure() {
27         VERIFY(0);
28     }
29 };
30
31 // Here's the implementation of 'invoke'.  It could be more general, but this
32 // meets our needs.
33
34 // One for function pointers...
35
36 template <class F, class R, class RV, class args_type, size_t... Indices>
37 typename enable_if<!std::is_member_function_pointer<F>::value, RV>::type inline 
38 invoke(RV, F f, void *, R & r, args_type & t, std::index_sequence<Indices...>) {
39     return f(r, std::get<Indices>(t)...);
40 }
41
42 // And one for pointers to member functions...
43
44 template <class F, class C, class RV, class R, 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, C *c, R & r, args_type & t, std::index_sequence<Indices...>) {
47     return (c->*f)(r, std::get<Indices>(t)...);
48 }
49
50 // The class marshalled_func_imp uses partial template specialization to
51 // implement the ::wrap static function.  ::wrap takes a function pointer or a
52 // pointer to a member function and returns a handler * object which
53 // unmarshalls arguments, verifies successful unmarshalling, calls the supplied
54 // function, and marshalls the response.
55
56 template <class Functor, class Instance, class Signature,
57           class ErrorHandler=VerifyOnFailure> struct marshalled_func_imp;
58
59 // Here we specialize on the Signature template parameter to obtain the list of
60 // argument types.  Note that we do not assume that the Functor parameter has
61 // the same pattern as Signature; this allows us to ignore the distinctions
62 // between various types of callable objects at this level of abstraction.
63
64 template <class F, class C, class ErrorHandler, class R, class RV, class... Args>
65 struct marshalled_func_imp<F, C, RV(R &, Args...), ErrorHandler> {
66     static inline handler *wrap(F f, C *c=nullptr) {
67         // This type definition represents storage for f's unmarshalled
68         // arguments.  decay is (most notably) stripping off const
69         // qualifiers.
70         using ArgsStorage = tuple<typename std::decay<Args>::type...>;
71         // Allocate a handler (i.e. function) to hold the lambda
72         // which will unmarshall RPCs and call f.
73         return new handler([=](unmarshall && u, marshall & m) -> RV {
74             // Unmarshall each argument with the correct type and store the
75             // result in a tuple.
76             ArgsStorage t{u._grab<typename std::decay<Args>::type>()...};
77             // Verify successful unmarshalling of the entire input stream.
78             if (!u.okdone())
79                 return (RV)ErrorHandler::unmarshall_args_failure();
80             // Allocate space for the RPC response -- will be passed into the
81             // function as an lvalue reference.
82             R r;
83             // Perform the invocation.
84             RV b = invoke(RV(), f, c, r, t, std::index_sequence_for<Args...>{});
85             // Marshall the response.
86             m << r;
87             // Make like a tree.
88             return b;
89         });
90     }
91 };
92
93 // More partial template specialization shenanigans to reduce the number of
94 // parameters which must be provided explicitly and to support a few common
95 // callable types.  C++11 doesn't allow partial function template
96 // specialization, so we use classes (structs).
97
98 template <class Functor, class ErrorHandler=VerifyOnFailure,
99     class Signature=Functor> struct marshalled_func;
100
101 template <class F, class ErrorHandler, class RV, class... Args>
102 struct marshalled_func<F, ErrorHandler, RV(*)(Args...)> :
103     public marshalled_func_imp<F, void, RV(Args...), ErrorHandler> {};
104
105 template <class F, class ErrorHandler, class RV, class C, class... Args>
106 struct marshalled_func<F, ErrorHandler, RV(C::*)(Args...)> :
107     public marshalled_func_imp<F, C, RV(Args...), ErrorHandler> {};
108
109 template <class F, class ErrorHandler, class Signature>
110 struct marshalled_func<F, ErrorHandler, std::function<Signature>> :
111     public marshalled_func_imp<F, void, Signature, ErrorHandler> {};
112
113 #endif