blob: 95c7d8f456450c30104f5337883fab2c7c1e7b0c [file] [log] [blame]
Alexander Afanasyev30f60e32012-07-10 14:21:16 -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 PERSISTENT_POLICY_H_
22#define PERSISTENT_POLICY_H_
23
24#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 Afanasyev30f60e32012-07-10 14:21:16 -070030
31/**
32 * @brief Traits for persistent replacement policy
33 *
34 * In this policy entries are added until there is a space (controlled by set_max_size call).
35 * If maximum is reached, new entries will not be added and nothing will be removed from the container
36 */
37struct persistent_policy_traits
38{
39 struct policy_hook_type : public boost::intrusive::list_member_hook<> {};
40
41 template<class Container>
42 struct container_hook
43 {
44 typedef boost::intrusive::member_hook< Container,
45 policy_hook_type,
46 &Container::policy_hook_ > type;
47 };
48
49 template<class Base,
50 class Container,
51 class Hook>
52 struct policy
53 {
54 typedef typename boost::intrusive::list< Container, Hook > policy_container;
55
56 // could be just typedef
57 class type : public policy_container
58 {
59 public:
60 typedef Container parent_trie;
61
62 type (Base &base)
63 : base_ (base)
64 , max_size_ (100) // when 0, policy is not enforced
65 {
66 }
67
68 inline void
69 update (typename parent_trie::iterator item)
70 {
71 // do nothing
72 }
73
74 inline bool
75 insert (typename parent_trie::iterator item)
76 {
77 if (max_size_ != 0 && policy_container::size () >= max_size_)
78 return false;
79
80 policy_container::push_back (*item);
81 return true;
82 }
83
84 inline void
85 lookup (typename parent_trie::iterator item)
86 {
87 // do nothing
88 }
89
90 inline void
91 erase (typename parent_trie::iterator item)
92 {
93 policy_container::erase (policy_container::s_iterator_to (*item));
94 }
95
96 inline void
97 clear ()
98 {
99 policy_container::clear ();
100 }
101
102 inline void
103 set_max_size (size_t max_size)
104 {
105 max_size_ = max_size;
106 }
107
108 inline size_t
109 get_max_size () const
110 {
111 return max_size_;
112 }
113
114 private:
115 // type () : base_(*((Base*)0)) { };
116
117 private:
118 Base &base_;
119 size_t max_size_;
120 };
121 };
122};
123
124} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700125} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700126} // ns3
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700127
128#endif // PERSISTENT_POLICY_H_