blob: c8f116c2c4661ce78e038baa3964b9c341676b55 [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).
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080035 * If maximum is reached, new entries will not be added and nothing will be removed from the
36 *container
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070037 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038struct persistent_policy_traits {
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)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040 static std::string
41 GetName()
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070042 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080043 return "Persistent";
44 }
45
46 struct policy_hook_type : public boost::intrusive::list_member_hook<> {
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070047 };
48
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049 template<class Container>
50 struct container_hook {
51 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
52 type;
53 };
54
55 template<class Base, class Container, class Hook>
56 struct policy {
57 typedef typename boost::intrusive::list<Container, Hook> policy_container;
58
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070059 // could be just typedef
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080060 class type : public policy_container {
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070061 public:
62 typedef Container parent_trie;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063
64 type(Base& base)
65 : base_(base)
66 , max_size_(100) // when 0, policy is not enforced
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070067 {
68 }
69
70 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071 update(typename parent_trie::iterator item)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070072 {
73 // do nothing
74 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070076 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077 insert(typename parent_trie::iterator item)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070078 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 if (max_size_ != 0 && policy_container::size() >= max_size_)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070080 return false;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081
82 policy_container::push_back(*item);
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070083 return true;
84 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070086 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087 lookup(typename parent_trie::iterator item)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070088 {
89 // do nothing
90 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080091
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070092 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093 erase(typename parent_trie::iterator item)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070094 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070096 }
97
98 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099 clear()
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700100 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101 policy_container::clear();
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700102 }
103
104 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105 set_max_size(size_t max_size)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700106 {
107 max_size_ = max_size;
108 }
109
110 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800111 get_max_size() const
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700112 {
113 return max_size_;
114 }
115
116 private:
117 // type () : base_(*((Base*)0)) { };
118
119 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120 Base& base_;
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700121 size_t max_size_;
122 };
123 };
124};
125
126} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700127} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700128} // ns3
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700129
130#endif // PERSISTENT_POLICY_H_