blob: 9b706b0ddedbc704f72129e48b9e4f46ab8f3268 [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{
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -080039 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
40 static std::string GetName () { return "Persistent"; }
41
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070042 struct policy_hook_type : public boost::intrusive::list_member_hook<> {};
43
44 template<class Container>
45 struct container_hook
46 {
47 typedef boost::intrusive::member_hook< Container,
48 policy_hook_type,
49 &Container::policy_hook_ > type;
50 };
51
52 template<class Base,
53 class Container,
54 class Hook>
55 struct policy
56 {
57 typedef typename boost::intrusive::list< Container, Hook > policy_container;
58
59 // could be just typedef
60 class type : public policy_container
61 {
62 public:
63 typedef Container parent_trie;
64
65 type (Base &base)
66 : base_ (base)
67 , max_size_ (100) // when 0, policy is not enforced
68 {
69 }
70
71 inline void
72 update (typename parent_trie::iterator item)
73 {
74 // do nothing
75 }
76
77 inline bool
78 insert (typename parent_trie::iterator item)
79 {
80 if (max_size_ != 0 && policy_container::size () >= max_size_)
81 return false;
82
83 policy_container::push_back (*item);
84 return true;
85 }
86
87 inline void
88 lookup (typename parent_trie::iterator item)
89 {
90 // do nothing
91 }
92
93 inline void
94 erase (typename parent_trie::iterator item)
95 {
96 policy_container::erase (policy_container::s_iterator_to (*item));
97 }
98
99 inline void
100 clear ()
101 {
102 policy_container::clear ();
103 }
104
105 inline void
106 set_max_size (size_t max_size)
107 {
108 max_size_ = max_size;
109 }
110
111 inline size_t
112 get_max_size () const
113 {
114 return max_size_;
115 }
116
117 private:
118 // type () : base_(*((Base*)0)) { };
119
120 private:
121 Base &base_;
122 size_t max_size_;
123 };
124 };
125};
126
127} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700128} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700129} // ns3
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700130
131#endif // PERSISTENT_POLICY_H_