Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 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. |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #ifndef NFD_TESTS_DAEMON_FW_INSTALL_STRATEGY_HPP |
| 27 | #define NFD_TESTS_DAEMON_FW_INSTALL_STRATEGY_HPP |
| 28 | |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame^] | 29 | #include "fw/forwarder.hpp" |
| 30 | |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 31 | namespace nfd { |
| 32 | namespace fw { |
| 33 | class Strategy; |
| 34 | } // namespace fw |
| 35 | |
| 36 | namespace tests { |
| 37 | |
| 38 | /** \brief install a strategy to forwarder |
| 39 | * \tparam S strategy type |
| 40 | * \param forwarder the forwarder |
| 41 | * \param args arguments to strategy constructor |
| 42 | * \throw std::bad_cast a strategy with duplicate strategyName is already installed |
| 43 | * and has an incompatible type |
| 44 | * \return a reference to the strategy |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame^] | 45 | * \deprecated use strategy registry |
| 46 | * \todo #3868 delete this function template |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 47 | */ |
| 48 | template<typename S, typename ...Args> |
| 49 | typename std::enable_if<std::is_base_of<fw::Strategy, S>::value, S&>::type |
| 50 | install(Forwarder& forwarder, Args&&... args) |
| 51 | { |
| 52 | auto strategy = make_unique<S>(ref(forwarder), std::forward<Args>(args)...); |
| 53 | fw::Strategy* installed = forwarder.getStrategyChoice().install(std::move(strategy)).second; |
| 54 | return dynamic_cast<S&>(*installed); |
| 55 | } |
| 56 | |
| 57 | /** \brief install a strategy to forwarder, and choose the strategy for a namespace |
| 58 | * \tparam S strategy type |
| 59 | * \param forwarder the forwarder |
| 60 | * \param prefix namespace to choose the strategy for |
| 61 | * \param args arguments to strategy constructor |
| 62 | * \throw std::bad_cast a strategy with duplicate strategyName is already installed |
| 63 | * and has an incompatible type |
| 64 | * \return a reference to the strategy |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame^] | 65 | * \todo #3868 disallow args |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 66 | */ |
| 67 | template<typename S, typename ...Args> |
| 68 | typename std::enable_if<std::is_base_of<fw::Strategy, S>::value, S&>::type |
| 69 | choose(Forwarder& forwarder, const Name& prefix, Args&&... args) |
| 70 | { |
| 71 | S& strategy = install<S>(forwarder, std::forward<Args>(args)...); |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame^] | 72 | StrategyChoice& sc = forwarder.getStrategyChoice(); |
| 73 | sc.insert(prefix, strategy.getName()); |
| 74 | return dynamic_cast<S&>(sc.findEffectiveStrategy(prefix)); |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** \brief install a strategy to forwarder, and choose the strategy as default |
| 78 | * \tparam S strategy type |
| 79 | * \param forwarder the forwarder |
| 80 | * \throw std::bad_cast a strategy with duplicate strategyName is already installed |
| 81 | * and has an incompatible type |
| 82 | * \return a reference to the strategy |
Junxiao Shi | 890afe9 | 2016-12-15 14:34:34 +0000 | [diff] [blame^] | 83 | * \todo #3868 merge into the other overload with default argument |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 84 | */ |
| 85 | template<typename S> |
| 86 | typename std::enable_if<std::is_base_of<fw::Strategy, S>::value, S&>::type |
| 87 | choose(Forwarder& forwarder) |
| 88 | { |
| 89 | return choose<S>(forwarder, "ndn:/"); |
| 90 | } |
| 91 | |
| 92 | } // namespace tests |
| 93 | } // namespace nfd |
| 94 | |
| 95 | #endif // NFD_TESTS_DAEMON_FW_INSTALL_STRATEGY_HPP |