More clean-ups and cool template stuff
[invirt/third/libt4.git] / rpc / marshall.h
index 676a682..abeaae7 100644 (file)
 #include <inttypes.h>
 #include "lang/verify.h"
 
+using proc_t = uint32_t;
+using status_t = int32_t;
+
 struct request_header {
-    request_header(int x=0, int p=0, unsigned c=0, unsigned s=0, int xi=0) :
+    request_header(int x=0, proc_t p=0, unsigned c=0, unsigned s=0, int xi=0) :
         xid(x), proc(p), clt_nonce(c), srv_nonce(s), xid_rep(xi) {}
     int xid;
-    int proc;
+    proc_t proc;
     unsigned int clt_nonce;
     unsigned int srv_nonce;
     int xid_rep;
@@ -134,7 +137,14 @@ marshall& operator<<(marshall &, int16_t);
 marshall& operator<<(marshall &, uint64_t);
 marshall& operator<<(marshall &, const std::string &);
 
-template <class A> marshall &
+template <class A, typename I=void>
+struct is_enumerable : std::false_type {};
+
+template<class A> struct is_enumerable<A,
+    decltype(std::declval<A&>().cbegin(), std::declval<A&>().cend(), void())
+> : std::true_type {};
+
+template <class A> typename std::enable_if<is_enumerable<A>::value, marshall>::type &
 operator<<(marshall &m, const A &x) {
     m << (unsigned int) x.size();
     for (const auto &a : x)
@@ -144,9 +154,17 @@ operator<<(marshall &m, const A &x) {
 
 template <class A, class B> marshall &
 operator<<(marshall &m, const std::pair<A,B> &d) {
-    m << d.first;
-    m << d.second;
-    return m;
+    return m << d.first << d.second;
+}
+
+template<typename E>
+using enum_type_t = typename std::enable_if<std::is_enum<E>::value, typename std::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 std::enable_if<std::is_enum<E>::value, marshall>::type &
+operator<<(marshall &m, E e) {
+    return m << from_enum(e);
 }
 
 class unmarshall;
@@ -162,6 +180,8 @@ unmarshall& operator>>(unmarshall &, size_t &);
 unmarshall& operator>>(unmarshall &, uint64_t &);
 unmarshall& operator>>(unmarshall &, int64_t &);
 unmarshall& operator>>(unmarshall &, std::string &);
+template <class E> typename std::enable_if<std::is_enum<E>::value, unmarshall>::type &
+operator>>(unmarshall &u, E &e);
 
 class unmarshall {
     private:
@@ -235,7 +255,8 @@ class unmarshall {
         }
 };
 
-template <class A> unmarshall & operator>>(unmarshall &u, A &x) {
+template <class A> typename std::enable_if<is_enumerable<A>::value, unmarshall>::type &
+operator>>(unmarshall &u, A &x) {
     unsigned n = u.grab<unsigned>();
     x.clear();
     while (n--)
@@ -257,6 +278,12 @@ operator>>(unmarshall &u, std::pair<A,B> &d) {
     return u >> d.first >> d.second;
 }
 
+template <class E> typename std::enable_if<std::is_enum<E>::value, unmarshall>::type &
+operator>>(unmarshall &u, E &e) {
+    e = to_enum<E>(u.grab<enum_type_t<E>>());
+    return u;
+}
+
 typedef std::function<int(unmarshall &, marshall &)> handler;
 
 //
@@ -311,17 +338,17 @@ struct VerifyOnFailure {
 
 // One for function pointers...
 
-template <class F, class R, class args_type, size_t ...Indices>
-typename std::enable_if<!std::is_member_function_pointer<F>::value, int>::type
-invoke(F f, void *, R & r, args_type & t, tuple_indices<Indices...>) {
+template <class F, class R, class RV, class args_type, size_t ...Indices>
+typename std::enable_if<!std::is_member_function_pointer<F>::value, RV>::type
+invoke(RV, F f, void *, R & r, args_type & t, tuple_indices<Indices...>) {
     return f(r, std::move(std::get<Indices>(t))...);
 }
 
 // And one for pointers to member functions...
 
-template <class F, class C, class R, class args_type, size_t ...Indices>
-typename std::enable_if<std::is_member_function_pointer<F>::value, int>::type
-invoke(F f, C *c, R & r, args_type & t, tuple_indices<Indices...>) {
+template <class F, class C, class RV, class R, class args_type, size_t ...Indices>
+typename std::enable_if<std::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, std::move(std::get<Indices>(t))...);
 }
 
@@ -339,8 +366,8 @@ template <class Functor, class Instance, class Signature,
 // 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... Args>
-struct marshalled_func_imp<F, C, int(R&, Args...), ErrorHandler> {
+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.
@@ -351,20 +378,20 @@ struct marshalled_func_imp<F, C, int(R&, Args...), ErrorHandler> {
         using ArgsStorage = std::tuple<typename std::decay<Args>::type...>;
         // Allocate a handler (i.e. std::function) to hold the lambda
         // which will unmarshall RPCs and call f.
-        return new handler([=](unmarshall &u, marshall &m) -> int {
+        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 std::decay<Args>::type>()...};
             // Verify successful unmarshalling of the entire input stream.
             if (!u.okdone())
-                return ErrorHandler::unmarshall_args_failure();
+                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.
-            int b = invoke(f, c, r, t, Indices());
+            RV b = invoke(RV(), f, c, r, t, Indices());
             // Marshall the response.
             m << r;
             // Make like a tree.
@@ -381,13 +408,13 @@ struct marshalled_func_imp<F, C, int(R&, Args...), ErrorHandler> {
 template <class Functor, class ErrorHandler=VerifyOnFailure,
     class Signature=Functor> struct marshalled_func;
 
-template <class F, class ErrorHandler, class... Args>
-struct marshalled_func<F, ErrorHandler, int(*)(Args...)> :
-    public marshalled_func_imp<F, void, int(Args...), ErrorHandler> {};
+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 C, class... Args>
-struct marshalled_func<F, ErrorHandler, int(C::*)(Args...)> :
-    public marshalled_func_imp<F, C, int(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, std::function<Signature>> :