From: Peter Iannucci Date: Fri, 17 Jan 2014 06:36:07 +0000 (-0500) Subject: C++11y allows us to eliminate some boilerplate! X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/commitdiff_plain/3efa02fb3a9f0a0566a7f3c99a1efb94e30ea4a6?ds=sidebyside C++11y allows us to eliminate some boilerplate! --- diff --git a/Makefile b/Makefile index 5ec634c..5dd06c5 100644 --- 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++ diff --git a/Makefile.osx b/Makefile.osx index d352c69..92638c7 100644 --- a/Makefile.osx +++ b/Makefile.osx @@ -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" diff --git a/endian.h b/endian.h index feb3bbd..7c78bbc 100644 --- a/endian.h +++ b/endian.h @@ -22,7 +22,7 @@ template inline T ntoh(T t) { return hton(t); } template inline tuple::type...> -tuple_hton_imp(tuple && t, tuple_indices) { +tuple_hton_imp(tuple && t, std::index_sequence) { return tuple< typename std::remove_reference::type... >(hton(std::get(t))...); @@ -31,7 +31,7 @@ tuple_hton_imp(tuple && t, tuple_indices) { template inline tuple::type...> hton(tuple && t) { - return tuple_hton_imp(std::forward>(t), TUPLE_INDICES(Args)); + return tuple_hton_imp(std::forward>(t), std::index_sequence_for{}); } template inline typename diff --git a/lock_client.cc b/lock_client.cc index 6ec3cbc..ca21d9d 100644 --- a/lock_client.cc +++ b/lock_client.cc @@ -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(1024,32000+1024)(global->random_generator); id = "127.0.0.1:" + std::to_string(rlock_port); - rlsrpc = unique_ptr(new rpcs(rlock_port)); + rlsrpc = std::make_unique(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(new rsm_client(xdst)); + rsmc = std::make_unique(xdst); releaser_thread = thread(&lock_client::releaser, this); rlsrpc->start(); } diff --git a/rpc/marshall.h b/rpc/marshall.h index 08b6aaa..8d5812a 100644 --- a/rpc/marshall.h +++ b/rpc/marshall.h @@ -116,7 +116,7 @@ MARSHALL_RAW_NETWORK_ORDER(int64_t) // the appropriate tag struct to make this possible. template inline marshall & -tuple_marshall_imp(marshall & m, tuple & t, tuple_indices) { +tuple_marshall_imp(marshall & m, tuple & t, std::index_sequence) { // 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 & t, tuple_indices) template inline marshall & operator<<(marshall & m, tuple && t) { - return tuple_marshall_imp(m, t, TUPLE_INDICES(Args)); + return tuple_marshall_imp(m, t, std::index_sequence_for{}); } template inline unmarshall & -tuple_unmarshall_imp(unmarshall & u, tuple t, tuple_indices) { +tuple_unmarshall_imp(unmarshall & u, tuple t, std::index_sequence) { (void)pass{(u >> std::get(t))...}; return u; } template inline unmarshall & operator>>(unmarshall & u, tuple && t) { - return tuple_unmarshall_imp(u, t, TUPLE_INDICES(Args)); + return tuple_unmarshall_imp(u, t, std::index_sequence_for{}); } // diff --git a/rpc/marshall_wrap.h b/rpc/marshall_wrap.h index 2e54e47..04f5268 100644 --- a/rpc/marshall_wrap.h +++ b/rpc/marshall_wrap.h @@ -17,14 +17,6 @@ typedef std::function 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 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 typename enable_if::value, RV>::type inline -invoke(RV, F f, void *, R & r, args_type & t, tuple_indices) { +invoke(RV, F f, void *, R & r, args_type & t, std::index_sequence) { return f(r, std::get(t)...); } @@ -51,7 +43,7 @@ invoke(RV, F f, void *, R & r, args_type & t, tuple_indices) { template typename enable_if::value, RV>::type inline -invoke(RV, F f, C *c, R & r, args_type & t, tuple_indices) { +invoke(RV, F f, C *c, R & r, args_type & t, std::index_sequence) { return (c->*f)(r, std::get(t)...); } @@ -88,10 +80,8 @@ struct marshalled_func_imp { // 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{}); // Marshall the response. m << r; // Make like a tree. diff --git a/rsm.cc b/rsm.cc index 711f0ad..c651e86 100644 --- 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(new config(_first, _me, this)); + cfg = std::make_unique(_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 --- 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 struct tuple_indices {}; -template struct make_indices_imp; -template struct make_indices_imp, E> { - typedef typename make_indices_imp, E>::type type; -}; -template struct make_indices_imp, E> { - typedef tuple_indices type; -}; -template struct make_tuple_indices { - typedef typename make_indices_imp, E>::type type; -}; - -#define TUPLE_INDICES(_ArgPack_) typename make_tuple_indices::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.