blob: 9f215fcfe8f37a7fcf3f5512a71ed037ff88c01c [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 Afanasyev9a989702012-06-29 17:44:00 -070030struct fifo_policy_traits
31{
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070032 struct policy_hook_type : public boost::intrusive::list_member_hook<> {};
Alexander Afanasyev9a989702012-06-29 17:44:00 -070033
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070034 template<class Container>
35 struct container_hook
Alexander Afanasyev9a989702012-06-29 17:44:00 -070036 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070037 // could be class/struct implementation
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070038 typedef boost::intrusive::member_hook< Container,
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070039 policy_hook_type,
40 &Container::policy_hook_ > type;
41 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070042
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070043 template<class Base,
44 class Container,
45 class Hook>
46 struct policy
47 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070048 typedef typename boost::intrusive::list< Container, Hook > policy_container;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070049
50 // could be just typedef
51 class type : public policy_container
Alexander Afanasyev9a989702012-06-29 17:44:00 -070052 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070053 public:
54 typedef Container parent_trie;
55
56 type (Base &base)
57 : base_ (base)
58 , max_size_ (100)
59 {
60 }
61
62 inline void
63 update (typename parent_trie::iterator item)
64 {
65 // do nothing
66 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070067
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070068 inline bool
69 insert (typename parent_trie::iterator item)
70 {
71 if (policy_container::size () >= max_size_)
72 {
73 base_.erase (&(*policy_container::begin ()));
74 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070075
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070076 policy_container::push_back (*item);
77 return true;
78 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070079
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070080 inline void
81 lookup (typename parent_trie::iterator item)
82 {
83 // do nothing
84 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070085
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070086 inline void
87 erase (typename parent_trie::iterator item)
88 {
89 policy_container::erase (policy_container::s_iterator_to (*item));
90 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070091
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070092 inline void
93 set_max_size (size_t max_size)
94 {
95 max_size_ = max_size;
96 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070097
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070098 inline size_t
99 get_max_size () const
100 {
101 return max_size_;
102 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700103
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700104 private:
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700105 type () : base_(*((Base*)0)) { };
106
107 private:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700108 Base &base_;
109 size_t max_size_;
110 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700111 };
112};
113
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700114} // ndnSIM
115
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700116#endif