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