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