security: move shared_ptr<PibImpl> when possible

Also rename m_impl to m_pib, since it's not a pimpl pointer.

Change-Id: I9e10ada54332318154374f61df6e387348ccc60a
diff --git a/src/security/pib/certificate-container.cpp b/src/security/pib/certificate-container.cpp
index e68b0cc..a671cb0 100644
--- a/src/security/pib/certificate-container.cpp
+++ b/src/security/pib/certificate-container.cpp
@@ -71,7 +71,7 @@
   bool isOtherEnd = other.m_container == nullptr || other.m_it == other.m_container->m_certNames.end();
   return ((isThisEnd || isOtherEnd) ?
           (isThisEnd == isOtherEnd) :
-          m_container->m_impl == other.m_container->m_impl && m_it == other.m_it);
+          m_container->m_pib == other.m_container->m_pib && m_it == other.m_it);
 }
 
 bool
@@ -80,12 +80,12 @@
   return !(*this == other);
 }
 
-CertificateContainer::CertificateContainer(const Name& keyName, shared_ptr<PibImpl> impl)
+CertificateContainer::CertificateContainer(const Name& keyName, shared_ptr<PibImpl> pibImpl)
   : m_keyName(keyName)
-  , m_impl(impl)
+  , m_pib(std::move(pibImpl))
 {
-  BOOST_ASSERT(impl != nullptr);
-  m_certNames = impl->getCertificatesOfKey(keyName);
+  BOOST_ASSERT(m_pib != nullptr);
+  m_certNames = m_pib->getCertificatesOfKey(keyName);
 }
 
 CertificateContainer::const_iterator
@@ -122,7 +122,7 @@
   const Name& certName = certificate.getName();
   m_certNames.insert(certName);
   m_certs[certName] = certificate;
-  m_impl->addCertificate(certificate);
+  m_pib->addCertificate(certificate);
 }
 
 void
@@ -136,7 +136,7 @@
 
   m_certNames.erase(certName);
   m_certs.erase(certName);
-  m_impl->removeCertificate(certName);
+  m_pib->removeCertificate(certName);
 }
 
 v2::Certificate
@@ -153,14 +153,14 @@
                                                 "is invalid or does not match key name"));
   }
 
-  m_certs[certName] = m_impl->getCertificate(certName);
+  m_certs[certName] = m_pib->getCertificate(certName);
   return m_certs[certName];
 }
 
 bool
 CertificateContainer::isConsistent() const
 {
-  return m_certNames == m_impl->getCertificatesOfKey(m_keyName);
+  return m_certNames == m_pib->getCertificatesOfKey(m_keyName);
 }
 
 } // namespace pib
diff --git a/src/security/pib/certificate-container.hpp b/src/security/pib/certificate-container.hpp
index 42e9534..cc1798a 100644
--- a/src/security/pib/certificate-container.hpp
+++ b/src/security/pib/certificate-container.hpp
@@ -22,9 +22,10 @@
 #ifndef NDN_SECURITY_PIB_CERTIFICATE_CONTAINER_HPP
 #define NDN_SECURITY_PIB_CERTIFICATE_CONTAINER_HPP
 
+#include "../v2/certificate.hpp"
+
 #include <set>
 #include <unordered_map>
-#include "../v2/certificate.hpp"
 
 namespace ndn {
 namespace security {
@@ -114,7 +115,6 @@
 
   /**
    * @brief Check if the container is consistent with the backend storage
-   *
    * @note this method is heavyweight and should be used in debugging mode only.
    */
   bool
@@ -123,9 +123,9 @@
 NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   /**
    * @brief Create certificate container for a key with @p keyName
-   * @param impl The PIB backend implementation.
+   * @param pibImpl The PIB backend implementation.
    */
-  CertificateContainer(const Name& keyName, shared_ptr<PibImpl> impl);
+  CertificateContainer(const Name& keyName, shared_ptr<PibImpl> pibImpl);
 
   const std::set<Name>&
   getCertNames() const
@@ -145,7 +145,7 @@
   /// @brief Cache of loaded certificates
   mutable std::unordered_map<Name, v2::Certificate> m_certs;
 
-  shared_ptr<PibImpl> m_impl;
+  shared_ptr<PibImpl> m_pib;
 
   friend class detail::KeyImpl;
 };
diff --git a/src/security/pib/detail/identity-impl.cpp b/src/security/pib/detail/identity-impl.cpp
index 284f14c..45b07b0 100644
--- a/src/security/pib/detail/identity-impl.cpp
+++ b/src/security/pib/detail/identity-impl.cpp
@@ -28,18 +28,18 @@
 namespace pib {
 namespace detail {
 
-IdentityImpl::IdentityImpl(const Name& identityName, shared_ptr<PibImpl> impl, bool needInit)
+IdentityImpl::IdentityImpl(const Name& identityName, shared_ptr<PibImpl> pibImpl, bool needInit)
   : m_name(identityName)
+  , m_pib(std::move(pibImpl))
+  , m_keys(identityName, m_pib)
   , m_isDefaultKeyLoaded(false)
-  , m_keys(identityName, impl)
-  , m_impl(impl)
 {
-  BOOST_ASSERT(impl != nullptr);
+  BOOST_ASSERT(m_pib != nullptr);
 
   if (needInit) {
-    m_impl->addIdentity(m_name);
+    m_pib->addIdentity(m_name);
   }
-  else if (!m_impl->hasIdentity(m_name)) {
+  else if (!m_pib->hasIdentity(m_name)) {
     BOOST_THROW_EXCEPTION(Pib::Error("Identity " + m_name.toUri() + " does not exist"));
   }
 }
@@ -48,7 +48,6 @@
 IdentityImpl::addKey(const uint8_t* key, size_t keyLen, const Name& keyName)
 {
   BOOST_ASSERT(m_keys.isConsistent());
-
   return m_keys.add(key, keyLen, keyName);
 }
 
@@ -67,7 +66,6 @@
 IdentityImpl::getKey(const Name& keyName) const
 {
   BOOST_ASSERT(m_keys.isConsistent());
-
   return m_keys.get(keyName);
 }
 
@@ -75,7 +73,6 @@
 IdentityImpl::getKeys() const
 {
   BOOST_ASSERT(m_keys.isConsistent());
-
   return m_keys;
 }
 
@@ -86,7 +83,7 @@
 
   m_defaultKey = m_keys.get(keyName);
   m_isDefaultKeyLoaded = true;
-  m_impl->setDefaultKeyOfIdentity(m_name, keyName);
+  m_pib->setDefaultKeyOfIdentity(m_name, keyName);
   return m_defaultKey;
 }
 
@@ -103,11 +100,10 @@
   BOOST_ASSERT(m_keys.isConsistent());
 
   if (!m_isDefaultKeyLoaded) {
-    m_defaultKey = m_keys.get(m_impl->getDefaultKeyOfIdentity(m_name));
+    m_defaultKey = m_keys.get(m_pib->getDefaultKeyOfIdentity(m_name));
     m_isDefaultKeyLoaded = true;
   }
-
-  BOOST_ASSERT(m_impl->getDefaultKeyOfIdentity(m_name) == m_defaultKey.getName());
+  BOOST_ASSERT(m_pib->getDefaultKeyOfIdentity(m_name) == m_defaultKey.getName());
 
   return m_defaultKey;
 }
diff --git a/src/security/pib/detail/identity-impl.hpp b/src/security/pib/detail/identity-impl.hpp
index ceff37d..b4823a1 100644
--- a/src/security/pib/detail/identity-impl.hpp
+++ b/src/security/pib/detail/identity-impl.hpp
@@ -47,13 +47,15 @@
    * @brief Create an Identity with @p identityName.
    *
    * @param identityName The name of the Identity.
-   * @param impl The PIB backend implementation.
+   * @param pibImpl The PIB backend implementation.
    * @param needInit If true, create the identity in backend when the identity does not exist.
    *                 Otherwise, throw Pib::Error when the identity does not exist.
    */
-  IdentityImpl(const Name& identityName, shared_ptr<PibImpl> impl, bool needInit = false);
+  IdentityImpl(const Name& identityName, shared_ptr<PibImpl> pibImpl, bool needInit = false);
 
-  /// @brief Get the name of the identity.
+  /**
+   * @brief Get the name of the identity.
+   */
   const Name&
   getName() const
   {
@@ -122,12 +124,11 @@
 private:
   Name m_name;
 
+  shared_ptr<PibImpl> m_pib;
+
+  KeyContainer m_keys;
   mutable bool m_isDefaultKeyLoaded;
   mutable Key m_defaultKey;
-
-  mutable KeyContainer m_keys;
-
-  shared_ptr<PibImpl> m_impl;
 };
 
 } // namespace detail
diff --git a/src/security/pib/detail/key-impl.cpp b/src/security/pib/detail/key-impl.cpp
index c0cd839..e88bdac 100644
--- a/src/security/pib/detail/key-impl.cpp
+++ b/src/security/pib/detail/key-impl.cpp
@@ -29,15 +29,15 @@
 namespace pib {
 namespace detail {
 
-KeyImpl::KeyImpl(const Name& keyName, const uint8_t* key, size_t keyLen, shared_ptr<PibImpl> impl)
+KeyImpl::KeyImpl(const Name& keyName, const uint8_t* key, size_t keyLen, shared_ptr<PibImpl> pibImpl)
   : m_identity(v2::extractIdentityFromKeyName(keyName))
   , m_keyName(keyName)
   , m_key(key, keyLen)
+  , m_pib(std::move(pibImpl))
+  , m_certificates(keyName, m_pib)
   , m_isDefaultCertificateLoaded(false)
-  , m_certificates(keyName, impl)
-  , m_impl(impl)
 {
-  BOOST_ASSERT(impl != nullptr);
+  BOOST_ASSERT(m_pib != nullptr);
 
   transform::PublicKey publicKey;
   try {
@@ -48,19 +48,19 @@
   }
   m_keyType = publicKey.getKeyType();
 
-  m_impl->addKey(m_identity, m_keyName, key, keyLen);
+  m_pib->addKey(m_identity, m_keyName, key, keyLen);
 }
 
-KeyImpl::KeyImpl(const Name& keyName, shared_ptr<PibImpl> impl)
+KeyImpl::KeyImpl(const Name& keyName, shared_ptr<PibImpl> pibImpl)
   : m_identity(v2::extractIdentityFromKeyName(keyName))
   , m_keyName(keyName)
+  , m_pib(std::move(pibImpl))
+  , m_certificates(keyName, m_pib)
   , m_isDefaultCertificateLoaded(false)
-  , m_certificates(keyName, impl)
-  , m_impl(impl)
 {
-  BOOST_ASSERT(impl != nullptr);
+  BOOST_ASSERT(m_pib != nullptr);
 
-  m_key = m_impl->getKeyBits(m_keyName);
+  m_key = m_pib->getKeyBits(m_keyName);
 
   transform::PublicKey key;
   key.loadPkcs8(m_key.buf(), m_key.size());
@@ -89,7 +89,6 @@
 KeyImpl::getCertificate(const Name& certName) const
 {
   BOOST_ASSERT(m_certificates.isConsistent());
-
   return m_certificates.get(certName);
 }
 
@@ -97,7 +96,6 @@
 KeyImpl::getCertificates() const
 {
   BOOST_ASSERT(m_certificates.isConsistent());
-
   return m_certificates;
 }
 
@@ -107,7 +105,7 @@
   BOOST_ASSERT(m_certificates.isConsistent());
 
   m_defaultCertificate = m_certificates.get(certName);
-  m_impl->setDefaultCertificateOfKey(m_keyName, certName);
+  m_pib->setDefaultCertificateOfKey(m_keyName, certName);
   m_isDefaultCertificateLoaded = true;
   return m_defaultCertificate;
 }
@@ -125,11 +123,10 @@
   BOOST_ASSERT(m_certificates.isConsistent());
 
   if (!m_isDefaultCertificateLoaded) {
-    m_defaultCertificate = m_impl->getDefaultCertificateOfKey(m_keyName);
+    m_defaultCertificate = m_pib->getDefaultCertificateOfKey(m_keyName);
     m_isDefaultCertificateLoaded = true;
   }
-
-  BOOST_ASSERT(m_impl->getDefaultCertificateOfKey(m_keyName).wireEncode() == m_defaultCertificate.wireEncode());
+  BOOST_ASSERT(m_pib->getDefaultCertificateOfKey(m_keyName).wireEncode() == m_defaultCertificate.wireEncode());
 
   return m_defaultCertificate;
 }
diff --git a/src/security/pib/detail/key-impl.hpp b/src/security/pib/detail/key-impl.hpp
index a7e683f..efc69b2 100644
--- a/src/security/pib/detail/key-impl.hpp
+++ b/src/security/pib/detail/key-impl.hpp
@@ -22,9 +22,8 @@
 #ifndef NDN_SECURITY_PIB_DETAIL_KEY_IMPL_HPP
 #define NDN_SECURITY_PIB_DETAIL_KEY_IMPL_HPP
 
-#include "../../../data.hpp"
-#include "../certificate-container.hpp"
 #include "../../security-common.hpp"
+#include "../certificate-container.hpp"
 
 namespace ndn {
 namespace security {
@@ -53,21 +52,23 @@
    * @param keyName The name of the key.
    * @param key The public key to add.
    * @param keyLen The length of the key.
-   * @param impl The Pib backend implementation.
+   * @param pibImpl The Pib backend implementation.
    * @throw Pib::Error a key with the same @p keyName already exists.
    */
-  KeyImpl(const Name& keyName, const uint8_t* key, size_t keyLen, shared_ptr<PibImpl> impl);
+  KeyImpl(const Name& keyName, const uint8_t* key, size_t keyLen, shared_ptr<PibImpl> pibImpl);
 
   /**
    * @brief Create a KeyImpl with @p keyName.
    *
    * @param keyName The name of the key.
-   * @param impl The Pib backend implementation.
+   * @param pibImpl The Pib backend implementation.
    * @throw Pib::Error the key does not exist.
    */
-  KeyImpl(const Name& keyName, shared_ptr<PibImpl> impl);
+  KeyImpl(const Name& keyName, shared_ptr<PibImpl> pibImpl);
 
-  /// @brief Get the name of the key.
+  /**
+   * @brief Get the name of the key.
+   */
   const Name&
   getName() const
   {
@@ -130,7 +131,9 @@
   v2::Certificate
   getCertificate(const Name& certName) const;
 
-  /// @brief Get all the certificates for this key.
+  /**
+   * @brief Get all the certificates for this key.
+   */
   const CertificateContainer&
   getCertificates() const;
 
@@ -165,12 +168,11 @@
   Buffer m_key;
   KeyType m_keyType;
 
-  mutable bool m_isDefaultCertificateLoaded;
-  mutable v2::Certificate m_defaultCertificate;
+  shared_ptr<PibImpl> m_pib;
 
   CertificateContainer m_certificates;
-
-  shared_ptr<PibImpl> m_impl;
+  mutable bool m_isDefaultCertificateLoaded;
+  mutable v2::Certificate m_defaultCertificate;
 };
 
 } // namespace detail
diff --git a/src/security/pib/key-container.cpp b/src/security/pib/key-container.cpp
index 4084cd3..7dc8358 100644
--- a/src/security/pib/key-container.cpp
+++ b/src/security/pib/key-container.cpp
@@ -71,7 +71,7 @@
   bool isOtherEnd = other.m_container == nullptr || other.m_it == other.m_container->m_keyNames.end();
   return ((isThisEnd || isOtherEnd) ?
           (isThisEnd == isOtherEnd) :
-          m_container->m_impl == other.m_container->m_impl && m_it == other.m_it);
+          m_container->m_pib == other.m_container->m_pib && m_it == other.m_it);
 }
 
 bool
@@ -80,12 +80,12 @@
   return !(*this == other);
 }
 
-KeyContainer::KeyContainer(const Name& identity, shared_ptr<PibImpl> impl)
+KeyContainer::KeyContainer(const Name& identity, shared_ptr<PibImpl> pibImpl)
   : m_identity(identity)
-  , m_impl(impl)
+  , m_pib(std::move(pibImpl))
 {
-  BOOST_ASSERT(impl != nullptr);
-  m_keyNames = impl->getKeysOfIdentity(identity);
+  BOOST_ASSERT(m_pib != nullptr);
+  m_keyNames = m_pib->getKeysOfIdentity(identity);
 }
 
 KeyContainer::const_iterator
@@ -121,7 +121,7 @@
   }
 
   m_keyNames.insert(keyName);
-  m_keys[keyName] = shared_ptr<detail::KeyImpl>(new detail::KeyImpl(keyName, key, keyLen, m_impl));
+  m_keys[keyName] = shared_ptr<detail::KeyImpl>(new detail::KeyImpl(keyName, key, keyLen, m_pib));
 
   return get(keyName);
 }
@@ -136,7 +136,7 @@
 
   m_keyNames.erase(keyName);
   m_keys.erase(keyName);
-  m_impl->removeKey(keyName);
+  m_pib->removeKey(keyName);
 }
 
 Key
@@ -154,7 +154,7 @@
     key = it->second;
   }
   else {
-    key = shared_ptr<detail::KeyImpl>(new detail::KeyImpl(keyName, m_impl));
+    key = shared_ptr<detail::KeyImpl>(new detail::KeyImpl(keyName, m_pib));
     m_keys[keyName] = key;
   }
 
@@ -164,7 +164,7 @@
 bool
 KeyContainer::isConsistent() const
 {
-  return m_keyNames == m_impl->getKeysOfIdentity(m_identity);
+  return m_keyNames == m_pib->getKeysOfIdentity(m_identity);
 }
 
 } // namespace pib
diff --git a/src/security/pib/key-container.hpp b/src/security/pib/key-container.hpp
index 0c88249..55a799e 100644
--- a/src/security/pib/key-container.hpp
+++ b/src/security/pib/key-container.hpp
@@ -22,9 +22,10 @@
 #ifndef NDN_SECURITY_PIB_KEY_CONTAINER_HPP
 #define NDN_SECURITY_PIB_KEY_CONTAINER_HPP
 
+#include "key.hpp"
+
 #include <set>
 #include <unordered_map>
-#include "key.hpp"
 
 namespace ndn {
 namespace security {
@@ -126,9 +127,9 @@
 NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   /**
    * @brief Create key container for @p identity
-   * @param impl The PIB backend implementation.
+   * @param pibImpl The PIB backend implementation.
    */
-  KeyContainer(const Name& identity, shared_ptr<PibImpl> impl);
+  KeyContainer(const Name& identity, shared_ptr<PibImpl> pibImpl);
 
   const std::set<Name>&
   getKeyNames() const
@@ -148,7 +149,8 @@
   /// @brief Cache of loaded detail::KeyImpl.
   mutable std::unordered_map<Name, shared_ptr<detail::KeyImpl>> m_keys;
 
-  shared_ptr<PibImpl> m_impl;
+  shared_ptr<PibImpl> m_pib;
+
   friend class detail::IdentityImpl;
 };