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 | |
Alexander Afanasyev | 0c39537 | 2014-12-20 15:54:02 -0800 | [diff] [blame] | 23 | #include "detail/multi-type-container.hpp" |
| 24 | #include "detail/multi-policy-container.hpp" |
| 25 | #include "detail/functor-hook.hpp" |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 26 | |
| 27 | #include <boost/mpl/size.hpp> |
| 28 | #include <boost/mpl/at.hpp> |
| 29 | #include <boost/mpl/range_c.hpp> |
| 30 | #include <boost/mpl/transform.hpp> |
| 31 | #include <boost/mpl/back_inserter.hpp> |
| 32 | #include <boost/mpl/vector.hpp> |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 33 | #include <boost/mpl/for_each.hpp> |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 34 | |
| 35 | #include <boost/intrusive/options.hpp> |
| 36 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 37 | namespace ns3 { |
| 38 | namespace ndn { |
| 39 | namespace ndnSIM { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 40 | |
| 41 | template<typename Policies> // e.g., mpl::vector1< lru_policy_traits > |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 42 | struct multi_policy_traits { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 43 | typedef Policies policy_traits; |
| 44 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 45 | struct getHook { |
| 46 | template<class Item> |
| 47 | struct apply { |
| 48 | typedef typename Item::policy_hook_type type; |
| 49 | }; |
| 50 | }; |
| 51 | typedef detail::multi_type_container< |
| 52 | typename boost::mpl::transform1<policy_traits, getHook>::type> policy_hook_type; |
| 53 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 54 | template<class Container> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 55 | struct container_hook { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 56 | typedef policy_hook_type type; |
| 57 | }; |
| 58 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 59 | template<class Base, class Container, class Hook> |
| 60 | struct policy { |
| 61 | typedef boost::mpl::range_c<int, 0, boost::mpl::size<policy_traits>::type::value> |
| 62 | policies_range; |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 63 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 64 | struct getPolicy { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 65 | template<class Number> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 66 | struct apply { |
| 67 | typedef typename boost::mpl::at_c<policy_traits, Number::value>::type:: |
| 68 | template policy<Base, Container, |
| 69 | boost::intrusive::function_hook<detail::FunctorHook<Hook, Container, |
| 70 | Number::value>>>::type |
| 71 | type; |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 72 | }; |
| 73 | }; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 74 | |
| 75 | typedef typename boost::mpl::transform1<policies_range, getPolicy, |
| 76 | boost::mpl::back_inserter<boost::mpl::vector0<>>>::type |
| 77 | policies; |
| 78 | |
| 79 | typedef detail::multi_policy_container<Base, policies> policy_container; |
| 80 | |
| 81 | class type : public policy_container { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 82 | public: |
Alexander Afanasyev | 6978611 | 2012-12-13 11:53:36 -0800 | [diff] [blame] | 83 | 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] | 84 | typedef Container parent_trie; |
| 85 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 86 | type(Base& base) |
| 87 | : policy_container(base) |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 88 | { |
| 89 | } |
| 90 | |
| 91 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 92 | update(typename parent_trie::iterator item) |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 93 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 94 | policy_container::update(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 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 97 | inline bool |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 98 | insert(typename parent_trie::iterator item) |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 99 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 100 | return policy_container::insert(item); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 101 | } |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 102 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 103 | inline void |
| 104 | lookup(typename parent_trie::iterator item) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 105 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 106 | policy_container::lookup(item); |
| 107 | } |
| 108 | |
| 109 | inline void |
| 110 | erase(typename parent_trie::iterator item) |
| 111 | { |
| 112 | policy_container::erase(item); |
| 113 | } |
| 114 | |
| 115 | inline void |
| 116 | clear() |
| 117 | { |
| 118 | policy_container::clear(); |
| 119 | } |
| 120 | |
| 121 | struct max_size_setter { |
| 122 | max_size_setter(policy_container& container, size_t size) |
| 123 | : m_container(container) |
| 124 | , m_size(size) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 125 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | template<typename U> |
| 129 | void |
| 130 | operator()(U index) |
| 131 | { |
| 132 | m_container.template get<U::value>().set_max_size(m_size); |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | private: |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 136 | policy_container& m_container; |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 137 | size_t m_size; |
| 138 | }; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 140 | inline void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 141 | set_max_size(size_t max_size) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 142 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 143 | boost::mpl::for_each<boost::mpl::range_c<int, 0, |
| 144 | boost::mpl::size<policy_traits>::type::value>>( |
| 145 | max_size_setter(*this, max_size)); |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | inline size_t |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 149 | get_max_size() const |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 150 | { |
| 151 | // 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] | 152 | return policy_container::template get<0>().get_max_size(); |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 153 | } |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 154 | }; |
| 155 | }; |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 156 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 157 | struct name_getter { |
| 158 | name_getter(std::string& name) |
| 159 | : m_name(name) |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 160 | { |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 163 | template<typename U> |
| 164 | void |
| 165 | operator()(U index) |
| 166 | { |
| 167 | if (!m_name.empty()) |
| 168 | m_name += "::"; |
| 169 | m_name += boost::mpl::at_c<policy_traits, U::value>::type::GetName(); |
| 170 | } |
| 171 | |
| 172 | std::string& m_name; |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | /// @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] | 176 | static std::string |
| 177 | GetName() |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 178 | { |
| 179 | // combine names of all internal policies |
| 180 | std::string name; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 181 | boost::mpl::for_each<boost::mpl::range_c<int, 0, boost::mpl::size<policy_traits>::type::value>>( |
| 182 | name_getter(name)); |
| 183 | |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 184 | return name; |
| 185 | } |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | } // ndnSIM |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 189 | } // ndn |
Alexander Afanasyev | e77db79 | 2012-08-09 11:10:58 -0700 | [diff] [blame] | 190 | } // ns3 |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 191 | |
| 192 | #endif // MULTI_POLICY_H_ |