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
name-tree.hpp
nfd::name_tree::Entry
An entry in the name tree.
Definition: name-tree-entry.hpp:42
nfd::name_tree::Entry::setParent
void setParent(Entry &entry)
Set parent of this entry.
Definition: name-tree-entry.cpp:41
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
nfd::name_tree::Entry::erasePitEntry
void erasePitEntry(pit::Entry *pitEntry)
Definition: name-tree-entry.cpp:98
nfd::name_tree::Entry::unsetParent
void unsetParent()
Unset parent of this entry.
Definition: name-tree-entry.cpp:52
name-tree-entry.hpp
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
nfd::name_tree::Entry::getName
const Name & getName() const
Definition: name-tree-entry.hpp:47
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::name_tree::Entry::setMeasurementsEntry
void setMeasurementsEntry(unique_ptr< measurements::Entry > measurementsEntry)
Definition: name-tree-entry.cpp:113
nfd::name_tree::Node
a hashtable node
Definition: name-tree-hashtable.hpp:62
ndn::Name::getPrefix
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:211
nfd::pit::Entry
An Interest table entry.
Definition: pit-entry.hpp:59
nfd::name_tree::Entry::insertPitEntry
void insertPitEntry(shared_ptr< pit::Entry > pitEntry)
Definition: name-tree-entry.cpp:88
nfd::name_tree::Entry::setFibEntry
void setFibEntry(unique_ptr< fib::Entry > fibEntry)
Definition: name-tree-entry.cpp:73
nfd::name_tree::Entry::Entry
Entry(const Name &prefix, Node *node)
Definition: name-tree-entry.cpp:32
nfd::name_tree::NameTree::getMaxDepth
static constexpr size_t getMaxDepth()
Maximum depth of the name tree.
Definition: name-tree.hpp:51
ndn::name
Definition: name-component-types.hpp:33
nfd::name_tree::Entry::hasTableEntries
bool hasTableEntries() const
Definition: name-tree-entry.cpp:64
nfd::name_tree::Entry::setStrategyChoiceEntry
void setStrategyChoiceEntry(unique_ptr< strategy_choice::Entry > strategyChoiceEntry)
Definition: name-tree-entry.cpp:128
nfd::name_tree::Entry::getParent
Entry * getParent() const
Definition: name-tree-entry.hpp:56