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