From 947913fc54325bea88dd52ea219ea2c69e334c97 Mon Sep 17 00:00:00 2001 From: whythat Date: Mon, 18 Jul 2016 12:23:42 +0300 Subject: [PATCH 1/3] use std::map::erase(const_iterator, const_iterator) to get non-constant iterator --- src/limitedmap.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/limitedmap.h b/src/limitedmap.h index 4d9bb4fa21..7841d7f4a4 100644 --- a/src/limitedmap.h +++ b/src/limitedmap.h @@ -66,8 +66,11 @@ public: } void update(const_iterator itIn, const mapped_type& v) { - // TODO: When we switch to C++11, use map.erase(itIn, itIn) to get the non-const iterator. - iterator itTarget = map.find(itIn->first); + // Using map::erase() with empty range instead of map::find() to get a non-const iterator, + // since it is a constant time operation in C++11. For more details, see + // https://stackoverflow.com/questions/765148/how-to-remove-constness-of-const-iterator + iterator itTarget = map.erase(itIn, itIn); + if (itTarget == map.end()) return; std::pair itPair = rmap.equal_range(itTarget->second); From 5e187e70012c247b96783f7fa9bcbd0289504ad5 Mon Sep 17 00:00:00 2001 From: whythat Date: Mon, 18 Jul 2016 12:26:21 +0300 Subject: [PATCH 2/3] use c++11 std::unique_ptr instead of boost::shared_ptr --- src/rpc/server.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 23149baa6d..4b594f5fa6 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -25,6 +25,8 @@ #include #include // for to_upper() +#include // for unique_ptr + using namespace RPCServer; using namespace std; @@ -34,9 +36,8 @@ static std::string rpcWarmupStatus("RPC server started"); static CCriticalSection cs_rpcWarmup; /* Timer-creating functions */ static RPCTimerInterface* timerInterface = NULL; -/* Map of name to timer. - * @note Can be changed to std::unique_ptr when C++11 */ -static std::map > deadlineTimers; +/* Map of name to timer. */ +static std::map > deadlineTimers; static struct CRPCSignals { @@ -490,7 +491,7 @@ void RPCRunLater(const std::string& name, boost::function func, int6 throw JSONRPCError(RPC_INTERNAL_ERROR, "No timer handler registered for RPC"); deadlineTimers.erase(name); LogPrint("rpc", "queue run of timer %s in %i seconds (using %s)\n", name, nSeconds, timerInterface->Name()); - deadlineTimers.insert(std::make_pair(name, boost::shared_ptr(timerInterface->NewTimer(func, nSeconds*1000)))); + deadlineTimers.insert(std::make_pair(name, std::unique_ptr(timerInterface->NewTimer(func, nSeconds*1000)))); } CRPCTable tableRPC; From c78408607536bf030947f69f6dc6e09e07873c84 Mon Sep 17 00:00:00 2001 From: whythat Date: Tue, 19 Jul 2016 02:15:59 +0300 Subject: [PATCH 3/3] use std::map::emplace() instead of std::map::insert() --- src/rpc/server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 4b594f5fa6..5fb97f7496 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -491,7 +491,7 @@ void RPCRunLater(const std::string& name, boost::function func, int6 throw JSONRPCError(RPC_INTERNAL_ERROR, "No timer handler registered for RPC"); deadlineTimers.erase(name); LogPrint("rpc", "queue run of timer %s in %i seconds (using %s)\n", name, nSeconds, timerInterface->Name()); - deadlineTimers.insert(std::make_pair(name, std::unique_ptr(timerInterface->NewTimer(func, nSeconds*1000)))); + deadlineTimers.emplace(name, std::unique_ptr(timerInterface->NewTimer(func, nSeconds*1000))); } CRPCTable tableRPC;