blob: c0bc932371b3601e583ab3ec976825c85f8e6f31 [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
Haowei Yuanf52dac72014-03-24 23:35:03 -050031Node::Node()
Junxiao Shi2570f3e2016-07-27 02:48:29 +000032 : m_prev(nullptr)
33 , m_next(nullptr)
HYuana9b85752014-02-26 02:32:30 -060034{
35}
36
37Node::~Node()
38{
39 // erase the Name Tree Nodes that were created to
40 // resolve hash collisions
Junxiao Shi2570f3e2016-07-27 02:48:29 +000041 // So before erasing a single node, make sure its m_next == nullptr
HYuana9b85752014-02-26 02:32:30 -060042 // See eraseEntryIfEmpty in name-tree.cpp
Junxiao Shi2570f3e2016-07-27 02:48:29 +000043 if (m_next != nullptr)
HYuana9b85752014-02-26 02:32:30 -060044 delete m_next;
45}
46
Junxiao Shi9f7455b2014-04-07 21:02:16 -070047Entry::Entry(const Name& name)
48 : m_hash(0)
49 , m_prefix(name)
HYuana9b85752014-02-26 02:32:30 -060050{
51}
52
Junxiao Shiee5a4442014-07-27 17:13:43 -070053bool
54Entry::isEmpty() const
55{
56 return m_children.empty() &&
Junxiao Shib184e532016-05-26 18:09:57 +000057 m_fibEntry == nullptr &&
Junxiao Shiee5a4442014-07-27 17:13:43 -070058 m_pitEntries.empty() &&
Junxiao Shib184e532016-05-26 18:09:57 +000059 m_measurementsEntry == nullptr &&
60 m_strategyChoiceEntry == nullptr;
Junxiao Shiee5a4442014-07-27 17:13:43 -070061}
62
63void
Junxiao Shia6de4292016-07-12 02:08:10 +000064Entry::setFibEntry(unique_ptr<fib::Entry> fibEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -070065{
Junxiao Shib184e532016-05-26 18:09:57 +000066 BOOST_ASSERT(fibEntry == nullptr || fibEntry->m_nameTreeEntry.expired());
Junxiao Shiee5a4442014-07-27 17:13:43 -070067
Junxiao Shib184e532016-05-26 18:09:57 +000068 if (m_fibEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -070069 m_fibEntry->m_nameTreeEntry.reset();
70 }
Junxiao Shia6de4292016-07-12 02:08:10 +000071 m_fibEntry = std::move(fibEntry);
Junxiao Shib184e532016-05-26 18:09:57 +000072
73 if (m_fibEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -070074 m_fibEntry->m_nameTreeEntry = this->shared_from_this();
75 }
76}
77
78void
79Entry::insertPitEntry(shared_ptr<pit::Entry> pitEntry)
80{
Junxiao Shib184e532016-05-26 18:09:57 +000081 BOOST_ASSERT(pitEntry != nullptr);
82 BOOST_ASSERT(pitEntry->m_nameTreeEntry.expired());
Junxiao Shiee5a4442014-07-27 17:13:43 -070083
84 m_pitEntries.push_back(pitEntry);
85 pitEntry->m_nameTreeEntry = this->shared_from_this();
86}
87
HYuana9b85752014-02-26 02:32:30 -060088void
Junxiao Shi9f7455b2014-04-07 21:02:16 -070089Entry::erasePitEntry(shared_ptr<pit::Entry> pitEntry)
HYuana9b85752014-02-26 02:32:30 -060090{
Junxiao Shib184e532016-05-26 18:09:57 +000091 BOOST_ASSERT(pitEntry != nullptr);
92 BOOST_ASSERT(pitEntry->m_nameTreeEntry.lock().get() == this);
Junxiao Shi9f7455b2014-04-07 21:02:16 -070093
Junxiao Shib184e532016-05-26 18:09:57 +000094 auto it = std::find(m_pitEntries.begin(), m_pitEntries.end(), pitEntry);
Junxiao Shi9f7455b2014-04-07 21:02:16 -070095 BOOST_ASSERT(it != m_pitEntries.end());
96
97 *it = m_pitEntries.back();
98 m_pitEntries.pop_back();
99 pitEntry->m_nameTreeEntry.reset();
100}
101
Junxiao Shiee5a4442014-07-27 17:13:43 -0700102void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000103Entry::setMeasurementsEntry(unique_ptr<measurements::Entry> measurementsEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700104{
Junxiao Shib184e532016-05-26 18:09:57 +0000105 BOOST_ASSERT(measurementsEntry == nullptr || measurementsEntry->m_nameTreeEntry.expired());
Junxiao Shiee5a4442014-07-27 17:13:43 -0700106
Junxiao Shib184e532016-05-26 18:09:57 +0000107 if (m_measurementsEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700108 m_measurementsEntry->m_nameTreeEntry.reset();
109 }
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000110 m_measurementsEntry = std::move(measurementsEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000111
112 if (m_measurementsEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700113 m_measurementsEntry->m_nameTreeEntry = this->shared_from_this();
114 }
115}
116
117void
Junxiao Shiff10da62016-07-13 17:57:43 +0000118Entry::setStrategyChoiceEntry(unique_ptr<strategy_choice::Entry> strategyChoiceEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700119{
Junxiao Shib184e532016-05-26 18:09:57 +0000120 BOOST_ASSERT(strategyChoiceEntry == nullptr || strategyChoiceEntry->m_nameTreeEntry.expired());
Junxiao Shiee5a4442014-07-27 17:13:43 -0700121
Junxiao Shib184e532016-05-26 18:09:57 +0000122 if (m_strategyChoiceEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700123 m_strategyChoiceEntry->m_nameTreeEntry.reset();
124 }
Junxiao Shiff10da62016-07-13 17:57:43 +0000125 m_strategyChoiceEntry = std::move(strategyChoiceEntry);
Junxiao Shib184e532016-05-26 18:09:57 +0000126
Junxiao Shib184e532016-05-26 18:09:57 +0000127 if (m_strategyChoiceEntry != nullptr) {
Junxiao Shiee5a4442014-07-27 17:13:43 -0700128 m_strategyChoiceEntry->m_nameTreeEntry = this->shared_from_this();
129 }
130}
131
HYuana9b85752014-02-26 02:32:30 -0600132} // namespace name_tree
133} // namespace nfd