blob: ec3c8485b9fb87805f423e3a26343aecb66fe74f [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
29namespace nfd {
30namespace fw {
31class Strategy;
32} // namespace fw
33
34namespace tests {
35
36/** \brief install a strategy to forwarder
37 * \tparam S strategy type
38 * \param forwarder the forwarder
39 * \param args arguments to strategy constructor
40 * \throw std::bad_cast a strategy with duplicate strategyName is already installed
41 * and has an incompatible type
42 * \return a reference to the strategy
43 */
44template<typename S, typename ...Args>
45typename std::enable_if<std::is_base_of<fw::Strategy, S>::value, S&>::type
46install(Forwarder& forwarder, Args&&... args)
47{
48 auto strategy = make_unique<S>(ref(forwarder), std::forward<Args>(args)...);
49 fw::Strategy* installed = forwarder.getStrategyChoice().install(std::move(strategy)).second;
50 return dynamic_cast<S&>(*installed);
51}
52
53/** \brief install a strategy to forwarder, and choose the strategy for a namespace
54 * \tparam S strategy type
55 * \param forwarder the forwarder
56 * \param prefix namespace to choose the strategy for
57 * \param args arguments to strategy constructor
58 * \throw std::bad_cast a strategy with duplicate strategyName is already installed
59 * and has an incompatible type
60 * \return a reference to the strategy
61 */
62template<typename S, typename ...Args>
63typename std::enable_if<std::is_base_of<fw::Strategy, S>::value, S&>::type
64choose(Forwarder& forwarder, const Name& prefix, Args&&... args)
65{
66 S& strategy = install<S>(forwarder, std::forward<Args>(args)...);
67 forwarder.getStrategyChoice().insert(prefix, strategy.getName());
68 return strategy;
69}
70
71/** \brief install a strategy to forwarder, and choose the strategy as default
72 * \tparam S strategy type
73 * \param forwarder the forwarder
74 * \throw std::bad_cast a strategy with duplicate strategyName is already installed
75 * and has an incompatible type
76 * \return a reference to the strategy
77 */
78template<typename S>
79typename std::enable_if<std::is_base_of<fw::Strategy, S>::value, S&>::type
80choose(Forwarder& forwarder)
81{
82 return choose<S>(forwarder, "ndn:/");
83}
84
85} // namespace tests
86} // namespace nfd
87
88#endif // NFD_TESTS_DAEMON_FW_INSTALL_STRATEGY_HPP