+typedef std::function<int(unmarshall &, marshall &)> handler;
+
+//
+// Automatic marshalling wrappers for RPC handlers
+//
+
+// PAI 2013/09/19
+// C++11 does neither of these two things for us:
+// 1) Declare variables using a parameter pack expansion, like so
+// Args ...args;
+// 2) Call a function with a std::tuple of the arguments it expects
+//
+// We implement an 'invoke' function for functions of the RPC handler
+// signature, i.e. int(R & r, const Args...)
+//
+// One thing we need in order to accomplish this is a way to cause the compiler
+// to specialize 'invoke' with a parameter pack containing a list of indices
+// for the elements of the tuple. This will allow us to call the underlying
+// function with the exploded contents of the tuple. The empty type
+// tuple_indices<size_t...> accomplishes this. It will be passed in to
+// 'invoke' as a parameter which will be ignored, but its type will force the
+// compiler to specialize 'invoke' appropriately.
+
+// The following implementation of tuple_indices is redistributed under the MIT
+// License as an insubstantial portion of the LLVM compiler infrastructure.
+
+template <size_t...> struct tuple_indices {};
+template <size_t S, class IntTuple, size_t E> struct make_indices_imp;
+template <size_t S, size_t ...Indices, size_t E> struct make_indices_imp<S, tuple_indices<Indices...>, E> {
+ typedef typename make_indices_imp<S+1, tuple_indices<Indices..., S>, E>::type type;
+};
+template <size_t E, size_t ...Indices> struct make_indices_imp<E, tuple_indices<Indices...>, E> {
+ typedef tuple_indices<Indices...> type;
+};
+template <size_t E, size_t S=0> struct make_tuple_indices {
+ typedef typename make_indices_imp<S, tuple_indices<>, E>::type type;
+};
+
+// This class encapsulates the default response to runtime unmarshalling
+// failures. The templated wrappers below may optionally use a different
+// class.
+
+struct VerifyOnFailure {
+ static inline int unmarshall_args_failure() {
+ VERIFY(0);
+ return 0;
+ }
+};
+
+// Here's the implementation of 'invoke'. It could be more general, but this
+// meets our needs.
+
+// One for function pointers...
+
+template <class F, class R, class args_type, size_t ...Indices>
+typename std::enable_if<!std::is_member_function_pointer<F>::value, int>::type
+invoke(F f, void *, R & r, args_type & t, tuple_indices<Indices...>) {
+ return f(r, std::move(std::get<Indices>(t))...);
+}
+
+// And one for pointers to member functions...
+
+template <class F, class C, class R, class args_type, size_t ...Indices>
+typename std::enable_if<std::is_member_function_pointer<F>::value, int>::type
+invoke(F f, C *c, R & r, args_type & t, tuple_indices<Indices...>) {
+ return (c->*f)(r, std::move(std::get<Indices>(t))...);
+}
+
+// The class marshalled_func_imp uses partial template specialization to
+// implement the ::wrap static function. ::wrap takes a function pointer or a
+// pointer to a member function and returns a handler * object which
+// unmarshalls arguments, verifies successful unmarshalling, calls the supplied
+// function, and marshalls the response.
+
+template <class Functor, class Instance, class Signature,
+ class ErrorHandler=VerifyOnFailure> struct marshalled_func_imp;
+
+// Here we specialize on the Signature template parameter to obtain the list of
+// argument types. Note that we do not assume that the Functor parameter has
+// the same pattern as Signature; this allows us to ignore the distinctions
+// between various types of callable objects at this level of abstraction.
+
+template <class F, class C, class ErrorHandler, class R, class... Args>
+struct marshalled_func_imp<F, C, int(R&, Args...), ErrorHandler> {
+ static inline handler *wrap(F f, C *c=nullptr) {
+ // This type definition corresponds to an empty struct with
+ // template parameters running from 0 up to (# args) - 1.
+ using Indices = typename make_tuple_indices<sizeof...(Args)>::type;
+ // This type definition represents storage for f's unmarshalled
+ // arguments. std::decay is (most notably) stripping off const
+ // qualifiers.
+ using ArgsStorage = std::tuple<typename std::decay<Args>::type...>;
+ // Allocate a handler (i.e. std::function) to hold the lambda
+ // which will unmarshall RPCs and call f.
+ return new handler([=](unmarshall &u, marshall &m) -> int {
+ // Unmarshall each argument with the correct type and store the
+ // result in a tuple.
+ ArgsStorage t = {u.grab<typename std::decay<Args>::type>()...};
+ // Verify successful unmarshalling of the entire input stream.
+ if (!u.okdone())
+ return ErrorHandler::unmarshall_args_failure();
+ // Allocate space for the RPC response -- will be passed into the
+ // function as an lvalue reference.
+ R r;
+ // Perform the invocation. Note that Indices() calls the default
+ // constructor of the empty struct with the special template
+ // parameters.
+ int b = invoke(f, c, r, t, Indices());
+ // Marshall the response.
+ m << r;
+ // Make like a tree.
+ return b;
+ });
+ }
+};
+
+// More partial template specialization shenanigans to reduce the number of
+// parameters which must be provided explicitly and to support a few common
+// callable types. C++11 doesn't allow partial function template
+// specialization, so we use classes (structs).
+
+template <class Functor, class ErrorHandler=VerifyOnFailure,
+ class Signature=Functor> struct marshalled_func;
+
+template <class F, class ErrorHandler, class... Args>
+struct marshalled_func<F, ErrorHandler, int(*)(Args...)> :
+ public marshalled_func_imp<F, void, int(Args...), ErrorHandler> {};
+
+template <class F, class ErrorHandler, class C, class... Args>
+struct marshalled_func<F, ErrorHandler, int(C::*)(Args...)> :
+ public marshalled_func_imp<F, C, int(Args...), ErrorHandler> {};
+
+template <class F, class ErrorHandler, class Signature>
+struct marshalled_func<F, ErrorHandler, std::function<Signature>> :
+ public marshalled_func_imp<F, void, Signature, ErrorHandler> {};
+