table: Allow iteration over PIT entries
Change-Id: If48d7f827ccb5df0480cfa27bd2f3db4e3f796fa
Refs: #2339
diff --git a/daemon/table/pit.hpp b/daemon/table/pit.hpp
index 00d90ca..3bdf44f 100644
--- a/daemon/table/pit.hpp
+++ b/daemon/table/pit.hpp
@@ -78,6 +78,62 @@
void
erase(shared_ptr<pit::Entry> pitEntry);
+public: // enumeration
+ class const_iterator;
+
+ /** \brief returns an iterator pointing to the first PIT entry
+ * \note Iteration order is implementation-specific and is undefined
+ * \note The returned iterator may get invalidated if PIT or another NameTree-based
+ * table is modified
+ */
+ const_iterator
+ begin() const;
+
+ /** \brief returns an iterator referring to the past-the-end PIT entry
+ * \note The returned iterator may get invalidated if PIT or another NameTree-based
+ * table is modified
+ */
+ const_iterator
+ end() const;
+
+ class const_iterator : public std::iterator<std::forward_iterator_tag, const pit::Entry>
+ {
+ public:
+ const_iterator();
+
+ explicit
+ const_iterator(const NameTree::const_iterator& it);
+
+ ~const_iterator();
+
+ const pit::Entry&
+ operator*() const;
+
+ shared_ptr<pit::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;
+ /** \brief Index of the current visiting PIT entry in NameTree node
+ *
+ * Index is used to ensure that dereferencing of m_nameTreeIterator happens only when
+ * const_iterator is dereferenced or advanced.
+ */
+ size_t m_iPitEntry;
+ };
+
private:
NameTree& m_nameTree;
size_t m_nItems;
@@ -89,6 +145,76 @@
return m_nItems;
}
+inline Pit::const_iterator
+Pit::end() const
+{
+ return const_iterator(m_nameTree.end());
+}
+
+inline
+Pit::const_iterator::const_iterator()
+ : m_iPitEntry(0)
+{
+}
+
+inline
+Pit::const_iterator::const_iterator(const NameTree::const_iterator& it)
+ : m_nameTreeIterator(it)
+ , m_iPitEntry(0)
+{
+}
+
+inline
+Pit::const_iterator::~const_iterator()
+{
+}
+
+inline Pit::const_iterator
+Pit::const_iterator::operator++(int)
+{
+ Pit::const_iterator temp(*this);
+ ++(*this);
+ return temp;
+}
+
+inline Pit::const_iterator&
+Pit::const_iterator::operator++()
+{
+ ++m_iPitEntry;
+ if (m_iPitEntry < m_nameTreeIterator->getPitEntries().size()) {
+ return *this;
+ }
+
+ ++m_nameTreeIterator;
+ m_iPitEntry = 0;
+ return *this;
+}
+
+inline const pit::Entry&
+Pit::const_iterator::operator*() const
+{
+ return *(this->operator->());
+}
+
+inline shared_ptr<pit::Entry>
+Pit::const_iterator::operator->() const
+{
+ return m_nameTreeIterator->getPitEntries().at(m_iPitEntry);
+}
+
+inline bool
+Pit::const_iterator::operator==(const Pit::const_iterator& other) const
+{
+ return m_nameTreeIterator == other.m_nameTreeIterator &&
+ m_iPitEntry == other.m_iPitEntry;
+}
+
+inline bool
+Pit::const_iterator::operator!=(const Pit::const_iterator& other) const
+{
+ return !(*this == other);
+}
+
} // namespace nfd
#endif // NFD_DAEMON_TABLE_PIT_HPP