blob: d34f373a92208b7eb281dad31cf815e9d3ae7f40 [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Alexander Afanasyev
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
9 */
10
11#ifndef PERSISTENT_POLICY_H_
12#define PERSISTENT_POLICY_H_
13
14#include <boost/intrusive/options.hpp>
15#include <boost/intrusive/list.hpp>
16
17namespace ndn {
18namespace trie {
19
20/**
21 * @brief Traits for persistent replacement policy
22 *
23 * In this policy entries are added until there is a space (controlled by set_max_size call).
24 * If maximum is reached, new entries will not be added and nothing will be removed from the container
25 */
26struct persistent_policy_traits
27{
28 /// @brief Name that can be used to identify the policy (e.g., for logging)
29 static std::string GetName () { return "Persistent"; }
30
31 struct policy_hook_type : public boost::intrusive::list_member_hook<> {};
32
33 template<class Container>
34 struct container_hook
35 {
36 typedef boost::intrusive::member_hook< Container,
37 policy_hook_type,
38 &Container::policy_hook_ > type;
39 };
40
41 template<class Base,
42 class Container,
43 class Hook>
44 struct policy
45 {
46 typedef typename boost::intrusive::list< Container, Hook > policy_container;
47
48 // could be just typedef
49 class type : public policy_container
50 {
51 public:
52 typedef Container parent_trie;
53
54 type (Base &base)
55 : base_ (base)
56 , max_size_ (100) // when 0, policy is not enforced
57 {
58 }
59
60 inline void
61 update (typename parent_trie::iterator item)
62 {
63 // do nothing
64 }
65
66 inline bool
67 insert (typename parent_trie::iterator item)
68 {
69 if (max_size_ != 0 && policy_container::size () >= max_size_)
70 return false;
71
72 policy_container::push_back (*item);
73 return true;
74 }
75
76 inline void
77 lookup (typename parent_trie::iterator item)
78 {
79 // do nothing
80 }
81
82 inline void
83 erase (typename parent_trie::iterator item)
84 {
85 policy_container::erase (policy_container::s_iterator_to (*item));
86 }
87
88 inline void
89 clear ()
90 {
91 policy_container::clear ();
92 }
93
94 inline void
95 set_max_size (size_t max_size)
96 {
97 max_size_ = max_size;
98 }
99
100 inline size_t
101 get_max_size () const
102 {
103 return max_size_;
104 }
105
106 private:
107 // type () : base_(*((Base*)0)) { };
108
109 private:
110 Base &base_;
111 size_t max_size_;
112 };
113 };
114};
115
116} // trie
117} // ndn
118
119#endif // PERSISTENT_POLICY_H_