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 | |
| 32 | template< class Base, class Value > |
| 33 | struct policy_wrap |
| 34 | { |
| 35 | policy_wrap (Base &base) : value_ (base) { } |
| 36 | Value value_; |
| 37 | }; |
| 38 | |
| 39 | template< class Base, class Super/*empy_wrap/previous level*/, class Value/*policy_wrap< element in vector >*/ > |
| 40 | struct inherit_with_base : Super, Value |
| 41 | { |
| 42 | inherit_with_base (Base &base) : Super (base), Value (base) { } |
| 43 | |
| 44 | void |
| 45 | update (typename Base::iterator item) |
| 46 | { |
| 47 | Value::value_.update (item); |
| 48 | Super::update (item); |
| 49 | } |
| 50 | |
| 51 | bool |
| 52 | insert (typename Base::iterator item) |
| 53 | { |
| 54 | bool ok = Value::value_.insert (item); |
| 55 | if (!ok) |
| 56 | return false; |
| 57 | |
| 58 | ok = Super::insert (item); |
| 59 | if (!ok) |
| 60 | { |
| 61 | Value::value_.erase (item); |
| 62 | return false; |
| 63 | } |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | lookup (typename Base::iterator item) |
| 69 | { |
| 70 | Value::value_.lookup (item); |
| 71 | Super::lookup (item); |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | erase (typename Base::iterator item) |
| 76 | { |
| 77 | Value::value_.erase (item); |
| 78 | Super::erase (item); |
| 79 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 80 | |
| 81 | void |
| 82 | clear () |
| 83 | { |
| 84 | Value::value_.clear (); |
| 85 | Super::clear (); |
| 86 | } |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | template< class Base > |
| 90 | struct empty_policy_wrap |
| 91 | { |
| 92 | empty_policy_wrap (Base &base) { } |
| 93 | |
| 94 | void update (typename Base::iterator item) {} |
| 95 | bool insert (typename Base::iterator item) { return true; } |
| 96 | void lookup (typename Base::iterator item) {} |
| 97 | void erase (typename Base::iterator item) {} |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 98 | void clear () {} |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | template< class Base, class Vector > |
| 102 | struct multi_policy_container |
| 103 | : public boost::mpl::fold< Vector, |
| 104 | empty_policy_wrap<Base>, |
| 105 | inherit_with_base<Base, |
| 106 | boost::mpl::_1/*empty/previous*/, |
| 107 | policy_wrap<Base, boost::mpl::_2>/*element in vector*/> |
| 108 | >::type |
| 109 | { |
| 110 | typedef typename boost::mpl::fold< Vector, |
| 111 | empty_policy_wrap<Base>, |
| 112 | inherit_with_base<Base, |
| 113 | boost::mpl::_1/*empty/previous*/, |
| 114 | policy_wrap<Base, boost::mpl::_2>/*element in vector*/> |
| 115 | >::type super; |
| 116 | |
| 117 | multi_policy_container (Base &base) |
| 118 | : super (base) |
| 119 | { } |
| 120 | |
| 121 | template<int N> |
| 122 | struct index |
| 123 | { |
| 124 | typedef typename boost::mpl::at_c<Vector, N>::type type; |
| 125 | }; |
| 126 | |
| 127 | template<class T> |
| 128 | T & |
| 129 | get () |
| 130 | { |
| 131 | return static_cast< policy_wrap<Base, T> &> (*this).value_; |
| 132 | } |
| 133 | |
| 134 | template<class T> |
| 135 | const T & |
| 136 | get () const |
| 137 | { |
| 138 | return static_cast< const policy_wrap<Base, T> &> (*this).value_; |
| 139 | } |
| 140 | |
| 141 | template<int N> |
| 142 | typename boost::mpl::at_c<Vector, N>::type & |
| 143 | get () |
| 144 | { |
| 145 | typedef typename boost::mpl::at_c<Vector, N>::type T; |
| 146 | return static_cast< policy_wrap<Base, T> &> (*this).value_; |
| 147 | } |
| 148 | |
| 149 | template<int N> |
| 150 | const typename boost::mpl::at_c<Vector, N>::type & |
| 151 | get () const |
| 152 | { |
| 153 | typedef typename boost::mpl::at_c<Vector, N>::type T; |
| 154 | return static_cast< const policy_wrap<Base, T> &> (*this).value_; |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | |
| 159 | } // detail |
| 160 | } // ndnSIM |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 161 | } // ndn |
Alexander Afanasyev | e77db79 | 2012-08-09 11:10:58 -0700 | [diff] [blame] | 162 | } // ns3 |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 163 | |
| 164 | #endif // MULTI_POLICY_CONTAINER_H_ |