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-2018, 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  , m_parent(nullptr)
36 {
37  BOOST_ASSERT(node != nullptr);
38  BOOST_ASSERT(name.size() <= NameTree::getMaxDepth());
39 }
40 
41 void
43 {
44  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);
51 }
52 
53 void
55 {
56  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;
63 }
64 
65 bool
67 {
68  return m_fibEntry != nullptr ||
69  !m_pitEntries.empty() ||
70  m_measurementsEntry != nullptr ||
71  m_strategyChoiceEntry != nullptr;
72 }
73 
74 void
75 Entry::setFibEntry(unique_ptr<fib::Entry> fibEntry)
76 {
77  BOOST_ASSERT(fibEntry == nullptr || fibEntry->m_nameTreeEntry == nullptr);
78 
79  if (m_fibEntry != nullptr) {
80  m_fibEntry->m_nameTreeEntry = nullptr;
81  }
82  m_fibEntry = std::move(fibEntry);
83 
84  if (m_fibEntry != nullptr) {
85  m_fibEntry->m_nameTreeEntry = this;
86  }
87 }
88 
89 void
90 Entry::insertPitEntry(shared_ptr<pit::Entry> pitEntry)
91 {
92  BOOST_ASSERT(pitEntry != nullptr);
93  BOOST_ASSERT(pitEntry->m_nameTreeEntry == nullptr);
94 
95  m_pitEntries.push_back(pitEntry);
96  pitEntry->m_nameTreeEntry = this;
97 }
98 
99 void
101 {
102  BOOST_ASSERT(pitEntry != nullptr);
103  BOOST_ASSERT(pitEntry->m_nameTreeEntry == this);
104 
105  auto it = std::find_if(m_pitEntries.begin(), m_pitEntries.end(),
106  [pitEntry] (const shared_ptr<pit::Entry>& pitEntry2) { return pitEntry2.get() == pitEntry; });
107  BOOST_ASSERT(it != m_pitEntries.end());
108 
109  pitEntry->m_nameTreeEntry = nullptr; // must be done before pitEntry is deallocated
110  *it = m_pitEntries.back(); // may deallocate pitEntry
111  m_pitEntries.pop_back();
112 }
113 
114 void
115 Entry::setMeasurementsEntry(unique_ptr<measurements::Entry> measurementsEntry)
116 {
117  BOOST_ASSERT(measurementsEntry == nullptr || measurementsEntry->m_nameTreeEntry == nullptr);
118 
119  if (m_measurementsEntry != nullptr) {
120  m_measurementsEntry->m_nameTreeEntry = nullptr;
121  }
122  m_measurementsEntry = std::move(measurementsEntry);
123 
124  if (m_measurementsEntry != nullptr) {
125  m_measurementsEntry->m_nameTreeEntry = this;
126  }
127 }
128 
129 void
130 Entry::setStrategyChoiceEntry(unique_ptr<strategy_choice::Entry> strategyChoiceEntry)
131 {
132  BOOST_ASSERT(strategyChoiceEntry == nullptr || strategyChoiceEntry->m_nameTreeEntry == nullptr);
133 
134  if (m_strategyChoiceEntry != nullptr) {
135  m_strategyChoiceEntry->m_nameTreeEntry = nullptr;
136  }
137  m_strategyChoiceEntry = std::move(strategyChoiceEntry);
138 
139  if (m_strategyChoiceEntry != nullptr) {
140  m_strategyChoiceEntry->m_nameTreeEntry = this;
141  }
142 }
143 
144 } // namespace name_tree
145 } // namespace nfd
void insertPitEntry(shared_ptr< pit::Entry > pitEntry)
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix of the name.
Definition: name.hpp:203
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:40
an Interest table entry
Definition: pit-entry.hpp:59
Represents an absolute name.
Definition: name.hpp:43
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:53