Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2011-2015 Regents of the University of California. |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 5 | * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and |
| 6 | * contributors. |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 8 | * ndnSIM is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 12 | * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 60a7b62 | 2014-12-20 17:04:07 -0800 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 19 | |
| 20 | #ifndef MULTI_POLICY_H_ |
| 21 | #define MULTI_POLICY_H_ |
| 22 | |
Spyridon Mastorakis | 460f57c | 2014-12-17 00:44:14 -0800 | [diff] [blame] | 23 | /// @cond include_hidden |
| 24 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 25 | #include "detail/multi-type-container.hpp" |
| 26 | #include "detail/multi-policy-container.hpp" |
| 27 | #include "detail/functor-hook.hpp" |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 28 | |
| 29 | #include <boost/mpl/size.hpp> |
| 30 | #include <boost/mpl/at.hpp> |
| 31 | #include <boost/mpl/range_c.hpp> |
| 32 | #include <boost/mpl/transform.hpp> |
| 33 | #include <boost/mpl/back_inserter.hpp> |
| 34 | #include <boost/mpl/vector.hpp> |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 35 | #include <boost/mpl/for_each.hpp> |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 36 | |
| 37 | #include <boost/intrusive/options.hpp> |
| 38 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 39 | namespace ns3 { |
| 40 | namespace ndn { |
| 41 | namespace ndnSIM { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 42 | |
| 43 | template<typename Policies> // e.g., mpl::vector1< lru_policy_traits > |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 44 | struct multi_policy_traits { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 45 | typedef Policies policy_traits; |
| 46 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 47 | struct getHook { |
| 48 | template<class Item> |
| 49 | struct apply { |
| 50 | typedef typename Item::policy_hook_type type; |
| 51 | }; |
| 52 | }; |
| 53 | typedef detail::multi_type_container< |
| 54 | typename boost::mpl::transform1<policy_traits, getHook>::type> policy_hook_type; |
| 55 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 56 | template<class Container> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 57 | struct container_hook { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 58 | typedef policy_hook_type type; |
| 59 | }; |
| 60 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 61 | template<class Base, class Container, class Hook> |
| 62 | struct policy { |
| 63 | typedef boost::mpl::range_c<int, 0, boost::mpl::size<policy_traits>::type::value> |
| 64 | policies_range; |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 65 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 66 | struct getPolicy { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 67 | template<class Number> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 68 | struct apply { |
| 69 | typedef typename boost::mpl::at_c<policy_traits, Number::value>::type:: |
| 70 | template policy<Base, Container, |
| 71 | boost::intrusive::function_hook<detail::FunctorHook<Hook, Container, |
| 72 | Number::value>>>::type |
| 73 | type; |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 74 | }; |
| 75 | }; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 76 | |
| 77 | typedef typename boost::mpl::transform1<policies_range, getPolicy, |
| 78 | boost::mpl::back_inserter<boost::mpl::vector0<>>>::type |
| 79 | policies; |
| 80 | |
| 81 | typedef detail::multi_policy_container<Base, policies> policy_container; |
| 82 | |
| 83 | class type : public policy_container { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 84 | public: |
Alexander Afanasyev | 6978611 | 2012-12-13 11:53:36 -0800 | [diff] [blame] | 85 | typedef policy policy_base; // to get access to get_time methods from outside |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 86 | typedef Container parent_trie; |
| 87 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 88 | type(Base& base) |
| 89 | : policy_container(base) |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 90 | { |
| 91 | } |
| 92 | |
| 93 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 94 | update(typename parent_trie::iterator item) |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 95 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 96 | policy_container::update(item); |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 97 | } |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 98 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 99 | inline bool |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 100 | insert(typename parent_trie::iterator item) |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 101 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 102 | return policy_container::insert(item); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 103 | } |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 105 | inline void |
| 106 | lookup(typename parent_trie::iterator item) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 107 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 108 | policy_container::lookup(item); |
| 109 | } |
| 110 | |
| 111 | inline void |
| 112 | erase(typename parent_trie::iterator item) |
| 113 | { |
| 114 | policy_container::erase(item); |
| 115 | } |
| 116 | |
| 117 | inline void |
| 118 | clear() |
| 119 | { |
| 120 | policy_container::clear(); |
| 121 | } |
| 122 | |
| 123 | struct max_size_setter { |
| 124 | max_size_setter(policy_container& container, size_t size) |
| 125 | : m_container(container) |
| 126 | , m_size(size) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 127 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | template<typename U> |
| 131 | void |
| 132 | operator()(U index) |
| 133 | { |
| 134 | m_container.template get<U::value>().set_max_size(m_size); |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | private: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 138 | policy_container& m_container; |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 139 | size_t m_size; |
| 140 | }; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 141 | |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 142 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 143 | set_max_size(size_t max_size) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 144 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 145 | boost::mpl::for_each<boost::mpl::range_c<int, 0, |
| 146 | boost::mpl::size<policy_traits>::type::value>>( |
| 147 | max_size_setter(*this, max_size)); |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | inline size_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 151 | get_max_size() const |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 152 | { |
| 153 | // as max size should be the same everywhere, get the value from the first available policy |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 154 | return policy_container::template get<0>().get_max_size(); |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 155 | } |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 156 | }; |
| 157 | }; |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 158 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 159 | struct name_getter { |
| 160 | name_getter(std::string& name) |
| 161 | : m_name(name) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 162 | { |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 165 | template<typename U> |
| 166 | void |
| 167 | operator()(U index) |
| 168 | { |
| 169 | if (!m_name.empty()) |
| 170 | m_name += "::"; |
| 171 | m_name += boost::mpl::at_c<policy_traits, U::value>::type::GetName(); |
| 172 | } |
| 173 | |
| 174 | std::string& m_name; |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | /// @brief Name that can be used to identify the policy (for NS-3 object model and logging) |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 178 | static std::string |
| 179 | GetName() |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 180 | { |
| 181 | // combine names of all internal policies |
| 182 | std::string name; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 183 | boost::mpl::for_each<boost::mpl::range_c<int, 0, boost::mpl::size<policy_traits>::type::value>>( |
| 184 | name_getter(name)); |
| 185 | |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 186 | return name; |
| 187 | } |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | } // ndnSIM |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 191 | } // ndn |
Alexander Afanasyev | e77db79 | 2012-08-09 11:10:58 -0700 | [diff] [blame] | 192 | } // ns3 |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 193 | |
Spyridon Mastorakis | 460f57c | 2014-12-17 00:44:14 -0800 | [diff] [blame] | 194 | /// @endcond |
| 195 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 196 | #endif // MULTI_POLICY_H_ |