name-tree-entry.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, 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.
10  *
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/>.
24  */
25 
26 #include "name-tree-entry.hpp"
27 #include "name-tree.hpp"
28 
29 namespace nfd::name_tree {
30 
31 Entry::Entry(const Name& name, Node* node)
32  : m_name(name)
33  , m_node(node)
34 {
35  BOOST_ASSERT(node != nullptr);
36  BOOST_ASSERT(name.size() <= NameTree::getMaxDepth());
37 }
38 
39 void
41 {
42  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;
47  m_parent->m_children.push_back(this);
48 }
49 
50 void
52 {
53  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;
60 }
61 
62 bool
64 {
65  return m_fibEntry != nullptr ||
66  !m_pitEntries.empty() ||
67  m_measurementsEntry != nullptr ||
68  m_strategyChoiceEntry != nullptr;
69 }
70 
71 void
72 Entry::setFibEntry(unique_ptr<fib::Entry> fibEntry)
73 {
74  BOOST_ASSERT(fibEntry == nullptr || fibEntry->m_nameTreeEntry == nullptr);
75 
76  if (m_fibEntry != nullptr) {
77  m_fibEntry->m_nameTreeEntry = nullptr;
78  }
79  m_fibEntry = std::move(fibEntry);
80 
81  if (m_fibEntry != nullptr) {
82  m_fibEntry->m_nameTreeEntry = this;
83  }
84 }
85 
86 void
87 Entry::insertPitEntry(shared_ptr<pit::Entry> pitEntry)
88 {
89  BOOST_ASSERT(pitEntry != nullptr);
90  BOOST_ASSERT(pitEntry->m_nameTreeEntry == nullptr);
91 
92  m_pitEntries.push_back(pitEntry);
93  pitEntry->m_nameTreeEntry = this;
94 }
95 
96 void
98 {
99  BOOST_ASSERT(pitEntry != nullptr);
100  BOOST_ASSERT(pitEntry->m_nameTreeEntry == this);
101 
102  auto it = std::find_if(m_pitEntries.begin(), m_pitEntries.end(),
103  [pitEntry] (const auto& pitEntry2) { return pitEntry2.get() == pitEntry; });
104  BOOST_ASSERT(it != m_pitEntries.end());
105 
106  pitEntry->m_nameTreeEntry = nullptr; // must be done before pitEntry is deallocated
107  *it = m_pitEntries.back(); // may deallocate pitEntry
108  m_pitEntries.pop_back();
109 }
110 
111 void
112 Entry::setMeasurementsEntry(unique_ptr<measurements::Entry> measurementsEntry)
113 {
114  BOOST_ASSERT(measurementsEntry == nullptr || measurementsEntry->m_nameTreeEntry == nullptr);
115 
116  if (m_measurementsEntry != nullptr) {
117  m_measurementsEntry->m_nameTreeEntry = nullptr;
118  }
119  m_measurementsEntry = std::move(measurementsEntry);
120 
121  if (m_measurementsEntry != nullptr) {
122  m_measurementsEntry->m_nameTreeEntry = this;
123  }
124 }
125 
126 void
127 Entry::setStrategyChoiceEntry(unique_ptr<strategy_choice::Entry> strategyChoiceEntry)
128 {
129  BOOST_ASSERT(strategyChoiceEntry == nullptr || strategyChoiceEntry->m_nameTreeEntry == nullptr);
130 
131  if (m_strategyChoiceEntry != nullptr) {
132  m_strategyChoiceEntry->m_nameTreeEntry = nullptr;
133  }
134  m_strategyChoiceEntry = std::move(strategyChoiceEntry);
135 
136  if (m_strategyChoiceEntry != nullptr) {
137  m_strategyChoiceEntry->m_nameTreeEntry = this;
138  }
139 }
140 
141 } // namespace nfd::name_tree
An entry in the name tree.
void unsetParent()
Unset parent of this entry.
Entry(const Name &prefix, Node *node)
void insertPitEntry(shared_ptr< pit::Entry > pitEntry)
void setMeasurementsEntry(unique_ptr< measurements::Entry > measurementsEntry)
void erasePitEntry(pit::Entry *pitEntry)
void setParent(Entry &entry)
Set parent of this entry.
Entry * getParent() const noexcept
void setStrategyChoiceEntry(unique_ptr< strategy_choice::Entry > strategyChoiceEntry)
void setFibEntry(unique_ptr< fib::Entry > fibEntry)
const Name & getName() const noexcept
bool hasTableEntries() const
static constexpr size_t getMaxDepth()
Maximum depth of the name tree.
Definition: name-tree.hpp:51
Represents an entry in the Interest table (PIT).
Definition: pit-entry.hpp:62