nlsr: refactor AdjacencyList to use iterators instead of bare ptrs
Change-Id: I42225ee63c3d64eb80e58bc51c8d1afd20dd2ce2
refs: #4068
diff --git a/src/adjacency-list.cpp b/src/adjacency-list.cpp
index fd2fd46..56807c7 100644
--- a/src/adjacency-list.cpp
+++ b/src/adjacency-list.cpp
@@ -240,32 +240,22 @@
return it;
}
-Adjacent *
+AdjacencyList::iterator
AdjacencyList::findAdjacent(const ndn::Name& adjName)
{
- std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
- m_adjList.end(),
- std::bind(&Adjacent::compare,
- _1, std::cref(adjName)));
- if (it != m_adjList.end()) {
- return &(*it);
- }
-
- return 0;
+ return std::find_if(m_adjList.begin(),
+ m_adjList.end(),
+ std::bind(&Adjacent::compare,
+ _1, std::cref(adjName)));
}
-Adjacent *
+AdjacencyList::iterator
AdjacencyList::findAdjacent(uint64_t faceId)
{
- std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
- m_adjList.end(),
- std::bind(&Adjacent::compareFaceId,
- _1, faceId));
- if (it != m_adjList.end()) {
- return &(*it);
- }
-
- return 0;
+ return std::find_if(m_adjList.begin(),
+ m_adjList.end(),
+ std::bind(&Adjacent::compareFaceId,
+ _1, faceId));
}
uint64_t
diff --git a/src/adjacency-list.hpp b/src/adjacency-list.hpp
index 4cf816e..c9553de 100644
--- a/src/adjacency-list.hpp
+++ b/src/adjacency-list.hpp
@@ -139,10 +139,10 @@
}
}
- Adjacent*
+ AdjacencyList::iterator
findAdjacent(const ndn::Name& adjName);
- Adjacent*
+ AdjacencyList::iterator
findAdjacent(uint64_t faceId);
uint64_t
@@ -173,4 +173,4 @@
};
} // namespace nlsr
-#endif //NLSR_ADJACENCY_LIST_HPP
+#endif // NLSR_ADJACENCY_LIST_HPP
diff --git a/src/hello-protocol.cpp b/src/hello-protocol.cpp
index 684009d..2f27020 100644
--- a/src/hello-protocol.cpp
+++ b/src/hello-protocol.cpp
@@ -112,7 +112,8 @@
m_nlsr.getKeyChain().sign(*data, m_nlsr.getDefaultCertName());
_LOG_DEBUG("Sending out data for name: " << interest.getName());
m_nlsr.getNlsrFace().put(*data);
- Adjacent* adjacent = m_nlsr.getAdjacencyList().findAdjacent(neighbor);
+
+ auto adjacent = m_nlsr.getAdjacencyList().findAdjacent(neighbor);
// If this neighbor was previously inactive, send our own hello interest, too
if (adjacent->getStatus() == Adjacent::STATUS_INACTIVE) {
// We can only do that if the neighbor currently has a face.
@@ -256,8 +257,8 @@
HelloProtocol::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
const ndn::Name& neighbor,const ndn::time::milliseconds& timeout)
{
- Adjacent* adjacent = m_nlsr.getAdjacencyList().findAdjacent(neighbor);
- if (adjacent != 0) {
+ auto adjacent = m_nlsr.getAdjacencyList().findAdjacent(neighbor);
+ if (adjacent != m_nlsr.getAdjacencyList().end()){
adjacent->setFaceId(commandSuccessResult.getFaceId());
ndn::Name broadcastKeyPrefix = DEFAULT_BROADCAST_PREFIX;
broadcastKeyPrefix.append("KEYS");
@@ -296,8 +297,8 @@
* Lsa unless all the neighbors are ACTIVE or DEAD. For considering the
* missconfigured(link) neighbour dead this is required.
*/
- Adjacent* adjacent = m_nlsr.getAdjacencyList().findAdjacent(name);
- if (adjacent != 0) {
+ auto adjacent = m_nlsr.getAdjacencyList().findAdjacent(name);
+ if (adjacent != m_nlsr.getAdjacencyList().end()) {
adjacent->setInterestTimedOutNo(adjacent->getInterestTimedOutNo() + 1);
Adjacent::Status status = adjacent->getStatus();
uint32_t infoIntTimedOutCount = adjacent->getInterestTimedOutNo();
diff --git a/src/nlsr.cpp b/src/nlsr.cpp
index 26276d7..2bcc157 100644
--- a/src/nlsr.cpp
+++ b/src/nlsr.cpp
@@ -375,9 +375,9 @@
if (kind == ndn::nfd::FACE_EVENT_DESTROYED) {
uint64_t faceId = faceEventNotification.getFaceId();
- Adjacent* adjacent = m_adjacencyList.findAdjacent(faceId);
+ auto adjacent = m_adjacencyList.findAdjacent(faceId);
- if (adjacent != nullptr) {
+ if (adjacent != m_adjacencyList.end()) {
_LOG_DEBUG("Face to " << adjacent->getName() << " with face id: " << faceId << " destroyed");
adjacent->setFaceId(0);