table: make StrategyChoice enumerable

refs #1633

Change-Id: I203d76645649cc8ef2e3dcfbd254fa4ebab6e9dc
diff --git a/daemon/table/strategy-choice-entry.cpp b/daemon/table/strategy-choice-entry.cpp
index 23c1b86..3271186 100644
--- a/daemon/table/strategy-choice-entry.cpp
+++ b/daemon/table/strategy-choice-entry.cpp
@@ -1,11 +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
+ * 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
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -36,6 +37,12 @@
 {
 }
 
+const Name&
+Entry::getStrategyName() const
+{
+  return m_strategy->getName();
+}
+
 void
 Entry::setStrategy(shared_ptr<fw::Strategy> strategy)
 {
diff --git a/daemon/table/strategy-choice-entry.hpp b/daemon/table/strategy-choice-entry.hpp
index 4fe904e..d937b1c 100644
--- a/daemon/table/strategy-choice-entry.hpp
+++ b/daemon/table/strategy-choice-entry.hpp
@@ -1,11 +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
+ * 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
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -49,6 +50,9 @@
   const Name&
   getPrefix() const;
 
+  const Name&
+  getStrategyName() const;
+
   fw::Strategy&
   getStrategy() const;
 
diff --git a/daemon/table/strategy-choice.cpp b/daemon/table/strategy-choice.cpp
index 2383118..34c583e 100644
--- a/daemon/table/strategy-choice.cpp
+++ b/daemon/table/strategy-choice.cpp
@@ -1,11 +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
+ * 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
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -37,6 +38,7 @@
 
 StrategyChoice::StrategyChoice(NameTree& nameTree, shared_ptr<Strategy> defaultStrategy)
   : m_nameTree(nameTree)
+  , m_nItems(0)
 {
   this->setDefaultStrategy(defaultStrategy);
 }
@@ -270,4 +272,10 @@
                 &clearStrategyInfo);
 }
 
+StrategyChoice::const_iterator
+StrategyChoice::begin() const
+{
+  return const_iterator(m_nameTree.fullEnumerate(&predicate_NameTreeEntry_hasStrategyChoiceEntry));
+}
+
 } // namespace nfd
diff --git a/daemon/table/strategy-choice.hpp b/daemon/table/strategy-choice.hpp
index 8ffe4d6..9441dcc 100644
--- a/daemon/table/strategy-choice.hpp
+++ b/daemon/table/strategy-choice.hpp
@@ -1,11 +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
+ * 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
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -68,7 +69,7 @@
   shared_ptr<const Name>
   get(const Name& prefix) const;
 
-public: // effect strategy
+public: // effective strategy
   /// get effective strategy for prefix
   fw::Strategy&
   findEffectiveStrategy(const Name& prefix) const;
@@ -81,10 +82,48 @@
   fw::Strategy&
   findEffectiveStrategy(const measurements::Entry& measurementsEntry) const;
 
+public: // enumeration
+  class const_iterator
+    : public std::iterator<std::forward_iterator_tag, const strategy_choice::Entry>
+  {
+  public:
+    explicit
+    const_iterator(const NameTree::const_iterator& it);
+
+    ~const_iterator();
+
+    const strategy_choice::Entry&
+    operator*() const;
+
+    shared_ptr<strategy_choice::Entry>
+    operator->() const;
+
+    const_iterator&
+    operator++();
+
+    const_iterator
+    operator++(int);
+
+    bool
+    operator==(const const_iterator& other) const;
+
+    bool
+    operator!=(const const_iterator& other) const;
+
+  private:
+    NameTree::const_iterator m_nameTreeIterator;
+  };
+
   /// number of entries stored
   size_t
   size() const;
 
+  const_iterator
+  begin() const;
+
+  const_iterator
+  end() const;
+
 private:
   shared_ptr<fw::Strategy>
   getStrategy(const Name& strategyName);
@@ -114,6 +153,63 @@
   return m_nItems;
 }
 
+inline StrategyChoice::const_iterator
+StrategyChoice::end() const
+{
+  return const_iterator(m_nameTree.end());
+}
+
+inline
+StrategyChoice::const_iterator::const_iterator(const NameTree::const_iterator& it)
+  : m_nameTreeIterator(it)
+{
+}
+
+inline
+StrategyChoice::const_iterator::~const_iterator()
+{
+}
+
+inline
+StrategyChoice::const_iterator
+StrategyChoice::const_iterator::operator++(int)
+{
+  StrategyChoice::const_iterator temp(*this);
+  ++(*this);
+  return temp;
+}
+
+inline StrategyChoice::const_iterator&
+StrategyChoice::const_iterator::operator++()
+{
+  ++m_nameTreeIterator;
+  return *this;
+}
+
+inline const strategy_choice::Entry&
+StrategyChoice::const_iterator::operator*() const
+{
+  return *(m_nameTreeIterator->getStrategyChoiceEntry());
+}
+
+inline shared_ptr<strategy_choice::Entry>
+StrategyChoice::const_iterator::operator->() const
+{
+  return m_nameTreeIterator->getStrategyChoiceEntry();
+}
+
+inline bool
+StrategyChoice::const_iterator::operator==(const StrategyChoice::const_iterator& other) const
+{
+  return m_nameTreeIterator == other.m_nameTreeIterator;
+}
+
+inline bool
+StrategyChoice::const_iterator::operator!=(const StrategyChoice::const_iterator& other) const
+{
+  return m_nameTreeIterator != other.m_nameTreeIterator;
+}
+
 } // namespace nfd
 
 #endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP
diff --git a/tests/daemon/table/strategy-choice.cpp b/tests/daemon/table/strategy-choice.cpp
index 3f8c2bc..e8f4ca3 100644
--- a/tests/daemon/table/strategy-choice.cpp
+++ b/tests/daemon/table/strategy-choice.cpp
@@ -1,11 +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
+ * 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
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -101,6 +102,44 @@
   BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/D")  .getName(), nameQ);
 }
 
+//XXX BOOST_CONCEPT_ASSERT((ForwardIterator<std::vector<int>::iterator>))
+//    is also failing. There might be a problem with ForwardIterator concept checking.
+//BOOST_CONCEPT_ASSERT((ForwardIterator<StrategyChoice::const_iterator>));
+
+BOOST_AUTO_TEST_CASE(Enumerate)
+{
+
+  Forwarder forwarder;
+  Name nameP("ndn:/strategy/P");
+  Name nameQ("ndn:/strategy/Q");
+  shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
+  shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
+
+  StrategyChoice& table = forwarder.getStrategyChoice();
+  table.install(strategyP);
+  table.install(strategyQ);
+
+  table.insert("ndn:/",      nameP);
+  table.insert("ndn:/A/B",   nameQ);
+  table.insert("ndn:/A/B/C", nameP);
+  table.insert("ndn:/D",     nameP);
+  table.insert("ndn:/E",     nameQ);
+
+  BOOST_CHECK_EQUAL(table.size(), 5);
+
+  std::map<Name, Name> map; // namespace=>strategyName
+  for (StrategyChoice::const_iterator it = table.begin(); it != table.end(); ++it) {
+    map[it->getPrefix()] = it->getStrategyName();
+  }
+  BOOST_CHECK_EQUAL(map.size(), 5);
+  BOOST_CHECK_EQUAL(map["ndn:/"],      nameP);
+  BOOST_CHECK_EQUAL(map["ndn:/A/B"],   nameQ);
+  BOOST_CHECK_EQUAL(map["ndn:/A/B/C"], nameP);
+  BOOST_CHECK_EQUAL(map["ndn:/D"],     nameP);
+  BOOST_CHECK_EQUAL(map["ndn:/E"],     nameQ);
+  BOOST_CHECK_EQUAL(map.size(), 5);
+}
+
 class PStrategyInfo : public fw::StrategyInfo
 {
 };