blob: a0dab284eb21c93818f9062bb73253b7b91fb189 [file] [log] [blame]
Junxiao Shia49a1ab2016-07-15 18:24:36 +00001/* -*- 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 Shi890afe92016-12-15 14:34:34 +000029#include "fw/forwarder.hpp"
30
Junxiao Shia49a1ab2016-07-15 18:24:36 +000031namespace nfd {
32namespace fw {
33class Strategy;
34} // namespace fw
35
36namespace 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 Shi890afe92016-12-15 14:34:34 +000045 * \deprecated use strategy registry
46 * \todo #3868 delete this function template
Junxiao Shia49a1ab2016-07-15 18:24:36 +000047 */
48template<typename S, typename ...Args>
49typename std::enable_if<std::is_base_of<fw::Strategy, S>::value, S&>::type
50install(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 Shi890afe92016-12-15 14:34:34 +000065 * \todo #3868 disallow args
Junxiao Shia49a1ab2016-07-15 18:24:36 +000066 */
67template<typename S, typename ...Args>
68typename std::enable_if<std::is_base_of<fw::Strategy, S>::value, S&>::type
69choose(Forwarder& forwarder, const Name& prefix, Args&&... args)
70{
71 S& strategy = install<S>(forwarder, std::forward<Args>(args)...);
Junxiao Shi890afe92016-12-15 14:34:34 +000072 StrategyChoice& sc = forwarder.getStrategyChoice();
73 sc.insert(prefix, strategy.getName());
74 return dynamic_cast<S&>(sc.findEffectiveStrategy(prefix));
Junxiao Shia49a1ab2016-07-15 18:24:36 +000075}
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 Shi890afe92016-12-15 14:34:34 +000083 * \todo #3868 merge into the other overload with default argument
Junxiao Shia49a1ab2016-07-15 18:24:36 +000084 */
85template<typename S>
86typename std::enable_if<std::is_base_of<fw::Strategy, S>::value, S&>::type
87choose(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