blob: 84c0c63324385e2b350b53b568e3b2aeba09adbf [file] [log] [blame]
Junxiao Shie5e2fce2014-02-10 20:01:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi39ef2612014-11-29 20:35:19 -07003 * 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 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 Shi39ef2612014-11-29 20:35:19 -070024 */
Junxiao Shie5e2fce2014-02-10 20:01:53 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_TABLE_STRATEGY_INFO_HOST_HPP
27#define NFD_DAEMON_TABLE_STRATEGY_INFO_HOST_HPP
Junxiao Shie5e2fce2014-02-10 20:01:53 -070028
29#include "fw/strategy-info.hpp"
30
31namespace nfd {
32
Junxiao Shi39ef2612014-11-29 20:35:19 -070033/** \brief base class for an entity onto which StrategyInfo objects may be placed
Junxiao Shie5e2fce2014-02-10 20:01:53 -070034 */
35class StrategyInfoHost
36{
37public:
Junxiao Shi39ef2612014-11-29 20:35:19 -070038 /** \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 Shie5e2fce2014-02-10 20:01:53 -070042 template<typename T>
43 shared_ptr<T>
Junxiao Shid938a6b2014-05-11 23:40:29 -070044 getStrategyInfo() const;
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070045
Junxiao Shi39ef2612014-11-29 20:35:19 -070046 /** \brief set a StrategyInfo item
47 * \tparam T type of StrategyInfo, must be a subclass of from nfd::fw::StrategyInfo
48 */
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070049 template<typename T>
Junxiao Shi39ef2612014-11-29 20:35:19 -070050 void
51 setStrategyInfo(shared_ptr<T> strategyInfo);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070052
Junxiao Shi39ef2612014-11-29 20:35:19 -070053 /** \brief get or create a StrategyInfo item
54 * \tparam T type of StrategyInfo, must be a subclass of from nfd::fw::StrategyInfo
55 *
Alexander Afanasyev7c10b3b2015-01-20 12:24:27 -080056 * If no StrategyInfo of type T is stored, it's created with \p args;
Junxiao Shi39ef2612014-11-29 20:35:19 -070057 * otherwise, the existing item is returned.
58 */
59 template<typename T, typename ...A>
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070060 shared_ptr<T>
Junxiao Shi39ef2612014-11-29 20:35:19 -070061 getOrCreateStrategyInfo(A&&... args);
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070062
Junxiao Shi39ef2612014-11-29 20:35:19 -070063 /** \brief clear all StrategyInfo items
64 */
Junxiao Shie5e2fce2014-02-10 20:01:53 -070065 void
66 clearStrategyInfo();
67
68private:
Junxiao Shi39ef2612014-11-29 20:35:19 -070069 std::map<int, shared_ptr<fw::StrategyInfo>> m_items;
Junxiao Shie5e2fce2014-02-10 20:01:53 -070070};
71
72
73template<typename T>
Junxiao Shie5e2fce2014-02-10 20:01:53 -070074shared_ptr<T>
Junxiao Shid938a6b2014-05-11 23:40:29 -070075StrategyInfoHost::getStrategyInfo() const
Junxiao Shie5e2fce2014-02-10 20:01:53 -070076{
Junxiao Shi39ef2612014-11-29 20:35:19 -070077 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 Shie5e2fce2014-02-10 20:01:53 -070085}
86
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070087template<typename T>
Junxiao Shi39ef2612014-11-29 20:35:19 -070088void
89StrategyInfoHost::setStrategyInfo(shared_ptr<T> item)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070090{
Junxiao Shi39ef2612014-11-29 20:35:19 -070091 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 Shi0b5fbbb2014-02-20 15:54:03 -070096 }
Junxiao Shi39ef2612014-11-29 20:35:19 -070097 else {
98 m_items[T::getTypeId()] = item;
99 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700100}
101
Junxiao Shi39ef2612014-11-29 20:35:19 -0700102template<typename T, typename ...A>
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700103shared_ptr<T>
Junxiao Shi39ef2612014-11-29 20:35:19 -0700104StrategyInfoHost::getOrCreateStrategyInfo(A&&... args)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700105{
Junxiao Shi39ef2612014-11-29 20:35:19 -0700106 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 Shi0b5fbbb2014-02-20 15:54:03 -0700113 }
Junxiao Shi39ef2612014-11-29 20:35:19 -0700114 return item;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700115}
116
Junxiao Shie5e2fce2014-02-10 20:01:53 -0700117} // namespace nfd
118
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700119#endif // NFD_DAEMON_TABLE_STRATEGY_INFO_HOST_HPP