blob: e8139ae997a22eb26c833a837c4f93f9e8712512 [file] [log] [blame]
Alexander Afanasyev9a989702012-06-29 17:44:00 -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 FIFO_POLICY_H_
22#define FIFO_POLICY_H_
23
Alexander Afanasyev9a989702012-06-29 17:44:00 -070024struct fifo_policy_traits
25{
Alexander Afanasyev903062f2012-07-04 18:25:26 -070026 struct policy_hook_type : public bi::list_member_hook<> {};
Alexander Afanasyev9a989702012-06-29 17:44:00 -070027
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070028 template<class Container>
29 struct container_hook
Alexander Afanasyev9a989702012-06-29 17:44:00 -070030 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070031 // could be class/struct implementation
32 typedef bi::member_hook< Container,
33 policy_hook_type,
34 &Container::policy_hook_ > type;
35 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070036
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070037 template<class Base,
38 class Container,
39 class Hook>
40 struct policy
41 {
42 typedef typename bi::list< Container, Hook > policy_container;
43
44 // could be just typedef
45 class type : public policy_container
Alexander Afanasyev9a989702012-06-29 17:44:00 -070046 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070047 public:
48 typedef Container parent_trie;
49
50 type (Base &base)
51 : base_ (base)
52 , max_size_ (100)
53 {
54 }
55
56 inline void
57 update (typename parent_trie::iterator item)
58 {
59 // do nothing
60 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070061
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070062 inline bool
63 insert (typename parent_trie::iterator item)
64 {
65 if (policy_container::size () >= max_size_)
66 {
67 base_.erase (&(*policy_container::begin ()));
68 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070069
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070070 policy_container::push_back (*item);
71 return true;
72 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070073
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070074 inline void
75 lookup (typename parent_trie::iterator item)
76 {
77 // do nothing
78 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070079
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070080 inline void
81 erase (typename parent_trie::iterator item)
82 {
83 policy_container::erase (policy_container::s_iterator_to (*item));
84 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070085
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070086 inline void
87 set_max_size (size_t max_size)
88 {
89 max_size_ = max_size;
90 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070091
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070092 inline size_t
93 get_max_size () const
94 {
95 return max_size_;
96 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070097
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070098 private:
Alexander Afanasyev903062f2012-07-04 18:25:26 -070099 type () : base_(*((Base*)0)) { };
100
101 private:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700102 Base &base_;
103 size_t max_size_;
104 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700105 };
106};
107
108#endif