Improve and simplify code with modern C++ features
Change-Id: I28d84df3087492ab2ecbeb91169a2cde12c9e31e
diff --git a/src/mgmt/dispatcher.cpp b/src/mgmt/dispatcher.cpp
index 2a14a91..7352720 100644
--- a/src/mgmt/dispatcher.cpp
+++ b/src/mgmt/dispatcher.cpp
@@ -55,11 +55,8 @@
Dispatcher::~Dispatcher()
{
std::vector<Name> topPrefixNames;
- std::transform(m_topLevelPrefixes.begin(), m_topLevelPrefixes.end(),
- std::back_inserter(topPrefixNames),
- [] (const std::unordered_map<Name, TopPrefixEntry>::value_type& entry) {
- return entry.second.topPrefix;
- });
+ std::transform(m_topLevelPrefixes.begin(), m_topLevelPrefixes.end(), std::back_inserter(topPrefixNames),
+ [] (const auto& entry) { return entry.second.topPrefix; });
for (const auto& name : topPrefixNames) {
removeTopPrefix(name);
@@ -71,7 +68,7 @@
const security::SigningInfo& signingInfo)
{
bool hasOverlap = std::any_of(m_topLevelPrefixes.begin(), m_topLevelPrefixes.end(),
- [&] (const std::unordered_map<Name, TopPrefixEntry>::value_type& x) {
+ [&prefix] (const auto& x) {
return x.first.isPrefixOf(prefix) || prefix.isPrefixOf(x.first);
});
if (hasOverlap) {
@@ -92,8 +89,8 @@
for (const auto& entry : m_handlers) {
Name fullPrefix = Name(prefix).append(entry.first);
- const InterestFilterId* filter = m_face.setInterestFilter(fullPrefix, bind(entry.second, prefix, _2));
- topPrefixEntry.interestFilters.push_back(filter);
+ const auto* filterId = m_face.setInterestFilter(fullPrefix, bind(entry.second, prefix, _2));
+ topPrefixEntry.interestFilters.push_back(filterId);
}
}
@@ -121,12 +118,12 @@
{
bool hasOverlapWithHandlers =
std::any_of(m_handlers.begin(), m_handlers.end(),
- [&] (const std::unordered_map<PartialName, InterestHandler>::value_type& entry) {
+ [&] (const auto& entry) {
return entry.first.isPrefixOf(relPrefix) || relPrefix.isPrefixOf(entry.first);
});
bool hasOverlapWithStreams =
std::any_of(m_streams.begin(), m_streams.end(),
- [&] (const std::unordered_map<PartialName, uint64_t>::value_type& entry) {
+ [&] (const auto& entry) {
return entry.first.isPrefixOf(relPrefix) || relPrefix.isPrefixOf(entry.first);
});
@@ -210,8 +207,8 @@
return;
}
- AcceptContinuation accept = bind(accepted, _1, prefix, interest, parameters);
- RejectContinuation reject = bind(rejected, _1, interest);
+ AcceptContinuation accept = [=] (const auto& req) { accepted(req, prefix, interest, parameters); };
+ RejectContinuation reject = [=] (RejectReply reply) { rejected(reply, interest); };
authorization(prefix, interest, parameters.get(), accept, reject);
}
@@ -225,7 +222,7 @@
{
if (validateParams(*parameters)) {
handler(prefix, interest, *parameters,
- bind(&Dispatcher::sendControlResponse, this, _1, interest, false));
+ [=] (const auto& resp) { this->sendControlResponse(resp, interest); });
}
else {
sendControlResponse(ControlResponse(400, "failed in validating parameters"), interest);
@@ -247,8 +244,8 @@
void
Dispatcher::addStatusDataset(const PartialName& relPrefix,
- const Authorization& authorization,
- const StatusDatasetHandler& handler)
+ Authorization authorize,
+ StatusDatasetHandler handle)
{
if (!m_topLevelPrefixes.empty()) {
BOOST_THROW_EXCEPTION(std::domain_error("one or more top-level prefix has been added"));
@@ -259,14 +256,17 @@
}
AuthorizationAcceptedCallback accepted =
- bind(&Dispatcher::processAuthorizedStatusDatasetInterest, this, _1, _2, _3, handler);
+ bind(&Dispatcher::processAuthorizedStatusDatasetInterest, this, _1, _2, _3, std::move(handle));
AuthorizationRejectedCallback rejected =
bind(&Dispatcher::afterAuthorizationRejected, this, _1, _2);
// follow the general path if storage is a miss
- InterestHandler missContinuation = bind(&Dispatcher::processStatusDatasetInterest, this,
- _1, _2, authorization, accepted, rejected);
- m_handlers[relPrefix] = bind(&Dispatcher::queryStorage, this, _1, _2, missContinuation);
+ InterestHandler missContinuation = bind(&Dispatcher::processStatusDatasetInterest, this, _1, _2,
+ std::move(authorize), std::move(accepted), std::move(rejected));
+
+ m_handlers[relPrefix] = [this, miss = std::move(missContinuation)] (auto&&... args) {
+ this->queryStorage(std::forward<decltype(args)>(args)..., miss);
+ };
}
void
@@ -283,8 +283,8 @@
return;
}
- AcceptContinuation accept = bind(accepted, _1, prefix, interest, nullptr);
- RejectContinuation reject = bind(rejected, _1, interest);
+ AcceptContinuation accept = [=] (const auto& req) { accepted(req, prefix, interest, nullptr); };
+ RejectContinuation reject = [=] (RejectReply reply) { rejected(reply, interest); };
authorization(prefix, interest, nullptr, accept, reject);
}
@@ -332,10 +332,12 @@
// register a handler for the subscriber of this notification stream
// keep silent if Interest does not match a stored notification
- m_handlers[relPrefix] = bind(&Dispatcher::queryStorage, this, _1, _2, nullptr);
+ m_handlers[relPrefix] = [this] (auto&&... args) {
+ this->queryStorage(std::forward<decltype(args)>(args)..., nullptr);
+ };
m_streams[relPrefix] = 0;
- return bind(&Dispatcher::postNotification, this, _1, relPrefix);
+ return [=] (const Block& b) { postNotification(b, relPrefix); };
}
void
diff --git a/src/mgmt/dispatcher.hpp b/src/mgmt/dispatcher.hpp
index 4845b54..6eca613 100644
--- a/src/mgmt/dispatcher.hpp
+++ b/src/mgmt/dispatcher.hpp
@@ -186,9 +186,9 @@
* \param relPrefix a prefix for this command, e.g., "faces/create";
* relPrefixes in ControlCommands, StatusDatasets, NotificationStreams must be
* non-overlapping (no relPrefix is a prefix of another relPrefix)
- * \param authorization Callback to authorize the incoming commands
- * \param validateParams Callback to validate parameters of the incoming commands
- * \param handler Callback to handle the commands
+ * \param authorize Callback to authorize the incoming commands
+ * \param validate Callback to validate parameters of the incoming commands
+ * \param handle Callback to handle the commands
* \pre no top-level prefix has been added
* \throw std::out_of_range \p relPrefix overlaps with an existing relPrefix
* \throw std::domain_error one or more top-level prefix has been added
@@ -209,17 +209,17 @@
template<typename CP>
void
addControlCommand(const PartialName& relPrefix,
- const Authorization& authorization,
- const ValidateParameters& validateParams,
- const ControlCommandHandler& handler);
+ Authorization authorize,
+ ValidateParameters validate,
+ ControlCommandHandler handle);
public: // StatusDataset
/** \brief register a StatusDataset or a prefix under which StatusDatasets can be requested
* \param relPrefix a prefix for this dataset, e.g., "faces/list";
* relPrefixes in ControlCommands, StatusDatasets, NotificationStreams must be
* non-overlapping (no relPrefix is a prefix of another relPrefix)
- * \param authorization should set identity to Name() if the dataset is public
- * \param handler Callback to process the incoming dataset requests
+ * \param authorize should set identity to Name() if the dataset is public
+ * \param handle Callback to process the incoming dataset requests
* \pre no top-level prefix has been added
* \throw std::out_of_range \p relPrefix overlaps with an existing relPrefix
* \throw std::domain_error one or more top-level prefix has been added
@@ -246,8 +246,8 @@
*/
void
addStatusDataset(const PartialName& relPrefix,
- const Authorization& authorization,
- const StatusDatasetHandler& handler);
+ Authorization authorize,
+ StatusDatasetHandler handle);
public: // NotificationStream
/** \brief register a NotificationStream
@@ -440,7 +440,7 @@
struct TopPrefixEntry
{
Name topPrefix;
- optional<const RegisteredPrefixId*> registeredPrefixId = nullopt;
+ optional<const RegisteredPrefixId*> registeredPrefixId;
std::vector<const InterestFilterId*> interestFilters;
};
std::unordered_map<Name, TopPrefixEntry> m_topLevelPrefixes;
@@ -461,9 +461,9 @@
template<typename CP>
void
Dispatcher::addControlCommand(const PartialName& relPrefix,
- const Authorization& authorization,
- const ValidateParameters& validateParams,
- const ControlCommandHandler& handler)
+ Authorization authorize,
+ ValidateParameters validate,
+ ControlCommandHandler handle)
{
if (!m_topLevelPrefixes.empty()) {
BOOST_THROW_EXCEPTION(std::domain_error("one or more top-level prefix has been added"));
@@ -473,19 +473,20 @@
BOOST_THROW_EXCEPTION(std::out_of_range("relPrefix overlaps with another relPrefix"));
}
- ControlParametersParser parser = [] (const name::Component& comp) -> shared_ptr<ControlParameters> {
+ auto parser = [] (const name::Component& comp) -> shared_ptr<ControlParameters> {
return make_shared<CP>(comp.blockFromValue());
};
AuthorizationAcceptedCallback accepted =
bind(&Dispatcher::processAuthorizedControlCommandInterest, this,
- _1, _2, _3, _4, validateParams, handler);
+ _1, _2, _3, _4, std::move(validate), std::move(handle));
AuthorizationRejectedCallback rejected =
bind(&Dispatcher::afterAuthorizationRejected, this, _1, _2);
m_handlers[relPrefix] = bind(&Dispatcher::processControlCommandInterest, this,
- _1, relPrefix, _2, parser, authorization, accepted, rejected);
+ _1, relPrefix, _2, std::move(parser), std::move(authorize),
+ std::move(accepted), std::move(rejected));
}
} // namespace mgmt
diff --git a/src/mgmt/nfd/controller.hpp b/src/mgmt/nfd/controller.hpp
index 6582a66..aeb4692 100644
--- a/src/mgmt/nfd/controller.hpp
+++ b/src/mgmt/nfd/controller.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2017 Regents of the University of California.
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -74,7 +74,7 @@
start(const ControlParameters& parameters,
const CommandSucceedCallback& onSuccess,
const CommandFailCallback& onFailure,
- const CommandOptions& options = CommandOptions())
+ const CommandOptions& options = {})
{
shared_ptr<ControlCommand> command = make_shared<Command>();
this->startCommand(command, parameters, onSuccess, onFailure, options);
@@ -83,10 +83,10 @@
/** \brief start dataset fetching
*/
template<typename Dataset>
- typename std::enable_if<std::is_default_constructible<Dataset>::value>::type
+ std::enable_if_t<std::is_default_constructible<Dataset>::value>
fetch(const std::function<void(typename Dataset::ResultType)>& onSuccess,
const DatasetFailCallback& onFailure,
- const CommandOptions& options = CommandOptions())
+ const CommandOptions& options = {})
{
this->fetchDataset(make_shared<Dataset>(), onSuccess, onFailure, options);
}
@@ -98,7 +98,7 @@
fetch(const ParamType& param,
const std::function<void(typename Dataset::ResultType)>& onSuccess,
const DatasetFailCallback& onFailure,
- const CommandOptions& options = CommandOptions())
+ const CommandOptions& options = {})
{
this->fetchDataset(make_shared<Dataset>(param), onSuccess, onFailure, options);
}