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