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