blob: 1943e06f6b6a7b902033a557f473e09e8f5e7ab1 [file] [log] [blame]
Alexander Afanasyev9a989702012-06-29 17:44:00 -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 FIFO_POLICY_H_
22#define FIFO_POLICY_H_
23
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070024#include <boost/intrusive/options.hpp>
25#include <boost/intrusive/list.hpp>
26
Alexander Afanasyeve77db792012-08-09 11:10:58 -070027namespace ns3
28{
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070029namespace ndnSIM
30{
31
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070032/**
33 * @brief Traits for First In First Out replacement policy
34 */
Alexander Afanasyev9a989702012-06-29 17:44:00 -070035struct fifo_policy_traits
36{
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070037 struct policy_hook_type : public boost::intrusive::list_member_hook<> {};
Alexander Afanasyev9a989702012-06-29 17:44:00 -070038
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070039 template<class Container>
40 struct container_hook
Alexander Afanasyev9a989702012-06-29 17:44:00 -070041 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070042 // could be class/struct implementation
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070043 typedef boost::intrusive::member_hook< Container,
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070044 policy_hook_type,
45 &Container::policy_hook_ > type;
46 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070047
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070048 template<class Base,
49 class Container,
50 class Hook>
51 struct policy
52 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070053 typedef typename boost::intrusive::list< Container, Hook > policy_container;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070054
55 // could be just typedef
56 class type : public policy_container
Alexander Afanasyev9a989702012-06-29 17:44:00 -070057 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070058 public:
59 typedef Container parent_trie;
60
61 type (Base &base)
62 : base_ (base)
63 , max_size_ (100)
64 {
65 }
66
67 inline void
68 update (typename parent_trie::iterator item)
69 {
70 // do nothing
71 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070072
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070073 inline bool
74 insert (typename parent_trie::iterator item)
75 {
Alexander Afanasyevbd6f3f42012-07-26 17:50:17 -070076 if (max_size_ != 0 && policy_container::size () >= max_size_)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070077 {
78 base_.erase (&(*policy_container::begin ()));
79 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070080
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070081 policy_container::push_back (*item);
82 return true;
83 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070084
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070085 inline void
86 lookup (typename parent_trie::iterator item)
87 {
88 // do nothing
89 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070090
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070091 inline void
92 erase (typename parent_trie::iterator item)
93 {
94 policy_container::erase (policy_container::s_iterator_to (*item));
95 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070096
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070097 inline void
Alexander Afanasyev78057c32012-07-06 15:18:46 -070098 clear ()
99 {
100 policy_container::clear ();
101 }
102
103 inline void
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700104 set_max_size (size_t max_size)
105 {
106 max_size_ = max_size;
107 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700108
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700109 inline size_t
110 get_max_size () const
111 {
112 return max_size_;
113 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700114
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700115 private:
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700116 type () : base_(*((Base*)0)) { };
117
118 private:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700119 Base &base_;
120 size_t max_size_;
121 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700122 };
123};
124
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700125} // ndnSIM
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700126} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700127
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700128#endif