cs-policy.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #ifndef NFD_DAEMON_TABLE_CS_POLICY_HPP
27 #define NFD_DAEMON_TABLE_CS_POLICY_HPP
28 
29 #include "cs-entry.hpp"
30 
31 namespace nfd {
32 namespace cs {
33 
34 class Cs;
35 
38 class Policy : noncopyable
39 {
40 public: // registry
41  template<typename P>
42  static void
43  registerPolicy(const std::string& policyName = P::POLICY_NAME)
44  {
45  Registry& registry = getRegistry();
46  BOOST_ASSERT(registry.count(policyName) == 0);
47  registry[policyName] = [] { return make_unique<P>(); };
48  }
49 
53  static unique_ptr<Policy>
54  create(const std::string& policyName);
55 
58  static std::set<std::string>
60 
61 public:
62  explicit
63  Policy(const std::string& policyName);
64 
65  virtual
66  ~Policy() = default;
67 
68  const std::string&
69  getName() const
70  {
71  return m_policyName;
72  }
73 
76  Cs*
77  getCs() const
78  {
79  return m_cs;
80  }
81 
84  void
85  setCs(Cs* cs)
86  {
87  m_cs = cs;
88  }
89 
92  size_t
93  getLimit() const
94  {
95  return m_limit;
96  }
97 
104  void
105  setLimit(size_t nMaxEntries);
106 
107 public:
111  using EntryRef = Table::const_iterator;
112 
118  signal::Signal<Policy, EntryRef> beforeEvict;
119 
126  void
128 
133  void
135 
139  void
141 
146  void
147  beforeUse(EntryRef i);
148 
149 protected:
158  virtual void
159  doAfterInsert(EntryRef i) = 0;
160 
166  virtual void
167  doAfterRefresh(EntryRef i) = 0;
168 
175  virtual void
176  doBeforeErase(EntryRef i) = 0;
177 
183  virtual void
184  doBeforeUse(EntryRef i) = 0;
185 
189  virtual void
190  evictEntries() = 0;
191 
192 protected:
193  DECLARE_SIGNAL_EMIT(beforeEvict)
194 
195 private: // registry
196  using CreateFunc = std::function<unique_ptr<Policy>()>;
197  using Registry = std::map<std::string, CreateFunc>; // indexed by policy name
198 
199  static Registry&
200  getRegistry();
201 
202 private:
203  std::string m_policyName;
204  size_t m_limit;
205  Cs* m_cs;
206 };
207 
208 } // namespace cs
209 } // namespace nfd
210 
214 #define NFD_REGISTER_CS_POLICY(P) \
215 static class NfdAuto ## P ## CsPolicyRegistrationClass \
216 { \
217 public: \
218  NfdAuto ## P ## CsPolicyRegistrationClass() \
219  { \
220  ::nfd::cs::Policy::registerPolicy<P>(); \
221  } \
222 } g_nfdAuto ## P ## CsPolicyRegistrationVariable
223 
224 #endif // NFD_DAEMON_TABLE_CS_POLICY_HPP
virtual void doAfterInsert(EntryRef i)=0
invoked after a new entry is created in CS
virtual ~Policy()=default
void afterRefresh(EntryRef i)
invoked by CS after an existing entry is refreshed by same Data
Definition: cs-policy.cpp:83
void afterInsert(EntryRef i)
invoked by CS after a new entry is inserted
Definition: cs-policy.cpp:76
static void registerPolicy(const std::string &policyName=P::POLICY_NAME)
Definition: cs-policy.hpp:43
static unique_ptr< Policy > create(const std::string &policyName)
Definition: cs-policy.cpp:46
implements the Content Store
Definition: cs.hpp:44
void setCs(Cs *cs)
sets cs
Definition: cs-policy.hpp:85
virtual void doBeforeErase(EntryRef i)=0
invoked before an entry is erased due to management command
void beforeErase(EntryRef i)
invoked by CS before an entry is erased due to management command
Definition: cs-policy.cpp:90
const std::string & getName() const
Definition: cs-policy.hpp:69
virtual void evictEntries()=0
evicts zero or more entries
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
size_t getLimit() const
gets hard limit (in number of entries)
Definition: cs-policy.hpp:93
void beforeUse(EntryRef i)
invoked by CS before an entry is used to match a lookup
Definition: cs-policy.cpp:97
static std::set< std::string > getPolicyNames()
Definition: cs-policy.cpp:54
represents a CS replacement policy
Definition: cs-policy.hpp:38
Table::const_iterator EntryRef
a reference to an CS entry
Definition: cs-policy.hpp:111
Cs * getCs() const
gets cs
Definition: cs-policy.hpp:77
void setLimit(size_t nMaxEntries)
sets hard limit (in number of entries)
Definition: cs-policy.cpp:68
signal::Signal< Policy, EntryRef > beforeEvict
emits when an entry is being evicted
Definition: cs-policy.hpp:118
virtual void doAfterRefresh(EntryRef i)=0
invoked after an existing entry is refreshed by same Data
Policy(const std::string &policyName)
Definition: cs-policy.cpp:62
virtual void doBeforeUse(EntryRef i)=0
invoked before an entry is used to match a lookup