Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, 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 | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_TABLE_STRATEGY_INFO_HOST_HPP |
| 27 | #define NFD_DAEMON_TABLE_STRATEGY_INFO_HOST_HPP |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 28 | |
| 29 | #include "fw/strategy-info.hpp" |
| 30 | |
| 31 | namespace nfd { |
| 32 | |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 33 | /** \brief base class for an entity onto which StrategyInfo objects may be placed |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 34 | */ |
| 35 | class StrategyInfoHost |
| 36 | { |
| 37 | public: |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 38 | /** \brief get a StrategyInfo item |
| 39 | * \tparam T type of StrategyInfo, must be a subclass of from nfd::fw::StrategyInfo |
| 40 | * \retval nullptr if no StrategyInfo of type T is stored |
| 41 | */ |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 42 | template<typename T> |
| 43 | shared_ptr<T> |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 44 | getStrategyInfo() const; |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 45 | |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 46 | /** \brief set a StrategyInfo item |
| 47 | * \tparam T type of StrategyInfo, must be a subclass of from nfd::fw::StrategyInfo |
| 48 | */ |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 49 | template<typename T> |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 50 | void |
| 51 | setStrategyInfo(shared_ptr<T> strategyInfo); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 52 | |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 53 | /** \brief get or create a StrategyInfo item |
| 54 | * \tparam T type of StrategyInfo, must be a subclass of from nfd::fw::StrategyInfo |
| 55 | * |
Alexander Afanasyev | 7c10b3b | 2015-01-20 12:24:27 -0800 | [diff] [blame^] | 56 | * If no StrategyInfo of type T is stored, it's created with \p args; |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 57 | * otherwise, the existing item is returned. |
| 58 | */ |
| 59 | template<typename T, typename ...A> |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 60 | shared_ptr<T> |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 61 | getOrCreateStrategyInfo(A&&... args); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 62 | |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 63 | /** \brief clear all StrategyInfo items |
| 64 | */ |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 65 | void |
| 66 | clearStrategyInfo(); |
| 67 | |
| 68 | private: |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 69 | std::map<int, shared_ptr<fw::StrategyInfo>> m_items; |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | |
| 73 | template<typename T> |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 74 | shared_ptr<T> |
Junxiao Shi | d938a6b | 2014-05-11 23:40:29 -0700 | [diff] [blame] | 75 | StrategyInfoHost::getStrategyInfo() const |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 76 | { |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 77 | static_assert(std::is_base_of<fw::StrategyInfo, T>::value, |
| 78 | "T must inherit from StrategyInfo"); |
| 79 | |
| 80 | auto it = m_items.find(T::getTypeId()); |
| 81 | if (it == m_items.end()) { |
| 82 | return nullptr; |
| 83 | } |
| 84 | return static_pointer_cast<T, fw::StrategyInfo>(it->second); |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 87 | template<typename T> |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 88 | void |
| 89 | StrategyInfoHost::setStrategyInfo(shared_ptr<T> item) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 90 | { |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 91 | static_assert(std::is_base_of<fw::StrategyInfo, T>::value, |
| 92 | "T must inherit from StrategyInfo"); |
| 93 | |
| 94 | if (item == nullptr) { |
| 95 | m_items.erase(T::getTypeId()); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 96 | } |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 97 | else { |
| 98 | m_items[T::getTypeId()] = item; |
| 99 | } |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 102 | template<typename T, typename ...A> |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 103 | shared_ptr<T> |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 104 | StrategyInfoHost::getOrCreateStrategyInfo(A&&... args) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 105 | { |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 106 | static_assert(std::is_base_of<fw::StrategyInfo, T>::value, |
| 107 | "T must inherit from StrategyInfo"); |
| 108 | |
| 109 | shared_ptr<T> item = this->getStrategyInfo<T>(); |
| 110 | if (!static_cast<bool>(item)) { |
| 111 | item = make_shared<T>(std::forward<A>(args)...); |
| 112 | this->setStrategyInfo(item); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 113 | } |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame] | 114 | return item; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 117 | } // namespace nfd |
| 118 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 119 | #endif // NFD_DAEMON_TABLE_STRATEGY_INFO_HOST_HPP |