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