Variadic templates for RPCs
[invirt/third/libt4.git] / rpc / marshall.h
index fcb5bab..27cebbb 100644 (file)
@@ -34,6 +34,10 @@ typedef int rpc_sz_t;
 #define DEFAULT_RPC_SZ 1024
 #define RPC_HEADER_SZ (std::max(sizeof(req_header), sizeof(reply_header)) + sizeof(rpc_sz_t))
 
+struct pass {
+    template<typename... Args> inline pass(Args&&...) {}
+};
+
 class marshall {
        private:
                char *_buf;     // Base of the raw bytes buffer (dynamically readjusted)
@@ -48,6 +52,10 @@ class marshall {
                        _ind = RPC_HEADER_SZ;
                }
 
+        template <typename... Args> marshall(const Args&... args) : marshall() {
+            (void)pass{(*this << args)...};
+        }
+
                ~marshall() { 
                        if (_buf) 
                                free(_buf); 
@@ -210,6 +218,15 @@ class unmarshall {
                        unpack(&h->ret);
                        _ind = RPC_HEADER_SZ;
                }
+
+        template <class OutputIterator>
+        void iterate(OutputIterator i, int n) {
+            while (n--) {
+                typename OutputIterator::value_type t;
+                *this >> t;
+                *i++ = t;
+            }
+        }
 };
 
 unmarshall& operator>>(unmarshall &, bool &);