C++11y allows us to eliminate some boilerplate!
authorPeter Iannucci <iannucci@mit.edu>
Fri, 17 Jan 2014 06:36:07 +0000 (01:36 -0500)
committerPeter Iannucci <iannucci@mit.edu>
Fri, 17 Jan 2014 06:36:07 +0000 (01:36 -0500)
Makefile
Makefile.osx
endian.h
lock_client.cc
rpc/marshall.h
rpc/marshall_wrap.h
rsm.cc
types.h

index 5ec634c..5dd06c5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-CXXFLAGS ?= -g -MMD -Werror -I. -std=c++11
+CXXFLAGS ?= -g -MMD -Werror -I. -std=c++1y
 LDFLAGS ?= 
 CXX ?= g++
 CC ?= g++
index d352c69..92638c7 100644 (file)
@@ -3,8 +3,8 @@ USE_CLANG = 1
 PEDANTRY =
 STDLIB =
 OPTFLAGS = -O3 #-fno-omit-frame-pointer -fsanitize=address ,thread,undefined -fsanitize-memory-track-origins 
-CXXFLAGS = -std=c++11 -ggdb3 -MMD -I. $(STDLIB) $(PEDANTRY) $(OPTFLAGS)
-LDFLAGS = -std=c++11 $(STDLIB) $(OPTFLAGS)
+CXXFLAGS = -std=c++1y -ggdb3 -MMD -I. $(STDLIB) $(PEDANTRY) $(OPTFLAGS)
+LDFLAGS = -std=c++1y $(STDLIB) $(OPTFLAGS)
 
 ifeq "$(USE_CLANG)" "1"
 
index feb3bbd..7c78bbc 100644 (file)
--- a/endian.h
+++ b/endian.h
@@ -22,7 +22,7 @@ template <class T> inline T ntoh(T t) { return hton(t); }
 
 template <class... Args, size_t... Indices>
 inline tuple<typename std::remove_reference<Args>::type...>
-tuple_hton_imp(tuple<Args...> && t, tuple_indices<Indices...>) {
+tuple_hton_imp(tuple<Args...> && t, std::index_sequence<Indices...>) {
     return tuple<
         typename std::remove_reference<Args>::type...
     >(hton(std::get<Indices>(t))...);
@@ -31,7 +31,7 @@ tuple_hton_imp(tuple<Args...> && t, tuple_indices<Indices...>) {
 template <class... Args>
 inline tuple<typename std::remove_reference<Args>::type...>
 hton(tuple<Args...> && t) {
-    return tuple_hton_imp(std::forward<tuple<Args...>>(t), TUPLE_INDICES(Args));
+    return tuple_hton_imp(std::forward<tuple<Args...>>(t), std::index_sequence_for<Args...>{});
 }
 
 template <class T> inline typename
index 6ec3cbc..ca21d9d 100644 (file)
@@ -28,10 +28,10 @@ lock_state & lock_client::get_lock_state(lock_protocol::lockid_t lid) {
 lock_client::lock_client(string xdst, lock_release_user *_lu) : lu(_lu), next_xid(0) {
     rlock_port = std::uniform_int_distribution<in_port_t>(1024,32000+1024)(global->random_generator);
     id = "127.0.0.1:" + std::to_string(rlock_port);
-    rlsrpc = unique_ptr<rpcs>(new rpcs(rlock_port));
+    rlsrpc = std::make_unique<rpcs>(rlock_port);
     rlsrpc->reg(rlock_protocol::revoke, &lock_client::revoke_handler, this);
     rlsrpc->reg(rlock_protocol::retry, &lock_client::retry_handler, this);
-    rsmc = unique_ptr<rsm_client>(new rsm_client(xdst));
+    rsmc = std::make_unique<rsm_client>(xdst);
     releaser_thread = thread(&lock_client::releaser, this);
     rlsrpc->start();
 }
index 08b6aaa..8d5812a 100644 (file)
@@ -116,7 +116,7 @@ MARSHALL_RAW_NETWORK_ORDER(int64_t)
 // 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...>) {
+tuple_marshall_imp(marshall & m, tuple<Args...> & t, std::index_sequence<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
@@ -128,18 +128,18 @@ tuple_marshall_imp(marshall & m, tuple<Args...> & t, tuple_indices<Indices...>)
 
 template <class... Args> inline marshall &
 operator<<(marshall & m, tuple<Args...> && t) {
-    return tuple_marshall_imp(m, t, TUPLE_INDICES(Args));
+    return tuple_marshall_imp(m, t, std::index_sequence_for<Args...>{});
 }
 
 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, std::index_sequence<Indices...>) {
     (void)pass{(u >> std::get<Indices>(t))...};
     return u;
 }
 
 template <class... Args> inline unmarshall &
 operator>>(unmarshall & u, tuple<Args & ...> && t) {
-    return tuple_unmarshall_imp(u, t, TUPLE_INDICES(Args));
+    return tuple_unmarshall_imp(u, t, std::index_sequence_for<Args...>{});
 }
 
 //
index 2e54e47..04f5268 100644 (file)
@@ -17,14 +17,6 @@ typedef std::function<rpc_protocol::status(unmarshall &&, marshall &)> handler;
 //
 // 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
@@ -43,7 +35,7 @@ struct VerifyOnFailure {
 
 template <class F, class R, class RV, class args_type, size_t... Indices>
 typename enable_if<!std::is_member_function_pointer<F>::value, RV>::type inline 
-invoke(RV, F f, void *, R & r, args_type & t, tuple_indices<Indices...>) {
+invoke(RV, F f, void *, R & r, args_type & t, std::index_sequence<Indices...>) {
     return f(r, std::get<Indices>(t)...);
 }
 
@@ -51,7 +43,7 @@ invoke(RV, F f, void *, R & r, args_type & t, tuple_indices<Indices...>) {
 
 template <class F, class C, class RV, class R, class args_type, size_t... Indices>
 typename enable_if<std::is_member_function_pointer<F>::value, RV>::type inline 
-invoke(RV, F f, C *c, R & r, args_type & t, tuple_indices<Indices...>) {
+invoke(RV, F f, C *c, R & r, args_type & t, std::index_sequence<Indices...>) {
     return (c->*f)(r, std::get<Indices>(t)...);
 }
 
@@ -88,10 +80,8 @@ struct marshalled_func_imp<F, C, RV(R &, Args...), ErrorHandler> {
             // Allocate space for the RPC response -- will be passed into the
             // function as an lvalue reference.
             R r;
-            // Perform the invocation.  Note that TUPLE_INDICES calls the
-            // default constructor of an empty struct with template parameters
-            // running from 0 up to (# args) - 1.
-            RV b = invoke(RV(), f, c, r, t, TUPLE_INDICES(Args));
+            // Perform the invocation.
+            RV b = invoke(RV(), f, c, r, t, std::index_sequence_for<Args...>{});
             // Marshall the response.
             m << r;
             // Make like a tree.
diff --git a/rsm.cc b/rsm.cc
index 711f0ad..c651e86 100644 (file)
--- a/rsm.cc
+++ b/rsm.cc
@@ -88,7 +88,7 @@ rsm_state_transfer::~rsm_state_transfer() {}
 
 rsm::rsm(const string & _first, const string & _me) : primary(_first)
 {
-    cfg = unique_ptr<config>(new config(_first, _me, this));
+    cfg = std::make_unique<config>(_first, _me, this);
 
     if (_first == _me) {
         // Commit the first view here. We can not have acceptor::acceptor
diff --git a/types.h b/types.h
index 797ccf0..53f496d 100644 (file)
--- a/types.h
+++ b/types.h
@@ -117,8 +117,8 @@ operator<<(std::ostream & o, const A & a) {
 // };
 
 #define MEMBERS(...) \
-inline auto _tuple_() -> decltype(std::tie(__VA_ARGS__)) { return std::tie(__VA_ARGS__); } \
-inline auto _tuple_() const -> decltype(std::tie(__VA_ARGS__)) { return std::tie(__VA_ARGS__); }
+inline auto _tuple_() { return std::tie(__VA_ARGS__); } \
+inline auto _tuple_() const { return std::tie(__VA_ARGS__); }
 
 // struct ordering and comparison operations; requires the use of MEMBERS.
 // usage:
@@ -133,24 +133,6 @@ LEXICOGRAPHIC_OPERATOR(_c_, <) LEXICOGRAPHIC_OPERATOR(_c_, <=) \
 LEXICOGRAPHIC_OPERATOR(_c_, >) LEXICOGRAPHIC_OPERATOR(_c_, >=) \
 LEXICOGRAPHIC_OPERATOR(_c_, ==) LEXICOGRAPHIC_OPERATOR(_c_, !=)
 
-// 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 {};
-template <size_t S, class IntTuple, size_t E> struct make_indices_imp;
-template <size_t S, size_t... Indices, size_t E> struct make_indices_imp<S, tuple_indices<Indices...>, E> {
-    typedef typename make_indices_imp<S+1, tuple_indices<Indices..., S>, E>::type type;
-};
-template <size_t E, size_t... Indices> struct make_indices_imp<E, tuple_indices<Indices...>, E> {
-    typedef tuple_indices<Indices...> type;
-};
-template <size_t E, size_t S=0> struct make_tuple_indices {
-    typedef typename make_indices_imp<S, tuple_indices<>, E>::type type;
-};
-
-#define TUPLE_INDICES(_ArgPack_) typename make_tuple_indices<sizeof...(_ArgPack_)>::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.