Use more C++17 features
Mainly structured bindings, inline variables, and class template
argument deduction, plus many more smaller things.
Change-Id: I810d17e0adb470426e4e30c898e03b3140ad052f
diff --git a/daemon/rib/readvertise/host-to-gateway-readvertise-policy.cpp b/daemon/rib/readvertise/host-to-gateway-readvertise-policy.cpp
index 89f11fd..5aa6e3b 100644
--- a/daemon/rib/readvertise/host-to-gateway-readvertise-policy.cpp
+++ b/daemon/rib/readvertise/host-to-gateway-readvertise-policy.cpp
@@ -33,8 +33,8 @@
namespace nfd {
namespace rib {
-static const name::Component IGNORE_COMPONENT("nrd");
-static const time::seconds DEFAULT_REFRESH_INTERVAL = 25_s;
+const name::Component IGNORE_COMPONENT("nrd");
+const time::seconds DEFAULT_REFRESH_INTERVAL = 25_s;
HostToGatewayReadvertisePolicy::HostToGatewayReadvertisePolicy(const ndn::KeyChain& keyChain,
const ConfigSection& section)
diff --git a/daemon/rib/readvertise/readvertise.cpp b/daemon/rib/readvertise/readvertise.cpp
index addad72..ded568d 100644
--- a/daemon/rib/readvertise/readvertise.cpp
+++ b/daemon/rib/readvertise/readvertise.cpp
@@ -34,8 +34,8 @@
NFD_LOG_INIT(Readvertise);
-const time::milliseconds Readvertise::RETRY_DELAY_MIN = 50_s;
-const time::milliseconds Readvertise::RETRY_DELAY_MAX = 1_h;
+constexpr time::milliseconds RETRY_DELAY_MIN = 50_s;
+constexpr time::milliseconds RETRY_DELAY_MAX = 1_h;
static time::milliseconds
randomizeTimer(time::milliseconds baseTimer)
@@ -74,20 +74,16 @@
return;
}
- ReadvertisedRouteContainer::iterator rrIt;
- bool isNew = false;
- std::tie(rrIt, isNew) = m_rrs.emplace(action->prefix);
-
- if (!isNew && rrIt->signer != action->signer) {
+ auto [rrIt, isNewRr] = m_rrs.emplace(action->prefix);
+ if (!isNewRr && rrIt->signer != action->signer) {
NFD_LOG_WARN("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
- ',' << ribRoute.route->origin << ") readvertising-as " << action->prefix <<
+ ',' << ribRoute.route->origin << ") readvertising-as " << action->prefix <<
" old-signer " << rrIt->signer << " new-signer " << action->signer);
}
rrIt->signer = action->signer;
- RouteRrIndex::iterator indexIt;
- std::tie(indexIt, isNew) = m_routeToRr.emplace(ribRoute, rrIt);
- BOOST_ASSERT(isNew);
+ bool isNewInMap = m_routeToRr.try_emplace(ribRoute, rrIt).second;
+ BOOST_VERIFY(isNewInMap);
if (rrIt->nRibRoutes++ > 0) {
NFD_LOG_DEBUG("add-route " << ribRoute.entry->getName() << '(' << ribRoute.route->faceId <<
diff --git a/daemon/rib/readvertise/readvertise.hpp b/daemon/rib/readvertise/readvertise.hpp
index 871702f..aeaa823 100644
--- a/daemon/rib/readvertise/readvertise.hpp
+++ b/daemon/rib/readvertise/readvertise.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-2022, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -68,18 +68,14 @@
withdraw(ReadvertisedRouteContainer::iterator rrIt);
private:
- /** \brief maps from RIB route to readvertised route derived from RIB route(s)
- */
- using RouteRrIndex = std::map<RibRouteRef, ReadvertisedRouteContainer::iterator>;
-
- static const time::milliseconds RETRY_DELAY_MIN;
- static const time::milliseconds RETRY_DELAY_MAX;
-
unique_ptr<ReadvertisePolicy> m_policy;
unique_ptr<ReadvertiseDestination> m_destination;
ReadvertisedRouteContainer m_rrs;
- RouteRrIndex m_routeToRr;
+ /**
+ * \brief maps from RIB route to readvertised route derived from RIB route(s)
+ */
+ std::map<RibRouteRef, ReadvertisedRouteContainer::iterator> m_routeToRr;
signal::ScopedConnection m_addRouteConn;
signal::ScopedConnection m_removeRouteConn;
diff --git a/daemon/rib/rib-entry.cpp b/daemon/rib/rib-entry.cpp
index 6acae09..1710aff 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-2021, Regents of the University of California,
+ * Copyright (c) 2014-2022, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -271,7 +271,7 @@
ndn::PrefixAnnouncement ann;
ann.setAnnouncedName(m_name);
- ann.setExpiration(ndn::clamp(
+ ann.setExpiration(std::clamp(
time::duration_cast<time::milliseconds>(entryExpiry - time::steady_clock::now()),
minExpiration, maxExpiration));
return ann;
@@ -280,16 +280,12 @@
std::ostream&
operator<<(std::ostream& os, const RibEntry& entry)
{
- os << "RibEntry {\n";
- os << " Name: " << entry.getName() << "\n";
-
+ os << "RibEntry {\n"
+ << " Name: " << entry.getName() << "\n";
for (const Route& route : entry) {
os << " " << route << "\n";
}
-
- os << "}";
-
- return os;
+ return os << "}";
}
} // namespace rib
diff --git a/daemon/rib/rib-entry.hpp b/daemon/rib/rib-entry.hpp
index 03cfc8c..79f1627 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-2021, Regents of the University of California,
+ * Copyright (c) 2014-2022, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -33,19 +33,15 @@
namespace nfd {
namespace rib {
-/** \brief Represents a RIB entry, which contains one or more Routes with the same prefix.
+/**
+ * \brief Represents a RIB entry, which contains one or more Routes with the same prefix.
*/
class RibEntry : public std::enable_shared_from_this<RibEntry>
{
public:
- typedef std::list<Route> RouteList;
- typedef RouteList::iterator iterator;
- typedef RouteList::const_iterator const_iterator;
-
- RibEntry()
- : m_nRoutesWithCaptureSet(0)
- {
- }
+ using RouteList = std::list<Route>;
+ using iterator = RouteList::iterator;
+ using const_iterator = RouteList::const_iterator;
void
setName(const Name& prefix);
@@ -210,7 +206,7 @@
* If the number is greater than zero, a route on the namespace has its capture
* flag set which means the namespace should not inherit any routes.
*/
- uint64_t m_nRoutesWithCaptureSet;
+ uint64_t m_nRoutesWithCaptureSet = 0;
};
inline void
@@ -228,7 +224,7 @@
inline void
RibEntry::setParent(shared_ptr<RibEntry> parent)
{
- m_parent = parent;
+ m_parent = std::move(parent);
}
inline shared_ptr<RibEntry>
diff --git a/daemon/rib/rib-update-batch.hpp b/daemon/rib/rib-update-batch.hpp
index f72a9e8..f3facb3 100644
--- a/daemon/rib/rib-update-batch.hpp
+++ b/daemon/rib/rib-update-batch.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-2022, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -33,20 +33,24 @@
namespace nfd {
namespace rib {
-typedef std::list<RibUpdate> RibUpdateList;
+using RibUpdateList = std::list<RibUpdate>;
-/** \brief Represents a collection of RibUpdates to be applied to a single FaceId.
+/**
+ * \brief Represents a collection of RibUpdates to be applied to a single FaceId.
*/
class RibUpdateBatch
{
public:
- typedef RibUpdateList::const_iterator const_iterator;
+ using const_iterator = RibUpdateList::const_iterator;
explicit
RibUpdateBatch(uint64_t faceId);
uint64_t
- getFaceId() const;
+ getFaceId() const
+ {
+ return m_faceId;
+ }
void
add(const RibUpdate& update);
@@ -65,12 +69,6 @@
RibUpdateList m_updates;
};
-inline uint64_t
-RibUpdateBatch::getFaceId() const
-{
- return m_faceId;
-}
-
} // namespace rib
} // namespace nfd
diff --git a/daemon/rib/rib-update.cpp b/daemon/rib/rib-update.cpp
index 8efe327..be2636d 100644
--- a/daemon/rib/rib-update.cpp
+++ b/daemon/rib/rib-update.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2015, Regents of the University of California,
+/*
+ * Copyright (c) 2014-2022, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -28,13 +28,8 @@
namespace nfd {
namespace rib {
-RibUpdate::RibUpdate()
-{
-
-}
-
std::ostream&
-operator<<(std::ostream& os, const RibUpdate::Action action)
+operator<<(std::ostream& os, RibUpdate::Action action)
{
switch (action) {
case RibUpdate::REGISTER:
@@ -47,22 +42,18 @@
os << "REMOVE_FACE";
break;
}
-
return os;
}
std::ostream&
operator<<(std::ostream& os, const RibUpdate& update)
{
- os << "RibUpdate {\n";
- os << " Name: " << update.getName() << "\n";
- os << " Action: " << update.getAction() << "\n";
- os << " " << update.getRoute() << "\n";
- os << "}";
-
- return os;
+ return os << "RibUpdate {\n"
+ << " Name: " << update.getName() << "\n"
+ << " Action: " << update.getAction() << "\n"
+ << " " << update.getRoute() << "\n"
+ << "}";
}
-
} // namespace rib
} // namespace nfd
diff --git a/daemon/rib/rib-update.hpp b/daemon/rib/rib-update.hpp
index ad5a390..bcdb342 100644
--- a/daemon/rib/rib-update.hpp
+++ b/daemon/rib/rib-update.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-2022, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -32,27 +32,23 @@
namespace nfd {
namespace rib {
-/** RibUpdate
- * \brief represents a route that will be added to or removed from a namespace
- *
- * \note This type is copyable so that it can be stored in STL containers.
+/**
+ * \brief Represents a route that will be added to or removed from a namespace
+ * \note This type is copyable so that it can be stored in STL containers.
*/
class RibUpdate
{
public:
enum Action {
- REGISTER = 0,
- UNREGISTER = 1,
-
- /** \brief An update triggered by a face destruction notification
- *
- * \note indicates a Route needs to be removed after a face is destroyed
+ REGISTER = 0,
+ UNREGISTER = 1,
+ /**
+ * \brief An update triggered by a face destruction notification
+ * \note indicates a Route needs to be removed after a face is destroyed
*/
- REMOVE_FACE = 2
+ REMOVE_FACE = 2,
};
- RibUpdate();
-
RibUpdate&
setAction(Action action);
@@ -117,7 +113,7 @@
}
std::ostream&
-operator<<(std::ostream& os, const RibUpdate::Action action);
+operator<<(std::ostream& os, RibUpdate::Action action);
std::ostream&
operator<<(std::ostream& os, const RibUpdate& update);
diff --git a/daemon/rib/rib.cpp b/daemon/rib/rib.cpp
index a81e77a..fe4db0b 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-2021, Regents of the University of California,
+ * Copyright (c) 2014-2022, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -96,10 +96,7 @@
// Name prefix exists
if (ribIt != m_rib.end()) {
shared_ptr<RibEntry> entry(ribIt->second);
-
- RibEntry::iterator entryIt;
- bool didInsert = false;
- std::tie(entryIt, didInsert) = entry->insertRoute(route);
+ auto [entryIt, didInsert] = entry->insertRoute(route);
if (didInsert) {
// The route was new and we successfully inserted it.
@@ -139,15 +136,13 @@
parent->addChild(entry);
}
- RibEntryList children = findDescendants(prefix);
-
+ auto children = findDescendants(prefix);
for (const auto& child : children) {
if (child->getParent() == parent) {
// Remove child from parent and inherit parent's child
if (parent != nullptr) {
parent->removeChild(child);
}
-
entry->addChild(child);
}
}
@@ -229,12 +224,12 @@
{
std::list<shared_ptr<RibEntry>> children;
- RibTable::const_iterator it = m_rib.find(prefix);
+ auto it = m_rib.find(prefix);
if (it != m_rib.end()) {
++it;
for (; it != m_rib.end(); ++it) {
if (prefix.isPrefixOf(it->first)) {
- children.push_back((it->second));
+ children.push_back(it->second);
}
else {
break;
@@ -250,9 +245,9 @@
{
std::list<shared_ptr<RibEntry>> children;
- for (const auto& pair : m_rib) {
- if (prefix.isPrefixOf(pair.first)) {
- children.push_back(pair.second);
+ for (const auto& [name, ribEntry] : m_rib) {
+ if (prefix.isPrefixOf(name)) {
+ children.push_back(ribEntry);
}
}
@@ -268,7 +263,6 @@
}
shared_ptr<RibEntry> entry(it->second);
-
shared_ptr<RibEntry> parent = entry->getParent();
// Remove self from parent's children
@@ -293,7 +287,7 @@
auto nextIt = m_rib.erase(it);
- // do something after erasing an entry.
+ // do something after erasing an entry
afterEraseEntry(entry->getName());
return nextIt;
@@ -304,10 +298,9 @@
{
RouteSet ancestorRoutes(&sortRoutes);
- shared_ptr<RibEntry> parent = entry.getParent();
-
+ auto parent = entry.getParent();
while (parent != nullptr) {
- for (const Route& route : parent->getRoutes()) {
+ for (const auto& route : parent->getRoutes()) {
if (route.isChildInherit()) {
ancestorRoutes.insert(route);
}
@@ -328,10 +321,9 @@
{
RouteSet ancestorRoutes(&sortRoutes);
- shared_ptr<RibEntry> parent = findParent(name);
-
+ auto parent = findParent(name);
while (parent != nullptr) {
- for (const Route& route : parent->getRoutes()) {
+ for (const auto& route : parent->getRoutes()) {
if (route.isChildInherit()) {
ancestorRoutes.insert(route);
}
@@ -353,9 +345,7 @@
const Rib::UpdateFailureCallback& onFailure)
{
BOOST_ASSERT(m_fibUpdater != nullptr);
-
addUpdateToQueue(update, onSuccess, onFailure);
-
sendBatchFromQueue();
}
@@ -372,11 +362,11 @@
void
Rib::beginRemoveFailedFaces(const std::set<uint64_t>& activeFaceIds)
{
- for (auto it = m_faceEntries.begin(); it != m_faceEntries.end(); ++it) {
- if (activeFaceIds.count(it->first) > 0) {
+ for (const auto& [faceId, ribEntry] : m_faceEntries) {
+ if (activeFaceIds.count(faceId) > 0) {
continue;
}
- enqueueRemoveFace(*it->second, it->first);
+ enqueueRemoveFace(*ribEntry, faceId);
}
sendBatchFromQueue();
}
@@ -428,7 +418,6 @@
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/rib.hpp b/daemon/rib/rib.hpp
index dbfdba8..f6912c3 100644
--- a/daemon/rib/rib.hpp
+++ b/daemon/rib/rib.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2021, Regents of the University of California,
+ * Copyright (c) 2014-2022, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -248,7 +248,7 @@
UpdateQueue m_updateBatches;
bool m_isUpdateInProgress = false;
- friend class FibUpdater;
+ friend FibUpdater;
};
std::ostream&
diff --git a/daemon/rib/service.cpp b/daemon/rib/service.cpp
index 0c674b3..d850ed2 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-2021, Regents of the University of California,
+ * Copyright (c) 2014-2022, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -43,8 +43,6 @@
NFD_LOG_INIT(RibService);
-Service* Service::s_instance = nullptr;
-
const std::string CFG_RIB = "rib";
const std::string CFG_LOCALHOST_SECURITY = "localhost_security";
const std::string CFG_LOCALHOP_SECURITY = "localhop_security";
@@ -52,8 +50,8 @@
const std::string CFG_PREFIX_PROPAGATE = "auto_prefix_propagate";
const std::string CFG_READVERTISE_NLSR = "readvertise_nlsr";
const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
-const uint64_t PROPAGATE_DEFAULT_COST = 15;
-const time::milliseconds PROPAGATE_DEFAULT_TIMEOUT = 10_s;
+constexpr uint64_t PROPAGATE_DEFAULT_COST = 15;
+constexpr time::milliseconds PROPAGATE_DEFAULT_TIMEOUT = 10_s;
static ConfigSection
loadConfigSectionFromFile(const std::string& filename)
@@ -64,10 +62,7 @@
return config;
}
-/**
- * \brief Look into the config file and construct appropriate transport to communicate with NFD
- * If NFD-RIB instance was initialized with config file, INFO format is assumed
- */
+// Look into NFD's config file and construct an appropriate transport to communicate with NFD.
static shared_ptr<ndn::Transport>
makeLocalNfdTransport(const ConfigSection& config)
{
diff --git a/daemon/rib/service.hpp b/daemon/rib/service.hpp
index ca5f57b..8928011 100644
--- a/daemon/rib/service.hpp
+++ b/daemon/rib/service.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-2022, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -107,7 +107,7 @@
applyConfig(const ConfigSection& section, const std::string& filename);
private:
- static Service* s_instance;
+ static inline Service* s_instance = nullptr;
ndn::KeyChain& m_keyChain;
ndn::Face m_face;