blob: 889ed979ed515e1194dc2995d9019c6fe731f412 [file] [log] [blame]
HYuana9b85752014-02-26 02:32:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shib184e532016-05-26 18:09:57 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shiee5a4442014-07-27 17:13:43 -070024 */
HYuana9b85752014-02-26 02:32:30 -060025
26#include "name-tree-entry.hpp"
27
28namespace nfd {
29namespace name_tree {
30
Haowei Yuanf52dac72014-03-24 23:35:03 -050031Node::Node()
32 : m_prev(0)
33 , m_next(0)
HYuana9b85752014-02-26 02:32:30 -060034{
35}
36
37Node::~Node()
38{
39 // erase the Name Tree Nodes that were created to
40 // resolve hash collisions
41 // So before erasing a single node, make sure its m_next == 0
42 // See eraseEntryIfEmpty in name-tree.cpp
Junxiao Shiee5a4442014-07-27 17:13:43 -070043 if (m_next != 0)
HYuana9b85752014-02-26 02:32:30 -060044 delete m_next;
45}
46
Junxiao Shi9f7455b2014-04-07 21:02:16 -070047Entry::Entry(const Name& name)
48 : m_hash(0)
49 , m_prefix(name)
HYuana9b85752014-02-26 02:32:30 -060050{
51}
52
53Entry::~Entry()
54{
55}
56
Junxiao Shiee5a4442014-07-27 17:13:43 -070057bool
58Entry::isEmpty() const
59{
60 return m_children.empty() &&
Junxiao Shib184e532016-05-26 18:09:57 +000061 m_fibEntry == nullptr &&
Junxiao Shiee5a4442014-07-27 17:13:43 -070062 m_pitEntries.empty() &&
Junxiao Shib184e532016-05-26 18:09:57 +000063 m_measurementsEntry == nullptr &&
64 m_strategyChoiceEntry == nullptr;
Junxiao Shiee5a4442014-07-27 17:13:43 -070065}
66
67void
68Entry::setFibEntry(shared_ptr<fib::Entry> fibEntry)
69{
Junxiao Shib184e532016-05-26 18:09:57 +000070 BOOST_ASSERT(fibEntry == nullptr || fibEntry->m_nameTreeEntry.expired());
Junxiao Shiee5a4442014-07-27 17:13:43 -070071
Junxiao Shib184e532016-05-26 18:09:57 +000072 if (m_fibEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -070073 m_fibEntry->m_nameTreeEntry.reset();
74 }
75 m_fibEntry = fibEntry;
Junxiao Shib184e532016-05-26 18:09:57 +000076
77 if (m_fibEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -070078 m_fibEntry->m_nameTreeEntry = this->shared_from_this();
79 }
80}
81
82void
83Entry::insertPitEntry(shared_ptr<pit::Entry> pitEntry)
84{
Junxiao Shib184e532016-05-26 18:09:57 +000085 BOOST_ASSERT(pitEntry != nullptr);
86 BOOST_ASSERT(pitEntry->m_nameTreeEntry.expired());
Junxiao Shiee5a4442014-07-27 17:13:43 -070087
88 m_pitEntries.push_back(pitEntry);
89 pitEntry->m_nameTreeEntry = this->shared_from_this();
90}
91
HYuana9b85752014-02-26 02:32:30 -060092void
Junxiao Shi9f7455b2014-04-07 21:02:16 -070093Entry::erasePitEntry(shared_ptr<pit::Entry> pitEntry)
HYuana9b85752014-02-26 02:32:30 -060094{
Junxiao Shib184e532016-05-26 18:09:57 +000095 BOOST_ASSERT(pitEntry != nullptr);
96 BOOST_ASSERT(pitEntry->m_nameTreeEntry.lock().get() == this);
Junxiao Shi9f7455b2014-04-07 21:02:16 -070097
Junxiao Shib184e532016-05-26 18:09:57 +000098 auto it = std::find(m_pitEntries.begin(), m_pitEntries.end(), pitEntry);
Junxiao Shi9f7455b2014-04-07 21:02:16 -070099 BOOST_ASSERT(it != m_pitEntries.end());
100
101 *it = m_pitEntries.back();
102 m_pitEntries.pop_back();
103 pitEntry->m_nameTreeEntry.reset();
104}
105
Junxiao Shiee5a4442014-07-27 17:13:43 -0700106void
107Entry::setMeasurementsEntry(shared_ptr<measurements::Entry> measurementsEntry)
108{
Junxiao Shib184e532016-05-26 18:09:57 +0000109 BOOST_ASSERT(measurementsEntry == nullptr || measurementsEntry->m_nameTreeEntry.expired());
Junxiao Shiee5a4442014-07-27 17:13:43 -0700110
Junxiao Shib184e532016-05-26 18:09:57 +0000111 if (m_measurementsEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700112 m_measurementsEntry->m_nameTreeEntry.reset();
113 }
114 m_measurementsEntry = measurementsEntry;
Junxiao Shib184e532016-05-26 18:09:57 +0000115
116 if (m_measurementsEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700117 m_measurementsEntry->m_nameTreeEntry = this->shared_from_this();
118 }
119}
120
121void
122Entry::setStrategyChoiceEntry(shared_ptr<strategy_choice::Entry> strategyChoiceEntry)
123{
Junxiao Shib184e532016-05-26 18:09:57 +0000124 BOOST_ASSERT(strategyChoiceEntry == nullptr || strategyChoiceEntry->m_nameTreeEntry.expired());
Junxiao Shiee5a4442014-07-27 17:13:43 -0700125
Junxiao Shib184e532016-05-26 18:09:57 +0000126 if (m_strategyChoiceEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700127 m_strategyChoiceEntry->m_nameTreeEntry.reset();
128 }
Junxiao Shib184e532016-05-26 18:09:57 +0000129
Junxiao Shiee5a4442014-07-27 17:13:43 -0700130 m_strategyChoiceEntry = strategyChoiceEntry;
Junxiao Shib184e532016-05-26 18:09:57 +0000131 if (m_strategyChoiceEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700132 m_strategyChoiceEntry->m_nameTreeEntry = this->shared_from_this();
133 }
134}
135
HYuana9b85752014-02-26 02:32:30 -0600136} // namespace name_tree
137} // namespace nfd