Files
NFD/daemon/table/fib-entry.cpp
T
Junxiao Shi efceadc378 fib: implicitly delete empty Entry
Fib::removeNextHopFromAllEntries automatically deletes fib::Entry
if the last nexthop record is being removed.

refs #1341

Change-Id: I36d42fe8f9fc8f03d194f845020aff408cd70488
2014-03-09 19:39:26 -07:00

74 lines
1.5 KiB
C++

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (C) 2014 Named Data Networking Project
* See COPYING for copyright and distribution information.
*/
#include "fib-entry.hpp"
namespace nfd {
namespace fib {
Entry::Entry(const Name& prefix)
: m_prefix(prefix)
{
}
static inline bool
predicate_NextHop_eq_Face(const NextHop& nexthop, shared_ptr<Face> face)
{
return nexthop.getFace() == face;
}
bool
Entry::hasNextHop(shared_ptr<Face> face) const
{
NextHopList::const_iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(),
bind(&predicate_NextHop_eq_Face, _1, face));
return it != m_nextHops.end();
}
void
Entry::addNextHop(shared_ptr<Face> face, int32_t cost)
{
NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(),
bind(&predicate_NextHop_eq_Face, _1, face));
if (it == m_nextHops.end()) {
m_nextHops.push_back(fib::NextHop(face));
it = m_nextHops.end() - 1;
}
// now it refers to the NextHop for face
it->setCost(cost);
this->sortNextHops();
}
void
Entry::removeNextHop(shared_ptr<Face> face)
{
NextHopList::iterator it = std::find_if(m_nextHops.begin(), m_nextHops.end(),
bind(&predicate_NextHop_eq_Face, _1, face));
if (it == m_nextHops.end()) {
return;
}
m_nextHops.erase(it);
}
static inline bool
compare_NextHop_cost(const NextHop& a, const NextHop& b)
{
return a.getCost() < b.getCost();
}
void
Entry::sortNextHops()
{
std::sort(m_nextHops.begin(), m_nextHops.end(), &compare_NextHop_cost);
}
} // namespace fib
} // namespace nfd