X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/be7cf844f59fa483423724e8e4b5e663e5b88ddd..3abd3952c1f4441f0dd6eae9883b2d01ed9cd56b:/rpc/marshall.h?ds=inline diff --git a/rpc/marshall.h b/rpc/marshall.h index 0e0af8d..69b10df 100644 --- a/rpc/marshall.h +++ b/rpc/marshall.h @@ -32,7 +32,7 @@ class marshall { // with header inline operator string() const { return buf_.substr(0,index_); } // without header - inline string content() { return buf_.substr(RPC_HEADER_SZ,index_-RPC_HEADER_SZ); } + inline string content() const { 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 @@ -54,7 +54,6 @@ class unmarshall { bool ok_ = false; public: - unmarshall() {} unmarshall(const string &s, bool has_header) : buf_(s),index_(RPC_HEADER_SZ) { if (!has_header) @@ -73,7 +72,7 @@ class unmarshall { index_ += n; } - template void + template inline void unpack_header(T & h) { VERIFY(sizeof(T)+sizeof(rpc_sz_t) <= RPC_HEADER_SZ); // first 4 bytes hold length field @@ -82,7 +81,7 @@ class unmarshall { index_ = RPC_HEADER_SZ; } - template inline T grab() { T t; *this >> t; return t; } + template inline T _grab() { T t; *this >> t; return t; } }; // @@ -176,10 +175,10 @@ operator<<(marshall &m, const A &x) { template inline typename enable_if::value, unmarshall>::type & operator>>(unmarshall &u, A &x) { - unsigned n = u.grab(); + unsigned n = u._grab(); x.clear(); while (n--) - x.emplace_back(u.grab()); + x.emplace_back(u._grab()); return u; } @@ -197,10 +196,10 @@ operator>>(unmarshall &u, pair &d) { // std::map template inline unmarshall & operator>>(unmarshall &u, map &x) { - unsigned n = u.grab(); + unsigned n = u._grab(); x.clear(); while (n--) - x.emplace(u.grab>()); + x.emplace(u._grab>()); return u; } @@ -212,7 +211,7 @@ inline marshall & operator<<(marshall &m, const string &s) { } inline unmarshall & operator>>(unmarshall &u, string &s) { - uint32_t sz = u.grab(); + uint32_t sz = u._grab(); if (u.ok()) { s.resize(sz); u.rawbytes(&s[0], sz); @@ -231,7 +230,20 @@ operator<<(marshall &m, E e) { template typename enable_if::value, unmarshall>::type & operator>>(unmarshall &u, E &e) { - e = to_enum(u.grab>()); + e = to_enum(u._grab>()); + return u; +} + +// +// Recursive marshalling +// + +inline marshall & operator<<(marshall &m, marshall &n) { + return m << n.content(); +} + +inline unmarshall & operator>>(unmarshall &u, unmarshall &v) { + v = unmarshall(u._grab(), false); return u; }