1 #ifndef marshall_wrap_h
2 #define marshall_wrap_h
6 typedef function<int(unmarshall &, marshall &)> handler;
9 // Automatic marshalling wrappers for RPC handlers
13 // C++11 does neither of these two things for us:
14 // 1) Declare variables using a parameter pack expansion, like so
16 // 2) Call a function with a tuple of the arguments it expects
18 // We implement an 'invoke' function for functions of the RPC handler
19 // signature, i.e. int(R & r, const Args...)
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.
29 // This class encapsulates the default response to runtime unmarshalling
30 // failures. The templated wrappers below may optionally use a different
33 struct VerifyOnFailure {
34 static inline int unmarshall_args_failure() {
40 // Here's the implementation of 'invoke'. It could be more general, but this
43 // One for function pointers...
45 template <class F, class R, class RV, class args_type, size_t... Indices>
46 typename enable_if<!is_member_function_pointer<F>::value, RV>::type
47 invoke(RV, F f, void *, R & r, args_type & t, tuple_indices<Indices...>) {
48 return f(r, move(get<Indices>(t))...);
51 // And one for pointers to member functions...
53 template <class F, class C, class RV, class R, class args_type, size_t... Indices>
54 typename enable_if<is_member_function_pointer<F>::value, RV>::type
55 invoke(RV, F f, C *c, R & r, args_type & t, tuple_indices<Indices...>) {
56 return (c->*f)(r, move(get<Indices>(t))...);
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.
65 template <class Functor, class Instance, class Signature,
66 class ErrorHandler=VerifyOnFailure> struct marshalled_func_imp;
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.
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 corresponds to an empty struct with
77 // template parameters running from 0 up to (# args) - 1.
78 using Indices = typename make_tuple_indices<sizeof...(Args)>::type;
79 // This type definition represents storage for f's unmarshalled
80 // arguments. decay is (most notably) stripping off const
82 using ArgsStorage = tuple<typename decay<Args>::type...>;
83 // Allocate a handler (i.e. function) to hold the lambda
84 // which will unmarshall RPCs and call f.
85 return new handler([=](unmarshall &u, marshall &m) -> RV {
86 // Unmarshall each argument with the correct type and store the
88 ArgsStorage t = {u._grab<typename decay<Args>::type>()...};
89 // Verify successful unmarshalling of the entire input stream.
91 return (RV)ErrorHandler::unmarshall_args_failure();
92 // Allocate space for the RPC response -- will be passed into the
93 // function as an lvalue reference.
95 // Perform the invocation. Note that Indices() calls the default
96 // constructor of the empty struct with the special template
98 RV b = invoke(RV(), f, c, r, t, Indices());
99 // Marshall the response.
107 // More partial template specialization shenanigans to reduce the number of
108 // parameters which must be provided explicitly and to support a few common
109 // callable types. C++11 doesn't allow partial function template
110 // specialization, so we use classes (structs).
112 template <class Functor, class ErrorHandler=VerifyOnFailure,
113 class Signature=Functor> struct marshalled_func;
115 template <class F, class ErrorHandler, class RV, class... Args>
116 struct marshalled_func<F, ErrorHandler, RV(*)(Args...)> :
117 public marshalled_func_imp<F, void, RV(Args...), ErrorHandler> {};
119 template <class F, class ErrorHandler, class RV, class C, class... Args>
120 struct marshalled_func<F, ErrorHandler, RV(C::*)(Args...)> :
121 public marshalled_func_imp<F, C, RV(Args...), ErrorHandler> {};
123 template <class F, class ErrorHandler, class Signature>
124 struct marshalled_func<F, ErrorHandler, function<Signature>> :
125 public marshalled_func_imp<F, void, Signature, ErrorHandler> {};