table: simplify Measurements with C++11 features

This commit also changes TableMeasurements test suite to use UnitTestTimeFixture.

refs #2100

Change-Id: I6af9fbae435fb50d26f6ec54abc7219680c8ade8
diff --git a/daemon/fw/ncc-strategy.cpp b/daemon/fw/ncc-strategy.cpp
index 749e97c..e5aaab9 100644
--- a/daemon/fw/ncc-strategy.cpp
+++ b/daemon/fw/ncc-strategy.cpp
@@ -171,13 +171,13 @@
       // going out of this strategy's namespace
       return;
     }
-    this->getMeasurements().extendLifetime(measurementsEntry, MEASUREMENTS_LIFETIME);
+    this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME);
 
     shared_ptr<MeasurementsEntryInfo> measurementsEntryInfo =
       this->getMeasurementsEntryInfo(measurementsEntry);
     measurementsEntryInfo->adjustPredictUp();
 
-    measurementsEntry = this->getMeasurements().getParent(measurementsEntry);
+    measurementsEntry = this->getMeasurements().getParent(*measurementsEntry);
   }
 }
 
@@ -198,13 +198,13 @@
       // going out of this strategy's namespace
       return;
     }
-    this->getMeasurements().extendLifetime(measurementsEntry, MEASUREMENTS_LIFETIME);
+    this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME);
 
     shared_ptr<MeasurementsEntryInfo> measurementsEntryInfo =
       this->getMeasurementsEntryInfo(measurementsEntry);
     measurementsEntryInfo->updateBestFace(inFace);
 
-    measurementsEntry = this->getMeasurements().getParent(measurementsEntry);
+    measurementsEntry = this->getMeasurements().getParent(*measurementsEntry);
   }
 
   shared_ptr<PitEntryInfo> pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>();
@@ -231,7 +231,7 @@
   info = make_shared<MeasurementsEntryInfo>();
   entry->setStrategyInfo(info);
 
-  shared_ptr<measurements::Entry> parentEntry = this->getMeasurements().getParent(entry);
+  shared_ptr<measurements::Entry> parentEntry = this->getMeasurements().getParent(*entry);
   if (static_cast<bool>(parentEntry)) {
     shared_ptr<MeasurementsEntryInfo> parentInfo = this->getMeasurementsEntryInfo(parentEntry);
     BOOST_ASSERT(static_cast<bool>(parentInfo));
diff --git a/daemon/table/measurements-accessor.hpp b/daemon/table/measurements-accessor.hpp
index 2108435..872cff2 100644
--- a/daemon/table/measurements-accessor.hpp
+++ b/daemon/table/measurements-accessor.hpp
@@ -45,36 +45,37 @@
 
   ~MeasurementsAccessor();
 
-  /// find or insert a Measurements entry for name
+  /** \brief find or insert a Measurements entry for name
+   */
   shared_ptr<measurements::Entry>
   get(const Name& name);
 
-  /// find or insert a Measurements entry for fibEntry->getPrefix()
+  /** \brief find or insert a Measurements entry for fibEntry->getPrefix()
+   */
   shared_ptr<measurements::Entry>
   get(const fib::Entry& fibEntry);
 
-  /// find or insert a Measurements entry for pitEntry->getName()
+  /** \brief find or insert a Measurements entry for pitEntry->getName()
+   */
   shared_ptr<measurements::Entry>
   get(const pit::Entry& pitEntry);
 
   /** \brief find or insert a Measurements entry for child's parent
-   *
-   *  If child is the root entry, returns null.
+   *  \retval nullptr if child is the root entry
    */
   shared_ptr<measurements::Entry>
-  getParent(shared_ptr<measurements::Entry> child);
+  getParent(const measurements::Entry& child);
 
   /** \brief extend lifetime of an entry
    *
    *  The entry will be kept until at least now()+lifetime.
    */
   void
-  extendLifetime(shared_ptr<measurements::Entry> entry, const time::nanoseconds& lifetime);
+  extendLifetime(measurements::Entry& entry, const time::nanoseconds& lifetime);
 
 private:
   /** \brief perform access control to Measurements entry
-   *
-   *  \return entry if strategy has access to namespace, otherwise 0
+   *  \return entry if strategy has access to namespace, otherwise nullptr
    */
   shared_ptr<measurements::Entry>
   filter(const shared_ptr<measurements::Entry>& entry);
@@ -104,13 +105,13 @@
 }
 
 inline shared_ptr<measurements::Entry>
-MeasurementsAccessor::getParent(shared_ptr<measurements::Entry> child)
+MeasurementsAccessor::getParent(const measurements::Entry& child)
 {
   return this->filter(m_measurements.getParent(child));
 }
 
 inline void
-MeasurementsAccessor::extendLifetime(shared_ptr<measurements::Entry> entry,
+MeasurementsAccessor::extendLifetime(measurements::Entry& entry,
                                      const time::nanoseconds& lifetime)
 {
   m_measurements.extendLifetime(entry, lifetime);
diff --git a/daemon/table/measurements.cpp b/daemon/table/measurements.cpp
index 4943bf4..8a24306 100644
--- a/daemon/table/measurements.cpp
+++ b/daemon/table/measurements.cpp
@@ -36,30 +36,20 @@
 {
 }
 
-Measurements::~Measurements()
-{
-}
-
-static inline bool
-predicate_NameTreeEntry_hasMeasurementsEntry(const name_tree::Entry& entry)
-{
-  return static_cast<bool>(entry.getMeasurementsEntry());
-}
-
 shared_ptr<measurements::Entry>
-Measurements::get(shared_ptr<name_tree::Entry> nameTreeEntry)
+Measurements::get(name_tree::Entry& nte)
 {
-  shared_ptr<measurements::Entry> entry = nameTreeEntry->getMeasurementsEntry();
-  if (static_cast<bool>(entry))
+  shared_ptr<measurements::Entry> entry = nte.getMeasurementsEntry();
+  if (entry != nullptr)
     return entry;
 
-  entry = make_shared<measurements::Entry>(nameTreeEntry->getPrefix());
-  nameTreeEntry->setMeasurementsEntry(entry);
+  entry = make_shared<measurements::Entry>(nte.getPrefix());
+  nte.setMeasurementsEntry(entry);
   ++m_nItems;
 
   entry->m_expiry = time::steady_clock::now() + getInitialLifetime();
   entry->m_cleanup = scheduler::schedule(getInitialLifetime(),
-                                         bind(&Measurements::cleanup, this, entry));
+                                         bind(&Measurements::cleanup, this, ref(*entry)));
 
   return entry;
 }
@@ -67,101 +57,86 @@
 shared_ptr<measurements::Entry>
 Measurements::get(const Name& name)
 {
-  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name);
-  return get(nameTreeEntry);
+  shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
+  return this->get(*nte);
 }
 
 shared_ptr<measurements::Entry>
 Measurements::get(const fib::Entry& fibEntry)
 {
-  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(fibEntry);
-
-  BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
-
-  return get(nameTreeEntry);
+  shared_ptr<name_tree::Entry> nte = m_nameTree.get(fibEntry);
+  return this->get(*nte);
 }
 
 shared_ptr<measurements::Entry>
 Measurements::get(const pit::Entry& pitEntry)
 {
-  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(pitEntry);
-
-  BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
-
-  return get(nameTreeEntry);
+  shared_ptr<name_tree::Entry> nte = m_nameTree.get(pitEntry);
+  return this->get(*nte);
 }
 
 shared_ptr<measurements::Entry>
-Measurements::getParent(shared_ptr<measurements::Entry> child)
+Measurements::getParent(const measurements::Entry& child)
 {
-  BOOST_ASSERT(child);
-
-  if (child->getName().size() == 0) {
-    return shared_ptr<measurements::Entry>();
+  if (child.getName().size() == 0) { // the root entry
+    return nullptr;
   }
 
-  shared_ptr<name_tree::Entry> nameTreeChild = m_nameTree.get(*child);
-  shared_ptr<name_tree::Entry> nameTreeEntry = nameTreeChild->getParent();
-  if (static_cast<bool>(nameTreeEntry)) {
-    return this->get(nameTreeEntry);
-  }
-  else {
-    BOOST_ASSERT(nameTreeChild->getPrefix().size() == 0); // root entry has no parent
-    return shared_ptr<measurements::Entry>();
-  }
+  shared_ptr<name_tree::Entry> nteChild = m_nameTree.get(child);
+  shared_ptr<name_tree::Entry> nte = nteChild->getParent();
+  BOOST_ASSERT(nte != nullptr);
+  return this->get(*nte);
 }
 
 shared_ptr<measurements::Entry>
 Measurements::findLongestPrefixMatch(const Name& name) const
 {
-  shared_ptr<name_tree::Entry> nameTreeEntry =
-    m_nameTree.findLongestPrefixMatch(name, &predicate_NameTreeEntry_hasMeasurementsEntry);
-  if (static_cast<bool>(nameTreeEntry)) {
-    return nameTreeEntry->getMeasurementsEntry();
+  shared_ptr<name_tree::Entry> nte = m_nameTree.findLongestPrefixMatch(name,
+      [] (const name_tree::Entry& nte) { return nte.getMeasurementsEntry() != nullptr; });
+  if (nte != nullptr) {
+    return nte->getMeasurementsEntry();
   }
-  return shared_ptr<measurements::Entry>();
+  return nullptr;
 }
 
 shared_ptr<measurements::Entry>
 Measurements::findExactMatch(const Name& name) const
 {
-  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(name);
-  if (static_cast<bool>(nameTreeEntry))
-    return nameTreeEntry->getMeasurementsEntry();
-  return shared_ptr<measurements::Entry>();
+  shared_ptr<name_tree::Entry> nte = m_nameTree.lookup(name);
+  if (nte != nullptr)
+    return nte->getMeasurementsEntry();
+  return nullptr;
 }
 
 void
-Measurements::extendLifetime(shared_ptr<measurements::Entry> entry,
+Measurements::extendLifetime(measurements::Entry& entry,
                              const time::nanoseconds& lifetime)
 {
-  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(*entry);
-  if (!static_cast<bool>(nameTreeEntry) ||
-      nameTreeEntry->getMeasurementsEntry().get() != entry.get()) {
+  shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
+  if (nte == nullptr ||
+      nte->getMeasurementsEntry().get() != &entry) {
     // entry is already gone; it is a dangling reference
     return;
   }
 
   time::steady_clock::TimePoint expiry = time::steady_clock::now() + lifetime;
-  if (entry->m_expiry >= expiry) {
+  if (entry.m_expiry >= expiry) {
     // has longer lifetime, not extending
     return;
   }
 
-  scheduler::cancel(entry->m_cleanup);
-  entry->m_expiry = expiry;
-  entry->m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, entry));
+  scheduler::cancel(entry.m_cleanup);
+  entry.m_expiry = expiry;
+  entry.m_cleanup = scheduler::schedule(lifetime, bind(&Measurements::cleanup, this, ref(entry)));
 }
 
 void
-Measurements::cleanup(shared_ptr<measurements::Entry> entry)
+Measurements::cleanup(measurements::Entry& entry)
 {
-  BOOST_ASSERT(static_cast<bool>(entry));
-
-  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.get(*entry);
-  if (static_cast<bool>(nameTreeEntry)) {
-    nameTreeEntry->setMeasurementsEntry(shared_ptr<measurements::Entry>());
-    m_nameTree.eraseEntryIfEmpty(nameTreeEntry);
+  shared_ptr<name_tree::Entry> nte = m_nameTree.get(entry);
+  if (nte != nullptr) {
+    nte->setMeasurementsEntry(nullptr);
+    m_nameTree.eraseEntryIfEmpty(nte);
     m_nItems--;
   }
 }
diff --git a/daemon/table/measurements.hpp b/daemon/table/measurements.hpp
index 7478c58..71d8db5 100644
--- a/daemon/table/measurements.hpp
+++ b/daemon/table/measurements.hpp
@@ -49,54 +49,56 @@
   explicit
   Measurements(NameTree& nametree);
 
-  ~Measurements();
-
-  /// find or insert a Measurements entry for name
+  /** \brief find or insert a Measurements entry for name
+   */
   shared_ptr<measurements::Entry>
   get(const Name& name);
 
-  /// find or insert a Measurements entry for fibEntry->getPrefix()
+  /** \brief find or insert a Measurements entry for fibEntry->getPrefix()
+   */
   shared_ptr<measurements::Entry>
   get(const fib::Entry& fibEntry);
 
-  /// find or insert a Measurements entry for pitEntry->getName()
+  /** \brief find or insert a Measurements entry for pitEntry->getName()
+   */
   shared_ptr<measurements::Entry>
   get(const pit::Entry& pitEntry);
 
-  /** find or insert a Measurements entry for child's parent
-   *
-   *  If child is the root entry, returns null.
+  /** \brief find or insert a Measurements entry for child's parent
+   *  \retval nullptr if child is the root entry
    */
   shared_ptr<measurements::Entry>
-  getParent(shared_ptr<measurements::Entry> child);
+  getParent(const measurements::Entry& child);
 
-  /// perform a longest prefix match
+  /** \brief perform a longest prefix match
+   */
   shared_ptr<measurements::Entry>
   findLongestPrefixMatch(const Name& name) const;
 
-  /// perform an exact match
+  /** \brief perform an exact match
+   */
   shared_ptr<measurements::Entry>
   findExactMatch(const Name& name) const;
 
   static time::nanoseconds
   getInitialLifetime();
 
-  /** extend lifetime of an entry
+  /** \brief extend lifetime of an entry
    *
    *  The entry will be kept until at least now()+lifetime.
    */
   void
-  extendLifetime(shared_ptr<measurements::Entry> entry, const time::nanoseconds& lifetime);
+  extendLifetime(measurements::Entry& entry, const time::nanoseconds& lifetime);
 
   size_t
   size() const;
 
 private:
   void
-  cleanup(shared_ptr<measurements::Entry> entry);
+  cleanup(measurements::Entry& entry);
 
   shared_ptr<measurements::Entry>
-  get(shared_ptr<name_tree::Entry> nameTreeEntry);
+  get(name_tree::Entry& nte);
 
 private:
   NameTree& m_nameTree;