Improve and simplify code with modern C++ features

Change-Id: I83bf5513c2a1f90ba5a59e93c473306864b27d94
diff --git a/rib/fib-updater.cpp b/rib/fib-updater.cpp
index 76a33c7..1d6328c 100644
--- a/rib/fib-updater.cpp
+++ b/rib/fib-updater.cpp
@@ -654,13 +654,11 @@
   }
 
   // Remove inherited routes from current namespace
-  for (Rib::RouteSet::const_iterator removeIt = routesToRemove.begin();
-       removeIt != routesToRemove.end(); )
-  {
+  for (auto removeIt = routesToRemove.begin(); removeIt != routesToRemove.end(); ) {
     // If a route on the namespace has the same face ID and child inheritance set,
     // ignore this route
     if (entry.hasChildInheritOnFaceId(removeIt->faceId)) {
-      routesToRemove.erase(removeIt++);
+      removeIt = routesToRemove.erase(removeIt);
       continue;
     }
 
@@ -674,11 +672,10 @@
   }
 
   // Add inherited routes to current namespace
-  for (Rib::RouteSet::const_iterator addIt = routesToAdd.begin(); addIt != routesToAdd.end(); ) {
-
+  for (auto addIt = routesToAdd.begin(); addIt != routesToAdd.end(); ) {
     // If a route on the namespace has the same face ID and child inherit set, ignore this face
     if (entry.hasChildInheritOnFaceId(addIt->faceId)) {
-      routesToAdd.erase(addIt++);
+      addIt = routesToAdd.erase(addIt);
       continue;
     }
 
diff --git a/rib/readvertise/readvertise.cpp b/rib/readvertise/readvertise.cpp
index 53074f8..7a467ef 100644
--- a/rib/readvertise/readvertise.cpp
+++ b/rib/readvertise/readvertise.cpp
@@ -32,15 +32,15 @@
 
 NFD_LOG_INIT(Readvertise);
 
-const time::milliseconds Readvertise::RETRY_DELAY_MIN = time::seconds(50);
-const time::milliseconds Readvertise::RETRY_DELAY_MAX = time::seconds(3600);
+const time::milliseconds Readvertise::RETRY_DELAY_MIN = 50_s;
+const time::milliseconds Readvertise::RETRY_DELAY_MAX = 3600_s;
 
 static time::milliseconds
 randomizeTimer(time::milliseconds baseTimer)
 {
   std::uniform_int_distribution<uint64_t> dist(-5, 5);
   time::milliseconds newTime = baseTimer + time::milliseconds(dist(getGlobalRng()));
-  return std::max(newTime, time::milliseconds(0));
+  return std::max(newTime, 0_ms);
 }
 
 Readvertise::Readvertise(Rib& rib, unique_ptr<ReadvertisePolicy> policy,
@@ -48,8 +48,8 @@
   : m_policy(std::move(policy))
   , m_destination(std::move(destination))
 {
-  m_addRouteConn = rib.afterAddRoute.connect(bind(&Readvertise::afterAddRoute, this, _1));
-  m_removeRouteConn = rib.beforeRemoveRoute.connect(bind(&Readvertise::beforeRemoveRoute, this, _1));
+  m_addRouteConn = rib.afterAddRoute.connect([this] (const auto& r) { this->afterAddRoute(r); });
+  m_removeRouteConn = rib.beforeRemoveRoute.connect([this] (const auto& r) { this->beforeRemoveRoute(r); });
 
   m_destination->afterAvailabilityChange.connect([this] (bool isAvailable) {
     if (isAvailable) {
@@ -156,17 +156,17 @@
   }
 
   m_destination->advertise(*rrIt,
-    [this, rrIt] {
+    [=] {
       NFD_LOG_DEBUG("advertise " << rrIt->prefix << " success");
       rrIt->retryDelay = RETRY_DELAY_MIN;
       rrIt->retryEvt = scheduler::schedule(randomizeTimer(m_policy->getRefreshInterval()),
-                                           bind(&Readvertise::advertise, this, rrIt));
+                                           [=] { advertise(rrIt); });
     },
-    [this, rrIt] (const std::string& msg) {
+    [=] (const std::string& msg) {
       NFD_LOG_DEBUG("advertise " << rrIt->prefix << " failure " << msg);
       rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2);
       rrIt->retryEvt = scheduler::schedule(randomizeTimer(rrIt->retryDelay),
-                                           bind(&Readvertise::advertise, this, rrIt));
+                                           [=] { advertise(rrIt); });
     });
 }
 
@@ -182,15 +182,14 @@
   }
 
   m_destination->withdraw(*rrIt,
-    [this, rrIt] {
+    [=] {
       NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " success");
       m_rrs.erase(rrIt);
     },
-    [this, rrIt] (const std::string& msg) {
+    [=] (const std::string& msg) {
       NFD_LOG_DEBUG("withdraw " << rrIt->prefix << " failure " << msg);
       rrIt->retryDelay = std::min(RETRY_DELAY_MAX, rrIt->retryDelay * 2);
-      rrIt->retryEvt = scheduler::schedule(randomizeTimer(rrIt->retryDelay),
-                                           bind(&Readvertise::withdraw, this, rrIt));
+      rrIt->retryEvt = scheduler::schedule(randomizeTimer(rrIt->retryDelay), [=] { withdraw(rrIt); });
     });
 }
 
diff --git a/rib/rib-entry.cpp b/rib/rib-entry.cpp
index 7fd0ed7..ff646fa 100644
--- a/rib/rib-entry.cpp
+++ b/rib/rib-entry.cpp
@@ -56,12 +56,10 @@
     }
 
     m_routes.push_back(route);
+    return {std::prev(m_routes.end()), true};
+  }
 
-    return std::make_pair(std::prev(m_routes.end()), true);
-  }
-  else {
-    return std::make_pair(it, false);
-  }
+  return {it, false};
 }
 
 void
@@ -96,16 +94,16 @@
 void
 RibEntry::addChild(shared_ptr<RibEntry> child)
 {
-  BOOST_ASSERT(!static_cast<bool>(child->getParent()));
+  BOOST_ASSERT(!child->getParent());
   child->setParent(this->shared_from_this());
-  m_children.push_back(child);
+  m_children.push_back(std::move(child));
 }
 
 void
 RibEntry::removeChild(shared_ptr<RibEntry> child)
 {
   BOOST_ASSERT(child->getParent().get() == this);
-  child->setParent(shared_ptr<RibEntry>());
+  child->setParent(nullptr);
   m_children.remove(child);
 }
 
@@ -136,9 +134,7 @@
 void
 RibEntry::removeInheritedRoute(const Route& route)
 {
-  RouteList::iterator it = std::find_if(m_inheritedRoutes.begin(), m_inheritedRoutes.end(),
-                                        bind(&compareFaceId, _1, route.faceId));
-  m_inheritedRoutes.erase(it);
+  m_inheritedRoutes.remove_if(bind(&compareFaceId, _1, route.faceId));
 }
 
 RibEntry::RouteList::const_iterator
@@ -151,9 +147,7 @@
 bool
 RibEntry::hasInheritedRoute(const Route& route) const
 {
-  RouteList::const_iterator it = findInheritedRoute(route);
-
-  return (it != m_inheritedRoutes.end());
+  return findInheritedRoute(route) != m_inheritedRoutes.end();
 }
 
 bool
diff --git a/rib/rib-manager.cpp b/rib/rib-manager.cpp
index ecf20d9..5b0e0c6 100644
--- a/rib/rib-manager.cpp
+++ b/rib/rib-manager.cpp
@@ -167,14 +167,14 @@
   }
 
   if (wantReadvertiseToNlsr && m_readvertiseNlsr == nullptr) {
-    NFD_LOG_DEBUG("Enabling readvertise-to-nlsr.");
-    m_readvertiseNlsr.reset(new Readvertise(
+    NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
+    m_readvertiseNlsr = make_unique<Readvertise>(
       m_rib,
       make_unique<ClientToNlsrReadvertisePolicy>(),
-      make_unique<NfdRibReadvertiseDestination>(m_nfdController, READVERTISE_NLSR_PREFIX, m_rib)));
+      make_unique<NfdRibReadvertiseDestination>(m_nfdController, READVERTISE_NLSR_PREFIX, m_rib));
   }
   else if (!wantReadvertiseToNlsr && m_readvertiseNlsr != nullptr) {
-    NFD_LOG_DEBUG("Disabling readvertise-to-nlsr.");
+    NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
     m_readvertiseNlsr.reset();
   }
 }
@@ -184,11 +184,11 @@
 {
   // register entry to the FIB
   m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
-     ControlParameters()
-       .setName(Name(topPrefix).append(MGMT_MODULE_NAME))
-       .setFaceId(0),
-     bind(&RibManager::onCommandPrefixAddNextHopSuccess, this, cref(topPrefix), _1),
-     bind(&RibManager::onCommandPrefixAddNextHopError, this, cref(topPrefix), _1));
+    ControlParameters()
+        .setName(Name(topPrefix).append(MGMT_MODULE_NAME))
+        .setFaceId(0),
+    [=] (const auto& res) { this->onCommandPrefixAddNextHopSuccess(topPrefix, res); },
+    [=] (const auto& res) { this->onCommandPrefixAddNextHopError(topPrefix, res); });
 
   // add top prefix to the dispatcher
   m_addTopPrefix(topPrefix);
@@ -438,8 +438,7 @@
 RibManager::onEnableLocalFieldsError(const ndn::nfd::ControlResponse& response)
 {
   BOOST_THROW_EXCEPTION(Error("Couldn't enable local fields (code: " +
-                              to_string(response.getCode()) + ", info: " + response.getText() +
-                              ")"));
+                              to_string(response.getCode()) + ", info: " + response.getText() + ")"));
 }
 
 } // namespace rib
diff --git a/rib/rib.cpp b/rib/rib.cpp
index d6b125b..6a221ea 100644
--- a/rib/rib.cpp
+++ b/rib/rib.cpp
@@ -68,16 +68,14 @@
 Route*
 Rib::find(const Name& prefix, const Route& route) const
 {
-  RibTable::const_iterator ribIt = m_rib.find(prefix);
+  auto ribIt = m_rib.find(prefix);
 
   // Name prefix exists
   if (ribIt != m_rib.end()) {
     shared_ptr<RibEntry> entry = ribIt->second;
-
-    RibEntry::iterator routeIt = entry->findRoute(route);
-
+    auto routeIt = entry->findRoute(route);
     if (routeIt != entry->end()) {
-      return &((*routeIt));
+      return &*routeIt;
     }
   }
 
@@ -87,7 +85,7 @@
 void
 Rib::insert(const Name& prefix, const Route& route)
 {
-  RibTable::iterator ribIt = m_rib.find(prefix);
+  auto ribIt = m_rib.find(prefix);
 
   // Name prefix exists
   if (ribIt != m_rib.end()) {
@@ -125,13 +123,13 @@
   }
   else {
     // New name prefix
-    shared_ptr<RibEntry> entry = make_shared<RibEntry>();
+    auto entry = make_shared<RibEntry>();
 
     m_rib[prefix] = entry;
     m_nItems++;
 
     entry->setName(prefix);
-    RibEntry::iterator routeIt = entry->insertRoute(route).first;
+    auto routeIt = entry->insertRoute(route).first;
 
     // Find prefix's parent
     shared_ptr<RibEntry> parent = findParent(prefix);
@@ -166,12 +164,12 @@
 void
 Rib::erase(const Name& prefix, const Route& route)
 {
-  RibTable::iterator ribIt = m_rib.find(prefix);
+  auto ribIt = m_rib.find(prefix);
 
   // Name prefix exists
   if (ribIt != m_rib.end()) {
     shared_ptr<RibEntry> entry = ribIt->second;
-    RibEntry::iterator routeIt = entry->findRoute(route);
+    auto routeIt = entry->findRoute(route);
 
     if (routeIt != entry->end()) {
       beforeRemoveRoute(RibRouteRef{entry, routeIt});
@@ -210,23 +208,21 @@
 Rib::findParent(const Name& prefix) const
 {
   for (int i = prefix.size() - 1; i >= 0; i--) {
-    RibTable::const_iterator it = m_rib.find(prefix.getPrefix(i));
-
+    auto it = m_rib.find(prefix.getPrefix(i));
     if (it != m_rib.end()) {
-      return (it->second);
+      return it->second;
     }
   }
 
-  return shared_ptr<RibEntry>();
+  return nullptr;
 }
 
-std::list<shared_ptr<RibEntry> >
+std::list<shared_ptr<RibEntry>>
 Rib::findDescendants(const Name& prefix) const
 {
-  std::list<shared_ptr<RibEntry> > children;
+  std::list<shared_ptr<RibEntry>> children;
 
   RibTable::const_iterator it = m_rib.find(prefix);
-
   if (it != m_rib.end()) {
     ++it;
     for (; it != m_rib.end(); ++it) {
@@ -247,7 +243,7 @@
 {
   std::list<shared_ptr<RibEntry>> children;
 
-  for (std::pair<Name, shared_ptr<RibEntry>> pair : m_rib) {
+  for (const auto& pair : m_rib) {
     if (prefix.isPrefixOf(pair.first)) {
       children.push_back(pair.second);
     }
@@ -288,7 +284,7 @@
     }
   }
 
-  RibTable::iterator nextIt = m_rib.erase(it);
+  auto nextIt = m_rib.erase(it);
 
   // do something after erasing an entry.
   afterEraseEntry(entry->getName());
@@ -462,8 +458,7 @@
 Rib::modifyInheritedRoutes(const RibUpdateList& inheritedRoutes)
 {
   for (const RibUpdate& update : inheritedRoutes) {
-    RibTable::iterator ribIt = m_rib.find(update.getName());
-
+    auto ribIt = m_rib.find(update.getName());
     BOOST_ASSERT(ribIt != m_rib.end());
     shared_ptr<RibEntry> entry(ribIt->second);
 
@@ -485,21 +480,18 @@
 {
   std::list<NameAndRoute> routes;
 
-  FaceLookupTable::iterator lookupIt = m_faceMap.find(faceId);
-
-  // No RIB entries have this face
+  auto lookupIt = m_faceMap.find(faceId);
   if (lookupIt == m_faceMap.end()) {
+    // No RIB entries have this face
     return routes;
   }
 
-  RibEntryList& ribEntries = lookupIt->second;
-
   // For each RIB entry that has faceId
-  for (const shared_ptr<RibEntry>& entry : ribEntries) {
+  for (const auto& entry : lookupIt->second) {
     // Find the routes in the entry
     for (const Route& route : *entry) {
       if (route.faceId == faceId) {
-        routes.push_back(NameAndRoute(entry->getName(), route));
+        routes.emplace_back(entry->getName(), route);
       }
     }
   }
diff --git a/rib/rib.hpp b/rib/rib.hpp
index 14ce12d..c8a0f5d 100644
--- a/rib/rib.hpp
+++ b/rib/rib.hpp
@@ -211,7 +211,7 @@
   modifyInheritedRoutes(const RibUpdateList& inheritedRoutes);
 
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  typedef std::pair<const Name&,const Route&> NameAndRoute;
+  using NameAndRoute = std::pair<const Name&, const Route&>;
 
   std::list<NameAndRoute>
   findRoutesWithFaceId(uint64_t faceId);