Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NFD_TABLE_STRATEGY_INFO_HOST_HPP |
| 8 | #define NFD_TABLE_STRATEGY_INFO_HOST_HPP |
| 9 | |
| 10 | #include "fw/strategy-info.hpp" |
| 11 | |
| 12 | namespace nfd { |
| 13 | |
| 14 | /** \class StrategyInfoHost |
| 15 | * \brief base class for an entity onto which StrategyInfo may be placed |
| 16 | */ |
| 17 | class StrategyInfoHost |
| 18 | { |
| 19 | public: |
| 20 | template<typename T> |
| 21 | void |
| 22 | setStrategyInfo(shared_ptr<T> strategyInfo); |
| 23 | |
| 24 | template<typename T> |
| 25 | shared_ptr<T> |
| 26 | getStrategyInfo(); |
| 27 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame^] | 28 | template<typename T> |
| 29 | shared_ptr<T> |
| 30 | getOrCreateStrategyInfo(); |
| 31 | |
| 32 | template<typename T, typename T1> |
| 33 | shared_ptr<T> |
| 34 | getOrCreateStrategyInfo(T1& a1); |
| 35 | |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 36 | void |
| 37 | clearStrategyInfo(); |
| 38 | |
| 39 | private: |
| 40 | shared_ptr<fw::StrategyInfo> m_strategyInfo; |
| 41 | }; |
| 42 | |
| 43 | |
| 44 | template<typename T> |
| 45 | void |
| 46 | StrategyInfoHost::setStrategyInfo(shared_ptr<T> strategyInfo) |
| 47 | { |
| 48 | m_strategyInfo = strategyInfo; |
| 49 | } |
| 50 | |
| 51 | template<typename T> |
| 52 | shared_ptr<T> |
| 53 | StrategyInfoHost::getStrategyInfo() |
| 54 | { |
| 55 | return static_pointer_cast<T, fw::StrategyInfo>(m_strategyInfo); |
| 56 | } |
| 57 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame^] | 58 | template<typename T> |
| 59 | shared_ptr<T> |
| 60 | StrategyInfoHost::getOrCreateStrategyInfo() |
| 61 | { |
| 62 | shared_ptr<T> info = this->getStrategyInfo<T>(); |
| 63 | if (!static_cast<bool>(info)) { |
| 64 | info = make_shared<T>(); |
| 65 | this->setStrategyInfo(info); |
| 66 | } |
| 67 | return info; |
| 68 | } |
| 69 | |
| 70 | template<typename T, typename T1> |
| 71 | shared_ptr<T> |
| 72 | StrategyInfoHost::getOrCreateStrategyInfo(T1& a1) |
| 73 | { |
| 74 | shared_ptr<T> info = this->getStrategyInfo<T>(); |
| 75 | if (!static_cast<bool>(info)) { |
| 76 | info = make_shared<T>(boost::ref(a1)); |
| 77 | this->setStrategyInfo(info); |
| 78 | } |
| 79 | return info; |
| 80 | } |
| 81 | |
Junxiao Shi | e5e2fce | 2014-02-10 20:01:53 -0700 | [diff] [blame] | 82 | } // namespace nfd |
| 83 | |
| 84 | #endif // NFD_TABLE_STRATEGY_INFO_HOST_HPP |