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