tools: simplify nfd-autoreg with range-based for

refs #4158

Change-Id: Ibe83df03a1618b987608d539173ad32f03aafc42
diff --git a/tools/nfd-autoreg.cpp b/tools/nfd-autoreg.cpp
index ca52e9b..6323196 100644
--- a/tools/nfd-autoreg.cpp
+++ b/tools/nfd-autoreg.cpp
@@ -26,12 +26,12 @@
 #include <ndn-cxx/face.hpp>
 #include <ndn-cxx/name.hpp>
 
-#include <ndn-cxx/security/key-chain.hpp>
-#include <ndn-cxx/util/face-uri.hpp>
+#include <ndn-cxx/encoding/buffer-stream.hpp>
 #include <ndn-cxx/mgmt/nfd/controller.hpp>
 #include <ndn-cxx/mgmt/nfd/face-monitor.hpp>
 #include <ndn-cxx/mgmt/nfd/face-status.hpp>
-#include <ndn-cxx/encoding/buffer-stream.hpp>
+#include <ndn-cxx/security/key-chain.hpp>
+#include <ndn-cxx/util/face-uri.hpp>
 
 #include <boost/program_options/options_description.hpp>
 #include <boost/program_options/variables_map.hpp>
@@ -40,13 +40,12 @@
 #include "core/version.hpp"
 #include "core/network.hpp"
 
-using namespace ndn::nfd;
-using ndn::util::FaceUri;
-using ::nfd::Network;
-
 namespace ndn {
 namespace nfd_autoreg {
 
+using util::FaceUri;
+using ::nfd::Network;
+
 class AutoregServer : boost::noncopyable
 {
 public:
@@ -89,15 +88,8 @@
   bool
   isBlacklisted(const boost::asio::ip::address& address)
   {
-    for (std::vector<Network>::const_iterator network = m_blackList.begin();
-         network != m_blackList.end();
-         ++network)
-      {
-        if (network->doesContain(address))
-          return true;
-      }
-
-    return false;
+    return std::any_of(m_blackList.begin(), m_blackList.end(),
+                       bind(&Network::doesContain, _1, address));
   }
 
   /**
@@ -106,38 +98,28 @@
   bool
   isWhitelisted(const boost::asio::ip::address& address)
   {
-    for (std::vector<Network>::const_iterator network = m_whiteList.begin();
-         network != m_whiteList.end();
-         ++network)
-      {
-        if (network->doesContain(address))
-          return true;
-      }
-
-    return false;
+    return std::any_of(m_whiteList.begin(), m_whiteList.end(),
+                       bind(&Network::doesContain, _1, address));
   }
 
   void
   registerPrefixesForFace(uint64_t faceId, const std::vector<Name>& prefixes)
   {
-    for (std::vector<Name>::const_iterator prefix = prefixes.begin();
-         prefix != prefixes.end();
-         ++prefix)
-      {
-        m_controller.start<RibRegisterCommand>(
-          ControlParameters()
-            .setName(*prefix)
-            .setFaceId(faceId)
-            .setOrigin(ROUTE_ORIGIN_AUTOREG)
-            .setCost(m_cost)
-            .setExpirationPeriod(time::milliseconds::max()),
-          bind(&AutoregServer::onRegisterCommandSuccess, this, faceId, *prefix),
-          bind(&AutoregServer::onRegisterCommandFailure, this, faceId, *prefix, _1));
-      }
+    for (const Name& prefix : prefixes) {
+      m_controller.start<nfd::RibRegisterCommand>(
+        nfd::ControlParameters()
+          .setName(prefix)
+          .setFaceId(faceId)
+          .setOrigin(nfd::ROUTE_ORIGIN_AUTOREG)
+          .setCost(m_cost)
+          .setExpirationPeriod(time::milliseconds::max()),
+        bind(&AutoregServer::onRegisterCommandSuccess, this, faceId, prefix),
+        bind(&AutoregServer::onRegisterCommandFailure, this, faceId, prefix, _1));
+    }
   }
 
   void
-  registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, FacePersistency facePersistency)
+  registerPrefixesIfNeeded(uint64_t faceId, const FaceUri& uri, nfd::FacePersistency facePersistency)
   {
     if (hasAllowedSchema(uri)) {
       boost::system::error_code ec;
@@ -148,7 +130,7 @@
         registerPrefixesForFace(faceId, m_allFacesPrefixes);
 
         // register autoreg prefixes if new face is on-demand and not blacklisted and whitelisted
-        if (facePersistency == FACE_PERSISTENCY_ON_DEMAND &&
+        if (facePersistency == nfd::FACE_PERSISTENCY_ON_DEMAND &&
             !isBlacklisted(address) && isWhitelisted(address)) {
           registerPrefixesForFace(faceId, m_autoregPrefixes);
         }
@@ -157,20 +139,18 @@
   }
 
   void
-  onNotification(const FaceEventNotification& notification)
+  onNotification(const nfd::FaceEventNotification& notification)
   {
-    if (notification.getKind() == FACE_EVENT_CREATED &&
-        notification.getFaceScope() != FACE_SCOPE_LOCAL)
-      {
-        std::cerr << "PROCESSING: " << notification << std::endl;
+    if (notification.getKind() == nfd::FACE_EVENT_CREATED &&
+        notification.getFaceScope() != nfd::FACE_SCOPE_LOCAL) {
+      std::cerr << "PROCESSING: " << notification << std::endl;
 
-        registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
-                                 notification.getFacePersistency());
-      }
-    else
-      {
-        std::cerr << "IGNORED: " << notification << std::endl;
-      }
+      registerPrefixesIfNeeded(notification.getFaceId(), FaceUri(notification.getRemoteUri()),
+                               notification.getFacePersistency());
+    }
+    else {
+      std::cerr << "IGNORED: " << notification << std::endl;
+    }
   }
 
   void
@@ -194,38 +174,25 @@
   startProcessing()
   {
     std::cerr << "AUTOREG prefixes: " << std::endl;
-    for (std::vector<Name>::const_iterator prefix = m_autoregPrefixes.begin();
-         prefix != m_autoregPrefixes.end();
-         ++prefix)
-      {
-        std::cout << "  " << *prefix << std::endl;
-      }
+    for (const Name& prefix : m_autoregPrefixes) {
+      std::cout << "  " << prefix << std::endl;
+    }
     std::cerr << "ALL-FACES-AUTOREG prefixes: " << std::endl;
-    for (std::vector<Name>::const_iterator prefix = m_allFacesPrefixes.begin();
-         prefix != m_allFacesPrefixes.end();
-         ++prefix)
-      {
-        std::cout << "  " << *prefix << std::endl;
-      }
+    for (const Name& prefix : m_allFacesPrefixes) {
+      std::cout << "  " << prefix << std::endl;
+    }
 
-    if (!m_blackList.empty())
-      {
-        std::cerr << "Blacklisted networks: " << std::endl;
-        for (std::vector<Network>::const_iterator network = m_blackList.begin();
-             network != m_blackList.end();
-             ++network)
-          {
-            std::cout << "  " << *network << std::endl;
-          }
+    if (!m_blackList.empty()) {
+      std::cerr << "Blacklisted networks: " << std::endl;
+      for (const Network& network : m_blackList) {
+        std::cout << "  " << network << std::endl;
       }
+    }
 
     std::cerr << "Whitelisted networks: " << std::endl;
-    for (std::vector<Network>::const_iterator network = m_whiteList.begin();
-         network != m_whiteList.end();
-         ++network)
-      {
-        std::cout << "  " << *network << std::endl;
-      }
+    for (const Network& network : m_whiteList) {
+      std::cout << "  " << network << std::endl;
+    }
 
     m_faceMonitor.onNotification.connect(bind(&AutoregServer::onNotification, this, _1));
     m_faceMonitor.start();
@@ -239,8 +206,8 @@
   void
   startFetchingFaceStatusDataset()
   {
-    m_controller.fetch<FaceDataset>(
-      [this] (const std::vector<FaceStatus>& faces) {
+    m_controller.fetch<nfd::FaceDataset>(
+      [this] (const std::vector<nfd::FaceStatus>& faces) {
         for (const auto& faceStatus : faces) {
           registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
                                    faceStatus.getFacePersistency());
@@ -273,55 +240,47 @@
       ;
 
     po::variables_map options;
-    try
-      {
-        po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
-        po::notify(options);
-      }
-    catch (const std::exception& e)
-      {
-        std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
-        usage(std::cerr, optionDesciption, argv[0]);
-        return 1;
-      }
+    try {
+      po::store(po::command_line_parser(argc, argv).options(optionDesciption).run(), options);
+      po::notify(options);
+    }
+    catch (const std::exception& e) {
+      std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
+      usage(std::cerr, optionDesciption, argv[0]);
+      return 1;
+    }
 
-    if (options.count("help"))
-      {
-        usage(std::cout, optionDesciption, argv[0]);
-        return 0;
-      }
+    if (options.count("help")) {
+      usage(std::cout, optionDesciption, argv[0]);
+      return 0;
+    }
 
-    if (options.count("version"))
-      {
-        std::cout << NFD_VERSION_BUILD_STRING << std::endl;
-        return 0;
-      }
+    if (options.count("version")) {
+      std::cout << NFD_VERSION_BUILD_STRING << std::endl;
+      return 0;
+    }
 
-    if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty())
-      {
-        std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
-                  << std::endl << std::endl;
-        usage(std::cerr, optionDesciption, argv[0]);
-        return 2;
-      }
+    if (m_autoregPrefixes.empty() && m_allFacesPrefixes.empty()) {
+      std::cerr << "ERROR: at least one --prefix or --all-faces-prefix must be specified"
+                << std::endl << std::endl;
+      usage(std::cerr, optionDesciption, argv[0]);
+      return 2;
+    }
 
-    if (m_whiteList.empty())
-      {
-        // Allow everything
-        m_whiteList.push_back(Network::getMaxRangeV4());
-        m_whiteList.push_back(Network::getMaxRangeV6());
-      }
+    if (m_whiteList.empty()) {
+      // Allow everything
+      m_whiteList.push_back(Network::getMaxRangeV4());
+      m_whiteList.push_back(Network::getMaxRangeV6());
+    }
 
-    try
-      {
-        startFetchingFaceStatusDataset();
-        startProcessing();
-      }
-    catch (const std::exception& e)
-      {
-        std::cerr << "ERROR: " << e.what() << std::endl;
-        return 2;
-      }
+    try {
+      startFetchingFaceStatusDataset();
+      startProcessing();
+    }
+    catch (const std::exception& e) {
+      std::cerr << "ERROR: " << e.what() << std::endl;
+      return 2;
+    }
 
     return 0;
   }
@@ -329,8 +288,8 @@
 private:
   Face m_face;
   KeyChain m_keyChain;
-  Controller m_controller;
-  FaceMonitor m_faceMonitor;
+  nfd::Controller m_controller;
+  nfd::FaceMonitor m_faceMonitor;
   std::vector<Name> m_autoregPrefixes;
   std::vector<Name> m_allFacesPrefixes;
   uint64_t m_cost;