blob: 77b66ef6ceac4d19588a93bf05b5b0012ea49d10 [file] [log] [blame]
HYuana9b85752014-02-26 02:32:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shib184e532016-05-26 18:09:57 +00003 * Copyright (c) 2014-2016, 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.
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"
27
28namespace nfd {
29namespace name_tree {
30
Junxiao Shib660b4c2016-08-06 20:47:44 +000031Entry::Entry(const Name& name, Node* node)
32 : m_name(name)
33 , m_node(node)
34 , m_parent(nullptr)
HYuana9b85752014-02-26 02:32:30 -060035{
Junxiao Shib660b4c2016-08-06 20:47:44 +000036 BOOST_ASSERT(node != nullptr);
HYuana9b85752014-02-26 02:32:30 -060037}
38
Junxiao Shib660b4c2016-08-06 20:47:44 +000039void
40Entry::setParent(Entry& entry)
HYuana9b85752014-02-26 02:32:30 -060041{
Junxiao Shib660b4c2016-08-06 20:47:44 +000042 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
48 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 Shib184e532016-05-26 18:09:57 +000075 BOOST_ASSERT(fibEntry == nullptr || fibEntry->m_nameTreeEntry.expired());
Junxiao Shiee5a4442014-07-27 17:13:43 -070076
Junxiao Shib184e532016-05-26 18:09:57 +000077 if (m_fibEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -070078 m_fibEntry->m_nameTreeEntry.reset();
79 }
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 Shiee5a4442014-07-27 17:13:43 -070083 m_fibEntry->m_nameTreeEntry = this->shared_from_this();
84 }
85}
86
87void
88Entry::insertPitEntry(shared_ptr<pit::Entry> pitEntry)
89{
Junxiao Shib184e532016-05-26 18:09:57 +000090 BOOST_ASSERT(pitEntry != nullptr);
91 BOOST_ASSERT(pitEntry->m_nameTreeEntry.expired());
Junxiao Shiee5a4442014-07-27 17:13:43 -070092
93 m_pitEntries.push_back(pitEntry);
94 pitEntry->m_nameTreeEntry = this->shared_from_this();
95}
96
HYuana9b85752014-02-26 02:32:30 -060097void
Junxiao Shi9f7455b2014-04-07 21:02:16 -070098Entry::erasePitEntry(shared_ptr<pit::Entry> pitEntry)
HYuana9b85752014-02-26 02:32:30 -060099{
Junxiao Shib184e532016-05-26 18:09:57 +0000100 BOOST_ASSERT(pitEntry != nullptr);
101 BOOST_ASSERT(pitEntry->m_nameTreeEntry.lock().get() == this);
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700102
Junxiao Shib184e532016-05-26 18:09:57 +0000103 auto it = std::find(m_pitEntries.begin(), m_pitEntries.end(), pitEntry);
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700104 BOOST_ASSERT(it != m_pitEntries.end());
105
106 *it = m_pitEntries.back();
107 m_pitEntries.pop_back();
108 pitEntry->m_nameTreeEntry.reset();
109}
110
Junxiao Shiee5a4442014-07-27 17:13:43 -0700111void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000112Entry::setMeasurementsEntry(unique_ptr<measurements::Entry> measurementsEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700113{
Junxiao Shib184e532016-05-26 18:09:57 +0000114 BOOST_ASSERT(measurementsEntry == nullptr || measurementsEntry->m_nameTreeEntry.expired());
Junxiao Shiee5a4442014-07-27 17:13:43 -0700115
Junxiao Shib184e532016-05-26 18:09:57 +0000116 if (m_measurementsEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700117 m_measurementsEntry->m_nameTreeEntry.reset();
118 }
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000119 m_measurementsEntry = std::move(measurementsEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000120
121 if (m_measurementsEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700122 m_measurementsEntry->m_nameTreeEntry = this->shared_from_this();
123 }
124}
125
126void
Junxiao Shiff10da62016-07-13 17:57:43 +0000127Entry::setStrategyChoiceEntry(unique_ptr<strategy_choice::Entry> strategyChoiceEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700128{
Junxiao Shib184e532016-05-26 18:09:57 +0000129 BOOST_ASSERT(strategyChoiceEntry == nullptr || strategyChoiceEntry->m_nameTreeEntry.expired());
Junxiao Shiee5a4442014-07-27 17:13:43 -0700130
Junxiao Shib184e532016-05-26 18:09:57 +0000131 if (m_strategyChoiceEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700132 m_strategyChoiceEntry->m_nameTreeEntry.reset();
133 }
Junxiao Shiff10da62016-07-13 17:57:43 +0000134 m_strategyChoiceEntry = std::move(strategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000135
Junxiao Shib184e532016-05-26 18:09:57 +0000136 if (m_strategyChoiceEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700137 m_strategyChoiceEntry->m_nameTreeEntry = this->shared_from_this();
138 }
139}
140
HYuana9b85752014-02-26 02:32:30 -0600141} // namespace name_tree
142} // namespace nfd