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/tools/ndn-autoconfig-server/program.cpp b/tools/ndn-autoconfig-server/program.cpp
index d0be0d1..21e7857 100644
--- a/tools/ndn-autoconfig-server/program.cpp
+++ b/tools/ndn-autoconfig-server/program.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,
@@ -67,7 +67,9 @@
m_face.put(*data);
}
},
- bind(&Program::handlePrefixRegistrationFailure, this, _1, _2));
+ [this] (auto&&... args) {
+ handlePrefixRegistrationFailure(std::forward<decltype(args)>(args)...);
+ });
}
void
@@ -84,8 +86,10 @@
m_dispatcher.addTopPrefix(ROUTABLE_PREFIXES_DATA_PREFIX, false);
m_face.registerPrefix(Name(ROUTABLE_PREFIXES_DATA_PREFIX).append(ROUTABLE_PREFIXES_DATA_SUFFIX),
- nullptr,
- bind(&Program::handlePrefixRegistrationFailure, this, _1, _2));
+ nullptr,
+ [this] (auto&&... args) {
+ handlePrefixRegistrationFailure(std::forward<decltype(args)>(args)...);
+ });
}
void
diff --git a/tools/ndn-autoconfig/multicast-discovery.cpp b/tools/ndn-autoconfig/multicast-discovery.cpp
index 200ae75..0a76b22 100644
--- a/tools/ndn-autoconfig/multicast-discovery.cpp
+++ b/tools/ndn-autoconfig/multicast-discovery.cpp
@@ -34,7 +34,6 @@
namespace autoconfig {
using nfd::ControlParameters;
-using nfd::ControlResponse;
const Name HUB_DISCOVERY_PREFIX("/localhop/ndn-autoconf/hub");
const uint64_t HUB_DISCOVERY_ROUTE_COST(1);
@@ -55,9 +54,9 @@
m_controller.fetch<nfd::FaceQueryDataset>(
filter,
- bind(&MulticastDiscovery::registerHubDiscoveryPrefix, this, _1),
+ [this] (const auto& dataset) { registerHubDiscoveryPrefix(dataset); },
[this] (uint32_t code, const std::string& reason) {
- this->fail("Error " + to_string(code) + " when querying multi-access faces: " + reason);
+ fail("Error " + to_string(code) + " when querying multi-access faces: " + reason);
});
}
@@ -65,7 +64,7 @@
MulticastDiscovery::registerHubDiscoveryPrefix(const std::vector<nfd::FaceStatus>& dataset)
{
if (dataset.empty()) {
- this->fail("No multi-access faces available");
+ fail("No multi-access faces available");
return;
}
@@ -82,11 +81,11 @@
m_controller.start<nfd::RibRegisterCommand>(
parameters,
- [this] (const ControlParameters&) {
+ [this] (const auto&) {
++m_nRegSuccess;
afterReg();
},
- [this, faceStatus] (const ControlResponse& resp) {
+ [this, faceStatus] (const auto& resp) {
std::cerr << "Error " << resp.getCode() << " when registering hub discovery prefix "
<< "for face " << faceStatus.getFaceId() << " (" << faceStatus.getRemoteUri()
<< "): " << resp.getText() << std::endl;
@@ -103,10 +102,10 @@
return; // continue waiting
}
if (m_nRegSuccess > 0) {
- this->setStrategy();
+ setStrategy();
}
else {
- this->fail("Cannot register hub discovery prefix for any face");
+ fail("Cannot register hub discovery prefix for any face");
}
}
@@ -119,10 +118,9 @@
m_controller.start<nfd::StrategyChoiceSetCommand>(
parameters,
- bind(&MulticastDiscovery::requestHubData, this),
- [this] (const ControlResponse& resp) {
- this->fail("Error " + to_string(resp.getCode()) + " when setting multicast strategy: " +
- resp.getText());
+ [this] (const auto&) { requestHubData(); },
+ [this] (const auto& resp) {
+ fail("Error " + to_string(resp.getCode()) + " when setting multicast strategy: " + resp.getText());
});
}
@@ -141,17 +139,17 @@
auto i = content.find(tlv::nfd::Uri);
if (i == content.elements_end()) {
- this->fail("Malformed hub Data: missing Uri element");
+ fail("Malformed hub Data: missing Uri element");
return;
}
- this->provideHubFaceUri(std::string(reinterpret_cast<const char*>(i->value()), i->value_size()));
+ provideHubFaceUri(std::string(reinterpret_cast<const char*>(i->value()), i->value_size()));
},
[this] (const Interest&, const lp::Nack& nack) {
- this->fail("Nack-" + boost::lexical_cast<std::string>(nack.getReason()) + " when retrieving hub Data");
+ fail("Nack-" + boost::lexical_cast<std::string>(nack.getReason()) + " when retrieving hub Data");
},
[this] (const Interest&) {
- this->fail("Timeout when retrieving hub Data");
+ fail("Timeout when retrieving hub Data");
});
}
diff --git a/tools/ndn-autoconfig/procedure.cpp b/tools/ndn-autoconfig/procedure.cpp
index dded94b..b0896ec 100644
--- a/tools/ndn-autoconfig/procedure.cpp
+++ b/tools/ndn-autoconfig/procedure.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,
@@ -52,16 +52,16 @@
Procedure::initialize(const Options& options)
{
BOOST_ASSERT(m_stages.empty());
- this->makeStages(options);
+ makeStages(options);
BOOST_ASSERT(!m_stages.empty());
for (size_t i = 0; i < m_stages.size(); ++i) {
- m_stages[i]->onSuccess.connect(bind(&Procedure::connect, this, _1));
+ m_stages[i]->onSuccess.connect([this] (const auto& uri) { connect(uri); });
if (i + 1 < m_stages.size()) {
- m_stages[i]->onFailure.connect([=] (const std::string&) { m_stages[i + 1]->start(); });
+ m_stages[i]->onFailure.connect([=] (const auto&) { m_stages[i + 1]->start(); });
}
else {
- m_stages[i]->onFailure.connect([=] (const std::string&) { this->onComplete(false); });
+ m_stages[i]->onFailure.connect([=] (const auto&) { onComplete(false); });
}
}
}
diff --git a/tools/nfd-autoreg.cpp b/tools/nfd-autoreg.cpp
index fed9cfb..3745cb0 100644
--- a/tools/nfd-autoreg.cpp
+++ b/tools/nfd-autoreg.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,
@@ -90,7 +90,7 @@
isBlacklisted(const boost::asio::ip::address& address) const
{
return std::any_of(m_blackList.begin(), m_blackList.end(),
- bind(&Network::doesContain, _1, address));
+ std::bind(&Network::doesContain, _1, address));
}
/**
@@ -100,7 +100,7 @@
isWhitelisted(const boost::asio::ip::address& address) const
{
return std::any_of(m_whiteList.begin(), m_whiteList.end(),
- bind(&Network::doesContain, _1, address));
+ std::bind(&Network::doesContain, _1, address));
}
void
@@ -114,8 +114,8 @@
.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));
+ std::bind(&AutoregServer::onRegisterCommandSuccess, this, faceId, prefix),
+ std::bind(&AutoregServer::onRegisterCommandFailure, this, faceId, prefix, _1));
}
}
@@ -154,12 +154,6 @@
}
}
- void
- signalHandler()
- {
- m_face.shutdown();
- }
-
static void
usage(std::ostream& os,
const boost::program_options::options_description& desc,
@@ -194,11 +188,11 @@
std::cout << " " << network << std::endl;
}
- m_faceMonitor.onNotification.connect(bind(&AutoregServer::onNotification, this, _1));
+ m_faceMonitor.onNotification.connect(std::bind(&AutoregServer::onNotification, this, _1));
m_faceMonitor.start();
boost::asio::signal_set signalSet(m_face.getIoService(), SIGINT, SIGTERM);
- signalSet.async_wait(bind(&AutoregServer::signalHandler, this));
+ signalSet.async_wait([this] (auto&&...) { m_face.shutdown(); });
m_face.processEvents();
}
@@ -207,13 +201,13 @@
startFetchingFaceStatusDataset()
{
m_controller.fetch<nfd::FaceDataset>(
- [this] (const std::vector<nfd::FaceStatus>& faces) {
+ [this] (const auto& faces) {
for (const auto& faceStatus : faces) {
registerPrefixesIfNeeded(faceStatus.getFaceId(), FaceUri(faceStatus.getRemoteUri()),
faceStatus.getFacePersistency());
}
},
- [] (uint32_t code, const std::string& reason) {});
+ [] (auto&&...) {});
}
int
diff --git a/tools/nfdc/command-arguments.hpp b/tools/nfdc/command-arguments.hpp
index 164a672..1d0c9bd 100644
--- a/tools/nfdc/command-arguments.hpp
+++ b/tools/nfdc/command-arguments.hpp
@@ -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,
@@ -30,6 +30,7 @@
#include "status-report.hpp"
#include <ndn-cxx/encoding/nfd-constants.hpp>
+#include <ndn-cxx/util/any.hpp>
#include <boost/logic/tribool.hpp>
diff --git a/tools/nfdc/face-module.cpp b/tools/nfdc/face-module.cpp
index e46279d2..18862aa 100644
--- a/tools/nfdc/face-module.cpp
+++ b/tools/nfdc/face-module.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,
@@ -236,7 +236,7 @@
if (isChangingParams) {
ctx.controller.start<ndn::nfd::FaceUpdateCommand>(
params,
- bind(updateFace, respParams, _1),
+ [=, &updateFace] (const auto& cp) { updateFace(respParams, cp); },
ctx.makeCommandFailureHandler("updating face"),
ctx.makeCommandOptions());
}
diff --git a/tools/nfdc/format-helpers.cpp b/tools/nfdc/format-helpers.cpp
index fa72512..a207bf8 100644
--- a/tools/nfdc/format-helpers.cpp
+++ b/tools/nfdc/format-helpers.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,
@@ -110,9 +110,9 @@
}
std::string
-formatTimestamp(time::system_clock::TimePoint t)
+formatTimestamp(time::system_clock::time_point t)
{
- return time::toString(t, "%Y-%m-%dT%H:%M:%S%F");
+ return time::toIsoExtendedString(t);
}
} // namespace xml
@@ -199,7 +199,7 @@
}
std::string
-formatTimestamp(time::system_clock::TimePoint t)
+formatTimestamp(time::system_clock::time_point t)
{
return time::toIsoString(t);
}
diff --git a/tools/nfdc/format-helpers.hpp b/tools/nfdc/format-helpers.hpp
index 1ee88ee..95f3310 100644
--- a/tools/nfdc/format-helpers.hpp
+++ b/tools/nfdc/format-helpers.hpp
@@ -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,
@@ -73,7 +73,7 @@
* Definition of this format: https://www.w3.org/TR/xmlschema11-2/#dateTime
*/
std::string
-formatTimestamp(time::system_clock::TimePoint t);
+formatTimestamp(time::system_clock::time_point t);
} // namespace xml
@@ -262,7 +262,7 @@
}
std::string
-formatTimestamp(time::system_clock::TimePoint t);
+formatTimestamp(time::system_clock::time_point t);
} // namespace text
diff --git a/tools/nfdc/forwarder-general-module.cpp b/tools/nfdc/forwarder-general-module.cpp
index 417b346..1542b9f 100644
--- a/tools/nfdc/forwarder-general-module.cpp
+++ b/tools/nfdc/forwarder-general-module.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,
@@ -46,7 +46,7 @@
onFailure, options);
}
-static time::system_clock::Duration
+static auto
calculateUptime(const ForwarderStatus& status)
{
return status.getCurrentTimestamp() - status.getStartTimestamp();
diff --git a/tools/nfdc/status.cpp b/tools/nfdc/status.cpp
index 441c3a9..a338b1d 100644
--- a/tools/nfdc/status.cpp
+++ b/tools/nfdc/status.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,
@@ -128,25 +128,29 @@
CommandDefinition defStatusShow("status", "show");
defStatusShow
.setTitle("print general status");
- parser.addCommand(defStatusShow, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantForwarderGeneral));
+ parser.addCommand(defStatusShow,
+ std::bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantForwarderGeneral));
parser.addAlias("status", "show", "");
CommandDefinition defChannelList("channel", "list");
defChannelList
.setTitle("print channel list");
- parser.addCommand(defChannelList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantChannels));
+ parser.addCommand(defChannelList,
+ std::bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantChannels));
parser.addAlias("channel", "list", "");
CommandDefinition defFibList("fib", "list");
defFibList
.setTitle("print FIB entries");
- parser.addCommand(defFibList, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantFib));
+ parser.addCommand(defFibList,
+ std::bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantFib));
parser.addAlias("fib", "list", "");
CommandDefinition defCsInfo("cs", "info");
defCsInfo
.setTitle("print CS information");
- parser.addCommand(defCsInfo, bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantCs));
+ parser.addCommand(defCsInfo,
+ std::bind(&reportStatusSingleSection, _1, &StatusReportOptions::wantCs));
parser.addAlias("cs", "info", "");
}