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-2019 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_IMPL_RECORD_CONTAINER_HPP
23 #define NDN_IMPL_RECORD_CONTAINER_HPP
24 
26 #include "ndn-cxx/util/signal.hpp"
27 
28 #include <atomic>
29 
30 namespace ndn {
31 
32 using RecordId = uintptr_t;
33 
34 template<typename T>
36 
40 template<typename T>
41 class RecordBase : noncopyable
42 {
43 public:
44  RecordId
45  getId() const
46  {
47  BOOST_ASSERT(m_id != 0);
48  return m_id;
49  }
50 
51 protected:
52  ~RecordBase() = default;
53 
56  void
58  {
59  BOOST_ASSERT(m_container != nullptr);
60  m_container->erase(m_id);
61  }
62 
63 private:
64  RecordContainer<T>* m_container = nullptr;
65  RecordId m_id = 0;
66  friend RecordContainer<T>;
67 };
68 
72 template<typename T>
73 class RecordContainer
74 {
75 public:
76  using Record = T;
77  using Container = std::map<RecordId, Record>;
78 
81  Record*
83  {
84  auto i = m_container.find(id);
85  if (i == m_container.end()) {
86  return nullptr;
87  }
88  return &i->second;
89  }
90 
93  template<typename ...TArgs>
94  Record&
95  put(RecordId id, TArgs&&... args)
96  {
97  BOOST_ASSERT(id != 0);
98  auto it = m_container.emplace(std::piecewise_construct, std::forward_as_tuple(id),
99  std::forward_as_tuple(std::forward<decltype(args)>(args)...));
100  BOOST_ASSERT(it.second);
101 
102  Record& record = it.first->second;
103  record.m_container = this;
104  record.m_id = id;
105  return record;
106  }
107 
108  RecordId
110  {
111  return ++m_lastId;
112  }
113 
116  template<typename ...TArgs>
117  Record&
118  insert(TArgs&&... args)
119  {
120  return put(allocateId(), std::forward<decltype(args)>(args)...);
121  }
122 
123  void
125  {
126  m_container.erase(id);
127  if (empty()) {
128  this->onEmpty();
129  }
130  }
131 
132  void
134  {
135  m_container.clear();
136  this->onEmpty();
137  }
138 
143  template<typename Visitor>
144  void
145  removeIf(const Visitor& f)
146  {
147  for (auto i = m_container.begin(); i != m_container.end(); ) {
148  bool wantErase = f(i->second);
149  if (wantErase) {
150  i = m_container.erase(i);
151  }
152  else {
153  ++i;
154  }
155  }
156  if (empty()) {
157  this->onEmpty();
158  }
159  }
160 
165  template<typename Visitor>
166  void
167  forEach(const Visitor& f)
168  {
169  removeIf([&f] (Record& record) {
170  f(record);
171  return false;
172  });
173  }
174 
175  NDN_CXX_NODISCARD bool
176  empty() const noexcept
177  {
178  return m_container.empty();
179  }
180 
181  size_t
182  size() const noexcept
183  {
184  return m_container.size();
185  }
186 
187 public:
191 
192 private:
193  Container m_container;
194  std::atomic<RecordId> m_lastId{0};
195 };
196 
197 } // namespace ndn
198 
199 #endif // NDN_IMPL_RECORD_CONTAINER_HPP
common.hpp
Common includes and macros used throughout the library.
ndn::RecordContainer::removeIf
void removeIf(const Visitor &f)
Visit all records with the option to erase.
Definition: record-container.hpp:145
signal.hpp
ndn::RecordContainer::insert
Record & insert(TArgs &&... args)
Insert a record with newly assigned ID.
Definition: record-container.hpp:118
ndn::RecordContainer::clear
void clear()
Definition: record-container.hpp:133
ndn::RecordId
uintptr_t RecordId
Definition: record-container.hpp:32
ndn::RecordContainer::erase
void erase(RecordId id)
Definition: record-container.hpp:124
ndn::util::signal::Signal
provides a lightweight signal / event system
Definition: signal.hpp:52
ndn::PendingInterest
Stores a pending Interest and associated callbacks.
Definition: pending-interest.hpp:66
ndn::RecordContainer::Record
T Record
Definition: record-container.hpp:76
ndn::RecordContainer::get
Record * get(RecordId id)
Retrieve record by ID.
Definition: record-container.hpp:82
ndn::RecordContainer::forEach
void forEach(const Visitor &f)
Visit all records.
Definition: record-container.hpp:167
NDN_CXX_NODISCARD
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
ndn::RecordContainer::allocateId
RecordId allocateId()
Definition: record-container.hpp:109
ndn::RecordContainer< PendingInterest >::Container
std::map< RecordId, Record > Container
Definition: record-container.hpp:77
ndn::RecordContainer::onEmpty
util::Signal< RecordContainer< T > > onEmpty
Signals when container becomes empty.
Definition: record-container.hpp:190
ndn::RecordBase::~RecordBase
~RecordBase()=default
ndn::RecordContainer::put
Record & put(RecordId id, TArgs &&... args)
Insert a record with given ID.
Definition: record-container.hpp:95
ndn::RecordContainer::empty
NDN_CXX_NODISCARD bool empty() const noexcept
Definition: record-container.hpp:176
ndn::RecordBase::getId
RecordId getId() const
Definition: record-container.hpp:45
ndn::RecordBase
Template of PendingInterest, RegisteredPrefix, and InterestFilterRecord.
Definition: record-container.hpp:42
ndn::RecordContainer::size
size_t size() const noexcept
Definition: record-container.hpp:182
ndn::RecordBase::deleteSelf
void deleteSelf()
Delete this record from the container.
Definition: record-container.hpp:57
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::RecordContainer
Container of PendingInterest, RegisteredPrefix, or InterestFilterRecord.
Definition: record-container.hpp:35