NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
record-container.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2021 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_CXX_IMPL_RECORD_CONTAINER_HPP
23 #define NDN_CXX_IMPL_RECORD_CONTAINER_HPP
24 
26 #include "ndn-cxx/util/signal.hpp"
27 
28 #include <atomic>
29 
30 namespace ndn {
31 namespace detail {
32 
33 using RecordId = uint64_t;
34 
35 template<typename T>
37 
41 template<typename T>
42 class RecordBase : noncopyable
43 {
44 public:
45  RecordId
46  getId() const
47  {
48  BOOST_ASSERT(m_id != 0);
49  return m_id;
50  }
51 
52 protected:
53  ~RecordBase() = default;
54 
57  void
59  {
60  BOOST_ASSERT(m_container != nullptr);
61  m_container->erase(m_id);
62  }
63 
64 private:
65  RecordContainer<T>* m_container = nullptr;
66  RecordId m_id = 0;
67  friend RecordContainer<T>;
68 };
69 
73 template<typename T>
74 class RecordContainer
75 {
76 public:
77  using Record = T;
78  using Container = std::map<RecordId, Record>;
79 
82  Record*
83  get(RecordId id)
84  {
85  auto i = m_container.find(id);
86  if (i == m_container.end()) {
87  return nullptr;
88  }
89  return &i->second;
90  }
91 
94  template<typename ...TArgs>
95  Record&
96  put(RecordId id, TArgs&&... args)
97  {
98  BOOST_ASSERT(id != 0);
99  auto it = m_container.emplace(std::piecewise_construct, std::forward_as_tuple(id),
100  std::forward_as_tuple(std::forward<decltype(args)>(args)...));
101  BOOST_ASSERT(it.second);
102 
103  Record& record = it.first->second;
104  record.m_container = this;
105  record.m_id = id;
106  return record;
107  }
108 
109  RecordId
111  {
112  return ++m_lastId;
113  }
114 
117  template<typename ...TArgs>
118  Record&
119  insert(TArgs&&... args)
120  {
121  return put(allocateId(), std::forward<decltype(args)>(args)...);
122  }
123 
124  void
126  {
127  m_container.erase(id);
128  if (empty()) {
129  this->onEmpty();
130  }
131  }
132 
133  void
135  {
136  m_container.clear();
137  this->onEmpty();
138  }
139 
144  template<typename Visitor>
145  void
146  removeIf(const Visitor& f)
147  {
148  for (auto i = m_container.begin(); i != m_container.end(); ) {
149  bool wantErase = f(i->second);
150  if (wantErase) {
151  i = m_container.erase(i);
152  }
153  else {
154  ++i;
155  }
156  }
157  if (empty()) {
158  this->onEmpty();
159  }
160  }
161 
166  template<typename Visitor>
167  void
168  forEach(const Visitor& f)
169  {
170  removeIf([&f] (Record& record) {
171  f(record);
172  return false;
173  });
174  }
175 
176  NDN_CXX_NODISCARD bool
177  empty() const noexcept
178  {
179  return m_container.empty();
180  }
181 
182  size_t
183  size() const noexcept
184  {
185  return m_container.size();
186  }
187 
188 public:
192 
193 private:
194  Container m_container;
195  std::atomic<RecordId> m_lastId{0};
196 };
197 
198 } // namespace detail
199 } // namespace ndn
200 
201 #endif // NDN_CXX_IMPL_RECORD_CONTAINER_HPP
Copyright (c) 2011-2015 Regents of the University of California.
void forEach(const Visitor &f)
Visit all records.
Record & insert(TArgs &&... args)
Insert a record with newly assigned ID.
NDN_CXX_NODISCARD bool empty() const noexcept
util::Signal< RecordContainer< T > > onEmpty
Signals when container becomes empty.
void removeIf(const Visitor &f)
Visit all records with the option to erase.
Container of PendingInterest, RegisteredPrefix, or InterestFilterRecord.
size_t size() const noexcept
provides a lightweight signal / event system
Definition: signal.hpp:52
void deleteSelf()
Delete this record from the container.
Common includes and macros used throughout the library.
Record & put(RecordId id, TArgs &&... args)
Insert a record with given ID.
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
uint64_t RecordId
Definition: face.hpp:44
Stores a pending Interest and associated callbacks.
Template of PendingInterest, RegisteredPrefix, and InterestFilterRecord.