blob: 7286524d1c64e54749d14e7bcf7949316f163cac [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
27namespace ndnSIM
28{
29
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070030/**
31 * @brief Traits for First In First Out replacement policy
32 */
Alexander Afanasyev9a989702012-06-29 17:44:00 -070033struct fifo_policy_traits
34{
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070035 struct policy_hook_type : public boost::intrusive::list_member_hook<> {};
Alexander Afanasyev9a989702012-06-29 17:44:00 -070036
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070037 template<class Container>
38 struct container_hook
Alexander Afanasyev9a989702012-06-29 17:44:00 -070039 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070040 // could be class/struct implementation
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070041 typedef boost::intrusive::member_hook< Container,
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070042 policy_hook_type,
43 &Container::policy_hook_ > type;
44 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070045
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070046 template<class Base,
47 class Container,
48 class Hook>
49 struct policy
50 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070051 typedef typename boost::intrusive::list< Container, Hook > policy_container;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070052
53 // could be just typedef
54 class type : public policy_container
Alexander Afanasyev9a989702012-06-29 17:44:00 -070055 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070056 public:
57 typedef Container parent_trie;
58
59 type (Base &base)
60 : base_ (base)
61 , max_size_ (100)
62 {
63 }
64
65 inline void
66 update (typename parent_trie::iterator item)
67 {
68 // do nothing
69 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070070
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070071 inline bool
72 insert (typename parent_trie::iterator item)
73 {
Alexander Afanasyevbd6f3f42012-07-26 17:50:17 -070074 if (max_size_ != 0 && policy_container::size () >= max_size_)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070075 {
76 base_.erase (&(*policy_container::begin ()));
77 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070078
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070079 policy_container::push_back (*item);
80 return true;
81 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070082
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070083 inline void
84 lookup (typename parent_trie::iterator item)
85 {
86 // do nothing
87 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070088
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070089 inline void
90 erase (typename parent_trie::iterator item)
91 {
92 policy_container::erase (policy_container::s_iterator_to (*item));
93 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070094
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070095 inline void
Alexander Afanasyev78057c32012-07-06 15:18:46 -070096 clear ()
97 {
98 policy_container::clear ();
99 }
100
101 inline void
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700102 set_max_size (size_t max_size)
103 {
104 max_size_ = max_size;
105 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700106
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700107 inline size_t
108 get_max_size () const
109 {
110 return max_size_;
111 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700112
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700113 private:
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700114 type () : base_(*((Base*)0)) { };
115
116 private:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700117 Base &base_;
118 size_t max_size_;
119 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700120 };
121};
122
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700123} // ndnSIM
124
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700125#endif