table: attach unique_ptr<strategy_choice::Entry> onto NameTree

refs #3164

Change-Id: I5cce7e43dba77cdaaa07347ec7ffca13f242113c
diff --git a/daemon/mgmt/strategy-choice-manager.cpp b/daemon/mgmt/strategy-choice-manager.cpp
index a28e237..1f0c208 100644
--- a/daemon/mgmt/strategy-choice-manager.cpp
+++ b/daemon/mgmt/strategy-choice-manager.cpp
@@ -35,7 +35,7 @@
                                              Dispatcher& dispatcher,
                                              CommandValidator& validator)
   : NfdManagerBase(dispatcher, validator, "strategy-choice")
-  , m_strategyChoice(strategyChoice)
+  , m_table(strategyChoice)
 {
   registerCommandHandler<ndn::nfd::StrategyChoiceSetCommand>("set",
     bind(&StrategyChoiceManager::setStrategy, this, _2, _3, _4, _5));
@@ -54,15 +54,15 @@
   const Name& prefix = parameters.getName();
   const Name& selectedStrategy = parameters.getStrategy();
 
-  if (!m_strategyChoice.hasStrategy(selectedStrategy)) {
+  if (!m_table.hasStrategy(selectedStrategy)) {
     NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unknown-strategy: "
                   << parameters.getStrategy());
     return done(ControlResponse(504, "Unsupported strategy"));
   }
 
-  if (m_strategyChoice.insert(prefix, selectedStrategy)) {
+  if (m_table.insert(prefix, selectedStrategy)) {
     NFD_LOG_DEBUG("strategy-choice result: SUCCESS");
-    auto currentStrategyChoice = m_strategyChoice.get(prefix);
+    auto currentStrategyChoice = m_table.get(prefix);
     BOOST_ASSERT(currentStrategyChoice.first);
     parameters.setStrategy(currentStrategyChoice.second);
     return done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
@@ -78,7 +78,7 @@
                                      ControlParameters parameters,
                                      const ndn::mgmt::CommandContinuation& done)
 {
-  m_strategyChoice.erase(parameters.getName());
+  m_table.erase(parameters.getName());
 
   NFD_LOG_DEBUG("strategy-choice result: SUCCESS");
   done(ControlResponse(200, "OK").setBody(parameters.wireEncode()));
@@ -88,7 +88,7 @@
 StrategyChoiceManager::listChoices(const Name& topPrefix, const Interest& interest,
                                    ndn::mgmt::StatusDatasetContext& context)
 {
-  for (auto&& i : m_strategyChoice) {
+  for (auto&& i : m_table) {
     ndn::nfd::StrategyChoice entry;
     entry.setName(i.getPrefix()).setStrategy(i.getStrategyName());
     context.append(entry.wireEncode());
diff --git a/daemon/mgmt/strategy-choice-manager.hpp b/daemon/mgmt/strategy-choice-manager.hpp
index eff3418..9fd4ad3 100644
--- a/daemon/mgmt/strategy-choice-manager.hpp
+++ b/daemon/mgmt/strategy-choice-manager.hpp
@@ -30,7 +30,9 @@
 
 namespace nfd {
 
+namespace strategy_choice {
 class StrategyChoice;
+} // namespace strategy_choice
 
 /**
  * @brief implement the Strategy Choice Management of NFD Management Protocol.
@@ -39,7 +41,7 @@
 class StrategyChoiceManager : public NfdManagerBase
 {
 public:
-  StrategyChoiceManager(StrategyChoice& strategyChoice,
+  StrategyChoiceManager(strategy_choice::StrategyChoice& table,
                         Dispatcher& dispatcher,
                         CommandValidator& validator);
 
@@ -59,7 +61,7 @@
               ndn::mgmt::StatusDatasetContext& context);
 
 private:
-  StrategyChoice& m_strategyChoice;
+  strategy_choice::StrategyChoice& m_table;
 };
 
 } // namespace nfd
diff --git a/daemon/table/name-tree-entry.cpp b/daemon/table/name-tree-entry.cpp
index 87853f2..3244059 100644
--- a/daemon/table/name-tree-entry.cpp
+++ b/daemon/table/name-tree-entry.cpp
@@ -119,15 +119,15 @@
 }
 
 void
-Entry::setStrategyChoiceEntry(shared_ptr<strategy_choice::Entry> strategyChoiceEntry)
+Entry::setStrategyChoiceEntry(unique_ptr<strategy_choice::Entry> strategyChoiceEntry)
 {
   BOOST_ASSERT(strategyChoiceEntry == nullptr || strategyChoiceEntry->m_nameTreeEntry.expired());
 
   if (m_strategyChoiceEntry != nullptr) {
     m_strategyChoiceEntry->m_nameTreeEntry.reset();
   }
+  m_strategyChoiceEntry = std::move(strategyChoiceEntry);
 
-  m_strategyChoiceEntry = strategyChoiceEntry;
   if (m_strategyChoiceEntry != nullptr) {
     m_strategyChoiceEntry->m_nameTreeEntry = this->shared_from_this();
   }
diff --git a/daemon/table/name-tree-entry.hpp b/daemon/table/name-tree-entry.hpp
index c650587..7bade1f 100644
--- a/daemon/table/name-tree-entry.hpp
+++ b/daemon/table/name-tree-entry.hpp
@@ -120,9 +120,9 @@
   getMeasurementsEntry() const;
 
   void
-  setStrategyChoiceEntry(shared_ptr<strategy_choice::Entry> strategyChoiceEntry);
+  setStrategyChoiceEntry(unique_ptr<strategy_choice::Entry> strategyChoiceEntry);
 
-  shared_ptr<strategy_choice::Entry>
+  strategy_choice::Entry*
   getStrategyChoiceEntry() const;
 
 private:
@@ -136,7 +136,7 @@
   unique_ptr<fib::Entry> m_fibEntry;
   std::vector<shared_ptr<pit::Entry> > m_pitEntries;
   shared_ptr<measurements::Entry> m_measurementsEntry;
-  shared_ptr<strategy_choice::Entry> m_strategyChoiceEntry;
+  unique_ptr<strategy_choice::Entry> m_strategyChoiceEntry;
 
   // get the Name Tree Node that is associated with this Name Tree Entry
   Node* m_node;
@@ -211,10 +211,10 @@
   return m_measurementsEntry;
 }
 
-inline shared_ptr<strategy_choice::Entry>
+inline strategy_choice::Entry*
 Entry::getStrategyChoiceEntry() const
 {
-  return m_strategyChoiceEntry;
+  return m_strategyChoiceEntry.get();
 }
 
 } // namespace name_tree
diff --git a/daemon/table/name-tree.cpp b/daemon/table/name-tree.cpp
index 796e701..bc7d7c7 100644
--- a/daemon/table/name-tree.cpp
+++ b/daemon/table/name-tree.cpp
@@ -277,7 +277,7 @@
 NameTree::lookup(const strategy_choice::Entry& strategyChoiceEntry) const
 {
   shared_ptr<name_tree::Entry> nte = this->getEntry(strategyChoiceEntry);
-  BOOST_ASSERT(nte == nullptr || nte->getStrategyChoiceEntry().get() == &strategyChoiceEntry);
+  BOOST_ASSERT(nte == nullptr || nte->getStrategyChoiceEntry() == &strategyChoiceEntry);
   return nte;
 }
 
diff --git a/daemon/table/strategy-choice.cpp b/daemon/table/strategy-choice.cpp
index de7cb13..5778237 100644
--- a/daemon/table/strategy-choice.cpp
+++ b/daemon/table/strategy-choice.cpp
@@ -30,8 +30,8 @@
 #include "measurements-entry.hpp"
 
 namespace nfd {
+namespace strategy_choice {
 
-using strategy_choice::Entry;
 using fw::Strategy;
 
 NFD_LOG_INIT("StrategyChoice");
@@ -96,7 +96,7 @@
   }
 
   shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(prefix);
-  shared_ptr<Entry> entry = nte->getStrategyChoiceEntry();
+  Entry* entry = nte->getStrategyChoiceEntry();
   Strategy* oldStrategy = nullptr;
   if (entry != nullptr) {
     if (entry->getStrategy().getName() == strategy->getName()) {
@@ -110,8 +110,9 @@
 
   if (entry == nullptr) {
     oldStrategy = &this->findEffectiveStrategy(prefix);
-    entry = make_shared<Entry>(prefix);
-    nte->setStrategyChoiceEntry(entry);
+    auto newEntry = make_unique<Entry>(prefix);
+    entry = newEntry.get();
+    nte->setStrategyChoiceEntry(std::move(newEntry));
     ++m_nItems;
     NFD_LOG_TRACE("insert(" << prefix << ") new entry " << strategy->getName());
   }
@@ -131,7 +132,7 @@
     return;
   }
 
-  shared_ptr<Entry> entry = nte->getStrategyChoiceEntry();
+  Entry* entry = nte->getStrategyChoiceEntry();
   if (entry == nullptr) {
     return;
   }
@@ -141,7 +142,7 @@
   Strategy& parentStrategy = this->findEffectiveStrategy(prefix.getPrefix(-1));
   this->changeStrategy(*entry, oldStrategy, parentStrategy);
 
-  nte->setStrategyChoiceEntry(shared_ptr<Entry>());
+  nte->setStrategyChoiceEntry(nullptr);
   m_nameTree.eraseEntryIfEmpty(nte);
   --m_nItems;
 }
@@ -151,24 +152,22 @@
 {
   shared_ptr<name_tree::Entry> nte = m_nameTree.findExactMatch(prefix);
   if (nte == nullptr) {
-    return { false, Name() };
+    return {false, Name()};
   }
 
-  shared_ptr<Entry> entry = nte->getStrategyChoiceEntry();
+  Entry* entry = nte->getStrategyChoiceEntry();
   if (entry == nullptr) {
-    return { false, Name() };
+    return {false, Name()};
   }
 
-  return { true, entry->getStrategy().getName() };
+  return {true, entry->getStrategy().getName()};
 }
 
 Strategy&
 StrategyChoice::findEffectiveStrategy(const Name& prefix) const
 {
   shared_ptr<name_tree::Entry> nte = m_nameTree.findLongestPrefixMatch(prefix,
-    [] (const name_tree::Entry& entry) {
-      return entry.getStrategyChoiceEntry() != nullptr;
-    });
+    [] (const name_tree::Entry& entry) { return entry.getStrategyChoiceEntry() != nullptr; });
 
   BOOST_ASSERT(nte != nullptr);
   return nte->getStrategyChoiceEntry()->getStrategy();
@@ -177,14 +176,12 @@
 Strategy&
 StrategyChoice::findEffectiveStrategy(shared_ptr<name_tree::Entry> nte) const
 {
-  shared_ptr<strategy_choice::Entry> entry = nte->getStrategyChoiceEntry();
+  Entry* entry = nte->getStrategyChoiceEntry();
   if (entry != nullptr)
     return entry->getStrategy();
 
   nte = m_nameTree.findLongestPrefixMatch(nte,
-    [] (const name_tree::Entry& entry) {
-      return entry.getStrategyChoiceEntry() != nullptr;
-    });
+    [] (const name_tree::Entry& entry) { return entry.getStrategyChoiceEntry() != nullptr; });
 
   BOOST_ASSERT(nte != nullptr);
   return nte->getStrategyChoiceEntry()->getStrategy();
@@ -213,15 +210,15 @@
 {
   this->install(strategy);
 
+  auto entry = make_unique<Entry>(Name());
+  entry->setStrategy(*strategy);
+
   // don't use .insert here, because it will invoke findEffectiveStrategy
   // which expects an existing root entry
   shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(Name());
-  shared_ptr<Entry> entry = make_shared<Entry>(Name());
-  nte->setStrategyChoiceEntry(entry);
+  nte->setStrategyChoiceEntry(std::move(entry));
   ++m_nItems;
   NFD_LOG_INFO("setDefaultStrategy " << strategy->getName());
-
-  entry->setStrategy(*strategy);
 }
 
 static inline void
@@ -244,7 +241,7 @@
 }
 
 void
-StrategyChoice::changeStrategy(strategy_choice::Entry& entry,
+StrategyChoice::changeStrategy(Entry& entry,
                                fw::Strategy& oldStrategy,
                                fw::Strategy& newStrategy)
 {
@@ -284,4 +281,5 @@
   return const_iterator(enumerable.begin());
 }
 
+} // namespace strategy_choice
 } // namespace nfd
diff --git a/daemon/table/strategy-choice.hpp b/daemon/table/strategy-choice.hpp
index 68ed395..7082092 100644
--- a/daemon/table/strategy-choice.hpp
+++ b/daemon/table/strategy-choice.hpp
@@ -1,12 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014,  Regents of the University of California,
- *                      Arizona Board of Regents,
- *                      Colorado State University,
- *                      University Pierre & Marie Curie, Sorbonne University,
- *                      Washington University in St. Louis,
- *                      Beijing Institute of Technology,
- *                      The University of Memphis
+ * Copyright (c) 2014-2016,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -30,6 +30,7 @@
 #include "name-tree.hpp"
 
 namespace nfd {
+namespace strategy_choice {
 
 /** \brief represents the Strategy Choice table
  *
@@ -105,7 +106,7 @@
 
 public: // enumeration
   class const_iterator
-    : public std::iterator<std::forward_iterator_tag, const strategy_choice::Entry>
+    : public std::iterator<std::forward_iterator_tag, const Entry>
   {
   public:
     explicit
@@ -113,10 +114,10 @@
 
     ~const_iterator();
 
-    const strategy_choice::Entry&
+    const Entry&
     operator*() const;
 
-    shared_ptr<strategy_choice::Entry>
+    const Entry*
     operator->() const;
 
     const_iterator&
@@ -156,7 +157,7 @@
   setDefaultStrategy(shared_ptr<fw::Strategy> strategy);
 
   void
-  changeStrategy(strategy_choice::Entry& entry,
+  changeStrategy(Entry& entry,
                  fw::Strategy& oldStrategy,
                  fw::Strategy& newStrategy);
 
@@ -167,7 +168,7 @@
   NameTree& m_nameTree;
   size_t m_nItems;
 
-  typedef std::map<Name, shared_ptr<fw::Strategy> > StrategyInstanceTable;
+  typedef std::map<Name, shared_ptr<fw::Strategy>> StrategyInstanceTable;
   StrategyInstanceTable m_strategyInstances;
 };
 
@@ -210,13 +211,13 @@
   return *this;
 }
 
-inline const strategy_choice::Entry&
+inline const Entry&
 StrategyChoice::const_iterator::operator*() const
 {
   return *(m_nameTreeIterator->getStrategyChoiceEntry());
 }
 
-inline shared_ptr<strategy_choice::Entry>
+inline const Entry*
 StrategyChoice::const_iterator::operator->() const
 {
   return m_nameTreeIterator->getStrategyChoiceEntry();
@@ -234,6 +235,10 @@
   return m_nameTreeIterator != other.m_nameTreeIterator;
 }
 
+} // namespace strategy_choice
+
+using strategy_choice::StrategyChoice;
+
 } // namespace nfd
 
 #endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP