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_CONTAINER_H_ |
| 21 | #define MULTI_POLICY_CONTAINER_H_ |
| 22 | |
Spyridon Mastorakis | 460f57c | 2014-12-17 00:44:14 -0800 | [diff] [blame] | 23 | /// @cond include_hidden |
| 24 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 25 | #include <boost/mpl/inherit_linearly.hpp> |
| 26 | #include <boost/mpl/at.hpp> |
| 27 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 28 | namespace ns3 { |
| 29 | namespace ndn { |
| 30 | namespace ndnSIM { |
| 31 | namespace detail { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 33 | template<class Base, class Value> |
| 34 | struct policy_wrap { |
| 35 | policy_wrap(Base& base) |
| 36 | : value_(base) |
| 37 | { |
| 38 | } |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 39 | Value value_; |
| 40 | }; |
| 41 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 42 | template<class Base, class Super /*empy_wrap/previous level*/, |
| 43 | class Value /*policy_wrap< element in vector >*/> |
| 44 | struct inherit_with_base : Super, Value { |
| 45 | inherit_with_base(Base& base) |
| 46 | : Super(base) |
| 47 | , Value(base) |
| 48 | { |
| 49 | } |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 50 | |
| 51 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 52 | update(typename Base::iterator item) |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 53 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 54 | Value::value_.update(item); |
| 55 | Super::update(item); |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bool |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 59 | insert(typename Base::iterator item) |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 60 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 61 | bool ok = Value::value_.insert(item); |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 62 | if (!ok) |
| 63 | return false; |
| 64 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 65 | ok = Super::insert(item); |
| 66 | if (!ok) { |
| 67 | Value::value_.erase(item); |
| 68 | return false; |
| 69 | } |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 70 | return true; |
| 71 | } |
| 72 | |
| 73 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 74 | lookup(typename Base::iterator item) |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 75 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 76 | Value::value_.lookup(item); |
| 77 | Super::lookup(item); |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 81 | erase(typename Base::iterator item) |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 82 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 83 | Value::value_.erase(item); |
| 84 | Super::erase(item); |
| 85 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 86 | |
| 87 | void |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 88 | clear() |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 89 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 90 | Value::value_.clear(); |
| 91 | Super::clear(); |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 92 | } |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 95 | template<class Base> |
| 96 | struct empty_policy_wrap { |
| 97 | empty_policy_wrap(Base& base) |
| 98 | { |
| 99 | } |
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 | void |
| 102 | update(typename Base::iterator item) |
| 103 | { |
| 104 | } |
| 105 | bool |
| 106 | insert(typename Base::iterator item) |
| 107 | { |
| 108 | return true; |
| 109 | } |
| 110 | void |
| 111 | lookup(typename Base::iterator item) |
| 112 | { |
| 113 | } |
| 114 | void |
| 115 | erase(typename Base::iterator item) |
| 116 | { |
| 117 | } |
| 118 | void |
| 119 | clear() |
| 120 | { |
| 121 | } |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 124 | template<class Base, class Vector> |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 125 | struct multi_policy_container |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 126 | : public boost::mpl:: |
| 127 | fold<Vector, empty_policy_wrap<Base>, |
| 128 | inherit_with_base<Base, boost::mpl::_1 /*empty/previous*/, |
| 129 | policy_wrap<Base, boost::mpl::_2> /*element in vector*/>>::type { |
| 130 | typedef typename boost::mpl:: |
| 131 | fold<Vector, empty_policy_wrap<Base>, |
| 132 | inherit_with_base<Base, boost::mpl::_1 /*empty/previous*/, |
| 133 | policy_wrap<Base, boost::mpl::_2> /*element in vector*/>>::type super; |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 134 | |
| 135 | typedef typename boost::mpl::at_c<Vector, 0>::type::iterator iterator; |
| 136 | typedef typename boost::mpl::at_c<Vector, 0>::type::const_iterator const_iterator; |
| 137 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 138 | iterator |
| 139 | begin() |
| 140 | { |
| 141 | return this->get<0>().begin(); |
| 142 | } |
| 143 | const_iterator |
| 144 | begin() const |
| 145 | { |
| 146 | return this->get<0>().begin(); |
| 147 | } |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 148 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 149 | iterator |
| 150 | end() |
| 151 | { |
| 152 | return this->get<0>().end(); |
| 153 | } |
| 154 | const_iterator |
| 155 | end() const |
| 156 | { |
| 157 | return this->get<0>().end(); |
| 158 | } |
Alexander Afanasyev | 8566f45 | 2012-12-10 15:21:51 -0800 | [diff] [blame] | 159 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 160 | size_t |
| 161 | size() const |
| 162 | { |
| 163 | return this->get<0>().size(); |
| 164 | } |
| 165 | |
| 166 | multi_policy_container(Base& base) |
| 167 | : super(base) |
| 168 | { |
| 169 | } |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 170 | |
| 171 | template<int N> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 172 | struct index { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 173 | typedef typename boost::mpl::at_c<Vector, N>::type type; |
| 174 | }; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 175 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 176 | template<class T> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 177 | T& |
| 178 | get() |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 179 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 180 | return static_cast<policy_wrap<Base, T>&>(*this).value_; |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | template<class T> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 184 | const T& |
| 185 | get() const |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 186 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 187 | return static_cast<const policy_wrap<Base, T>&>(*this).value_; |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | template<int N> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 191 | typename boost::mpl::at_c<Vector, N>::type& |
| 192 | get() |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 193 | { |
| 194 | typedef typename boost::mpl::at_c<Vector, N>::type T; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 195 | return static_cast<policy_wrap<Base, T>&>(*this).value_; |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | template<int N> |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 199 | const typename boost::mpl::at_c<Vector, N>::type& |
| 200 | get() const |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 201 | { |
| 202 | typedef typename boost::mpl::at_c<Vector, N>::type T; |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 203 | return static_cast<const policy_wrap<Base, T>&>(*this).value_; |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 204 | } |
| 205 | }; |
| 206 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 207 | } // detail |
| 208 | } // ndnSIM |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 209 | } // ndn |
Alexander Afanasyev | e77db79 | 2012-08-09 11:10:58 -0700 | [diff] [blame] | 210 | } // ns3 |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 211 | |
Spyridon Mastorakis | 460f57c | 2014-12-17 00:44:14 -0800 | [diff] [blame] | 212 | /// @endcond |
| 213 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 214 | #endif // MULTI_POLICY_CONTAINER_H_ |