blob: a4f898be8249a9d139de7066eebc1804c60de439 [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/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040029namespace nfd::name_tree {
HYuana9b85752014-02-26 02:32:30 -060030
Junxiao Shib660b4c2016-08-06 20:47:44 +000031Entry::Entry(const Name& name, Node* node)
32 : m_name(name)
33 , m_node(node)
HYuana9b85752014-02-26 02:32:30 -060034{
Junxiao Shib660b4c2016-08-06 20:47:44 +000035 BOOST_ASSERT(node != nullptr);
Junxiao Shi057d1492018-03-20 17:14:18 +000036 BOOST_ASSERT(name.size() <= NameTree::getMaxDepth());
HYuana9b85752014-02-26 02:32:30 -060037}
38
Junxiao Shib660b4c2016-08-06 20:47:44 +000039void
40Entry::setParent(Entry& entry)
HYuana9b85752014-02-26 02:32:30 -060041{
Junxiao Shib660b4c2016-08-06 20:47:44 +000042 BOOST_ASSERT(this->getParent() == nullptr);
43 BOOST_ASSERT(!this->getName().empty());
44 BOOST_ASSERT(entry.getName() == this->getName().getPrefix(-1));
45
46 m_parent = &entry;
Junxiao Shib660b4c2016-08-06 20:47:44 +000047 m_parent->m_children.push_back(this);
HYuana9b85752014-02-26 02:32:30 -060048}
49
Junxiao Shib660b4c2016-08-06 20:47:44 +000050void
51Entry::unsetParent()
HYuana9b85752014-02-26 02:32:30 -060052{
Junxiao Shib660b4c2016-08-06 20:47:44 +000053 BOOST_ASSERT(this->getParent() != nullptr);
54
55 auto i = std::find(m_parent->m_children.begin(), m_parent->m_children.end(), this);
56 BOOST_ASSERT(i != m_parent->m_children.end());
57 m_parent->m_children.erase(i);
58
59 m_parent = nullptr;
HYuana9b85752014-02-26 02:32:30 -060060}
61
Junxiao Shiee5a4442014-07-27 17:13:43 -070062bool
Junxiao Shib660b4c2016-08-06 20:47:44 +000063Entry::hasTableEntries() const
Junxiao Shiee5a4442014-07-27 17:13:43 -070064{
Junxiao Shib660b4c2016-08-06 20:47:44 +000065 return m_fibEntry != nullptr ||
66 !m_pitEntries.empty() ||
67 m_measurementsEntry != nullptr ||
68 m_strategyChoiceEntry != nullptr;
Junxiao Shiee5a4442014-07-27 17:13:43 -070069}
70
71void
Junxiao Shia6de4292016-07-12 02:08:10 +000072Entry::setFibEntry(unique_ptr<fib::Entry> fibEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -070073{
Junxiao Shi340d5532016-08-13 04:00:35 +000074 BOOST_ASSERT(fibEntry == nullptr || fibEntry->m_nameTreeEntry == nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -070075
Junxiao Shib184e532016-05-26 18:09:57 +000076 if (m_fibEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +000077 m_fibEntry->m_nameTreeEntry = nullptr;
Junxiao Shiee5a4442014-07-27 17:13:43 -070078 }
Junxiao Shia6de4292016-07-12 02:08:10 +000079 m_fibEntry = std::move(fibEntry);
Junxiao Shib184e532016-05-26 18:09:57 +000080
81 if (m_fibEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +000082 m_fibEntry->m_nameTreeEntry = this;
Junxiao Shiee5a4442014-07-27 17:13:43 -070083 }
84}
85
86void
87Entry::insertPitEntry(shared_ptr<pit::Entry> pitEntry)
88{
Junxiao Shib184e532016-05-26 18:09:57 +000089 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shi340d5532016-08-13 04:00:35 +000090 BOOST_ASSERT(pitEntry->m_nameTreeEntry == nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -070091
92 m_pitEntries.push_back(pitEntry);
Junxiao Shi340d5532016-08-13 04:00:35 +000093 pitEntry->m_nameTreeEntry = this;
Junxiao Shiee5a4442014-07-27 17:13:43 -070094}
95
HYuana9b85752014-02-26 02:32:30 -060096void
Junxiao Shidbef6dc2016-08-15 02:58:36 +000097Entry::erasePitEntry(pit::Entry* pitEntry)
HYuana9b85752014-02-26 02:32:30 -060098{
Junxiao Shib184e532016-05-26 18:09:57 +000099 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shi340d5532016-08-13 04:00:35 +0000100 BOOST_ASSERT(pitEntry->m_nameTreeEntry == this);
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700101
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000102 auto it = std::find_if(m_pitEntries.begin(), m_pitEntries.end(),
Davide Pesavento50a6af32019-02-21 00:04:40 -0500103 [pitEntry] (const auto& pitEntry2) { return pitEntry2.get() == pitEntry; });
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700104 BOOST_ASSERT(it != m_pitEntries.end());
105
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000106 pitEntry->m_nameTreeEntry = nullptr; // must be done before pitEntry is deallocated
107 *it = m_pitEntries.back(); // may deallocate pitEntry
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700108 m_pitEntries.pop_back();
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700109}
110
Junxiao Shiee5a4442014-07-27 17:13:43 -0700111void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000112Entry::setMeasurementsEntry(unique_ptr<measurements::Entry> measurementsEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700113{
Junxiao Shi340d5532016-08-13 04:00:35 +0000114 BOOST_ASSERT(measurementsEntry == nullptr || measurementsEntry->m_nameTreeEntry == nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700115
Junxiao Shib184e532016-05-26 18:09:57 +0000116 if (m_measurementsEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +0000117 m_measurementsEntry->m_nameTreeEntry = nullptr;
Junxiao Shiee5a4442014-07-27 17:13:43 -0700118 }
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000119 m_measurementsEntry = std::move(measurementsEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000120
121 if (m_measurementsEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +0000122 m_measurementsEntry->m_nameTreeEntry = this;
Junxiao Shiee5a4442014-07-27 17:13:43 -0700123 }
124}
125
126void
Junxiao Shiff10da62016-07-13 17:57:43 +0000127Entry::setStrategyChoiceEntry(unique_ptr<strategy_choice::Entry> strategyChoiceEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700128{
Junxiao Shi340d5532016-08-13 04:00:35 +0000129 BOOST_ASSERT(strategyChoiceEntry == nullptr || strategyChoiceEntry->m_nameTreeEntry == nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700130
Junxiao Shib184e532016-05-26 18:09:57 +0000131 if (m_strategyChoiceEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +0000132 m_strategyChoiceEntry->m_nameTreeEntry = nullptr;
Junxiao Shiee5a4442014-07-27 17:13:43 -0700133 }
Junxiao Shiff10da62016-07-13 17:57:43 +0000134 m_strategyChoiceEntry = std::move(strategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000135
Junxiao Shib184e532016-05-26 18:09:57 +0000136 if (m_strategyChoiceEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +0000137 m_strategyChoiceEntry->m_nameTreeEntry = this;
Junxiao Shiee5a4442014-07-27 17:13:43 -0700138 }
139}
140
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400141} // namespace nfd::name_tree