+// 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 RV, class... Args>
+struct marshalled_func_imp<F, C, RV(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. decay is (most notably) stripping off const
+ // qualifiers.
+ using ArgsStorage = tuple<typename decay<Args>::type...>;
+ // Allocate a handler (i.e. function) to hold the lambda
+ // which will unmarshall RPCs and call f.
+ return new handler([=](unmarshall &u, marshall &m) -> RV {
+ // Unmarshall each argument with the correct type and store the
+ // result in a tuple.
+ ArgsStorage t = {u.grab<typename decay<Args>::type>()...};
+ // Verify successful unmarshalling of the entire input stream.
+ if (!u.okdone())
+ return (RV)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.
+ RV b = invoke(RV(), 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 RV, class... Args>
+struct marshalled_func<F, ErrorHandler, RV(*)(Args...)> :
+ public marshalled_func_imp<F, void, RV(Args...), ErrorHandler> {};
+
+template <class F, class ErrorHandler, class RV, class C, class... Args>
+struct marshalled_func<F, ErrorHandler, RV(C::*)(Args...)> :
+ public marshalled_func_imp<F, C, RV(Args...), ErrorHandler> {};
+
+template <class F, class ErrorHandler, class Signature>
+struct marshalled_func<F, ErrorHandler, function<Signature>> :
+ public marshalled_func_imp<F, void, Signature, ErrorHandler> {};
+
+template <class ...Args, size_t ...Indices> unmarshall &
+tuple_unmarshall_imp(unmarshall & u, tuple<Args &...> t, tuple_indices<Indices...>) {
+ (void)pass{(u >> get<Indices>(t))...};
+ return u;
+}
+
+template <class... Args> unmarshall &
+operator>>(unmarshall & u, tuple<Args &...> && t) {
+ using Indices = typename make_tuple_indices<sizeof...(Args)>::type;
+ return tuple_unmarshall_imp(u, t, Indices());
+}
+
+template <class ...Args, size_t ...Indices> marshall &
+tuple_marshall_imp(marshall & m, tuple<Args...> & t, tuple_indices<Indices...>) {
+ (void)pass{(m << get<Indices>(t))...};
+ return m;
+}
+
+template <class... Args> marshall &
+operator<<(marshall & m, tuple<Args...> && t) {
+ using Indices = typename make_tuple_indices<sizeof...(Args)>::type;
+ return tuple_marshall_imp(m, t, Indices());
+}
+
+// for structs or classes containing a MEMBERS declaration
+#define MARSHALLABLE(_c_) \
+inline unmarshall & operator>>(unmarshall &u, _c_ &a) { return u >> a._tuple_(); } \
+inline marshall & operator<<(marshall &m, _c_ a) { return m << a._tuple_(); }
+