Reduce usage of std::bind()
C++14 lambdas are easier to read, easier to debug,
and can usually be better optimized by the compiler.
Change-Id: I294f275904f91942a8de946fe63e77078a7608a6
diff --git a/daemon/rib/fib-updater.cpp b/daemon/rib/fib-updater.cpp
index 6e600e4..5208e61 100644
--- a/daemon/rib/fib-updater.cpp
+++ b/daemon/rib/fib-updater.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2019, Regents of the University of California,
+ * Copyright (c) 2014-2021, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -95,13 +95,13 @@
const Name& prefix = update.getName();
const Route& route = update.getRoute();
- Rib::const_iterator it = m_rib.find(prefix);
+ auto it = m_rib.find(prefix);
// Name prefix exists
if (it != m_rib.end()) {
shared_ptr<const RibEntry> entry(it->second);
- RibEntry::const_iterator existingRoute = entry->findRoute(route);
+ auto existingRoute = entry->findRoute(route);
// Route will be new
if (existingRoute == entry->end()) {
@@ -114,8 +114,7 @@
// Route already exists
RibEntry entryCopy = *entry;
- Route& routeToUpdate = *(entryCopy.findRoute(route));
-
+ Route& routeToUpdate = *entryCopy.findRoute(route);
routeToUpdate.flags = route.flags;
routeToUpdate.cost = route.cost;
routeToUpdate.expires = route.expires;
@@ -149,16 +148,14 @@
const Name& prefix = update.getName();
const Route& route = update.getRoute();
- Rib::const_iterator ribIt = m_rib.find(prefix);
+ auto ribIt = m_rib.find(prefix);
// Name prefix exists
if (ribIt != m_rib.end()) {
shared_ptr<const RibEntry> entry(ribIt->second);
-
const bool hadCapture = entry->hasCapture();
- RibEntry::const_iterator existing = entry->findRoute(route);
-
+ auto existing = entry->findRoute(route);
if (existing != entry->end()) {
RibEntry temp = *entry;
@@ -240,8 +237,8 @@
.setName(update.name)
.setFaceId(update.faceId)
.setCost(update.cost),
- bind(&FibUpdater::onUpdateSuccess, this, update, onSuccess, onFailure),
- bind(&FibUpdater::onUpdateError, this, update, onSuccess, onFailure, _1, nTimeouts));
+ [=] (const auto&) { onUpdateSuccess(update, onSuccess, onFailure); },
+ [=] (const auto& resp) { onUpdateError(update, onSuccess, onFailure, resp, nTimeouts); });
}
void
@@ -254,12 +251,12 @@
ControlParameters()
.setName(update.name)
.setFaceId(update.faceId),
- bind(&FibUpdater::onUpdateSuccess, this, update, onSuccess, onFailure),
- bind(&FibUpdater::onUpdateError, this, update, onSuccess, onFailure, _1, nTimeouts));
+ [=] (const auto&) { onUpdateSuccess(update, onSuccess, onFailure); },
+ [=] (const auto& resp) { onUpdateError(update, onSuccess, onFailure, resp, nTimeouts); });
}
void
-FibUpdater::onUpdateSuccess(const FibUpdate update,
+FibUpdater::onUpdateSuccess(const FibUpdate& update,
const FibUpdateSuccessCallback& onSuccess,
const FibUpdateFailureCallback& onFailure)
{
@@ -280,7 +277,7 @@
}
void
-FibUpdater::onUpdateError(const FibUpdate update,
+FibUpdater::onUpdateError(const FibUpdate& update,
const FibUpdateSuccessCallback& onSuccess,
const FibUpdateFailureCallback& onFailure,
const ndn::nfd::ControlResponse& response, uint32_t nTimeouts)
@@ -310,14 +307,13 @@
}
void
-FibUpdater::addFibUpdate(FibUpdate update)
+FibUpdater::addFibUpdate(const FibUpdate& update)
{
FibUpdateList& updates = (update.faceId == m_batchFaceId) ? m_updatesForBatchFaceId :
m_updatesForNonBatchFaceId;
- // If an update with the same name and route already exists,
- // replace it
- FibUpdateList::iterator it = std::find_if(updates.begin(), updates.end(),
+ // If an update with the same name and route already exists, replace it
+ auto it = std::find_if(updates.begin(), updates.end(),
[&update] (const FibUpdate& other) {
return update.name == other.name && update.faceId == other.faceId;
});
@@ -400,7 +396,7 @@
// If there is an ancestor route which is the same as the new route, replace it
// with the new route
- Rib::RouteSet::iterator it = ancestorRoutes.find(route);
+ auto it = ancestorRoutes.find(route);
// There is a route that needs to be overwritten, erase and then replace
if (it != ancestorRoutes.end()) {
@@ -525,7 +521,7 @@
else {
// Look for an ancestor that was blocked previously
const Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
- Rib::RouteSet::iterator it = ancestorRoutes.find(route);
+ auto it = ancestorRoutes.find(route);
// If an ancestor is found, add it to children
if (it != ancestorRoutes.end()) {
@@ -616,7 +612,7 @@
if (!entry.hasCapture() && entry.getNRoutes() != 0) {
// If there is an ancestor route which is the same as the erased route, add that route
// to the current entry
- Rib::RouteSet::iterator it = ancestorRoutes.find(route);
+ auto it = ancestorRoutes.find(route);
if (it != ancestorRoutes.end()) {
addInheritedRoute(entry.getName(), *it);
diff --git a/daemon/rib/fib-updater.hpp b/daemon/rib/fib-updater.hpp
index 52f3d74..d43f50a 100644
--- a/daemon/rib/fib-updater.hpp
+++ b/daemon/rib/fib-updater.hpp
@@ -149,7 +149,7 @@
* the FIB update process is considered a success.
*/
void
- onUpdateSuccess(const FibUpdate update,
+ onUpdateSuccess(const FibUpdate& update,
const FibUpdateSuccessCallback& onSuccess,
const FibUpdateFailureCallback& onFailure);
@@ -169,7 +169,7 @@
* Otherwise, a non-recoverable error has occurred and an exception is thrown.
*/
void
- onUpdateError(const FibUpdate update,
+ onUpdateError(const FibUpdate& update,
const FibUpdateSuccessCallback& onSuccess,
const FibUpdateFailureCallback& onFailure,
const ndn::nfd::ControlResponse& response, uint32_t nTimeouts);
@@ -183,7 +183,7 @@
* Otherwise, the update is added to m_updatesForBatchNonFaceId.
*/
void
- addFibUpdate(const FibUpdate update);
+ addFibUpdate(const FibUpdate& update);
/** \brief creates records of the passed routes added to the entry and creates FIB updates
*/
diff --git a/daemon/rib/readvertise/nfd-rib-readvertise-destination.cpp b/daemon/rib/readvertise/nfd-rib-readvertise-destination.cpp
index a76aaed..0592040 100644
--- a/daemon/rib/readvertise/nfd-rib-readvertise-destination.cpp
+++ b/daemon/rib/readvertise/nfd-rib-readvertise-destination.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2019, Regents of the University of California,
+ * Copyright (c) 2014-2021, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -44,10 +44,17 @@
, m_commandOptions(options)
, m_controlParameters(parameters)
{
- m_ribInsertConn = rib.afterInsertEntry.connect(
- std::bind(&NfdRibReadvertiseDestination::handleRibInsert, this, _1));
- m_ribEraseConn = rib.afterEraseEntry.connect(
- std::bind(&NfdRibReadvertiseDestination::handleRibErase, this, _1));
+ m_ribInsertConn = rib.afterInsertEntry.connect([this] (const Name& name) {
+ if (name.isPrefixOf(m_commandOptions.getPrefix())) {
+ setAvailability(true);
+ }
+ });
+
+ m_ribEraseConn = rib.afterEraseEntry.connect([this] (const Name& name) {
+ if (name.isPrefixOf(m_commandOptions.getPrefix())) {
+ setAvailability(false);
+ }
+ });
}
void
@@ -59,7 +66,7 @@
m_controller.start<ndn::nfd::RibRegisterCommand>(
getControlParameters().setName(rr.prefix),
- [=] (const ControlParameters& cp) { successCb(); },
+ [=] (const ControlParameters&) { successCb(); },
[=] (const ControlResponse& cr) { failureCb(cr.getText()); },
getCommandOptions().setSigningInfo(rr.signer));
}
@@ -73,38 +80,10 @@
m_controller.start<ndn::nfd::RibUnregisterCommand>(
getControlParameters().setName(rr.prefix),
- [=] (const ControlParameters& cp) { successCb(); },
+ [=] (const ControlParameters&) { successCb(); },
[=] (const ControlResponse& cr) { failureCb(cr.getText()); },
getCommandOptions().setSigningInfo(rr.signer));
}
-ndn::nfd::ControlParameters
-NfdRibReadvertiseDestination::getControlParameters()
-{
- return m_controlParameters;
-}
-
-ndn::nfd::CommandOptions
-NfdRibReadvertiseDestination::getCommandOptions()
-{
- return m_commandOptions;
-}
-
-void
-NfdRibReadvertiseDestination::handleRibInsert(const ndn::Name& name)
-{
- if (name.isPrefixOf(m_commandOptions.getPrefix())) {
- setAvailability(true);
- }
-}
-
-void
-NfdRibReadvertiseDestination::handleRibErase(const ndn::Name& name)
-{
- if (name.isPrefixOf(m_commandOptions.getPrefix())) {
- setAvailability(false);
- }
-}
-
} // namespace rib
} // namespace nfd
diff --git a/daemon/rib/readvertise/nfd-rib-readvertise-destination.hpp b/daemon/rib/readvertise/nfd-rib-readvertise-destination.hpp
index 9c5bce6..0f61669 100644
--- a/daemon/rib/readvertise/nfd-rib-readvertise-destination.hpp
+++ b/daemon/rib/readvertise/nfd-rib-readvertise-destination.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2019, Regents of the University of California,
+ * Copyright (c) 2014-2021, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -45,8 +45,7 @@
Rib& rib,
const ndn::nfd::CommandOptions& options = ndn::nfd::CommandOptions(),
const ndn::nfd::ControlParameters& parameters =
- ndn::nfd::ControlParameters()
- .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT));
+ ndn::nfd::ControlParameters().setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT));
/** \brief add a name prefix into NFD RIB
*/
@@ -64,17 +63,16 @@
protected:
ndn::nfd::ControlParameters
- getControlParameters();
+ getControlParameters() const
+ {
+ return m_controlParameters;
+ }
ndn::nfd::CommandOptions
- getCommandOptions();
-
-private:
- void
- handleRibInsert(const Name& name);
-
- void
- handleRibErase(const Name& name);
+ getCommandOptions() const
+ {
+ return m_commandOptions;
+ }
private:
ndn::nfd::Controller& m_controller;
diff --git a/daemon/rib/rib-entry.cpp b/daemon/rib/rib-entry.cpp
index 7385d16..6acae09 100644
--- a/daemon/rib/rib-entry.cpp
+++ b/daemon/rib/rib-entry.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2019, Regents of the University of California,
+ * Copyright (c) 2014-2021, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -33,22 +33,30 @@
NFD_LOG_INIT(RibEntry);
+static bool
+compareFaceIdAndOrigin(const Route& lhs, const Route& rhs)
+{
+ return lhs.faceId == rhs.faceId && lhs.origin == rhs.origin;
+}
+
RibEntry::RouteList::iterator
RibEntry::findRoute(const Route& route)
{
- return std::find_if(begin(), end(), bind(&compareFaceIdAndOrigin, _1, route));
+ return std::find_if(begin(), end(),
+ [&] (const auto& r) { return compareFaceIdAndOrigin(r, route); });
}
RibEntry::RouteList::const_iterator
RibEntry::findRoute(const Route& route) const
{
- return std::find_if(begin(), end(), bind(&compareFaceIdAndOrigin, _1, route));
+ return std::find_if(begin(), end(),
+ [&] (const auto& r) { return compareFaceIdAndOrigin(r, route); });
}
std::pair<RibEntry::iterator, bool>
RibEntry::insertRoute(const Route& route)
{
- iterator it = findRoute(route);
+ auto it = findRoute(route);
if (it == end()) {
if (route.flags & ndn::nfd::ROUTE_FLAG_CAPTURE) {
@@ -65,23 +73,21 @@
void
RibEntry::eraseRoute(const Route& route)
{
- RibEntry::iterator it = findRoute(route);
+ auto it = findRoute(route);
eraseRoute(it);
}
bool
RibEntry::hasRoute(const Route& route)
{
- RibEntry::const_iterator it = findRoute(route);
-
+ auto it = findRoute(route);
return it != end();
}
bool
-RibEntry::hasFaceId(const uint64_t faceId) const
+RibEntry::hasFaceId(uint64_t faceId) const
{
- RibEntry::const_iterator it = std::find_if(begin(), end(), bind(&compareFaceId, _1, faceId));
-
+ auto it = std::find_if(begin(), end(), [faceId] (const auto& r) { return r.faceId == faceId; });
return it != end();
}
@@ -134,14 +140,14 @@
void
RibEntry::removeInheritedRoute(const Route& route)
{
- m_inheritedRoutes.remove_if(bind(&compareFaceId, _1, route.faceId));
+ m_inheritedRoutes.remove_if([id = route.faceId] (const auto& r) { return r.faceId == id; });
}
RibEntry::RouteList::const_iterator
RibEntry::findInheritedRoute(const Route& route) const
{
return std::find_if(m_inheritedRoutes.begin(), m_inheritedRoutes.end(),
- bind(&compareFaceId, _1, route.faceId));
+ [id = route.faceId] (const auto& r) { return r.faceId == id; });
}
bool
@@ -243,7 +249,7 @@
time::milliseconds maxExpiration) const
{
const Route* bestAnnRoute = nullptr;
- auto entryExpiry = time::steady_clock::TimePoint::min();
+ auto entryExpiry = time::steady_clock::time_point::min();
for (const Route& route : *this) {
if (route.expires) {
@@ -255,7 +261,7 @@
}
}
else {
- entryExpiry = time::steady_clock::TimePoint::max();
+ entryExpiry = time::steady_clock::time_point::max();
}
}
diff --git a/daemon/rib/rib-entry.hpp b/daemon/rib/rib-entry.hpp
index 45d5a87..03cfc8c 100644
--- a/daemon/rib/rib-entry.hpp
+++ b/daemon/rib/rib-entry.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2019, Regents of the University of California,
+ * Copyright (c) 2014-2021, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -94,7 +94,7 @@
eraseRoute(RouteList::iterator route);
bool
- hasFaceId(const uint64_t faceId) const;
+ hasFaceId(uint64_t faceId) const;
const RouteList&
getRoutes() const;
diff --git a/daemon/rib/rib.cpp b/daemon/rib/rib.cpp
index 2d1db7f..a81e77a 100644
--- a/daemon/rib/rib.cpp
+++ b/daemon/rib/rib.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2019, Regents of the University of California,
+ * Copyright (c) 2014-2021, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -426,8 +426,8 @@
// Until task #1698, each RibUpdateBatch contains exactly one RIB update
BOOST_ASSERT(batch.size() == 1);
- auto fibSuccessCb = bind(&Rib::onFibUpdateSuccess, this, batch, _1, item.managerSuccessCallback);
- auto fibFailureCb = bind(&Rib::onFibUpdateFailure, this, item.managerFailureCallback, _1, _2);
+ auto fibSuccessCb = std::bind(&Rib::onFibUpdateSuccess, this, batch, _1, item.managerSuccessCallback);
+ auto fibFailureCb = std::bind(&Rib::onFibUpdateFailure, this, item.managerFailureCallback, _1, _2);
m_fibUpdater->computeAndSendFibUpdates(batch, fibSuccessCb, fibFailureCb);
}
diff --git a/daemon/rib/route.cpp b/daemon/rib/route.cpp
index 04999bb..99d80e5 100644
--- a/daemon/rib/route.cpp
+++ b/daemon/rib/route.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2018, Regents of the University of California,
+ * Copyright (c) 2014-2021, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -31,21 +31,21 @@
const uint64_t PA_ROUTE_COST = 2048; ///< cost of route created by prefix announcement
-static time::steady_clock::TimePoint
+static time::steady_clock::time_point
computeExpiration(const ndn::PrefixAnnouncement& ann)
{
- time::steady_clock::Duration validityEnd = time::steady_clock::Duration::max();
+ auto validityEnd = time::steady_clock::duration::max();
if (ann.getValidityPeriod()) {
auto now = time::system_clock::now();
if (!ann.getValidityPeriod()->isValid(now)) {
- validityEnd = time::steady_clock::Duration::zero();
+ validityEnd = time::steady_clock::duration::zero();
}
else {
validityEnd = ann.getValidityPeriod()->getPeriod().second - now;
}
}
return time::steady_clock::now() +
- std::min(validityEnd, time::duration_cast<time::steady_clock::Duration>(ann.getExpiration()));
+ std::min(validityEnd, time::duration_cast<time::steady_clock::duration>(ann.getExpiration()));
}
Route::Route(const ndn::PrefixAnnouncement& ann, uint64_t faceId)
diff --git a/daemon/rib/route.hpp b/daemon/rib/route.hpp
index 92df470..18df250 100644
--- a/daemon/rib/route.hpp
+++ b/daemon/rib/route.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2019, Regents of the University of California,
+ * Copyright (c) 2014-2021, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -82,7 +82,7 @@
ndn::nfd::RouteOrigin origin = ndn::nfd::ROUTE_ORIGIN_APP;
uint64_t cost = 0;
std::underlying_type_t<ndn::nfd::RouteFlags> flags = ndn::nfd::ROUTE_FLAGS_NONE;
- optional<time::steady_clock::TimePoint> expires;
+ optional<time::steady_clock::time_point> expires;
/** \brief The prefix announcement that caused the creation of this route.
*
@@ -98,7 +98,7 @@
* not yet valid or has expired. In this case, the exact value of this field does not matter.
* If this field is after the current time, it indicates when the prefix announcement expires.
*/
- time::steady_clock::TimePoint annExpires;
+ time::steady_clock::time_point annExpires;
private:
scheduler::EventId m_expirationEvent;
@@ -113,18 +113,6 @@
return !(lhs == rhs);
}
-inline bool
-compareFaceIdAndOrigin(const Route& lhs, const Route& rhs)
-{
- return (lhs.faceId == rhs.faceId && lhs.origin == rhs.origin);
-}
-
-inline bool
-compareFaceId(const Route& route, const uint64_t faceId)
-{
- return (route.faceId == faceId);
-}
-
std::ostream&
operator<<(std::ostream& os, const Route& route);
diff --git a/daemon/rib/service.cpp b/daemon/rib/service.cpp
index 596fa27..0c674b3 100644
--- a/daemon/rib/service.cpp
+++ b/daemon/rib/service.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2020, Regents of the University of California,
+ * Copyright (c) 2014-2021, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -45,7 +45,7 @@
Service* Service::s_instance = nullptr;
-const std::string CFG_SECTION = "rib";
+const std::string CFG_RIB = "rib";
const std::string CFG_LOCALHOST_SECURITY = "localhost_security";
const std::string CFG_LOCALHOP_SECURITY = "localhop_security";
const std::string CFG_PA_VALIDATION = "prefix_announcement_validation";
@@ -126,7 +126,9 @@
s_instance = this;
ConfigFile config(ConfigFile::ignoreUnknownSection);
- config.addSectionHandler(CFG_SECTION, bind(&Service::processConfig, this, _1, _2, _3));
+ config.addSectionHandler(CFG_RIB, [this] (auto&&... args) {
+ processConfig(std::forward<decltype(args)>(args)...);
+ });
configParse(config, true);
configParse(config, false);
@@ -185,10 +187,10 @@
// AutoPrefixPropagator does not support config dry-run
}
else if (key == CFG_READVERTISE_NLSR) {
- ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
+ ConfigFile::parseYesNo(item, CFG_RIB + "." + CFG_READVERTISE_NLSR);
}
else {
- NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
+ NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_RIB + "." + key));
}
}
@@ -239,10 +241,10 @@
}
}
else if (key == CFG_READVERTISE_NLSR) {
- wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_SECTION + "." + CFG_READVERTISE_NLSR);
+ wantReadvertiseNlsr = ConfigFile::parseYesNo(item, CFG_RIB + "." + CFG_READVERTISE_NLSR);
}
else {
- NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_SECTION + "." + key));
+ NDN_THROW(ConfigFile::Error("Unrecognized option " + CFG_RIB + "." + key));
}
}