Use std::move in more places

Plus various code simplifications

Change-Id: I19805e4a635e4c74afaff68f9d8968475217ec6e
diff --git a/src/mgmt/nfd/controller.cpp b/src/mgmt/nfd/controller.cpp
index 7ed8005..1af70b7 100644
--- a/src/mgmt/nfd/controller.cpp
+++ b/src/mgmt/nfd/controller.cpp
@@ -22,7 +22,6 @@
 #include "controller.hpp"
 #include "face.hpp"
 #include "security/v2/key-chain.hpp"
-#include "security/validator-null.hpp"
 #include "util/segment-fetcher.hpp"
 
 #include <boost/lexical_cast.hpp>
@@ -49,28 +48,25 @@
 void
 Controller::startCommand(const shared_ptr<ControlCommand>& command,
                          const ControlParameters& parameters,
-                         const CommandSucceedCallback& onSuccess1,
-                         const CommandFailCallback& onFailure1,
+                         const CommandSucceedCallback& onSuccess,
+                         const CommandFailCallback& onFailure,
                          const CommandOptions& options)
 {
-  const CommandSucceedCallback& onSuccess = onSuccess1 ?
-    onSuccess1 : [] (const ControlParameters&) {};
-  const CommandFailCallback& onFailure = onFailure1 ?
-    onFailure1 : [] (const ControlResponse&) {};
-
   Name requestName = command->getRequestName(options.getPrefix(), parameters);
   Interest interest = m_signer.makeCommandInterest(requestName, options.getSigningInfo());
   interest.setInterestLifetime(options.getTimeout());
 
   m_face.expressInterest(interest,
     [=] (const Interest&, const Data& data) {
-      this->processCommandResponse(data, command, onSuccess, onFailure);
+      processCommandResponse(data, command, onSuccess, onFailure);
     },
     [=] (const Interest&, const lp::Nack&) {
-      onFailure(ControlResponse(Controller::ERROR_NACK, "network Nack received"));
+      if (onFailure)
+        onFailure(ControlResponse(Controller::ERROR_NACK, "network Nack received"));
     },
     [=] (const Interest&) {
-      onFailure(ControlResponse(Controller::ERROR_TIMEOUT, "request timed out"));
+      if (onFailure)
+        onFailure(ControlResponse(Controller::ERROR_TIMEOUT, "request timed out"));
     });
 }
 
@@ -82,10 +78,11 @@
 {
   m_validator.validate(data,
     [=] (const Data& data) {
-      this->processValidatedCommandResponse(data, command, onSuccess, onFailure);
+      processValidatedCommandResponse(data, command, onSuccess, onFailure);
     },
-    [=] (const Data& data, const security::v2::ValidationError& error) {
-      onFailure(ControlResponse(ERROR_VALIDATION, boost::lexical_cast<std::string>(error)));
+    [=] (const Data&, const auto& error) {
+      if (onFailure)
+        onFailure(ControlResponse(ERROR_VALIDATION, boost::lexical_cast<std::string>(error)));
     }
   );
 }
@@ -101,13 +98,15 @@
     response.wireDecode(data.getContent().blockFromValue());
   }
   catch (const tlv::Error& e) {
-    onFailure(ControlResponse(ERROR_SERVER, e.what()));
+    if (onFailure)
+      onFailure(ControlResponse(ERROR_SERVER, e.what()));
     return;
   }
 
   uint32_t code = response.getCode();
   if (code >= ERROR_LBOUND) {
-    onFailure(response);
+    if (onFailure)
+      onFailure(response);
     return;
   }
 
@@ -116,7 +115,8 @@
     parameters.wireDecode(response.getBody());
   }
   catch (const tlv::Error& e) {
-    onFailure(ControlResponse(ERROR_SERVER, e.what()));
+    if (onFailure)
+      onFailure(ControlResponse(ERROR_SERVER, e.what()));
     return;
   }
 
@@ -124,11 +124,13 @@
     command->validateResponse(parameters);
   }
   catch (const ControlCommand::ArgumentError& e) {
-    onFailure(ControlResponse(ERROR_SERVER, e.what()));
+    if (onFailure)
+      onFailure(ControlResponse(ERROR_SERVER, e.what()));
     return;
   }
 
-  onSuccess(parameters);
+  if (onSuccess)
+    onSuccess(parameters);
 }
 
 void
@@ -137,21 +139,26 @@
                          const DatasetFailCallback& onFailure,
                          const CommandOptions& options)
 {
-  Interest baseInterest(prefix);
-
   SegmentFetcher::Options fetcherOptions;
   fetcherOptions.maxTimeout = options.getTimeout();
-  auto fetcher = SegmentFetcher::start(m_face, baseInterest, m_validator, fetcherOptions);
-  fetcher->onComplete.connect(processResponse);
-  fetcher->onError.connect([=] (uint32_t code, const std::string& msg) {
+
+  auto fetcher = SegmentFetcher::start(m_face, Interest(prefix), m_validator, fetcherOptions);
+  if (processResponse) {
+    fetcher->onComplete.connect(processResponse);
+  }
+  if (onFailure) {
+    fetcher->onError.connect([=] (uint32_t code, const std::string& msg) {
       processDatasetFetchError(onFailure, code, msg);
     });
+  }
 }
 
 void
 Controller::processDatasetFetchError(const DatasetFailCallback& onFailure,
                                      uint32_t code, std::string msg)
 {
+  BOOST_ASSERT(onFailure);
+
   switch (static_cast<SegmentFetcher::ErrorCode>(code)) {
     // It's intentional to cast as SegmentFetcher::ErrorCode, and to not have a 'default' clause.
     // This forces the switch statement to handle every defined SegmentFetcher::ErrorCode,
diff --git a/src/mgmt/nfd/controller.hpp b/src/mgmt/nfd/controller.hpp
index 8797b39..0553df1 100644
--- a/src/mgmt/nfd/controller.hpp
+++ b/src/mgmt/nfd/controller.hpp
@@ -52,20 +52,21 @@
 public:
   /** \brief a callback on command success
    */
-  typedef function<void(const ControlParameters&)> CommandSucceedCallback;
+  using CommandSucceedCallback = function<void(const ControlParameters&)>;
 
   /** \brief a callback on command failure
    */
-  typedef function<void(const ControlResponse&)> CommandFailCallback;
+  using CommandFailCallback = function<void(const ControlResponse&)>;
 
   /** \brief a callback on dataset retrieval failure
    */
-  typedef function<void(uint32_t code, const std::string& reason)> DatasetFailCallback;
+  using DatasetFailCallback = function<void(uint32_t code, const std::string& reason)>;
 
   /** \brief construct a Controller that uses face for transport,
    *         and uses the passed KeyChain to sign commands
    */
-  Controller(Face& face, KeyChain& keyChain, security::v2::Validator& validator = security::getAcceptAllValidator());
+  Controller(Face& face, KeyChain& keyChain,
+             security::v2::Validator& validator = security::getAcceptAllValidator());
 
   /** \brief start command execution
    */
@@ -76,8 +77,7 @@
         const CommandFailCallback& onFailure,
         const CommandOptions& options = CommandOptions())
   {
-    shared_ptr<ControlCommand> command = make_shared<Command>();
-    this->startCommand(command, parameters, onSuccess, onFailure, options);
+    startCommand(make_shared<Command>(), parameters, onSuccess, onFailure, options);
   }
 
   /** \brief start dataset fetching
@@ -88,7 +88,7 @@
         const DatasetFailCallback& onFailure,
         const CommandOptions& options = CommandOptions())
   {
-    this->fetchDataset(make_shared<Dataset>(), onSuccess, onFailure, options);
+    fetchDataset(make_shared<Dataset>(), onSuccess, onFailure, options);
   }
 
   /** \brief start dataset fetching
@@ -100,7 +100,7 @@
         const DatasetFailCallback& onFailure,
         const CommandOptions& options = CommandOptions())
   {
-    this->fetchDataset(make_shared<Dataset>(param), onSuccess, onFailure, options);
+    fetchDataset(make_shared<Dataset>(param), onSuccess, onFailure, options);
   }
 
 private:
@@ -175,41 +175,40 @@
 };
 
 template<typename Dataset>
-inline void
+void
 Controller::fetchDataset(shared_ptr<Dataset> dataset,
-                         const std::function<void(typename Dataset::ResultType)>& onSuccess1,
-                         const DatasetFailCallback& onFailure1,
+                         const std::function<void(typename Dataset::ResultType)>& onSuccess,
+                         const DatasetFailCallback& onFailure,
                          const CommandOptions& options)
 {
-  const std::function<void(typename Dataset::ResultType)>& onSuccess = onSuccess1 ?
-    onSuccess1 : [] (const typename Dataset::ResultType&) {};
-  const DatasetFailCallback& onFailure = onFailure1 ?
-    onFailure1 : [] (uint32_t, const std::string&) {};
-
   Name prefix = dataset->getDatasetPrefix(options.getPrefix());
-  this->fetchDataset(prefix,
-                     bind(&Controller::processDatasetResponse<Dataset>, this, dataset, onSuccess, onFailure, _1),
-                     onFailure,
-                     options);
+  fetchDataset(prefix,
+    [=, d = std::move(dataset)] (ConstBufferPtr p) {
+      processDatasetResponse(std::move(d), onSuccess, onFailure, std::move(p));
+    },
+    onFailure, options);
 }
 
 template<typename Dataset>
-inline void
+void
 Controller::processDatasetResponse(shared_ptr<Dataset> dataset,
                                    const std::function<void(typename Dataset::ResultType)>& onSuccess,
                                    const DatasetFailCallback& onFailure,
                                    ConstBufferPtr payload)
 {
   typename Dataset::ResultType result;
+
   try {
-    result = dataset->parseResult(payload);
+    result = dataset->parseResult(std::move(payload));
   }
   catch (const tlv::Error& e) {
-    onFailure(ERROR_SERVER, e.what());
+    if (onFailure)
+      onFailure(ERROR_SERVER, e.what());
     return;
   }
 
-  onSuccess(result);
+  if (onSuccess)
+    onSuccess(result);
 }
 
 } // namespace nfd
diff --git a/src/mgmt/nfd/status-dataset.cpp b/src/mgmt/nfd/status-dataset.cpp
index 5b6d66d..88ce648 100644
--- a/src/mgmt/nfd/status-dataset.cpp
+++ b/src/mgmt/nfd/status-dataset.cpp
@@ -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).
  *
@@ -85,7 +85,7 @@
 ForwarderGeneralStatusDataset::ResultType
 ForwarderGeneralStatusDataset::parseResult(ConstBufferPtr payload) const
 {
-  return ForwarderStatus(Block(tlv::Content, payload));
+  return ForwarderStatus(Block(tlv::Content, std::move(payload)));
 }
 
 FaceDatasetBase::FaceDatasetBase(const PartialName& datasetName)
@@ -96,7 +96,7 @@
 FaceDatasetBase::ResultType
 FaceDatasetBase::parseResult(ConstBufferPtr payload) const
 {
-  return parseDatasetVector<FaceStatus>(payload);
+  return parseDatasetVector<FaceStatus>(std::move(payload));
 }
 
 FaceDataset::FaceDataset()
@@ -124,7 +124,7 @@
 ChannelDataset::ResultType
 ChannelDataset::parseResult(ConstBufferPtr payload) const
 {
-  return parseDatasetVector<ChannelStatus>(payload);
+  return parseDatasetVector<ChannelStatus>(std::move(payload));
 }
 
 FibDataset::FibDataset()
@@ -135,7 +135,7 @@
 FibDataset::ResultType
 FibDataset::parseResult(ConstBufferPtr payload) const
 {
-  return parseDatasetVector<FibEntry>(payload);
+  return parseDatasetVector<FibEntry>(std::move(payload));
 }
 
 CsInfoDataset::CsInfoDataset()
@@ -146,7 +146,7 @@
 CsInfoDataset::ResultType
 CsInfoDataset::parseResult(ConstBufferPtr payload) const
 {
-  return CsInfo(Block(payload));
+  return CsInfo(Block(std::move(payload)));
 }
 
 StrategyChoiceDataset::StrategyChoiceDataset()
@@ -157,7 +157,7 @@
 StrategyChoiceDataset::ResultType
 StrategyChoiceDataset::parseResult(ConstBufferPtr payload) const
 {
-  return parseDatasetVector<StrategyChoice>(payload);
+  return parseDatasetVector<StrategyChoice>(std::move(payload));
 }
 
 RibDataset::RibDataset()
@@ -168,7 +168,7 @@
 RibDataset::ResultType
 RibDataset::parseResult(ConstBufferPtr payload) const
 {
-  return parseDatasetVector<RibEntry>(payload);
+  return parseDatasetVector<RibEntry>(std::move(payload));
 }
 
 } // namespace nfd