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