blob: 76cf2cfa84ad3d3d268256778df8d3af038a400b [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 PAYLOAD_POLICY_H_
22#define PAYLOAD_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 policy that keeps items in a sorted order using payload member
34 */
35template<class Member>
36struct payload_policy_traits
37{
38 struct policy_hook_type : public boost::intrusive::set_member_hook<> {};
39
40 template<class Container>
41 struct container_hook
42 {
43 typedef boost::intrusive::member_hook< Container,
44 policy_hook_type,
45 &Container::policy_hook_ > type;
46 };
47
48 template<class Base,
49 class Container,
50 class Hook>
51 struct policy
52 {
53 typedef typename boost::intrusive::list< Container, Hook > policy_container;
54
55 // could be just typedef
56 class type : public policy_container
57 {
58 public:
59 typedef Container parent_trie;
60
61 type (Base &base)
62 : base_ (base)
63 , max_size_ (100)
64 {
65 }
66
67 inline void
68 update (typename parent_trie::iterator item)
69 {
70 // do relocation
71 policy_container::splice (policy_container::end (),
72 *this,
73 policy_container::s_iterator_to (*item));
74 }
75
76 inline bool
77 insert (typename parent_trie::iterator item)
78 {
79 if (policy_container::size () >= max_size_)
80 {
81 base_.erase (&(*policy_container::begin ()));
82 }
83
84 policy_container::push_back (*item);
85 return true;
86 }
87
88 inline void
89 lookup (typename parent_trie::iterator item)
90 {
91 // do relocation
92 policy_container::splice (policy_container::end (),
93 *this,
94 policy_container::s_iterator_to (*item));
95 }
96
97 inline void
98 erase (typename parent_trie::iterator item)
99 {
100 policy_container::erase (policy_container::s_iterator_to (*item));
101 }
102
103 inline void
104 clear ()
105 {
106 policy_container::clear ();
107 }
108
109 inline void
110 set_max_size (size_t max_size)
111 {
112 max_size_ = max_size;
113 }
114
115 inline size_t
116 get_max_size () const
117 {
118 return max_size_;
119 }
120
121 private:
122 type () : base_(*((Base*)0)) { };
123
124 private:
125 Base &base_;
126 size_t max_size_;
127 };
128 };
129};
130
131} // ndnSIM
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700132} // ns3
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700133
134#endif // PAYLOAD_POLICY_H