Skip to content

Commit 3859072

Browse files
committed
Merge #8353: Trivial: tiny c++11 refactors
c784086 use std::map::emplace() instead of std::map::insert() (whythat) 5e187e7 use c++11 std::unique_ptr instead of boost::shared_ptr (whythat) 947913f use std::map::erase(const_iterator, const_iterator) to get non-constant iterator (whythat)
2 parents efce84d + c784086 commit 3859072

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/limitedmap.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ class limitedmap
6666
}
6767
void update(const_iterator itIn, const mapped_type& v)
6868
{
69-
// TODO: When we switch to C++11, use map.erase(itIn, itIn) to get the non-const iterator.
70-
iterator itTarget = map.find(itIn->first);
69+
// Using map::erase() with empty range instead of map::find() to get a non-const iterator,
70+
// since it is a constant time operation in C++11. For more details, see
71+
// https://stackoverflow.com/questions/765148/how-to-remove-constness-of-const-iterator
72+
iterator itTarget = map.erase(itIn, itIn);
73+
7174
if (itTarget == map.end())
7275
return;
7376
std::pair<rmap_iterator, rmap_iterator> itPair = rmap.equal_range(itTarget->second);

src/rpc/server.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <boost/thread.hpp>
2626
#include <boost/algorithm/string/case_conv.hpp> // for to_upper()
2727

28+
#include <memory> // for unique_ptr
29+
2830
using namespace RPCServer;
2931
using namespace std;
3032

@@ -34,9 +36,8 @@ static std::string rpcWarmupStatus("RPC server started");
3436
static CCriticalSection cs_rpcWarmup;
3537
/* Timer-creating functions */
3638
static RPCTimerInterface* timerInterface = NULL;
37-
/* Map of name to timer.
38-
* @note Can be changed to std::unique_ptr when C++11 */
39-
static std::map<std::string, boost::shared_ptr<RPCTimerBase> > deadlineTimers;
39+
/* Map of name to timer. */
40+
static std::map<std::string, std::unique_ptr<RPCTimerBase> > deadlineTimers;
4041

4142
static struct CRPCSignals
4243
{
@@ -490,7 +491,7 @@ void RPCRunLater(const std::string& name, boost::function<void(void)> func, int6
490491
throw JSONRPCError(RPC_INTERNAL_ERROR, "No timer handler registered for RPC");
491492
deadlineTimers.erase(name);
492493
LogPrint("rpc", "queue run of timer %s in %i seconds (using %s)\n", name, nSeconds, timerInterface->Name());
493-
deadlineTimers.insert(std::make_pair(name, boost::shared_ptr<RPCTimerBase>(timerInterface->NewTimer(func, nSeconds*1000))));
494+
deadlineTimers.emplace(name, std::unique_ptr<RPCTimerBase>(timerInterface->NewTimer(func, nSeconds*1000)));
494495
}
495496

496497
CRPCTable tableRPC;

0 commit comments

Comments
 (0)