blob: c145129bb954026e93341a77a6c5bc079470734d [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 Pesavento50a6af32019-02-21 00:04:40 -05003 * Copyright (c) 2014-2019, 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)
HYuana9b85752014-02-26 02:32:30 -060035{
Junxiao Shib660b4c2016-08-06 20:47:44 +000036 BOOST_ASSERT(node != nullptr);
Junxiao Shi057d1492018-03-20 17:14:18 +000037 BOOST_ASSERT(name.size() <= NameTree::getMaxDepth());
HYuana9b85752014-02-26 02:32:30 -060038}
39
Junxiao Shib660b4c2016-08-06 20:47:44 +000040void
41Entry::setParent(Entry& entry)
HYuana9b85752014-02-26 02:32:30 -060042{
Junxiao Shib660b4c2016-08-06 20:47:44 +000043 BOOST_ASSERT(this->getParent() == nullptr);
44 BOOST_ASSERT(!this->getName().empty());
45 BOOST_ASSERT(entry.getName() == this->getName().getPrefix(-1));
46
47 m_parent = &entry;
Junxiao Shib660b4c2016-08-06 20:47:44 +000048 m_parent->m_children.push_back(this);
HYuana9b85752014-02-26 02:32:30 -060049}
50
Junxiao Shib660b4c2016-08-06 20:47:44 +000051void
52Entry::unsetParent()
HYuana9b85752014-02-26 02:32:30 -060053{
Junxiao Shib660b4c2016-08-06 20:47:44 +000054 BOOST_ASSERT(this->getParent() != nullptr);
55
56 auto i = std::find(m_parent->m_children.begin(), m_parent->m_children.end(), this);
57 BOOST_ASSERT(i != m_parent->m_children.end());
58 m_parent->m_children.erase(i);
59
60 m_parent = nullptr;
HYuana9b85752014-02-26 02:32:30 -060061}
62
Junxiao Shiee5a4442014-07-27 17:13:43 -070063bool
Junxiao Shib660b4c2016-08-06 20:47:44 +000064Entry::hasTableEntries() const
Junxiao Shiee5a4442014-07-27 17:13:43 -070065{
Junxiao Shib660b4c2016-08-06 20:47:44 +000066 return m_fibEntry != nullptr ||
67 !m_pitEntries.empty() ||
68 m_measurementsEntry != nullptr ||
69 m_strategyChoiceEntry != nullptr;
Junxiao Shiee5a4442014-07-27 17:13:43 -070070}
71
72void
Junxiao Shia6de4292016-07-12 02:08:10 +000073Entry::setFibEntry(unique_ptr<fib::Entry> fibEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -070074{
Junxiao Shi340d5532016-08-13 04:00:35 +000075 BOOST_ASSERT(fibEntry == nullptr || fibEntry->m_nameTreeEntry == nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -070076
Junxiao Shib184e532016-05-26 18:09:57 +000077 if (m_fibEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +000078 m_fibEntry->m_nameTreeEntry = nullptr;
Junxiao Shiee5a4442014-07-27 17:13:43 -070079 }
Junxiao Shia6de4292016-07-12 02:08:10 +000080 m_fibEntry = std::move(fibEntry);
Junxiao Shib184e532016-05-26 18:09:57 +000081
82 if (m_fibEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +000083 m_fibEntry->m_nameTreeEntry = this;
Junxiao Shiee5a4442014-07-27 17:13:43 -070084 }
85}
86
87void
88Entry::insertPitEntry(shared_ptr<pit::Entry> pitEntry)
89{
Junxiao Shib184e532016-05-26 18:09:57 +000090 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shi340d5532016-08-13 04:00:35 +000091 BOOST_ASSERT(pitEntry->m_nameTreeEntry == nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -070092
93 m_pitEntries.push_back(pitEntry);
Junxiao Shi340d5532016-08-13 04:00:35 +000094 pitEntry->m_nameTreeEntry = this;
Junxiao Shiee5a4442014-07-27 17:13:43 -070095}
96
HYuana9b85752014-02-26 02:32:30 -060097void
Junxiao Shidbef6dc2016-08-15 02:58:36 +000098Entry::erasePitEntry(pit::Entry* pitEntry)
HYuana9b85752014-02-26 02:32:30 -060099{
Junxiao Shib184e532016-05-26 18:09:57 +0000100 BOOST_ASSERT(pitEntry != nullptr);
Junxiao Shi340d5532016-08-13 04:00:35 +0000101 BOOST_ASSERT(pitEntry->m_nameTreeEntry == this);
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700102
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000103 auto it = std::find_if(m_pitEntries.begin(), m_pitEntries.end(),
Davide Pesavento50a6af32019-02-21 00:04:40 -0500104 [pitEntry] (const auto& pitEntry2) { return pitEntry2.get() == pitEntry; });
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700105 BOOST_ASSERT(it != m_pitEntries.end());
106
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000107 pitEntry->m_nameTreeEntry = nullptr; // must be done before pitEntry is deallocated
108 *it = m_pitEntries.back(); // may deallocate pitEntry
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700109 m_pitEntries.pop_back();
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700110}
111
Junxiao Shiee5a4442014-07-27 17:13:43 -0700112void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000113Entry::setMeasurementsEntry(unique_ptr<measurements::Entry> measurementsEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700114{
Junxiao Shi340d5532016-08-13 04:00:35 +0000115 BOOST_ASSERT(measurementsEntry == nullptr || measurementsEntry->m_nameTreeEntry == nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700116
Junxiao Shib184e532016-05-26 18:09:57 +0000117 if (m_measurementsEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +0000118 m_measurementsEntry->m_nameTreeEntry = nullptr;
Junxiao Shiee5a4442014-07-27 17:13:43 -0700119 }
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000120 m_measurementsEntry = std::move(measurementsEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000121
122 if (m_measurementsEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +0000123 m_measurementsEntry->m_nameTreeEntry = this;
Junxiao Shiee5a4442014-07-27 17:13:43 -0700124 }
125}
126
127void
Junxiao Shiff10da62016-07-13 17:57:43 +0000128Entry::setStrategyChoiceEntry(unique_ptr<strategy_choice::Entry> strategyChoiceEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700129{
Junxiao Shi340d5532016-08-13 04:00:35 +0000130 BOOST_ASSERT(strategyChoiceEntry == nullptr || strategyChoiceEntry->m_nameTreeEntry == nullptr);
Junxiao Shiee5a4442014-07-27 17:13:43 -0700131
Junxiao Shib184e532016-05-26 18:09:57 +0000132 if (m_strategyChoiceEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +0000133 m_strategyChoiceEntry->m_nameTreeEntry = nullptr;
Junxiao Shiee5a4442014-07-27 17:13:43 -0700134 }
Junxiao Shiff10da62016-07-13 17:57:43 +0000135 m_strategyChoiceEntry = std::move(strategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000136
Junxiao Shib184e532016-05-26 18:09:57 +0000137 if (m_strategyChoiceEntry != nullptr) {
Junxiao Shi340d5532016-08-13 04:00:35 +0000138 m_strategyChoiceEntry->m_nameTreeEntry = this;
Junxiao Shiee5a4442014-07-27 17:13:43 -0700139 }
140}
141
HYuana9b85752014-02-26 02:32:30 -0600142} // namespace name_tree
143} // namespace nfd