Improve and simplify code with modern C++ features
Change-Id: I83bf5513c2a1f90ba5a59e93c473306864b27d94
diff --git a/daemon/fw/access-strategy.cpp b/daemon/fw/access-strategy.cpp
index a766424..8460a00 100644
--- a/daemon/fw/access-strategy.cpp
+++ b/daemon/fw/access-strategy.cpp
@@ -35,8 +35,7 @@
AccessStrategy::AccessStrategy(Forwarder& forwarder, const Name& name)
: Strategy(forwarder)
- , m_removeFaceInfoConn(this->beforeRemoveFace.connect(
- bind(&AccessStrategy::removeFaceInfo, this, _1)))
+ , m_removeFaceInfoConn(beforeRemoveFace.connect([this] (const Face& face) { removeFaceInfo(face); }))
{
ParsedInstanceName parsed = parseInstanceName(name);
if (!parsed.parameters.empty()) {
@@ -177,14 +176,13 @@
return;
}
- pit::InRecordCollection::iterator inRecord = pitEntry->getInRecord(*inFace);
+ auto inRecord = pitEntry->getInRecord(*inFace);
BOOST_ASSERT(inRecord != pitEntry->in_end());
// in-record is erased only if Interest is satisfied, and RTO timer should have been cancelled
// note: if this strategy is extended to send Nacks, that would also erase in-record,
// and RTO timer should be cancelled in that case as well
const Interest& interest = inRecord->getInterest();
-
const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
NFD_LOG_DEBUG(pitEntry->getInterest() << " timeoutFrom " << firstOutFaceId <<
@@ -227,14 +225,14 @@
return;
}
- pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(inFace);
+ auto outRecord = pitEntry->getOutRecord(inFace);
if (outRecord == pitEntry->out_end()) { // no out-record
NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
" no-out-record");
return;
}
- time::steady_clock::Duration rtt = time::steady_clock::now() - outRecord->getLastRenewed();
+ auto rtt = time::steady_clock::now() - outRecord->getLastRenewed();
NFD_LOG_DEBUG(pitEntry->getInterest() << " dataFrom " << inFace.getId() <<
" rtt=" << time::duration_cast<time::microseconds>(rtt).count());
this->updateMeasurements(inFace, data, time::duration_cast<RttEstimator::Duration>(rtt));
@@ -260,7 +258,7 @@
AccessStrategy::MtInfo::MtInfo()
: lastNexthop(face::INVALID_FACEID)
- , rtt(1, time::milliseconds(1), 0.1)
+ , rtt(1, 1_ms, 0.1)
{
}
@@ -292,14 +290,13 @@
BOOST_ASSERT(me != nullptr);
}
- static const time::nanoseconds ME_LIFETIME = time::seconds(8);
- this->getMeasurements().extendLifetime(*me, ME_LIFETIME);
+ this->getMeasurements().extendLifetime(*me, 8_s);
return me->insertStrategyInfo<MtInfo>().first;
}
AccessStrategy::FaceInfo::FaceInfo()
- : rtt(1, time::milliseconds(1), 0.1)
+ : rtt(1, 1_ms, 0.1)
{
}
diff --git a/daemon/fw/asf-measurements.cpp b/daemon/fw/asf-measurements.cpp
index 0d772ac..f743e06 100644
--- a/daemon/fw/asf-measurements.cpp
+++ b/daemon/fw/asf-measurements.cpp
@@ -168,7 +168,7 @@
FaceInfo* info = nullptr;
if (it == m_fit.end()) {
- const auto& pair = m_fit.insert(std::make_pair(faceId, FaceInfo()));
+ const auto& pair = m_fit.emplace(faceId, FaceInfo());
info = &pair.first->second;
extendFaceInfoLifetime(*info, faceId);
@@ -193,9 +193,7 @@
scheduler::cancel(info.getMeasurementExpirationEventId());
// Refresh measurement
- scheduler::EventId id = scheduler::schedule(AsfMeasurements::MEASUREMENTS_LIFETIME,
- bind(&NamespaceInfo::expireFaceInfo, this, faceId));
-
+ auto id = scheduler::schedule(AsfMeasurements::MEASUREMENTS_LIFETIME, [=] { expireFaceInfo(faceId); });
info.setMeasurementExpirationEventId(id);
}
diff --git a/daemon/fw/asf-measurements.hpp b/daemon/fw/asf-measurements.hpp
index ee5885d..b5b1f8e 100644
--- a/daemon/fw/asf-measurements.hpp
+++ b/daemon/fw/asf-measurements.hpp
@@ -254,8 +254,7 @@
const FaceInfoTable::iterator
insert(FaceId faceId)
{
- const auto& pair = m_fit.insert(std::make_pair(faceId, FaceInfo()));
- return pair.first;
+ return m_fit.emplace(faceId, FaceInfo()).first;
}
bool
@@ -317,7 +316,7 @@
extendLifetime(measurements::Entry& me);
public:
- static constexpr time::microseconds MEASUREMENTS_LIFETIME = time::seconds(300);
+ static constexpr time::microseconds MEASUREMENTS_LIFETIME = 300_s;
private:
MeasurementsAccessor& m_measurements;
diff --git a/daemon/fw/asf-probing-module.cpp b/daemon/fw/asf-probing-module.cpp
index 24b4577..63f9867 100644
--- a/daemon/fw/asf-probing-module.cpp
+++ b/daemon/fw/asf-probing-module.cpp
@@ -70,7 +70,7 @@
const Face& faceUsed)
{
FaceInfoFacePairSet rankedFaces(
- [] (FaceInfoFacePair pairLhs, FaceInfoFacePair pairRhs) -> bool {
+ [] (const auto& pairLhs, const auto& pairRhs) -> bool {
// Sort by RTT
// If a face has timed-out, rank it behind non-timed-out faces
FaceInfo& lhs = *pairLhs.first;
@@ -93,14 +93,13 @@
}
FaceInfo* info = m_measurements.getFaceInfo(fibEntry, interest, hopFace.getId());
-
// If no RTT has been recorded, probe this face
if (info == nullptr || !info->hasSrttMeasurement()) {
return &hopFace;
}
// Add FaceInfo to container sorted by RTT
- rankedFaces.insert(std::make_pair(info, &hopFace));
+ rankedFaces.insert({info, &hopFace});
}
if (rankedFaces.empty()) {
@@ -149,7 +148,7 @@
uint64_t rank = 1;
double offset = 0.0;
- for (const FaceInfoFacePair pair : rankedFaces) {
+ for (const auto& pair : rankedFaces) {
double probability = getProbingProbability(rank++, rankSum, rankedFaces.size());
// Is the random number within the bounds of this face's probability + the previous faces'
diff --git a/daemon/fw/asf-probing-module.hpp b/daemon/fw/asf-probing-module.hpp
index 785abd8..02ef155 100644
--- a/daemon/fw/asf-probing-module.hpp
+++ b/daemon/fw/asf-probing-module.hpp
@@ -80,8 +80,8 @@
getRandomNumber(double start, double end);
public:
- static constexpr time::milliseconds DEFAULT_PROBING_INTERVAL = time::milliseconds(60000);
- static constexpr time::milliseconds MIN_PROBING_INTERVAL = time::milliseconds(1000);
+ static constexpr time::milliseconds DEFAULT_PROBING_INTERVAL = 1_min;
+ static constexpr time::milliseconds MIN_PROBING_INTERVAL = 1_s;
private:
time::milliseconds m_probingInterval;
diff --git a/daemon/fw/best-route-strategy.cpp b/daemon/fw/best-route-strategy.cpp
index 69cbec7..e4462e4 100644
--- a/daemon/fw/best-route-strategy.cpp
+++ b/daemon/fw/best-route-strategy.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2017, Regents of the University of California,
+ * Copyright (c) 2014-2018, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -44,10 +44,8 @@
}
const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
- const fib::NextHopList& nexthops = fibEntry.getNextHops();
-
- for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
- Face& outFace = it->getFace();
+ for (const auto& nexthop : fibEntry.getNextHops()) {
+ Face& outFace = nexthop.getFace();
if (!wouldViolateScope(inFace, interest, outFace) &&
canForwardToLegacy(*pitEntry, outFace)) {
this->sendInterest(pitEntry, outFace, interest);
diff --git a/daemon/fw/best-route-strategy2.cpp b/daemon/fw/best-route-strategy2.cpp
index 2956ed7..0ab6b25 100644
--- a/daemon/fw/best-route-strategy2.cpp
+++ b/daemon/fw/best-route-strategy2.cpp
@@ -69,7 +69,7 @@
* \param wantUnused if true, NextHop must not have unexpired out-record
* \param now time::steady_clock::now(), ignored if !wantUnused
*/
-static inline bool
+static bool
isNextHopEligible(const Face& inFace, const Interest& interest,
const fib::NextHop& nexthop,
const shared_ptr<pit::Entry>& pitEntry,
@@ -88,7 +88,7 @@
if (wantUnused) {
// nexthop must not have unexpired out-record
- pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(outFace);
+ auto outRecord = pitEntry->getOutRecord(outFace);
if (outRecord != pitEntry->out_end() && outRecord->getExpiry() > now) {
return false;
}
@@ -100,23 +100,26 @@
/** \brief pick an eligible NextHop with earliest out-record
* \note It is assumed that every nexthop has an out-record.
*/
-static inline fib::NextHopList::const_iterator
+static fib::NextHopList::const_iterator
findEligibleNextHopWithEarliestOutRecord(const Face& inFace, const Interest& interest,
const fib::NextHopList& nexthops,
const shared_ptr<pit::Entry>& pitEntry)
{
- fib::NextHopList::const_iterator found = nexthops.end();
- time::steady_clock::TimePoint earliestRenewed = time::steady_clock::TimePoint::max();
- for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
+ auto found = nexthops.end();
+ auto earliestRenewed = time::steady_clock::TimePoint::max();
+
+ for (auto it = nexthops.begin(); it != nexthops.end(); ++it) {
if (!isNextHopEligible(inFace, interest, *it, pitEntry))
continue;
- pit::OutRecordCollection::iterator outRecord = pitEntry->getOutRecord(it->getFace());
+
+ auto outRecord = pitEntry->getOutRecord(it->getFace());
BOOST_ASSERT(outRecord != pitEntry->out_end());
if (outRecord->getLastRenewed() < earliestRenewed) {
found = it;
earliestRenewed = outRecord->getLastRenewed();
}
}
+
return found;
}
@@ -133,13 +136,13 @@
const fib::Entry& fibEntry = this->lookupFib(*pitEntry);
const fib::NextHopList& nexthops = fibEntry.getNextHops();
- fib::NextHopList::const_iterator it = nexthops.end();
+ auto it = nexthops.end();
if (suppression == RetxSuppressionResult::NEW) {
// forward to nexthop with lowest cost except downstream
- it = std::find_if(nexthops.begin(), nexthops.end(),
- bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry,
- false, time::steady_clock::TimePoint::min()));
+ it = std::find_if(nexthops.begin(), nexthops.end(), [&] (const auto& nexthop) {
+ return isNextHopEligible(inFace, interest, nexthop, pitEntry);
+ });
if (it == nexthops.end()) {
NFD_LOG_DEBUG(interest << " from=" << inFace.getId() << " noNextHop");
@@ -160,9 +163,10 @@
}
// find an unused upstream with lowest cost except downstream
- it = std::find_if(nexthops.begin(), nexthops.end(),
- bind(&isNextHopEligible, cref(inFace), interest, _1, pitEntry,
- true, time::steady_clock::now()));
+ it = std::find_if(nexthops.begin(), nexthops.end(), [&] (const auto& nexthop) {
+ return isNextHopEligible(inFace, interest, nexthop, pitEntry, true, time::steady_clock::now());
+ });
+
if (it != nexthops.end()) {
Face& outFace = it->getFace();
this->sendInterest(pitEntry, outFace, interest);
diff --git a/daemon/fw/face-table.cpp b/daemon/fw/face-table.cpp
index 3e19dd1..7e1880f 100644
--- a/daemon/fw/face-table.cpp
+++ b/daemon/fw/face-table.cpp
@@ -86,7 +86,7 @@
" remote=" << face->getRemoteUri() <<
" local=" << face->getLocalUri());
- connectFaceClosedSignal(*face, bind(&FaceTable::remove, this, faceId));
+ connectFaceClosedSignal(*face, [=] { remove(faceId); });
this->afterAdd(*face);
}
diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp
index accfb38..7e28db2 100644
--- a/daemon/fw/forwarder.cpp
+++ b/daemon/fw/forwarder.cpp
@@ -132,8 +132,8 @@
// is pending?
if (!pitEntry->hasInRecords()) {
m_cs.find(interest,
- bind(&Forwarder::onContentStoreHit, this, ref(inFace), pitEntry, _1, _2),
- bind(&Forwarder::onContentStoreMiss, this, ref(inFace), pitEntry, _1));
+ bind(&Forwarder::onContentStoreHit, this, std::ref(inFace), pitEntry, _1, _2),
+ bind(&Forwarder::onContentStoreMiss, this, std::ref(inFace), pitEntry, _1));
}
else {
this->onContentStoreMiss(inFace, pitEntry, interest);
@@ -511,15 +511,7 @@
scheduler::cancel(pitEntry->expiryTimer);
- pitEntry->expiryTimer = scheduler::schedule(duration,
- bind(&Forwarder::onInterestFinalize, this, pitEntry));
-}
-
-static inline void
-insertNonceToDnl(DeadNonceList& dnl, const pit::Entry& pitEntry,
- const pit::OutRecord& outRecord)
-{
- dnl.add(pitEntry.getName(), outRecord.getLastNonce());
+ pitEntry->expiryTimer = scheduler::schedule(duration, [=] { onInterestFinalize(pitEntry); });
}
void
@@ -540,13 +532,14 @@
// Dead Nonce List insert
if (upstream == nullptr) {
// insert all outgoing Nonces
- const pit::OutRecordCollection& outRecords = pitEntry.getOutRecords();
- std::for_each(outRecords.begin(), outRecords.end(),
- bind(&insertNonceToDnl, ref(m_deadNonceList), cref(pitEntry), _1));
+ const auto& outRecords = pitEntry.getOutRecords();
+ std::for_each(outRecords.begin(), outRecords.end(), [&] (const auto& outRecord) {
+ m_deadNonceList.add(pitEntry.getName(), outRecord.getLastNonce());
+ });
}
else {
// insert outgoing Nonce of a specific face
- pit::OutRecordCollection::iterator outRecord = pitEntry.getOutRecord(*upstream);
+ auto outRecord = pitEntry.getOutRecord(*upstream);
if (outRecord != pitEntry.getOutRecords().end()) {
m_deadNonceList.add(pitEntry.getName(), outRecord->getLastNonce());
}
diff --git a/daemon/fw/ncc-strategy.cpp b/daemon/fw/ncc-strategy.cpp
index bdb322a..0f7f4e1 100644
--- a/daemon/fw/ncc-strategy.cpp
+++ b/daemon/fw/ncc-strategy.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2017, Regents of the University of California,
+ * Copyright (c) 2014-2018, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -32,9 +32,9 @@
NFD_REGISTER_STRATEGY(NccStrategy);
-const time::microseconds NccStrategy::DEFER_FIRST_WITHOUT_BEST_FACE = time::microseconds(4000);
-const time::microseconds NccStrategy::DEFER_RANGE_WITHOUT_BEST_FACE = time::microseconds(75000);
-const time::nanoseconds NccStrategy::MEASUREMENTS_LIFETIME = time::seconds(16);
+const time::microseconds NccStrategy::DEFER_FIRST_WITHOUT_BEST_FACE = 4_ms;
+const time::microseconds NccStrategy::DEFER_RANGE_WITHOUT_BEST_FACE = 75_ms;
+const time::nanoseconds NccStrategy::MEASUREMENTS_LIFETIME = 16_s;
NccStrategy::NccStrategy(Forwarder& forwarder, const Name& name)
: Strategy(forwarder)
@@ -118,7 +118,7 @@
}
if (nUpstreams > 0) {
- pitEntryInfo->maxInterval = std::max(time::microseconds(1),
+ pitEntryInfo->maxInterval = std::max(1_us,
time::microseconds((2 * deferRange.count() + nUpstreams - 1) / nUpstreams));
}
else {
@@ -142,7 +142,7 @@
if (pitEntry == nullptr) {
return;
}
- pit::InRecordCollection::const_iterator inRecord = pitEntry->getInRecord(*inFace);
+ auto inRecord = pitEntry->getInRecord(*inFace);
if (inRecord == pitEntry->in_end()) {
return;
}
@@ -163,10 +163,9 @@
this->sendInterest(pitEntry, *previousFace, interest);
}
- const fib::NextHopList& nexthops = fibEntry.getNextHops();
bool isForwarded = false;
- for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
- Face& face = it->getFace();
+ for (const auto& nexthop : fibEntry.getNextHops()) {
+ Face& face = nexthop.getFace();
if (!wouldViolateScope(*inFace, interest, face) &&
canForwardToLegacy(*pitEntry, face)) {
isForwarded = true;
@@ -271,9 +270,9 @@
return *info;
}
-const time::microseconds NccStrategy::MeasurementsEntryInfo::INITIAL_PREDICTION = time::microseconds(8192);
-const time::microseconds NccStrategy::MeasurementsEntryInfo::MIN_PREDICTION = time::microseconds(127);
-const time::microseconds NccStrategy::MeasurementsEntryInfo::MAX_PREDICTION = time::milliseconds(160);
+const time::microseconds NccStrategy::MeasurementsEntryInfo::INITIAL_PREDICTION = 8192_us;
+const time::microseconds NccStrategy::MeasurementsEntryInfo::MIN_PREDICTION = 127_us;
+const time::microseconds NccStrategy::MeasurementsEntryInfo::MAX_PREDICTION = 160_ms;
NccStrategy::MeasurementsEntryInfo::MeasurementsEntryInfo()
: prediction(INITIAL_PREDICTION)
diff --git a/daemon/fw/retx-suppression-exponential.cpp b/daemon/fw/retx-suppression-exponential.cpp
index f2ff1b2..b110588 100644
--- a/daemon/fw/retx-suppression-exponential.cpp
+++ b/daemon/fw/retx-suppression-exponential.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2017, Regents of the University of California,
+ * Copyright (c) 2014-2018, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -28,11 +28,9 @@
namespace nfd {
namespace fw {
-const RetxSuppressionExponential::Duration RetxSuppressionExponential::DEFAULT_INITIAL_INTERVAL =
- time::milliseconds(1);
-const float RetxSuppressionExponential::DEFAULT_MULTIPLIER = 2.0;
-const RetxSuppressionExponential::Duration RetxSuppressionExponential::DEFAULT_MAX_INTERVAL =
- time::milliseconds(250);
+const RetxSuppressionExponential::Duration RetxSuppressionExponential::DEFAULT_INITIAL_INTERVAL = 1_ms;
+const RetxSuppressionExponential::Duration RetxSuppressionExponential::DEFAULT_MAX_INTERVAL = 250_ms;
+const float RetxSuppressionExponential::DEFAULT_MULTIPLIER = 2.0f;
class RetxSuppressionExponential::PitInfo : public StrategyInfo
{
@@ -63,8 +61,8 @@
, m_multiplier(multiplier)
, m_maxInterval(maxInterval)
{
- BOOST_ASSERT(initialInterval > time::milliseconds::zero());
- BOOST_ASSERT(multiplier >= 1.0);
+ BOOST_ASSERT(initialInterval > 0_us);
+ BOOST_ASSERT(multiplier >= 1.0f);
BOOST_ASSERT(maxInterval >= initialInterval);
}
@@ -76,9 +74,9 @@
return RetxSuppressionResult::NEW;
}
- time::steady_clock::TimePoint lastOutgoing = getLastOutgoing(pitEntry);
- time::steady_clock::TimePoint now = time::steady_clock::now();
- time::steady_clock::Duration sinceLastOutgoing = now - lastOutgoing;
+ auto lastOutgoing = getLastOutgoing(pitEntry);
+ auto now = time::steady_clock::now();
+ auto sinceLastOutgoing = now - lastOutgoing;
PitInfo* pi = pitEntry.insertStrategyInfo<PitInfo>(m_initialInterval).first;
bool shouldSuppress = sinceLastOutgoing < pi->suppressionInterval;
@@ -102,9 +100,9 @@
return RetxSuppressionResult::NEW;
}
- time::steady_clock::TimePoint lastOutgoing = outRecord->getLastRenewed();
- time::steady_clock::TimePoint now = time::steady_clock::now();
- time::steady_clock::Duration sinceLastOutgoing = now - lastOutgoing;
+ auto lastOutgoing = outRecord->getLastRenewed();
+ auto now = time::steady_clock::now();
+ auto sinceLastOutgoing = now - lastOutgoing;
// insertStrategyInfo does not insert m_initialInterval again if it already exists
PitInfo* pi = outRecord->insertStrategyInfo<PitInfo>(m_initialInterval).first;
diff --git a/daemon/fw/retx-suppression-fixed.cpp b/daemon/fw/retx-suppression-fixed.cpp
index 6245ae3..dddd742 100644
--- a/daemon/fw/retx-suppression-fixed.cpp
+++ b/daemon/fw/retx-suppression-fixed.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2017, Regents of the University of California,
+ * Copyright (c) 2014-2018, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -28,12 +28,12 @@
namespace nfd {
namespace fw {
-const time::milliseconds RetxSuppressionFixed::DEFAULT_MIN_RETX_INTERVAL(100);
+const time::milliseconds RetxSuppressionFixed::DEFAULT_MIN_RETX_INTERVAL = 100_ms;
RetxSuppressionFixed::RetxSuppressionFixed(const time::milliseconds& minRetxInterval)
: m_minRetxInterval(minRetxInterval)
{
- BOOST_ASSERT(minRetxInterval > time::milliseconds::zero());
+ BOOST_ASSERT(minRetxInterval > 0_ms);
}
RetxSuppressionResult