Cosmetic improvements.
[invirt/third/libt4.git] / rpc / marshall.h
index 6412612..7b716e2 100644 (file)
@@ -4,9 +4,6 @@
 #include "types.h"
 #include "rpc_protocol.h"
 
 #include "types.h"
 #include "rpc_protocol.h"
 
-class marshall;
-class unmarshall;
-
 //
 // Marshall and unmarshall objects
 //
 //
 // Marshall and unmarshall objects
 //
@@ -18,7 +15,7 @@ class marshall {
 
     public:
         template <typename... Args>
 
     public:
         template <typename... Args>
-        marshall(const Args&... args) {
+        marshall(const Args & ... args) {
             (void)pass{(*this << args)...};
         }
 
             (void)pass{(*this << args)...};
         }
 
@@ -35,7 +32,7 @@ class marshall {
         inline string content() const { return buf_.substr(rpc_protocol::RPC_HEADER_SZ,index_-rpc_protocol::RPC_HEADER_SZ); }
 
         // letting S be a defaulted template parameter forces the compiler to
         inline string content() const { return buf_.substr(rpc_protocol::RPC_HEADER_SZ,index_-rpc_protocol::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
+        // 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_protocol::rpc_sz_t> inline void
         pack_header(const T & h) {
         // (i.e. we define an operator for marshalling uint32_t)
         template <class T, class S=rpc_protocol::rpc_sz_t> inline void
         pack_header(const T & h) {
@@ -54,11 +51,13 @@ class unmarshall {
         bool ok_ = false;
 
     public:
         bool ok_ = false;
 
     public:
-        unmarshall(const string &s, bool has_header)
+        template <typename... Args>
+        unmarshall(const string & s, bool has_header, Args && ... args)
             : buf_(s),index_(rpc_protocol::RPC_HEADER_SZ) {
             if (!has_header)
                 buf_.insert(0, rpc_protocol::RPC_HEADER_SZ, 0);
             ok_ = (buf_.size() >= rpc_protocol::RPC_HEADER_SZ);
             : buf_(s),index_(rpc_protocol::RPC_HEADER_SZ) {
             if (!has_header)
                 buf_.insert(0, rpc_protocol::RPC_HEADER_SZ, 0);
             ok_ = (buf_.size() >= rpc_protocol::RPC_HEADER_SZ);
+            (void)pass{(*this >> args)...};
         }
 
         bool ok() const { return ok_; }
         }
 
         bool ok() const { return ok_; }
@@ -89,8 +88,8 @@ class unmarshall {
 //
 
 #define MARSHALL_RAW_NETWORK_ORDER_AS(_c_, _d_) \
 //
 
 #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; }
+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_)
 
 
 #define MARSHALL_RAW_NETWORK_ORDER(_c_) MARSHALL_RAW_NETWORK_ORDER_AS(_c_, _c_)
 
@@ -133,13 +132,13 @@ operator<<(marshall & m, tuple<Args...> && t) {
 }
 
 template <class... Args, size_t... Indices> inline unmarshall &
 }
 
 template <class... Args, size_t... Indices> inline unmarshall &
-tuple_unmarshall_imp(unmarshall & u, tuple<Args &...> t, tuple_indices<Indices...>) {
+tuple_unmarshall_imp(unmarshall & u, tuple<Args & ...> t, tuple_indices<Indices...>) {
     (void)pass{(u >> get<Indices>(t))...};
     return u;
 }
 
 template <class... Args> unmarshall &
     (void)pass{(u >> get<Indices>(t))...};
     return u;
 }
 
 template <class... Args> unmarshall &
-operator>>(unmarshall & u, tuple<Args &...> && t) {
+operator>>(unmarshall & u, tuple<Args & ...> && t) {
     using Indices = typename make_tuple_indices<sizeof...(Args)>::type;
     return tuple_unmarshall_imp(u, t, Indices());
 }
     using Indices = typename make_tuple_indices<sizeof...(Args)>::type;
     return tuple_unmarshall_imp(u, t, Indices());
 }
@@ -149,13 +148,13 @@ operator>>(unmarshall & u, tuple<Args &...> && t) {
 //
 
 // Implements struct marshalling via tuple marshalling of members.
 //
 
 // 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_(); }
+#define MARSHALLABLE_STRUCT(_c_) \
+inline unmarshall & operator>>(unmarshall & u, _c_ & a) { return u >> a._tuple_(); } \
+inline marshall & operator<<(marshall & m, const _c_ a) { return m << a._tuple_(); }
 
 // our first two marshallable structs...
 
 // our first two marshallable structs...
-MARSHALLABLE(rpc_protocol::request_header)
-MARSHALLABLE(rpc_protocol::reply_header)
+MARSHALLABLE_STRUCT(rpc_protocol::request_header)
+MARSHALLABLE_STRUCT(rpc_protocol::reply_header)
 
 //
 // Marshalling for STL containers
 
 //
 // Marshalling for STL containers
@@ -164,9 +163,9 @@ MARSHALLABLE(rpc_protocol::reply_header)
 // 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 &
 // 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) {
+operator<<(marshall & m, const A & x) {
     m << (unsigned int)x.size();
     m << (unsigned int)x.size();
-    for (const auto &a : x)
+    for (const auto & a : x)
         m << a;
     return m;
 }
         m << a;
     return m;
 }
@@ -174,7 +173,7 @@ operator<<(marshall &m, const A &x) {
 // 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 &
 // 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) {
+operator>>(unmarshall & u, A & x) {
     unsigned n = u._grab<unsigned>();
     x.clear();
     while (n--)
     unsigned n = u._grab<unsigned>();
     x.clear();
     while (n--)
@@ -184,18 +183,18 @@ operator>>(unmarshall &u, A &x) {
 
 // std::pair<A, B>
 template <class A, class B> inline marshall &
 
 // std::pair<A, B>
 template <class A, class B> inline marshall &
-operator<<(marshall &m, const pair<A,B> &d) {
+operator<<(marshall & m, const pair<A,B> & d) {
     return m << d.first << d.second;
 }
 
 template <class A, class B> inline unmarshall &
     return m << d.first << d.second;
 }
 
 template <class A, class B> inline unmarshall &
-operator>>(unmarshall &u, pair<A,B> &d) {
+operator>>(unmarshall & u, pair<A,B> & d) {
     return u >> d.first >> d.second;
 }
 
 // std::map<A, B>
 template <class A, class B> inline unmarshall &
     return u >> d.first >> d.second;
 }
 
 // std::map<A, B>
 template <class A, class B> inline unmarshall &
-operator>>(unmarshall &u, map<A,B> &x) {
+operator>>(unmarshall & u, map<A,B> & x) {
     unsigned n = u._grab<unsigned>();
     x.clear();
     while (n--)
     unsigned n = u._grab<unsigned>();
     x.clear();
     while (n--)
@@ -204,13 +203,13 @@ operator>>(unmarshall &u, map<A,B> &x) {
 }
 
 // std::string
 }
 
 // std::string
-inline marshall & operator<<(marshall &m, const string &s) {
+inline marshall & operator<<(marshall & m, const string & s) {
     m << (uint32_t)s.size();
     m.rawbytes(s.data(), s.size());
     return m;
 }
 
     m << (uint32_t)s.size();
     m.rawbytes(s.data(), s.size());
     return m;
 }
 
-inline unmarshall & operator>>(unmarshall &u, string &s) {
+inline unmarshall & operator>>(unmarshall & u, string & s) {
     uint32_t sz = u._grab<uint32_t>();
     if (u.ok()) {
         s.resize(sz);
     uint32_t sz = u._grab<uint32_t>();
     if (u.ok()) {
         s.resize(sz);
@@ -224,12 +223,12 @@ inline unmarshall & operator>>(unmarshall &u, string &s) {
 //
 
 template <class E> typename enable_if<is_enum<E>::value, marshall>::type &
 //
 
 template <class E> typename enable_if<is_enum<E>::value, marshall>::type &
-operator<<(marshall &m, E e) {
+operator<<(marshall & m, E e) {
     return m << from_enum(e);
 }
 
 template <class E> typename enable_if<is_enum<E>::value, unmarshall>::type &
     return m << from_enum(e);
 }
 
 template <class E> typename enable_if<is_enum<E>::value, unmarshall>::type &
-operator>>(unmarshall &u, E &e) {
+operator>>(unmarshall & u, E & e) {
     e = to_enum<E>(u._grab<enum_type_t<E>>());
     return u;
 }
     e = to_enum<E>(u._grab<enum_type_t<E>>());
     return u;
 }
@@ -238,11 +237,11 @@ operator>>(unmarshall &u, E &e) {
 // Recursive marshalling
 //
 
 // Recursive marshalling
 //
 
-inline marshall & operator<<(marshall &m, marshall &n) {
+inline marshall & operator<<(marshall & m, marshall & n) {
     return m << n.content();
 }
 
     return m << n.content();
 }
 
-inline unmarshall & operator>>(unmarshall &u, unmarshall &v) {
+inline unmarshall & operator>>(unmarshall & u, unmarshall & v) {
     v = unmarshall(u._grab<string>(), false);
     return u;
 }
     v = unmarshall(u._grab<string>(), false);
     return u;
 }