Use std::move in more places

Plus various code simplifications

Change-Id: I19805e4a635e4c74afaff68f9d8968475217ec6e
diff --git a/src/security/pib/identity-container.cpp b/src/security/pib/identity-container.cpp
index 7c018ce..0e4749f 100644
--- a/src/security/pib/identity-container.cpp
+++ b/src/security/pib/identity-container.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).
  *
@@ -81,10 +81,10 @@
 }
 
 IdentityContainer::IdentityContainer(shared_ptr<PibImpl> pibImpl)
-  : m_pibImpl(pibImpl)
+  : m_pibImpl(std::move(pibImpl))
 {
-  BOOST_ASSERT(pibImpl != nullptr);
-  m_identityNames = pibImpl->getIdentities();
+  BOOST_ASSERT(m_pibImpl != nullptr);
+  m_identityNames = m_pibImpl->getIdentities();
 }
 
 IdentityContainer::const_iterator
@@ -116,8 +116,7 @@
 {
   if (m_identityNames.count(identityName) == 0) {
     m_identityNames.insert(identityName);
-    m_identities[identityName] =
-      shared_ptr<detail::IdentityImpl>(new detail::IdentityImpl(identityName, m_pibImpl, true));
+    m_identities[identityName] = make_shared<detail::IdentityImpl>(identityName, m_pibImpl, true);
   }
   return get(identityName);
 }
@@ -140,7 +139,7 @@
     id = it->second;
   }
   else {
-    id = shared_ptr<detail::IdentityImpl>(new detail::IdentityImpl(identityName, m_pibImpl, false));
+    id = make_shared<detail::IdentityImpl>(identityName, m_pibImpl, false);
     m_identities[identityName] = id;
   }
   return Identity(id);
diff --git a/src/security/pib/key-container.cpp b/src/security/pib/key-container.cpp
index 7dc8358..ee47e37 100644
--- a/src/security/pib/key-container.cpp
+++ b/src/security/pib/key-container.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).
  *
@@ -121,7 +121,7 @@
   }
 
   m_keyNames.insert(keyName);
-  m_keys[keyName] = shared_ptr<detail::KeyImpl>(new detail::KeyImpl(keyName, key, keyLen, m_pib));
+  m_keys[keyName] = make_shared<detail::KeyImpl>(keyName, key, keyLen, m_pib);
 
   return get(keyName);
 }
@@ -154,7 +154,7 @@
     key = it->second;
   }
   else {
-    key = shared_ptr<detail::KeyImpl>(new detail::KeyImpl(keyName, m_pib));
+    key = make_shared<detail::KeyImpl>(keyName, m_pib);
     m_keys[keyName] = key;
   }
 
diff --git a/src/security/pib/pib.cpp b/src/security/pib/pib.cpp
index 68b9add..4fba641 100644
--- a/src/security/pib/pib.cpp
+++ b/src/security/pib/pib.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).
  *
@@ -34,9 +34,9 @@
   , m_location(location)
   , m_isDefaultIdentityLoaded(false)
   , m_identities(impl)
-  , m_impl(impl)
+  , m_impl(std::move(impl))
 {
-  BOOST_ASSERT(impl != nullptr);
+  BOOST_ASSERT(m_impl != nullptr);
 }
 
 Pib::~Pib() = default;
diff --git a/src/security/pib/pib.hpp b/src/security/pib/pib.hpp
index 947e5a5..27d83da 100644
--- a/src/security/pib/pib.hpp
+++ b/src/security/pib/pib.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).
  *
@@ -67,7 +67,7 @@
   ~Pib();
 
   /**
-   * @brief return the scheme of the PibLocator
+   * @brief return the scheme of the PIB Locator
    */
   std::string
   getScheme() const
@@ -110,19 +110,21 @@
   Identity
   getIdentity(const Name& identityName) const;
 
-  /// @brief Get all the identities
+  /**
+   * @brief Get all the identities
+   */
   const IdentityContainer&
   getIdentities() const;
 
   /**
    * @brief Get the default identity.
-   * @throw Pib::Error if no default identity.
+   * @throw Pib::Error if no default identity exists.
    */
   const Identity&
   getDefaultIdentity() const;
 
 NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private
-  /*
+  /**
    * @brief Create a Pib instance
    *
    * @param scheme The scheme for the Pib
@@ -131,17 +133,8 @@
    */
   Pib(const std::string& scheme, const std::string& location, shared_ptr<PibImpl> impl);
 
-  /*
-   * @brief Create an identity with name @p identityName and return a reference to it.
-   *
-   * If there already exists an identity for the name @p identityName, then it is returned.
-   * If no default identity is set, the newly created identity will be set as the default.
-   *
-   * @param identityName The name for the identity to be added
-   */
-
   /**
-   * @brief Add an @p identity.
+   * @brief Add an identity.
    *
    * If no default identity is set before, the new identity will be set as the default identity
    *
@@ -150,8 +143,8 @@
   Identity
   addIdentity(const Name& identity);
 
-  /*
-   * @brief Remove an @p identity.
+  /**
+   * @brief Remove an identity.
    *
    * If the default identity is being removed, no default identity will be selected.
    */
@@ -159,7 +152,7 @@
   removeIdentity(const Name& identity);
 
   /**
-   * @brief Set an @p identity as the default identity.
+   * @brief Set an identity as the default identity.
    *
    * Create the identity if it does not exist.
    *
diff --git a/src/security/tpm/back-end-file.cpp b/src/security/tpm/back-end-file.cpp
index a560fad..12c1e37 100644
--- a/src/security/tpm/back-end-file.cpp
+++ b/src/security/tpm/back-end-file.cpp
@@ -125,11 +125,11 @@
   setKeyName(*keyHandle, identityName, params);
 
   try {
-    saveKey(keyHandle->getKeyName(), key);
+    saveKey(keyHandle->getKeyName(), *key);
     return keyHandle;
   }
   catch (const std::runtime_error& e) {
-    BOOST_THROW_EXCEPTION(Error("Cannot write key to disk: "s + e.what()));
+    BOOST_THROW_EXCEPTION(Error("Cannot write key to file: "s + e.what()));
   }
 }
 
@@ -137,27 +137,28 @@
 BackEndFile::doDeleteKey(const Name& keyName)
 {
   boost::filesystem::path keyPath(m_impl->toFileName(keyName));
+  if (!boost::filesystem::exists(keyPath))
+    return;
 
-  if (boost::filesystem::exists(keyPath)) {
-    try {
-      boost::filesystem::remove(keyPath);
-    }
-    catch (const boost::filesystem::filesystem_error&) {
-      BOOST_THROW_EXCEPTION(Error("Cannot delete key"));
-    }
+  try {
+    boost::filesystem::remove(keyPath);
+  }
+  catch (const boost::filesystem::filesystem_error& e) {
+    BOOST_THROW_EXCEPTION(Error("Cannot remove key file: "s + e.what()));
   }
 }
 
 ConstBufferPtr
 BackEndFile::doExportKey(const Name& keyName, const char* pw, size_t pwLen)
 {
-  shared_ptr<PrivateKey> key;
+  unique_ptr<PrivateKey> key;
   try {
     key = loadKey(keyName);
   }
-  catch (const PrivateKey::Error&) {
-    BOOST_THROW_EXCEPTION(Error("Cannot export private key"));
+  catch (const PrivateKey::Error& e) {
+    BOOST_THROW_EXCEPTION(Error("Cannot export private key: "s + e.what()));
   }
+
   OBufferStream os;
   key->savePkcs8(os, pw, pwLen);
   return os.buf();
@@ -167,33 +168,33 @@
 BackEndFile::doImportKey(const Name& keyName, const uint8_t* buf, size_t size, const char* pw, size_t pwLen)
 {
   try {
-    auto key = make_shared<PrivateKey>();
-    key->loadPkcs8(buf, size, pw, pwLen);
+    PrivateKey key;
+    key.loadPkcs8(buf, size, pw, pwLen);
     saveKey(keyName, key);
   }
-  catch (const PrivateKey::Error&) {
-    BOOST_THROW_EXCEPTION(Error("Cannot import private key"));
+  catch (const PrivateKey::Error& e) {
+    BOOST_THROW_EXCEPTION(Error("Cannot import private key: "s + e.what()));
   }
 }
 
-shared_ptr<PrivateKey>
+unique_ptr<PrivateKey>
 BackEndFile::loadKey(const Name& keyName) const
 {
-  auto key = make_shared<PrivateKey>();
-  std::fstream is(m_impl->toFileName(keyName).string(), std::ios_base::in);
+  std::ifstream is(m_impl->toFileName(keyName).string());
+  auto key = make_unique<PrivateKey>();
   key->loadPkcs1Base64(is);
   return key;
 }
 
 void
-BackEndFile::saveKey(const Name& keyName, shared_ptr<PrivateKey> key)
+BackEndFile::saveKey(const Name& keyName, const PrivateKey& key)
 {
   std::string fileName = m_impl->toFileName(keyName).string();
-  std::fstream os(fileName, std::ios_base::out);
-  key->savePkcs1Base64(os);
+  std::ofstream os(fileName);
+  key.savePkcs1Base64(os);
 
   // set file permission
-  ::chmod(fileName.c_str(), 0000400);
+  ::chmod(fileName.data(), 0000400);
 }
 
 } // namespace tpm
diff --git a/src/security/tpm/back-end-file.hpp b/src/security/tpm/back-end-file.hpp
index 5609cba..bd2838d 100644
--- a/src/security/tpm/back-end-file.hpp
+++ b/src/security/tpm/back-end-file.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).
  *
@@ -26,6 +26,7 @@
 
 namespace ndn {
 namespace security {
+
 namespace transform {
 class PrivateKey;
 } // namespace transform
@@ -123,14 +124,14 @@
   /**
    * @brief Load a private key with name @p keyName from the key file directory
    */
-  shared_ptr<transform::PrivateKey>
+  unique_ptr<transform::PrivateKey>
   loadKey(const Name& keyName) const;
 
   /**
    * @brief Save a private key with name @p keyName into the key file directory
    */
   void
-  saveKey(const Name& keyName, shared_ptr<transform::PrivateKey> key);
+  saveKey(const Name& keyName, const transform::PrivateKey& key);
 
 private:
   class Impl;
diff --git a/src/security/tpm/tpm.hpp b/src/security/tpm/tpm.hpp
index 4ce57f2..367a6b9 100644
--- a/src/security/tpm/tpm.hpp
+++ b/src/security/tpm/tpm.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).
  *
@@ -144,7 +144,7 @@
   unlockTpm(const char* password, size_t passwordLength) const;
 
 NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  /*
+  /**
    * @brief Create a new TPM instance with the specified @p location.
    *
    * @param scheme The scheme for the TPM
diff --git a/src/security/v2/trust-anchor-container.cpp b/src/security/v2/trust-anchor-container.cpp
index 80ef5cc..9137d6f 100644
--- a/src/security/v2/trust-anchor-container.cpp
+++ b/src/security/v2/trust-anchor-container.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,6 +85,7 @@
   auto cert = m_anchors.lower_bound(keyName);
   if (cert == m_anchors.end() || !keyName.isPrefixOf(cert->getName()))
     return nullptr;
+
   return &*cert;
 }
 
@@ -123,7 +124,7 @@
 TrustAnchorContainer::refresh()
 {
   for (auto it = m_groups.begin(); it != m_groups.end(); ++it) {
-    m_groups.modify(it, [] (shared_ptr<TrustAnchorGroup>& group) { group->refresh(); });
+    m_groups.modify(it, [] (const auto& group) { group->refresh(); });
   }
 }