blob: 9fcc06124ded6c40f846b473021d11c7200ae3c5 [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
27namespace ndnSIM
28{
29namespace detail
30{
31
32template< class Base, class Value >
33struct policy_wrap
34{
35 policy_wrap (Base &base) : value_ (base) { }
36 Value value_;
37};
38
39template< class Base, class Super/*empy_wrap/previous level*/, class Value/*policy_wrap< element in vector >*/ >
40struct 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 }
80};
81
82template< class Base >
83struct empty_policy_wrap
84{
85 empty_policy_wrap (Base &base) { }
86
87 void update (typename Base::iterator item) {}
88 bool insert (typename Base::iterator item) { return true; }
89 void lookup (typename Base::iterator item) {}
90 void erase (typename Base::iterator item) {}
91};
92
93template< class Base, class Vector >
94struct multi_policy_container
95 : public boost::mpl::fold< Vector,
96 empty_policy_wrap<Base>,
97 inherit_with_base<Base,
98 boost::mpl::_1/*empty/previous*/,
99 policy_wrap<Base, boost::mpl::_2>/*element in vector*/>
100 >::type
101{
102 typedef typename boost::mpl::fold< Vector,
103 empty_policy_wrap<Base>,
104 inherit_with_base<Base,
105 boost::mpl::_1/*empty/previous*/,
106 policy_wrap<Base, boost::mpl::_2>/*element in vector*/>
107 >::type super;
108
109 multi_policy_container (Base &base)
110 : super (base)
111 { }
112
113 template<int N>
114 struct index
115 {
116 typedef typename boost::mpl::at_c<Vector, N>::type type;
117 };
118
119 template<class T>
120 T &
121 get ()
122 {
123 return static_cast< policy_wrap<Base, T> &> (*this).value_;
124 }
125
126 template<class T>
127 const T &
128 get () const
129 {
130 return static_cast< const policy_wrap<Base, T> &> (*this).value_;
131 }
132
133 template<int N>
134 typename boost::mpl::at_c<Vector, N>::type &
135 get ()
136 {
137 typedef typename boost::mpl::at_c<Vector, N>::type T;
138 return static_cast< policy_wrap<Base, T> &> (*this).value_;
139 }
140
141 template<int N>
142 const typename boost::mpl::at_c<Vector, N>::type &
143 get () const
144 {
145 typedef typename boost::mpl::at_c<Vector, N>::type T;
146 return static_cast< const policy_wrap<Base, T> &> (*this).value_;
147 }
148};
149
150
151} // detail
152} // ndnSIM
153
154#endif // MULTI_POLICY_CONTAINER_H_