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 // This class encapsulates the default response to runtime unmarshalling
22 // failures. The templated wrappers below may optionally use a different
25 struct VerifyOnFailure {
26 static inline int unmarshall_args_failure() {
31 // Here's the implementation of 'invoke'. It could be more general, but this
34 // One for function pointers...
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)...);
42 // And one for pointers to member functions...
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)...);
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.
56 template <class Functor, class Instance, class Signature,
57 class ErrorHandler=VerifyOnFailure> struct marshalled_func_imp;
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.
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
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
76 ArgsStorage t{u._grab<typename std::decay<Args>::type>()...};
77 // Verify successful unmarshalling of the entire input stream.
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.
83 // Perform the invocation.
84 RV b = invoke(RV(), f, c, r, t, std::index_sequence_for<Args...>{});
85 // Marshall the response.
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).
98 template <class Functor, class ErrorHandler=VerifyOnFailure,
99 class Signature=Functor> struct marshalled_func;
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> {};
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> {};
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> {};