blob: 889d84f185548ce6d801d3d18ee3d73eabe9af2e [file] [log] [blame]
HYuana9b85752014-02-26 02:32:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi057d1492018-03-20 17:14:18 +00002/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
Junxiao Shib184e532016-05-26 18:09:57 +00004 * 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"
Junxiao Shi057d1492018-03-20 17:14:18 +000027#include "name-tree.hpp"
HYuana9b85752014-02-26 02:32:30 -060028
29namespace nfd {
30namespace name_tree {
31
Junxiao Shib660b4c2016-08-06 20:47:44 +000032Entry::Entry(const Name& name, Node* node)
33 : m_name(name)
34 , m_node(node)
35 , m_parent(nullptr)
HYuana9b85752014-02-26 02:32:30 -060036{
Junxiao Shib660b4c2016-08-06 20:47:44 +000037 BOOST_ASSERT(node != nullptr);
Junxiao Shi057d1492018-03-20 17:14:18 +000038 BOOST_ASSERT(name.size() <= NameTree::getMaxDepth());
HYuana9b85752014-02-26 02:32:30 -060039}
40
Junxiao Shib660b4c2016-08-06 20:47:44 +000041void
42Entry::setParent(Entry& entry)
HYuana9b85752014-02-26 02:32:30 -060043{
Junxiao Shib660b4c2016-08-06 20:47:44 +000044 BOOST_ASSERT(this->getParent() == nullptr);
45 BOOST_ASSERT(!this->getName().empty());
46 BOOST_ASSERT(entry.getName() == this->getName().getPrefix(-1));
47
48 m_parent = &entry;
49
50 m_parent->m_children.push_back(this);
HYuana9b85752014-02-26 02:32:30 -060051}
52
Junxiao Shib660b4c2016-08-06 20:47:44 +000053void
54Entry::unsetParent()
HYuana9b85752014-02-26 02:32:30 -060055{
Junxiao Shib660b4c2016-08-06 20:47:44 +000056 BOOST_ASSERT(this->getParent() != nullptr);
57
58 auto i = std::find(m_parent->m_children.begin(), m_parent->m_children.end(), this);
59 BOOST_ASSERT(i != m_parent->m_children.end());
60 m_parent->m_children.erase(i);
61
62 m_parent = nullptr;
HYuana9b85752014-02-26 02:32:30 -060063}
64
Junxiao Shiee5a4442014-07-27 17:13:43 -070065bool
Junxiao Shib660b4c2016-08-06 20:47:44 +000066Entry::hasTableEntries() const
Junxiao Shiee5a4442014-07-27 17:13:43 -070067{
Junxiao Shib660b4c2016-08-06 20:47:44 +000068 return m_fibEntry != nullptr ||
69 !m_pitEntries.empty() ||
70 m_measurementsEntry != nullptr ||
71 m_strategyChoiceEntry != nullptr;
Junxiao Shiee5a4442014-07-27 17:13:43 -070072}
73
74void
Junxiao Shia6de4292016-07-12 02:08:10 +000075Entry::setFibEntry(unique_ptr<fib::Entry> fibEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -070076{
Junxiao Shi340d5532016-08-13 04:00:35 +000077 BOOST_ASSERT(fibEntry == nullptr || fibEntry->m_nameTreeEntry == nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -070078
Junxiao Shib184e532016-05-26 18:09:57 +000079 if (m_fibEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +000080 m_fibEntry->m_nameTreeEntry = nullptr;
Junxiao Shiee5a4442014-07-27 17:13:43 -070081 }
Junxiao Shia6de4292016-07-12 02:08:10 +000082 m_fibEntry = std::move(fibEntry);
Junxiao Shib184e532016-05-26 18:09:57 +000083
84 if (m_fibEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +000085 m_fibEntry->m_nameTreeEntry = this;
Junxiao Shiee5a4442014-07-27 17:13:43 -070086 }
87}
88
89void
90Entry::insertPitEntry(shared_ptr<pit::Entry> pitEntry)
91{
Junxiao Shib184e532016-05-26 18:09:57 +000092 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shi340d5532016-08-13 04:00:35 +000093 BOOST_ASSERT(pitEntry->m_nameTreeEntry == nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -070094
95 m_pitEntries.push_back(pitEntry);
Junxiao Shi340d5532016-08-13 04:00:35 +000096 pitEntry->m_nameTreeEntry = this;
Junxiao Shiee5a4442014-07-27 17:13:43 -070097}
98
HYuana9b85752014-02-26 02:32:30 -060099void
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000100Entry::erasePitEntry(pit::Entry* pitEntry)
HYuana9b85752014-02-26 02:32:30 -0600101{
Junxiao Shib184e532016-05-26 18:09:57 +0000102 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shi340d5532016-08-13 04:00:35 +0000103 BOOST_ASSERT(pitEntry->m_nameTreeEntry == this);
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700104
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000105 auto it = std::find_if(m_pitEntries.begin(), m_pitEntries.end(),
106 [pitEntry] (const shared_ptr<pit::Entry>& pitEntry2) { return pitEntry2.get() == pitEntry; });
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700107 BOOST_ASSERT(it != m_pitEntries.end());
108
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000109 pitEntry->m_nameTreeEntry = nullptr; // must be done before pitEntry is deallocated
110 *it = m_pitEntries.back(); // may deallocate pitEntry
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700111 m_pitEntries.pop_back();
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700112}
113
Junxiao Shiee5a4442014-07-27 17:13:43 -0700114void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000115Entry::setMeasurementsEntry(unique_ptr<measurements::Entry> measurementsEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700116{
Junxiao Shi340d5532016-08-13 04:00:35 +0000117 BOOST_ASSERT(measurementsEntry == nullptr || measurementsEntry->m_nameTreeEntry == nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700118
Junxiao Shib184e532016-05-26 18:09:57 +0000119 if (m_measurementsEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +0000120 m_measurementsEntry->m_nameTreeEntry = nullptr;
Junxiao Shiee5a4442014-07-27 17:13:43 -0700121 }
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000122 m_measurementsEntry = std::move(measurementsEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000123
124 if (m_measurementsEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +0000125 m_measurementsEntry->m_nameTreeEntry = this;
Junxiao Shiee5a4442014-07-27 17:13:43 -0700126 }
127}
128
129void
Junxiao Shiff10da62016-07-13 17:57:43 +0000130Entry::setStrategyChoiceEntry(unique_ptr<strategy_choice::Entry> strategyChoiceEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700131{
Junxiao Shi340d5532016-08-13 04:00:35 +0000132 BOOST_ASSERT(strategyChoiceEntry == nullptr || strategyChoiceEntry->m_nameTreeEntry == nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700133
Junxiao Shib184e532016-05-26 18:09:57 +0000134 if (m_strategyChoiceEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +0000135 m_strategyChoiceEntry->m_nameTreeEntry = nullptr;
Junxiao Shiee5a4442014-07-27 17:13:43 -0700136 }
Junxiao Shiff10da62016-07-13 17:57:43 +0000137 m_strategyChoiceEntry = std::move(strategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000138
Junxiao Shib184e532016-05-26 18:09:57 +0000139 if (m_strategyChoiceEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +0000140 m_strategyChoiceEntry->m_nameTreeEntry = this;
Junxiao Shiee5a4442014-07-27 17:13:43 -0700141 }
142}
143
HYuana9b85752014-02-26 02:32:30 -0600144} // namespace name_tree
145} // namespace nfd