More marshalling simplification
authorPeter Iannucci <iannucci@mit.edu>
Mon, 30 Sep 2013 18:10:03 +0000 (14:10 -0400)
committerPeter Iannucci <iannucci@mit.edu>
Mon, 30 Sep 2013 18:10:03 +0000 (14:10 -0400)
Makefile
rpc/connection.cc
rpc/marshall.cc [deleted file]
rpc/marshall.h
rpc/marshall_wrap.h [new file with mode: 0644]
rpc/rpc.h
rpc/rpc_protocol.h
threaded_log.h
types.h

index 010818d..5c2080b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ EXTRA_TARGETS ?=
 
 all: lock_demo lock_server lock_tester rsm_tester rpc/rpctest $(EXTRA_TARGETS)
 
-rpc/librpc.a: rpc/rpc.o rpc/marshall.o rpc/connection.o rpc/pollmgr.o rpc/thr_pool.o
+rpc/librpc.a: rpc/rpc.o rpc/connection.o rpc/pollmgr.o rpc/thr_pool.o
        rm -f $@
        ar cq $@ $^
        ranlib rpc/librpc.a
index c16f6dc..4681ae9 100644 (file)
@@ -171,10 +171,6 @@ bool connection::writepdu() {
     if (wpdu_.solong == wpdu_.buf.size())
         return true;
 
-    if (wpdu_.solong == 0) {
-        rpc_sz_t sz = hton((rpc_sz_t)(wpdu_.buf.size() - sizeof(uint32_t)));
-        copy((const char *)&sz, (const char *)(&sz+1), &wpdu_.buf[0]);
-    }
     ssize_t n = write(fd_, &wpdu_.buf[wpdu_.solong], (wpdu_.buf.size()-wpdu_.solong));
     if (n < 0) {
         if (errno != EAGAIN) {
@@ -219,7 +215,6 @@ bool connection::readpdu() {
 
         VERIFY(rpdu_.buf.size() == 0);
         rpdu_.buf = string(sz+sizeof(sz1), 0);
-        copy((const char *)&sz1, (const char *)(&sz1 + 1), &rpdu_.buf[0]);
         rpdu_.solong = sizeof(sz1);
     }
 
diff --git a/rpc/marshall.cc b/rpc/marshall.cc
deleted file mode 100644 (file)
index b8371cf..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-#include "types.h"
-#include "marshall.h"
-
-MARSHALL_RAW_NETWORK_ORDER_AS(bool, uint8_t)
-MARSHALL_RAW_NETWORK_ORDER(uint8_t)
-MARSHALL_RAW_NETWORK_ORDER(int8_t)
-MARSHALL_RAW_NETWORK_ORDER(uint16_t)
-MARSHALL_RAW_NETWORK_ORDER(int16_t)
-MARSHALL_RAW_NETWORK_ORDER(uint32_t)
-MARSHALL_RAW_NETWORK_ORDER(int32_t)
-MARSHALL_RAW_NETWORK_ORDER_AS(size_t, uint32_t)
-MARSHALL_RAW_NETWORK_ORDER(uint64_t)
-MARSHALL_RAW_NETWORK_ORDER(int64_t)
-
-marshall & operator<<(marshall &m, const string &s) {
-    m << (uint32_t)s.size();
-    m.rawbytes(s.data(), s.size());
-    return m;
-}
-
-unmarshall & operator>>(unmarshall &u, string &s) {
-    uint32_t sz = u.grab<uint32_t>();
-    if (u.ok()) {
-        s.resize(sz);
-        u.rawbytes(&s[0], sz);
-    }
-    return u;
-}
index ecf16f7..0e0af8d 100644 (file)
@@ -4,43 +4,18 @@
 #include "types.h"
 #include "rpc_protocol.h"
 
-// for structs or classes containing a MEMBERS declaration
 class marshall;
 class unmarshall;
-#define FORWARD_MARSHALLABLE(_c_) \
-extern unmarshall & operator>>(unmarshall &u, typename remove_reference<_c_>::type &a); \
-extern marshall & operator<<(marshall &m, const _c_ a);
-#define MARSHALLABLE(_c_) \
-inline unmarshall & operator>>(unmarshall &u, _c_ &a) { return u >> a._tuple_(); } \
-inline marshall & operator<<(marshall &m, const _c_ a) { return m << a._tuple_(); }
-
-// for plain old data
-#define MARSHALL_RAW_NETWORK_ORDER_AS(_c_, _d_) \
-marshall & operator<<(marshall &m, _c_ x) { _d_ y = hton((_d_)x); m.rawbytes(&y, sizeof(_d_)); return m; } \
-unmarshall & operator>>(unmarshall &u, _c_ &x) { _d_ y; u.rawbytes(&y, sizeof(_d_)); x = (_c_)ntoh(y); return u; }
 
-#define MARSHALL_RAW_NETWORK_ORDER(_c_) MARSHALL_RAW_NETWORK_ORDER_AS(_c_, _c_)
-
-FORWARD_MARSHALLABLE(request_header)
-ENDIAN_SWAPPABLE(request_header)
-
-FORWARD_MARSHALLABLE(reply_header)
-ENDIAN_SWAPPABLE(reply_header)
-
-// Template parameter pack expansion is not allowed in certain contexts, but
-// brace initializers (for instance, calls to constructors of empty structs)
-// are fair game.  
-struct pass { template <typename... Args> inline pass(Args&&...) {} };
+//
+// Marshall and unmarshall objects
+//
 
 class marshall {
     private:
         string buf_ = string(DEFAULT_RPC_SZ, 0); // Raw bytes buffer
         size_t index_ = RPC_HEADER_SZ; // Read/write head position
 
-        inline void reserve(size_t n) {
-            if (index_+n > buf_.size())
-                buf_.resize(index_+n);
-        }
     public:
         template <typename... Args>
         marshall(const Args&... args) {
@@ -48,75 +23,36 @@ class marshall {
         }
 
         void rawbytes(const void *p, size_t n) {
-            reserve(n);
+            if (index_+n > buf_.size())
+                buf_.resize(index_+n);
             copy((char *)p, (char *)p+n, &buf_[index_]);
             index_ += n;
         }
 
         // with header
-        operator string () const { return buf_.substr(0,index_); }
+        inline operator string() const { return buf_.substr(0,index_); }
         // without header
-        string content() { return buf_.substr(RPC_HEADER_SZ,index_-RPC_HEADER_SZ); }
-
-        template <class T>
-        void pack_header(const T &h) {
-            VERIFY(sizeof(T)+sizeof(rpc_sz_t) <= RPC_HEADER_SZ);
+        inline string content() { return buf_.substr(RPC_HEADER_SZ,index_-RPC_HEADER_SZ); }
+
+        // letting S be a defaulted template parameter forces the compiler to
+        // delay looking up operator<<(marshall&, rpc_sz_t) until we define it
+        // (i.e. we define an operator for marshalling uint32_t)
+        template <class T, class S=rpc_sz_t> inline void
+        pack_header(const T & h) {
+            VERIFY(sizeof(T)+sizeof(S) <= RPC_HEADER_SZ);
             size_t saved_sz = index_;
-            index_ = sizeof(rpc_sz_t); // first 4 bytes hold length field
-            *this << h;
+            index_ = 0;
+            *this << (S)(saved_sz - sizeof(S)) << (T)h;
             index_ = saved_sz;
         }
 };
 
-FORWARD_MARSHALLABLE(bool);
-FORWARD_MARSHALLABLE(uint8_t);
-FORWARD_MARSHALLABLE(int8_t);
-FORWARD_MARSHALLABLE(uint16_t);
-FORWARD_MARSHALLABLE(int16_t);
-FORWARD_MARSHALLABLE(uint32_t);
-FORWARD_MARSHALLABLE(int32_t);
-FORWARD_MARSHALLABLE(size_t);
-FORWARD_MARSHALLABLE(uint64_t);
-FORWARD_MARSHALLABLE(int64_t);
-FORWARD_MARSHALLABLE(string &);
-
-template <class A> typename enable_if<is_iterable<A>::value, marshall>::type &
-operator<<(marshall &m, const A &x) {
-    m << (unsigned int)x.size();
-    for (const auto &a : x)
-        m << a;
-    return m;
-}
-
-template <class A, class B> marshall &
-operator<<(marshall &m, const pair<A,B> &d) {
-    return m << d.first << d.second;
-}
-
-template<typename E>
-using enum_type_t = typename enable_if<is_enum<E>::value, typename underlying_type<E>::type>::type;
-template<typename E> constexpr inline enum_type_t<E> from_enum(E e) noexcept { return (enum_type_t<E>)e; }
-template<typename E> constexpr inline E to_enum(enum_type_t<E> value) noexcept { return (E)value; }
-
-template <class E> typename enable_if<is_enum<E>::value, marshall>::type &
-operator<<(marshall &m, E e) {
-    return m << from_enum(e);
-}
-
-template <class E> typename enable_if<is_enum<E>::value, unmarshall>::type &
-operator>>(unmarshall &u, E &e);
-
 class unmarshall {
     private:
         string buf_;
         size_t index_ = 0;
         bool ok_ = false;
 
-        inline bool ensure(size_t n) {
-            if (index_+n > buf_.size())
-                ok_ = false;
-            return ok_;
-        }
     public:
         unmarshall() {}
         unmarshall(const string &s, bool has_header)
@@ -130,15 +66,17 @@ class unmarshall {
         bool okdone() const { return ok_ && index_ == buf_.size(); }
 
         void rawbytes(void * t, size_t n) {
-            VERIFY(ensure(n));
+            if (index_+n > buf_.size())
+                ok_ = false;
+            VERIFY(ok_);
             copy(&buf_[index_], &buf_[index_+n], (char *)t);
             index_ += n;
         }
 
-        template <class T>
-        void unpack_header(T & h) {
-            // first 4 bytes hold length field
+        template <class T> void
+        unpack_header(T & h) {
             VERIFY(sizeof(T)+sizeof(rpc_sz_t) <= RPC_HEADER_SZ);
+            // first 4 bytes hold length field
             index_ = sizeof(rpc_sz_t);
             *this >> h;
             index_ = RPC_HEADER_SZ;
@@ -147,181 +85,154 @@ class unmarshall {
         template <class T> inline T grab() { T t; *this >> t; return t; }
 };
 
-template <class A> typename enable_if<is_iterable<A>::value, unmarshall>::type &
-operator>>(unmarshall &u, A &x) {
-    unsigned n = u.grab<unsigned>();
-    x.clear();
-    while (n--)
-        x.emplace_back(u.grab<typename A::value_type>());
-    return u;
-}
+//
+// Marshalling for plain old data
+//
 
-template <class A, class B> unmarshall &
-operator>>(unmarshall &u, map<A,B> &x) {
-    unsigned n = u.grab<unsigned>();
-    x.clear();
-    while (n--)
-        x.emplace(u.grab<pair<A,B>>());
-    return u;
+#define MARSHALL_RAW_NETWORK_ORDER_AS(_c_, _d_) \
+inline marshall & operator<<(marshall &m, _c_ x) { _d_ y = hton((_d_)x); m.rawbytes(&y, sizeof(_d_)); return m; } \
+inline unmarshall & operator>>(unmarshall &u, _c_ &x) { _d_ y; u.rawbytes(&y, sizeof(_d_)); x = (_c_)ntoh(y); return u; }
+
+#define MARSHALL_RAW_NETWORK_ORDER(_c_) MARSHALL_RAW_NETWORK_ORDER_AS(_c_, _c_)
+
+MARSHALL_RAW_NETWORK_ORDER_AS(bool, uint8_t)
+MARSHALL_RAW_NETWORK_ORDER(uint8_t)
+MARSHALL_RAW_NETWORK_ORDER(int8_t)
+MARSHALL_RAW_NETWORK_ORDER(uint16_t)
+MARSHALL_RAW_NETWORK_ORDER(int16_t)
+MARSHALL_RAW_NETWORK_ORDER(uint32_t)
+MARSHALL_RAW_NETWORK_ORDER(int32_t)
+MARSHALL_RAW_NETWORK_ORDER_AS(size_t, uint32_t)
+MARSHALL_RAW_NETWORK_ORDER(uint64_t)
+MARSHALL_RAW_NETWORK_ORDER(int64_t)
+
+//
+// Marshalling for tuples (used to implement marshalling for structs)
+//
+
+// In order to iterate over the tuple elements, we first need a template
+// parameter pack containing the tuple's indices.  The function templates named
+// *_imp below accept an empty tag struct as their last argument, and use its
+// template arguments to index the tuple.  The operator<< overloads instantiate
+// the appropriate tag struct to make this possible.
+
+template <class... Args, size_t... Indices> inline marshall &
+tuple_marshall_imp(marshall & m, tuple<Args...> & t, tuple_indices<Indices...>) {
+    // Note that brace initialization is used for the empty structure "pack",
+    // forcing the comma-separated expressions expanded from the parameter pack
+    // to be evaluated in order.  Order matters because the elements must be
+    // serialized consistently!  The empty struct resulting from construction
+    // is discarded.
+    (void)pass{(m << get<Indices>(t))...};
+    return m;
 }
 
-template <class A, class B> unmarshall &
-operator>>(unmarshall &u, pair<A,B> &d) {
-    return u >> d.first >> d.second;
+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());
 }
 
-template <class E> typename enable_if<is_enum<E>::value, unmarshall>::type &
-operator>>(unmarshall &u, E &e) {
-    e = to_enum<E>(u.grab<enum_type_t<E>>());
+template <class... Args, size_t... Indices> inline unmarshall &
+tuple_unmarshall_imp(unmarshall & u, tuple<Args &...> t, tuple_indices<Indices...>) {
+    (void)pass{(u >> get<Indices>(t))...};
     return u;
 }
 
-typedef function<int(unmarshall &, marshall &)> handler;
+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());
+}
 
 //
-// Automatic marshalling wrappers for RPC handlers
+// Marshalling for structs or classes containing a MEMBERS declaration
 //
 
-// 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 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.
-
-// 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;
-    }
-};
+// Implements struct marshalling via tuple marshalling of members.
+#define MARSHALLABLE(_c_) \
+inline unmarshall & operator>>(unmarshall &u, _c_ &a) { return u >> a._tuple_(); } \
+inline marshall & operator<<(marshall &m, const _c_ a) { return m << a._tuple_(); }
 
-// Here's the implementation of 'invoke'.  It could be more general, but this
-// meets our needs.
+// our first two marshallable structs...
+MARSHALLABLE(request_header)
+MARSHALLABLE(reply_header)
 
-// One for function pointers...
+//
+// Marshalling for STL containers
+//
 
-template <class F, class R, class RV, class args_type, size_t... Indices>
-typename enable_if<!is_member_function_pointer<F>::value, RV>::type
-invoke(RV, F f, void *, R & r, args_type & t, tuple_indices<Indices...>) {
-    return f(r, move(get<Indices>(t))...);
+// this overload is visible for type A only if A::cbegin and A::cend exist
+template <class A> inline typename
+enable_if<is_const_iterable<A>::value, marshall>::type &
+operator<<(marshall &m, const A &x) {
+    m << (unsigned int)x.size();
+    for (const auto &a : x)
+        m << a;
+    return m;
 }
 
-// And one for pointers to member functions...
-
-template <class F, class C, class RV, class R, class args_type, size_t... Indices>
-typename enable_if<is_member_function_pointer<F>::value, RV>::type
-invoke(RV, F f, C *c, R & r, args_type & t, tuple_indices<Indices...>) {
-    return (c->*f)(r, move(get<Indices>(t))...);
+// visible for type A if A::emplace_back(a) makes sense
+template <class A> inline typename
+enable_if<supports_emplace_back<A>::value, unmarshall>::type &
+operator>>(unmarshall &u, A &x) {
+    unsigned n = u.grab<unsigned>();
+    x.clear();
+    while (n--)
+        x.emplace_back(u.grab<typename A::value_type>());
+    return u;
 }
 
-// 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> {};
+// std::pair<A, B>
+template <class A, class B> inline marshall &
+operator<<(marshall &m, const pair<A,B> &d) {
+    return m << d.first << d.second;
+}
 
-template <class F, class ErrorHandler, class Signature>
-struct marshalled_func<F, ErrorHandler, function<Signature>> :
-    public marshalled_func_imp<F, void, Signature, ErrorHandler> {};
+template <class A, class B> inline unmarshall &
+operator>>(unmarshall &u, pair<A,B> &d) {
+    return u >> d.first >> d.second;
+}
 
-template <class... Args, size_t... Indices> unmarshall &
-tuple_unmarshall_imp(unmarshall & u, tuple<Args &...> t, tuple_indices<Indices...>) {
-    (void)pass{(u >> get<Indices>(t))...};
+// std::map<A, B>
+template <class A, class B> inline unmarshall &
+operator>>(unmarshall &u, map<A,B> &x) {
+    unsigned n = u.grab<unsigned>();
+    x.clear();
+    while (n--)
+        x.emplace(u.grab<pair<A,B>>());
     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());
+// std::string
+inline marshall & operator<<(marshall &m, const string &s) {
+    m << (uint32_t)s.size();
+    m.rawbytes(s.data(), s.size());
+    return m;
 }
 
-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;
+inline unmarshall & operator>>(unmarshall &u, string &s) {
+    uint32_t sz = u.grab<uint32_t>();
+    if (u.ok()) {
+        s.resize(sz);
+        u.rawbytes(&s[0], sz);
+    }
+    return u;
 }
 
-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());
+//
+// Marshalling for strongly-typed enums
+//
+
+template <class E> typename enable_if<is_enum<E>::value, marshall>::type &
+operator<<(marshall &m, E e) {
+    return m << from_enum(e);
 }
 
-MARSHALLABLE(request_header)
-MARSHALLABLE(reply_header)
+template <class E> typename enable_if<is_enum<E>::value, unmarshall>::type &
+operator>>(unmarshall &u, E &e) {
+    e = to_enum<E>(u.grab<enum_type_t<E>>());
+    return u;
+}
 
 #endif
diff --git a/rpc/marshall_wrap.h b/rpc/marshall_wrap.h
new file mode 100644 (file)
index 0000000..2d7fbfb
--- /dev/null
@@ -0,0 +1,127 @@
+#ifndef marshall_wrap_h
+#define marshall_wrap_h
+
+#include "marshall.h"
+
+typedef 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 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.
+
+// 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 RV, class args_type, size_t... Indices>
+typename enable_if<!is_member_function_pointer<F>::value, RV>::type
+invoke(RV, F f, void *, R & r, args_type & t, tuple_indices<Indices...>) {
+    return f(r, move(get<Indices>(t))...);
+}
+
+// And one for pointers to member functions...
+
+template <class F, class C, class RV, class R, class args_type, size_t... Indices>
+typename enable_if<is_member_function_pointer<F>::value, RV>::type
+invoke(RV, F f, C *c, R & r, args_type & t, tuple_indices<Indices...>) {
+    return (c->*f)(r, move(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 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> {};
+
+#endif
index 2b32e28..065cabc 100644 (file)
--- a/rpc/rpc.h
+++ b/rpc/rpc.h
@@ -7,6 +7,7 @@
 
 #include "thr_pool.h"
 #include "marshall.h"
+#include "marshall_wrap.h"
 #include "connection.h"
 
 class rpc_const {
index 2ef9ab2..8107f04 100644 (file)
@@ -17,6 +17,8 @@ struct request_header {
     MEMBERS(xid, proc, clt_nonce, srv_nonce, xid_rep)
 };
 
+ENDIAN_SWAPPABLE(request_header)
+
 struct reply_header {
     int xid;
     int ret;
@@ -24,6 +26,8 @@ struct reply_header {
     MEMBERS(xid, ret)
 };
 
+ENDIAN_SWAPPABLE(reply_header)
+
 const size_t RPC_HEADER_SZ = max(sizeof(request_header), sizeof(reply_header)) + sizeof(rpc_sz_t);
 const size_t DEFAULT_RPC_SZ = 1024; // size of initial buffer allocation
 const size_t MAX_PDU = 10<<20; //maximum PDF is 10M
index 750c5d2..5d3942c 100644 (file)
@@ -19,7 +19,7 @@ namespace std {
 }
 
 template <class A>
-typename enable_if<is_iterable<A>::value && !is_same<A,string>::value, ostream>::type &
+typename enable_if<is_const_iterable<A>::value && !is_same<A,string>::value, ostream>::type &
 operator<<(ostream &o, const A &a) {
     return o << "[" << implode(a, ", ") << "]";
 }
diff --git a/types.h b/types.h
index e78186c..cdb629a 100644 (file)
--- a/types.h
+++ b/types.h
@@ -99,6 +99,7 @@ using std::is_same;
 using std::underlying_type;
 using std::enable_if;
 using std::remove_reference;
+using std::add_const;
 
 #include <utility>
 using std::pair;
@@ -108,15 +109,29 @@ using std::forward;
 #include <vector>
 using std::vector;
 
+// type traits and manipulators
 
-template <class A, typename I=void> struct is_iterable : false_type {};
+template <class A, typename I=void> struct is_const_iterable : false_type {};
 
-template<class A> struct is_iterable<A,
+template<class A> struct is_const_iterable<A,
     decltype(declval<A&>().cbegin(), declval<A&>().cend(), void())
 > : true_type {};
 
+template <class A, typename I=void> struct supports_emplace_back : false_type {};
+
+template<class A> struct supports_emplace_back<A,
+    decltype(declval<A&>().emplace_back(declval<typename A::value_type>()), void())
+> : true_type {};
+
+template<typename E>
+using enum_type_t = typename enable_if<is_enum<E>::value, typename underlying_type<E>::type>::type;
+template<typename E> constexpr inline enum_type_t<E> from_enum(E e) noexcept { return (enum_type_t<E>)e; }
+template<typename E> constexpr inline E to_enum(enum_type_t<E> value) noexcept { return (E)value; }
+
+// string manipulation
+
 template <class C>
-inline typename enable_if<is_iterable<C>::value, string>::type
+inline typename enable_if<is_const_iterable<C>::value, string>::type
 implode(const C & v, string delim=" ") {
     if (v.begin() == v.end())
         return string();
@@ -141,10 +156,25 @@ inline vector<string> explode(const string &s, string delim=" ") {
 #include "lang/verify.h"
 #include "threaded_log.h"
 
+// struct tuple adapter, useful for marshalling
+// used like
+// struct foo {
+//     int a, b;
+//     MEMBERS(a, b)
+// };
+
 #define MEMBERS(...) \
 inline auto _tuple_() -> decltype(tie(__VA_ARGS__)) { return tie(__VA_ARGS__); } \
 inline auto _tuple_() const -> decltype(tie(__VA_ARGS__)) { return tie(__VA_ARGS__); }
 
+// struct ordering and comparison
+// used like
+// struct foo {
+//     int a, b;
+//     MEMBERS(a, b)
+// };
+// LEXICOGRAPHIC_COMPARISON(foo)
+
 #define LEXICOGRAPHIC_OPERATOR(_c_, _op_) \
 inline bool operator _op_(const _c_ &b) const { return _tuple_() _op_ b._tuple_(); }
 
@@ -153,7 +183,9 @@ LEXICOGRAPHIC_OPERATOR(_c_, <) LEXICOGRAPHIC_OPERATOR(_c_, <=) \
 LEXICOGRAPHIC_OPERATOR(_c_, >) LEXICOGRAPHIC_OPERATOR(_c_, >=) \
 LEXICOGRAPHIC_OPERATOR(_c_, ==) LEXICOGRAPHIC_OPERATOR(_c_, !=)
 
-// The following implementation of tuple_indices is redistributed under the MIT
+// crucial tool for tuple indexing in variadic templates
+//
+// This 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 {};
@@ -168,6 +200,11 @@ template <size_t E, size_t S=0> struct make_tuple_indices {
     typedef typename make_indices_imp<S, tuple_indices<>, E>::type type;
 };
 
+// Template parameter pack expansion is not allowed in certain contexts, but
+// brace initializers (for instance, calls to constructors of empty structs)
+// are fair game.  
+struct pass { template <typename... Args> inline pass(Args&&...) {} };
+
 #include "endian.h"
 
 #endif