NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019, 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 {
30 namespace name_tree {
31 
32 Entry::Entry(const Name& name, Node* node)
33  : m_name(name)
34  , m_node(node)
35 {
36  BOOST_ASSERT(node != nullptr);
37  BOOST_ASSERT(name.size() <= NameTree::getMaxDepth());
38 }
39 
40 void
42 {
43  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;
48  m_parent->m_children.push_back(this);
49 }
50 
51 void
53 {
54  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;
61 }
62 
63 bool
65 {
66  return m_fibEntry != nullptr ||
67  !m_pitEntries.empty() ||
68  m_measurementsEntry != nullptr ||
69  m_strategyChoiceEntry != nullptr;
70 }
71 
72 void
73 Entry::setFibEntry(unique_ptr<fib::Entry> fibEntry)
74 {
75  BOOST_ASSERT(fibEntry == nullptr || fibEntry->m_nameTreeEntry == nullptr);
76 
77  if (m_fibEntry != nullptr) {
78  m_fibEntry->m_nameTreeEntry = nullptr;
79  }
80  m_fibEntry = std::move(fibEntry);
81 
82  if (m_fibEntry != nullptr) {
83  m_fibEntry->m_nameTreeEntry = this;
84  }
85 }
86 
87 void
88 Entry::insertPitEntry(shared_ptr<pit::Entry> pitEntry)
89 {
90  BOOST_ASSERT(pitEntry != nullptr);
91  BOOST_ASSERT(pitEntry->m_nameTreeEntry == nullptr);
92 
93  m_pitEntries.push_back(pitEntry);
94  pitEntry->m_nameTreeEntry = this;
95 }
96 
97 void
99 {
100  BOOST_ASSERT(pitEntry != nullptr);
101  BOOST_ASSERT(pitEntry->m_nameTreeEntry == this);
102 
103  auto it = std::find_if(m_pitEntries.begin(), m_pitEntries.end(),
104  [pitEntry] (const auto& pitEntry2) { return pitEntry2.get() == pitEntry; });
105  BOOST_ASSERT(it != m_pitEntries.end());
106 
107  pitEntry->m_nameTreeEntry = nullptr; // must be done before pitEntry is deallocated
108  *it = m_pitEntries.back(); // may deallocate pitEntry
109  m_pitEntries.pop_back();
110 }
111 
112 void
113 Entry::setMeasurementsEntry(unique_ptr<measurements::Entry> measurementsEntry)
114 {
115  BOOST_ASSERT(measurementsEntry == nullptr || measurementsEntry->m_nameTreeEntry == nullptr);
116 
117  if (m_measurementsEntry != nullptr) {
118  m_measurementsEntry->m_nameTreeEntry = nullptr;
119  }
120  m_measurementsEntry = std::move(measurementsEntry);
121 
122  if (m_measurementsEntry != nullptr) {
123  m_measurementsEntry->m_nameTreeEntry = this;
124  }
125 }
126 
127 void
128 Entry::setStrategyChoiceEntry(unique_ptr<strategy_choice::Entry> strategyChoiceEntry)
129 {
130  BOOST_ASSERT(strategyChoiceEntry == nullptr || strategyChoiceEntry->m_nameTreeEntry == nullptr);
131 
132  if (m_strategyChoiceEntry != nullptr) {
133  m_strategyChoiceEntry->m_nameTreeEntry = nullptr;
134  }
135  m_strategyChoiceEntry = std::move(strategyChoiceEntry);
136 
137  if (m_strategyChoiceEntry != nullptr) {
138  m_strategyChoiceEntry->m_nameTreeEntry = this;
139  }
140 }
141 
142 } // namespace name_tree
143 } // namespace nfd
void insertPitEntry(shared_ptr< pit::Entry > pitEntry)
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:209
void erasePitEntry(pit::Entry *pitEntry)
Entry(const Name &prefix, Node *node)
void setParent(Entry &entry)
Set parent of this entry.
void setMeasurementsEntry(unique_ptr< measurements::Entry > measurementsEntry)
bool hasTableEntries() const
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
An Interest table entry.
Definition: pit-entry.hpp:58
Represents an absolute name.
Definition: name.hpp:41
size_t size() const
Returns the number of components.
Definition: name.hpp:151
void unsetParent()
Unset parent of this entry.
Entry * getParent() const
const Name & getName() const
void setFibEntry(unique_ptr< fib::Entry > fibEntry)
void setStrategyChoiceEntry(unique_ptr< strategy_choice::Entry > strategyChoiceEntry)
An entry in the name tree.
static constexpr size_t getMaxDepth()
Maximum depth of the name tree.
Definition: name-tree.hpp:51