HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 24 | */ |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_TABLE_NAME_TREE_ENTRY_HPP |
| 27 | #define NFD_DAEMON_TABLE_NAME_TREE_ENTRY_HPP |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 28 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 29 | #include "table/fib-entry.hpp" |
| 30 | #include "table/pit-entry.hpp" |
| 31 | #include "table/measurements-entry.hpp" |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 32 | #include "table/strategy-choice-entry.hpp" |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | namespace nfd::name_tree { |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 35 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 36 | class Node; |
| 37 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 38 | /** |
| 39 | * \brief An entry in the name tree. |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 40 | */ |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 41 | class Entry : noncopyable |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 42 | { |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 43 | public: |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 44 | Entry(const Name& prefix, Node* node); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 45 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 46 | const Name& |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 47 | getName() const noexcept |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 48 | { |
| 49 | return m_name; |
| 50 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 51 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 52 | /** \return entry of getName().getPrefix(-1) |
| 53 | * \retval nullptr this entry is the root entry, i.e. getName() == Name() |
| 54 | */ |
| 55 | Entry* |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 56 | getParent() const noexcept |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 57 | { |
| 58 | return m_parent; |
| 59 | } |
| 60 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 61 | /** \brief Set parent of this entry. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 62 | * \param entry entry of getName().getPrefix(-1) |
| 63 | * \pre getParent() == nullptr |
| 64 | * \post getParent() == &entry |
| 65 | * \post entry.getChildren() contains this |
| 66 | */ |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 67 | void |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 68 | setParent(Entry& entry); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 69 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 70 | /** \brief Unset parent of this entry. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 71 | * \post getParent() == nullptr |
| 72 | * \post parent.getChildren() does not contain this |
| 73 | */ |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 74 | void |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 75 | unsetParent(); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 76 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 77 | /** |
| 78 | * \brief Check whether this entry has any children. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 79 | */ |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 80 | bool |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 81 | hasChildren() const |
| 82 | { |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 83 | return !m_children.empty(); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 84 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 85 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 86 | /** |
| 87 | * \brief Returns the children of this entry. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 88 | */ |
| 89 | const std::vector<Entry*>& |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 90 | getChildren() const noexcept |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 91 | { |
| 92 | return m_children; |
| 93 | } |
| 94 | |
| 95 | /** \retval true this entry has no children and no table entries |
| 96 | * \retval false this entry has child or attached table entry |
| 97 | */ |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 98 | bool |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 99 | isEmpty() const |
| 100 | { |
| 101 | return !this->hasChildren() && !this->hasTableEntries(); |
| 102 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 103 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 104 | public: // attached table entries |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 105 | /** \retval true at least one table entries is attached |
| 106 | * \retval false no table entry is attached |
| 107 | */ |
| 108 | bool |
| 109 | hasTableEntries() const; |
| 110 | |
| 111 | fib::Entry* |
| 112 | getFibEntry() const |
| 113 | { |
| 114 | return m_fibEntry.get(); |
| 115 | } |
| 116 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 117 | void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 118 | setFibEntry(unique_ptr<fib::Entry> fibEntry); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 119 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 120 | bool |
| 121 | hasPitEntries() const |
| 122 | { |
| 123 | return !this->getPitEntries().empty(); |
| 124 | } |
| 125 | |
| 126 | const std::vector<shared_ptr<pit::Entry>>& |
| 127 | getPitEntries() const |
| 128 | { |
| 129 | return m_pitEntries; |
| 130 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 131 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 132 | void |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 133 | insertPitEntry(shared_ptr<pit::Entry> pitEntry); |
| 134 | |
| 135 | void |
Junxiao Shi | dbef6dc | 2016-08-15 02:58:36 +0000 | [diff] [blame] | 136 | erasePitEntry(pit::Entry* pitEntry); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 137 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 138 | measurements::Entry* |
| 139 | getMeasurementsEntry() const |
| 140 | { |
| 141 | return m_measurementsEntry.get(); |
| 142 | } |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 143 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 144 | void |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 145 | setMeasurementsEntry(unique_ptr<measurements::Entry> measurementsEntry); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 146 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 147 | strategy_choice::Entry* |
| 148 | getStrategyChoiceEntry() const |
| 149 | { |
| 150 | return m_strategyChoiceEntry.get(); |
| 151 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 152 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 153 | void |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 154 | setStrategyChoiceEntry(unique_ptr<strategy_choice::Entry> strategyChoiceEntry); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 155 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 156 | /** \return name tree entry on which a table entry is attached, |
| 157 | * or nullptr if the table entry is detached |
| 158 | * \note This function is for NameTree internal use. Other components |
| 159 | * should use NameTree::getEntry(tableEntry) instead. |
| 160 | */ |
| 161 | template<typename ENTRY> |
| 162 | static Entry* |
| 163 | get(const ENTRY& tableEntry) |
| 164 | { |
| 165 | return tableEntry.m_nameTreeEntry; |
| 166 | } |
| 167 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 168 | private: |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 169 | Name m_name; |
| 170 | Node* m_node; |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 171 | Entry* m_parent = nullptr; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 172 | std::vector<Entry*> m_children; |
| 173 | |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 174 | unique_ptr<fib::Entry> m_fibEntry; |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 175 | std::vector<shared_ptr<pit::Entry>> m_pitEntries; |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 176 | unique_ptr<measurements::Entry> m_measurementsEntry; |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 177 | unique_ptr<strategy_choice::Entry> m_strategyChoiceEntry; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 178 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 179 | friend Node* getNode(const Entry& entry); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 180 | }; |
| 181 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 182 | /** \brief A functor to get a table entry from a name tree entry. |
Junxiao Shi | 13ad4b7 | 2016-08-15 04:51:21 +0000 | [diff] [blame] | 183 | * \tparam ENTRY type of single table entry attached to name tree entry, such as fib::Entry |
| 184 | */ |
| 185 | template<typename ENTRY> |
| 186 | class GetTableEntry |
| 187 | { |
| 188 | public: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 189 | /** \brief A function pointer to the getter on Entry class that returns ENTRY. |
Junxiao Shi | 13ad4b7 | 2016-08-15 04:51:21 +0000 | [diff] [blame] | 190 | */ |
| 191 | using Getter = ENTRY* (Entry::*)() const; |
| 192 | |
Junxiao Shi | 3aba754 | 2016-12-24 02:42:52 +0000 | [diff] [blame] | 193 | /** \note The default argument is needed to ensure FIB and StrategyChoice iterators |
| 194 | * are default-constructible. |
| 195 | */ |
Junxiao Shi | 13ad4b7 | 2016-08-15 04:51:21 +0000 | [diff] [blame] | 196 | explicit |
Junxiao Shi | 3aba754 | 2016-12-24 02:42:52 +0000 | [diff] [blame] | 197 | GetTableEntry(Getter getter = nullptr) |
Junxiao Shi | 13ad4b7 | 2016-08-15 04:51:21 +0000 | [diff] [blame] | 198 | : m_getter(getter) |
| 199 | { |
| 200 | } |
| 201 | |
| 202 | const ENTRY& |
| 203 | operator()(const Entry& nte) const |
| 204 | { |
| 205 | return *(nte.*m_getter)(); |
| 206 | } |
| 207 | |
| 208 | private: |
| 209 | Getter m_getter; |
| 210 | }; |
| 211 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 212 | } // namespace nfd::name_tree |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 213 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 214 | #endif // NFD_DAEMON_TABLE_NAME_TREE_ENTRY_HPP |