blob: cdf76c3eaaaed30a1aa262c3e9fdd0d03d6fbb2f [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev30f60e32012-07-10 14:21:16 -07004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev30f60e32012-07-10 14:21:16 -07007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070019
20#ifndef PAYLOAD_POLICY_H_
21#define PAYLOAD_POLICY_H_
22
23#include <boost/intrusive/options.hpp>
24#include <boost/intrusive/list.hpp>
25
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070026namespace ns3 {
27namespace ndn {
28namespace ndnSIM {
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070029
30/**
31 * @brief Traits for policy that keeps items in a sorted order using payload member
32 */
33template<class Member>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080034struct payload_policy_traits {
35 struct policy_hook_type : public boost::intrusive::set_member_hook<> {
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070036 };
37
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038 template<class Container>
39 struct container_hook {
40 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
41 type;
42 };
43
44 template<class Base, class Container, class Hook>
45 struct policy {
46 typedef typename boost::intrusive::list<Container, Hook> policy_container;
47
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070048 // could be just typedef
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049 class type : public policy_container {
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070050 public:
51 typedef Container parent_trie;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052
53 type(Base& base)
54 : base_(base)
55 , max_size_(100)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070056 {
57 }
58
59 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080060 update(typename parent_trie::iterator item)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070061 {
62 // do relocation
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080063 policy_container::splice(policy_container::end(), *this,
64 policy_container::s_iterator_to(*item));
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070065 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070067 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 insert(typename parent_trie::iterator item)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070069 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070 if (policy_container::size() >= max_size_) {
71 base_.erase(&(*policy_container::begin()));
72 }
73
74 policy_container::push_back(*item);
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070075 return true;
76 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070078 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 lookup(typename parent_trie::iterator item)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070080 {
81 // do relocation
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082 policy_container::splice(policy_container::end(), *this,
83 policy_container::s_iterator_to(*item));
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070084 }
85
86 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087 erase(typename parent_trie::iterator item)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070088 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070090 }
91
92 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093 clear()
94 {
95 policy_container::clear();
96 }
97
98 inline void
99 set_max_size(size_t max_size)
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700100 {
101 max_size_ = max_size;
102 }
103
104 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105 get_max_size() const
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700106 {
107 return max_size_;
108 }
109
110 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800111 type()
112 : base_(*((Base*)0)){};
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700113
114 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800115 Base& base_;
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700116 size_t max_size_;
117 };
118 };
119};
120
121} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700122} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700123} // ns3
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700124
125#endif // PAYLOAD_POLICY_H