NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
multi-policy.h
1 /* -*- 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>
34 #include <boost/mpl/for_each.hpp>
35 
36 #include <boost/intrusive/options.hpp>
37 
38 namespace ns3 {
39 namespace ndn {
40 namespace ndnSIM {
41 
42 template<typename Policies> // e.g., mpl::vector1< lru_policy_traits >
44 {
45  typedef Policies policy_traits;
46 
47  struct getHook { template<class Item> struct apply { typedef typename Item::policy_hook_type type; }; };
49 
50  template<class Container>
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 
86 
87  class type : public policy_container
88  {
89  public:
90  typedef policy policy_base; // to get access to get_time methods from outside
91  typedef Container parent_trie;
92 
93  type (Base &base)
94  : policy_container (base)
95  {
96  }
97 
98  inline void
99  update (typename parent_trie::iterator item)
100  {
101  policy_container::update (item);
102  }
103 
104  inline bool
105  insert (typename parent_trie::iterator item)
106  {
107  return policy_container::insert (item);
108  }
109 
110  inline void
111  lookup (typename parent_trie::iterator item)
112  {
113  policy_container::lookup (item);
114  }
115 
116  inline void
117  erase (typename parent_trie::iterator item)
118  {
119  policy_container::erase (item);
120  }
121 
122  inline void
123  clear ()
124  {
125  policy_container::clear ();
126  }
127 
129  {
130  max_size_setter (policy_container &container, size_t size) : m_container (container), m_size (size) { }
131 
132  template< typename U > void operator() (U index)
133  {
134  m_container.template get<U::value> ().set_max_size (m_size);
135  }
136 
137  private:
138  policy_container &m_container;
139  size_t m_size;
140  };
141 
142  inline void
143  set_max_size (size_t max_size)
144  {
145  boost::mpl::for_each< boost::mpl::range_c<int, 0, boost::mpl::size<policy_traits>::type::value> >
146  (max_size_setter (*this, max_size));
147  }
148 
149  inline size_t
150  get_max_size () const
151  {
152  // as max size should be the same everywhere, get the value from the first available policy
153  return policy_container::template get<0> ().get_max_size ();
154  }
155 
156  };
157  };
158 
159 
160  struct name_getter
161  {
162  name_getter (std::string &name) : m_name (name) { }
163 
164  template< typename U > void operator() (U index)
165  {
166  if (!m_name.empty ())
167  m_name += "::";
168  m_name += boost::mpl::at_c< policy_traits, U::value >::type::GetName ();
169  }
170 
171  std::string &m_name;
172  };
173 
175  static std::string GetName ()
176  {
177  // combine names of all internal policies
178  std::string name;
179  boost::mpl::for_each< boost::mpl::range_c<int, 0, boost::mpl::size<policy_traits>::type::value> > (name_getter (name));
180 
181  return name;
182  }
183 };
184 
185 } // ndnSIM
186 } // ndn
187 } // ns3
188 
189 #endif // MULTI_POLICY_H_
static std::string GetName()
Name that can be used to identify the policy (for NS-3 object model and logging)
Definition: multi-policy.h:175