1 #ifndef marshall_wrap_h
2 #define marshall_wrap_h
6 typedef std::function<rpc_protocol::status(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() {
39 // Here's the implementation of 'invoke'. It could be more general, but this
42 // One for function pointers...
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)...);
50 // And one for pointers to member functions...
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)...);
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.
64 template <class Functor, class Instance, class Signature,
65 class ErrorHandler=VerifyOnFailure> struct marshalled_func_imp;
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.
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
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
84 ArgsStorage t{u._grab<typename std::decay<Args>::type>()...};
85 // Verify successful unmarshalling of the entire input stream.
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.
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.
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).
108 template <class Functor, class ErrorHandler=VerifyOnFailure,
109 class Signature=Functor> struct marshalled_func;
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> {};
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> {};
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> {};