blob: 2527c16fe99ef2d9112c5eea5452685e0eb0e710 [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 Afanasyev2b4c9472012-08-09 15:00:38 -070027namespace ns3 {
28namespace ndn {
29namespace ndnSIM {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070030
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070031/**
32 * @brief Traits for First In First Out replacement policy
33 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080034struct fifo_policy_traits {
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -080035 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080036 static std::string
37 GetName()
Alexander Afanasyev9a989702012-06-29 17:44:00 -070038 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039 return "Fifo";
40 }
41
42 struct policy_hook_type : public boost::intrusive::list_member_hook<> {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070043 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070044
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080045 template<class Container>
46 struct container_hook {
47 // could be class/struct implementation
48 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
49 type;
50 };
51
52 template<class Base, class Container, class Hook>
53 struct policy {
54 typedef typename boost::intrusive::list<Container, Hook> policy_container;
55
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070056 // could be just typedef
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057 class type : public policy_container {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070058 public:
59 typedef Container parent_trie;
60
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 type(Base& base)
62 : base_(base)
63 , max_size_(100)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070064 {
65 }
66
67 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 update(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070069 {
70 // do nothing
71 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070073 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 insert(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070075 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076 if (max_size_ != 0 && policy_container::size() >= max_size_) {
77 base_.erase(&(*policy_container::begin()));
78 }
79
80 policy_container::push_back(*item);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070081 return true;
82 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080083
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070084 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085 lookup(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070086 {
87 // do nothing
88 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070090 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080091 erase(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070092 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070094 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070095
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070096 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 clear()
Alexander Afanasyev78057c32012-07-06 15:18:46 -070098 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099 policy_container::clear();
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700100 }
101
102 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 set_max_size(size_t max_size)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700104 {
105 max_size_ = max_size;
106 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700107
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700108 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109 get_max_size() const
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700110 {
111 return max_size_;
112 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700113
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700114 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800115 type()
116 : base_(*((Base*)0)){};
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700117
118 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119 Base& base_;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700120 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 Afanasyev2b4c9472012-08-09 15:00:38 -0700126} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700127} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700128
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700129#endif