core: eliminate MapValueIterator in favor of boost::adaptors::map_values
refs #2239
Change-Id: Ifcc03513b1cbe580de12725baa4338f822a71417
diff --git a/core/map-value-iterator.hpp b/core/map-value-iterator.hpp
deleted file mode 100644
index 1dfdd5c..0000000
--- a/core/map-value-iterator.hpp
+++ /dev/null
@@ -1,87 +0,0 @@
-/* -*- 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
- *
- * This file is part of NFD (Named Data Networking Forwarding Daemon).
- * See AUTHORS.md for complete list of NFD authors and contributors.
- *
- * NFD is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- **/
-
-#ifndef NFD_CORE_MAP_VALUE_ITERATOR_H
-#define NFD_CORE_MAP_VALUE_ITERATOR_H
-
-#include "common.hpp"
-#include <boost/iterator/transform_iterator.hpp>
-
-namespace nfd {
-
-/** \class MapValueIterator
- * \brief ForwardIterator to iterator over map values
- */
-template<typename Map>
-class MapValueIterator
- : public boost::transform_iterator<
- function<const typename Map::mapped_type&(const typename Map::value_type&)>,
- typename Map::const_iterator>
-{
-public:
- explicit
- MapValueIterator(typename Map::const_iterator it)
- : boost::transform_iterator<
- function<const typename Map::mapped_type&(const typename Map::value_type&)>,
- typename Map::const_iterator>(it, &takeSecond)
- {
- }
-
-private:
- static const typename Map::mapped_type&
- takeSecond(const typename Map::value_type& pair)
- {
- return pair.second;
- }
-};
-
-/** \class MapValueReverseIterator
- * \brief ReverseIterator to iterator over map values
- */
-template<typename Map>
-class MapValueReverseIterator
- : public boost::transform_iterator<
- function<const typename Map::mapped_type&(const typename Map::value_type&)>,
- typename Map::const_reverse_iterator>
-{
-public:
- explicit
- MapValueReverseIterator(typename Map::const_reverse_iterator it)
- : boost::transform_iterator<
- function<const typename Map::mapped_type&(const typename Map::value_type&)>,
- typename Map::const_reverse_iterator>(it, &takeSecond)
- {
- }
-
-private:
- static const typename Map::mapped_type&
- takeSecond(const typename Map::value_type& pair)
- {
- return pair.second;
- }
-};
-
-} // namespace nfd
-
-#endif // NFD_CORE_MAP_VALUE_ITERATOR_H
diff --git a/daemon/fw/face-table.cpp b/daemon/fw/face-table.cpp
index a95c562..f1db53b 100644
--- a/daemon/fw/face-table.cpp
+++ b/daemon/fw/face-table.cpp
@@ -42,6 +42,19 @@
}
+shared_ptr<Face>
+FaceTable::get(FaceId id) const
+{
+ std::map<FaceId, shared_ptr<Face> >::const_iterator i = m_faces.find(id);
+ return (i == m_faces.end()) ? (shared_ptr<Face>()) : (i->second);
+}
+
+size_t
+FaceTable::size() const
+{
+ return m_faces.size();
+}
+
void
FaceTable::add(shared_ptr<Face> face)
{
@@ -104,6 +117,22 @@
m_forwarder.getFib().removeNextHopFromAllEntries(face);
}
+FaceTable::ForwardRange
+FaceTable::getForwardRange() const
+{
+ return m_faces | boost::adaptors::map_values;
+}
+FaceTable::const_iterator
+FaceTable::begin() const
+{
+ return this->getForwardRange().begin();
+}
+
+FaceTable::const_iterator
+FaceTable::end() const
+{
+ return this->getForwardRange().end();
+}
} // namespace nfd
diff --git a/daemon/fw/face-table.hpp b/daemon/fw/face-table.hpp
index a255cfe..70ceb11 100644
--- a/daemon/fw/face-table.hpp
+++ b/daemon/fw/face-table.hpp
@@ -27,7 +27,7 @@
#define NFD_DAEMON_FW_FACE_TABLE_HPP
#include "face/face.hpp"
-#include "core/map-value-iterator.hpp"
+#include <boost/range/adaptor/map.hpp>
namespace nfd
{
@@ -59,15 +59,13 @@
size() const;
public: // enumeration
- typedef std::map<FaceId, shared_ptr<Face> > FaceMap;
+ typedef std::map<FaceId, shared_ptr<Face>> FaceMap;
+
+ typedef boost::select_second_const_range<FaceMap> ForwardRange;
/** \brief ForwardIterator for shared_ptr<Face>
*/
- typedef MapValueIterator<FaceMap> const_iterator;
-
- /** \brief ReverseIterator for shared_ptr<Face>
- */
- typedef MapValueReverseIterator<FaceMap> const_reverse_iterator;
+ typedef boost::range_iterator<ForwardRange>::type const_iterator;
const_iterator
begin() const;
@@ -75,12 +73,6 @@
const_iterator
end() const;
- const_reverse_iterator
- rbegin() const;
-
- const_reverse_iterator
- rend() const;
-
public: // events
/** \brief fires after a Face is added
*/
@@ -101,49 +93,15 @@
void
remove(shared_ptr<Face> face);
+ ForwardRange
+ getForwardRange() const;
+
private:
Forwarder& m_forwarder;
FaceId m_lastFaceId;
FaceMap m_faces;
};
-inline shared_ptr<Face>
-FaceTable::get(FaceId id) const
-{
- std::map<FaceId, shared_ptr<Face> >::const_iterator i = m_faces.find(id);
- return (i == m_faces.end()) ? (shared_ptr<Face>()) : (i->second);
-}
-
-inline size_t
-FaceTable::size() const
-{
- return m_faces.size();
-}
-
-inline FaceTable::const_iterator
-FaceTable::begin() const
-{
- return const_iterator(m_faces.begin());
-}
-
-inline FaceTable::const_iterator
-FaceTable::end() const
-{
- return const_iterator(m_faces.end());
-}
-
-inline FaceTable::const_reverse_iterator
-FaceTable::rbegin() const
-{
- return const_reverse_iterator(m_faces.rbegin());
-}
-
-inline FaceTable::const_reverse_iterator
-FaceTable::rend() const
-{
- return const_reverse_iterator(m_faces.rend());
-}
-
} // namespace nfd
#endif // NFD_DAEMON_FW_FACE_TABLE_HPP
diff --git a/daemon/mgmt/face-query-status-publisher.cpp b/daemon/mgmt/face-query-status-publisher.cpp
index 02fd4b6..d208087 100644
--- a/daemon/mgmt/face-query-status-publisher.cpp
+++ b/daemon/mgmt/face-query-status-publisher.cpp
@@ -25,6 +25,7 @@
#include "face-query-status-publisher.hpp"
#include "core/logger.hpp"
+#include <boost/range/adaptor/reversed.hpp>
#include <ndn-cxx/management/nfd-face-status.hpp>
@@ -95,10 +96,7 @@
{
size_t totalLength = 0;
- for (FaceTable::const_reverse_iterator i = m_faceTable.rbegin();
- i != m_faceTable.rend(); ++i) {
- const shared_ptr<Face>& face = *i;
-
+ for (const shared_ptr<Face>& face : m_faceTable | boost::adaptors::reversed) {
if (doesMatchFilter(face)) {
ndn::nfd::FaceStatus status = face->getFaceStatus();
totalLength += status.wireEncode(outBuffer);
diff --git a/daemon/mgmt/face-status-publisher.cpp b/daemon/mgmt/face-status-publisher.cpp
index 04542c8..759f9ce 100644
--- a/daemon/mgmt/face-status-publisher.cpp
+++ b/daemon/mgmt/face-status-publisher.cpp
@@ -25,6 +25,7 @@
#include "face-status-publisher.hpp"
#include "core/logger.hpp"
#include "fw/face-table.hpp"
+#include <boost/range/adaptor/reversed.hpp>
#include <ndn-cxx/management/nfd-face-status.hpp>
@@ -54,9 +55,7 @@
{
size_t totalLength = 0;
- for (FaceTable::const_reverse_iterator i = m_faceTable.rbegin();
- i != m_faceTable.rend(); ++i) {
- const shared_ptr<Face>& face = *i;
+ for (const shared_ptr<Face>& face : m_faceTable | boost::adaptors::reversed) {
ndn::nfd::FaceStatus status = face->getFaceStatus();
totalLength += status.wireEncode(outBuffer);
}
diff --git a/tests/core/map-value-iterator.cpp b/tests/core/map-value-iterator.cpp
deleted file mode 100644
index 8a46da6..0000000
--- a/tests/core/map-value-iterator.cpp
+++ /dev/null
@@ -1,57 +0,0 @@
-/* -*- 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
- *
- * This file is part of NFD (Named Data Networking Forwarding Daemon).
- * See AUTHORS.md for complete list of NFD authors and contributors.
- *
- * NFD is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- **/
-
-#include "core/map-value-iterator.hpp"
-#include <boost/concept_check.hpp>
-
-#include "tests/test-common.hpp"
-
-namespace nfd {
-namespace tests {
-
-BOOST_FIXTURE_TEST_SUITE(CoreMapValueIterator, BaseFixture)
-
-BOOST_AUTO_TEST_CASE(Basic)
-{
- typedef std::map<char, int> CharIntMap;
- typedef MapValueIterator<CharIntMap> CharIntMapValueIterator;
- BOOST_CONCEPT_ASSERT((boost::ForwardIterator<CharIntMapValueIterator>));
-
- CharIntMap map;
- map['a'] = 1918;
- map['b'] = 2675;
- map['c'] = 7783;
- map['d'] = 2053;
-
- CharIntMapValueIterator begin(map.begin());
- CharIntMapValueIterator end (map.end ());
-
- int expected[] = { 1918, 2675, 7783, 2053 };
- BOOST_CHECK_EQUAL_COLLECTIONS(begin, end, expected, expected + 4);
-}
-
-BOOST_AUTO_TEST_SUITE_END()
-
-} // namespace tests
-} // namespace nfd