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