blob: 644d86bc8c59142f4593d4f1ec636d16592348a1 [file] [log] [blame]
HYuana9b85752014-02-26 02:32:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento50a6af32019-02-21 00:04:40 -05002/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shib184e532016-05-26 18:09:57 +00004 * 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
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_TABLE_NAME_TREE_ENTRY_HPP
27#define NFD_DAEMON_TABLE_NAME_TREE_ENTRY_HPP
HYuana9b85752014-02-26 02:32:30 -060028
HYuana9b85752014-02-26 02:32:30 -060029#include "table/fib-entry.hpp"
30#include "table/pit-entry.hpp"
31#include "table/measurements-entry.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070032#include "table/strategy-choice-entry.hpp"
HYuana9b85752014-02-26 02:32:30 -060033
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040034namespace nfd::name_tree {
HYuana9b85752014-02-26 02:32:30 -060035
Junxiao Shi340d5532016-08-13 04:00:35 +000036class Node;
37
Davide Pesavento50a6af32019-02-21 00:04:40 -050038/** \brief An entry in the name tree
HYuana9b85752014-02-26 02:32:30 -060039 */
Junxiao Shi340d5532016-08-13 04:00:35 +000040class Entry : noncopyable
HYuana9b85752014-02-26 02:32:30 -060041{
HYuana9b85752014-02-26 02:32:30 -060042public:
Junxiao Shib660b4c2016-08-06 20:47:44 +000043 Entry(const Name& prefix, Node* node);
HYuana9b85752014-02-26 02:32:30 -060044
HYuana9b85752014-02-26 02:32:30 -060045 const Name&
Junxiao Shib660b4c2016-08-06 20:47:44 +000046 getName() const
47 {
48 return m_name;
49 }
HYuana9b85752014-02-26 02:32:30 -060050
Junxiao Shib660b4c2016-08-06 20:47:44 +000051 /** \return entry of getName().getPrefix(-1)
52 * \retval nullptr this entry is the root entry, i.e. getName() == Name()
53 */
54 Entry*
55 getParent() const
56 {
57 return m_parent;
58 }
59
Davide Pesavento50a6af32019-02-21 00:04:40 -050060 /** \brief Set parent of this entry
Junxiao Shib660b4c2016-08-06 20:47:44 +000061 * \param entry entry of getName().getPrefix(-1)
62 * \pre getParent() == nullptr
63 * \post getParent() == &entry
64 * \post entry.getChildren() contains this
65 */
HYuana9b85752014-02-26 02:32:30 -060066 void
Junxiao Shib660b4c2016-08-06 20:47:44 +000067 setParent(Entry& entry);
HYuana9b85752014-02-26 02:32:30 -060068
Davide Pesavento50a6af32019-02-21 00:04:40 -050069 /** \brief Unset parent of this entry
Junxiao Shib660b4c2016-08-06 20:47:44 +000070 * \post getParent() == nullptr
71 * \post parent.getChildren() does not contain this
72 */
HYuana9b85752014-02-26 02:32:30 -060073 void
Junxiao Shib660b4c2016-08-06 20:47:44 +000074 unsetParent();
HYuana9b85752014-02-26 02:32:30 -060075
Davide Pesavento50a6af32019-02-21 00:04:40 -050076 /** \brief Check whether this entry has any children
Junxiao Shib660b4c2016-08-06 20:47:44 +000077 */
Haowei Yuane1079fc2014-03-08 14:41:25 -060078 bool
Junxiao Shib660b4c2016-08-06 20:47:44 +000079 hasChildren() const
80 {
Davide Pesavento50a6af32019-02-21 00:04:40 -050081 return !m_children.empty();
Junxiao Shib660b4c2016-08-06 20:47:44 +000082 }
Haowei Yuane1079fc2014-03-08 14:41:25 -060083
Junxiao Shib660b4c2016-08-06 20:47:44 +000084 /** \return children of this entry
85 */
86 const std::vector<Entry*>&
87 getChildren() const
88 {
89 return m_children;
90 }
91
92 /** \retval true this entry has no children and no table entries
93 * \retval false this entry has child or attached table entry
94 */
Junxiao Shi40631842014-03-01 13:52:37 -070095 bool
Junxiao Shib660b4c2016-08-06 20:47:44 +000096 isEmpty() const
97 {
98 return !this->hasChildren() && !this->hasTableEntries();
99 }
HYuana9b85752014-02-26 02:32:30 -0600100
Junxiao Shiee5a4442014-07-27 17:13:43 -0700101public: // attached table entries
Junxiao Shib660b4c2016-08-06 20:47:44 +0000102 /** \retval true at least one table entries is attached
103 * \retval false no table entry is attached
104 */
105 bool
106 hasTableEntries() const;
107
108 fib::Entry*
109 getFibEntry() const
110 {
111 return m_fibEntry.get();
112 }
113
HYuana9b85752014-02-26 02:32:30 -0600114 void
Junxiao Shia6de4292016-07-12 02:08:10 +0000115 setFibEntry(unique_ptr<fib::Entry> fibEntry);
HYuana9b85752014-02-26 02:32:30 -0600116
Junxiao Shib660b4c2016-08-06 20:47:44 +0000117 bool
118 hasPitEntries() const
119 {
120 return !this->getPitEntries().empty();
121 }
122
123 const std::vector<shared_ptr<pit::Entry>>&
124 getPitEntries() const
125 {
126 return m_pitEntries;
127 }
HYuana9b85752014-02-26 02:32:30 -0600128
HYuana9b85752014-02-26 02:32:30 -0600129 void
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700130 insertPitEntry(shared_ptr<pit::Entry> pitEntry);
131
132 void
Junxiao Shidbef6dc2016-08-15 02:58:36 +0000133 erasePitEntry(pit::Entry* pitEntry);
HYuana9b85752014-02-26 02:32:30 -0600134
Junxiao Shib660b4c2016-08-06 20:47:44 +0000135 measurements::Entry*
136 getMeasurementsEntry() const
137 {
138 return m_measurementsEntry.get();
139 }
Junxiao Shie349ea12014-03-12 01:32:42 -0700140
HYuana9b85752014-02-26 02:32:30 -0600141 void
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000142 setMeasurementsEntry(unique_ptr<measurements::Entry> measurementsEntry);
HYuana9b85752014-02-26 02:32:30 -0600143
Junxiao Shib660b4c2016-08-06 20:47:44 +0000144 strategy_choice::Entry*
145 getStrategyChoiceEntry() const
146 {
147 return m_strategyChoiceEntry.get();
148 }
HYuana9b85752014-02-26 02:32:30 -0600149
Junxiao Shibb5105f2014-03-03 12:06:45 -0700150 void
Junxiao Shiff10da62016-07-13 17:57:43 +0000151 setStrategyChoiceEntry(unique_ptr<strategy_choice::Entry> strategyChoiceEntry);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700152
Junxiao Shi340d5532016-08-13 04:00:35 +0000153 /** \return name tree entry on which a table entry is attached,
154 * or nullptr if the table entry is detached
155 * \note This function is for NameTree internal use. Other components
156 * should use NameTree::getEntry(tableEntry) instead.
157 */
158 template<typename ENTRY>
159 static Entry*
160 get(const ENTRY& tableEntry)
161 {
162 return tableEntry.m_nameTreeEntry;
163 }
164
HYuana9b85752014-02-26 02:32:30 -0600165private:
Junxiao Shib660b4c2016-08-06 20:47:44 +0000166 Name m_name;
167 Node* m_node;
Davide Pesavento50a6af32019-02-21 00:04:40 -0500168 Entry* m_parent = nullptr;
Junxiao Shib660b4c2016-08-06 20:47:44 +0000169 std::vector<Entry*> m_children;
170
Junxiao Shia6de4292016-07-12 02:08:10 +0000171 unique_ptr<fib::Entry> m_fibEntry;
Junxiao Shi029401f2016-08-05 12:55:14 +0000172 std::vector<shared_ptr<pit::Entry>> m_pitEntries;
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000173 unique_ptr<measurements::Entry> m_measurementsEntry;
Junxiao Shiff10da62016-07-13 17:57:43 +0000174 unique_ptr<strategy_choice::Entry> m_strategyChoiceEntry;
HYuana9b85752014-02-26 02:32:30 -0600175
Junxiao Shib660b4c2016-08-06 20:47:44 +0000176 friend Node* getNode(const Entry& entry);
HYuana9b85752014-02-26 02:32:30 -0600177};
178
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000179/** \brief a functor to get a table entry from a name tree entry
180 * \tparam ENTRY type of single table entry attached to name tree entry, such as fib::Entry
181 */
182template<typename ENTRY>
183class GetTableEntry
184{
185public:
Davide Pesavento50a6af32019-02-21 00:04:40 -0500186 /** \brief A function pointer to the getter on Entry class that returns ENTRY
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000187 */
188 using Getter = ENTRY* (Entry::*)() const;
189
Junxiao Shi3aba7542016-12-24 02:42:52 +0000190 /** \note The default argument is needed to ensure FIB and StrategyChoice iterators
191 * are default-constructible.
192 */
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000193 explicit
Junxiao Shi3aba7542016-12-24 02:42:52 +0000194 GetTableEntry(Getter getter = nullptr)
Junxiao Shi13ad4b72016-08-15 04:51:21 +0000195 : m_getter(getter)
196 {
197 }
198
199 const ENTRY&
200 operator()(const Entry& nte) const
201 {
202 return *(nte.*m_getter)();
203 }
204
205private:
206 Getter m_getter;
207};
208
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400209} // namespace nfd::name_tree
HYuana9b85752014-02-26 02:32:30 -0600210
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700211#endif // NFD_DAEMON_TABLE_NAME_TREE_ENTRY_HPP