blob: 0745cd37409cc7757696848791aa6a87ac829259 [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) 2012 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_H_
22#define MULTI_POLICY_H_
23
24#include "detail/multi-type-container.h"
25#include "detail/multi-policy-container.h"
26#include "detail/functor-hook.h"
27
28#include <boost/mpl/size.hpp>
29#include <boost/mpl/at.hpp>
30#include <boost/mpl/range_c.hpp>
31#include <boost/mpl/transform.hpp>
32#include <boost/mpl/back_inserter.hpp>
33#include <boost/mpl/vector.hpp>
Alexander Afanasyev8566f452012-12-10 15:21:51 -080034#include <boost/mpl/for_each.hpp>
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070035
36#include <boost/intrusive/options.hpp>
37
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070038namespace ns3 {
39namespace ndn {
40namespace ndnSIM {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070041
42template<typename Policies> // e.g., mpl::vector1< lru_policy_traits >
43struct multi_policy_traits
44{
45 typedef Policies policy_traits;
46
47 struct getHook { template<class Item> struct apply { typedef typename Item::policy_hook_type type; }; };
48 typedef detail::multi_type_container< typename boost::mpl::transform1<policy_traits, getHook>::type > policy_hook_type;
49
50 template<class Container>
51 struct container_hook
52 {
53 typedef policy_hook_type type;
54 };
55
56 template<class Base,
57 class Container,
58 class Hook>
59 struct policy
60 {
61 typedef boost::mpl::range_c<int, 0, boost::mpl::size<policy_traits>::type::value> policies_range;
62
63 struct getPolicy
64 {
65 template<class Number>
66 struct apply
67 {
68 typedef
69 typename boost::mpl::at_c<policy_traits, Number::value>::type::
70 template policy<Base,
71 Container,
72 boost::intrusive::function_hook< detail::FunctorHook <Hook,
73 Container,
74 Number::value> > >::type
75 type;
76 };
77 };
78
79 typedef
80 typename boost::mpl::transform1<policies_range,
81 getPolicy,
82 boost::mpl::back_inserter< boost::mpl::vector0<> > >::type policies;
83
84
85 typedef detail::multi_policy_container< Base, policies > policy_container;
86
87 class type : public policy_container
88 {
89 public:
90 typedef Container parent_trie;
91
92 type (Base &base)
93 : policy_container (base)
94 {
95 }
96
97 inline void
98 update (typename parent_trie::iterator item)
99 {
100 policy_container::update (item);
101 }
102
103 inline bool
104 insert (typename parent_trie::iterator item)
105 {
106 return policy_container::insert (item);
107 }
108
109 inline void
110 lookup (typename parent_trie::iterator item)
111 {
112 policy_container::lookup (item);
113 }
114
115 inline void
116 erase (typename parent_trie::iterator item)
117 {
118 policy_container::erase (item);
119 }
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700120
121 inline void
122 clear ()
123 {
124 policy_container::clear ();
125 }
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800126
127 struct max_size_setter
128 {
129 max_size_setter (policy_container &container, size_t size) : m_container (container), m_size (size) { }
130
131 template< typename U > void operator() (U index)
132 {
133 m_container.template get<U::value> ().set_max_size (m_size);
134 }
135
136 private:
137 policy_container &m_container;
138 size_t m_size;
139 };
140
141 inline void
142 set_max_size (size_t max_size)
143 {
144 boost::mpl::for_each< boost::mpl::range_c<int, 0, boost::mpl::size<policy_traits>::type::value> >
145 (max_size_setter (*this, max_size));
146 }
147
148 inline size_t
149 get_max_size () const
150 {
151 // as max size should be the same everywhere, get the value from the first available policy
152 return policy_container::template get<0> ().get_max_size ();
153 }
154
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700155 };
156 };
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800157
158
159 struct name_getter
160 {
161 name_getter (std::string &name) : m_name (name) { }
162
163 template< typename U > void operator() (U index)
164 {
165 m_name += boost::mpl::at_c< policy_traits, U::value >::type::GetName ();
166 }
167
168 std::string &m_name;
169 };
170
171 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
172 static std::string GetName ()
173 {
174 // combine names of all internal policies
175 std::string name;
176 boost::mpl::for_each< boost::mpl::range_c<int, 0, boost::mpl::size<policy_traits>::type::value> > (name_getter (name));
177
178 return name;
179 }
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700180};
181
182} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700183} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700184} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700185
186#endif // MULTI_POLICY_H_