identity-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-2023 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_SECURITY_PIB_IDENTITY_CONTAINER_HPP
23 #define NDN_CXX_SECURITY_PIB_IDENTITY_CONTAINER_HPP
24 
26 
27 #include <set>
28 #include <unordered_map>
29 
30 namespace ndn::security::pib {
31 
32 class PibImpl;
33 
42 class IdentityContainer : noncopyable
43 {
44 private:
45  using NameSet = std::set<Name>;
46 
47 public:
48  class const_iterator : public boost::forward_iterator_helper<const_iterator, const Identity>
49  {
50  public:
51  const_iterator() = default;
52 
53  Identity
54  operator*() const
55  {
56  BOOST_ASSERT(m_container != nullptr);
57  return m_container->get(*m_it);
58  }
59 
62  {
63  ++m_it;
64  return *this;
65  }
66 
67  friend bool
68  operator==(const const_iterator& lhs, const const_iterator& rhs) noexcept
69  {
70  return lhs.equals(rhs);
71  }
72 
73  private:
74  const_iterator(NameSet::const_iterator it, const IdentityContainer& container) noexcept
75  : m_it(it)
76  , m_container(&container)
77  {
78  }
79 
80  bool
81  equals(const const_iterator& other) const noexcept;
82 
83  private:
84  NameSet::const_iterator m_it;
85  const IdentityContainer* m_container = nullptr;
86 
87  friend IdentityContainer;
88  };
89 
91 
92 public:
94  begin() const noexcept
95  {
96  return {m_identityNames.begin(), *this};
97  }
98 
99  const_iterator
100  end() const noexcept
101  {
102  return {};
103  }
104 
105  const_iterator
106  find(const Name& identity) const;
107 
111  [[nodiscard]] bool
112  empty() const noexcept
113  {
114  return m_identityNames.empty();
115  }
116 
120  size_t
121  size() const noexcept
122  {
123  return m_identityNames.size();
124  }
125 
129  Identity
130  add(const Name& identity);
131 
135  void
136  remove(const Name& identity);
137 
142  Identity
143  get(const Name& identity) const;
144 
150  void
151  reset();
152 
157  bool
158  isConsistent() const;
159 
160 NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // private interface for Pib
165  explicit
166  IdentityContainer(shared_ptr<PibImpl> pibImpl);
167 
169  // cache of loaded IdentityImpl
170  mutable std::unordered_map<Name, shared_ptr<IdentityImpl>> m_identities;
171 
172 private:
173  NameSet m_identityNames;
174  const shared_ptr<PibImpl> m_pib;
175 
176  friend class Pib;
177 };
178 
179 } // namespace ndn::security::pib
180 
181 #endif // NDN_CXX_SECURITY_PIB_IDENTITY_CONTAINER_HPP
Represents an absolute name.
Definition: name.hpp:45
friend bool operator==(const const_iterator &lhs, const const_iterator &rhs) noexcept
Container of identities of a PIB.
bool empty() const noexcept
Check whether the container is empty.
size_t size() const noexcept
Return the number of identities in the container.
const_iterator begin() const noexcept
Identity get(const Name &identity) const
Return an identity by name.
const_iterator find(const Name &identity) const
const_iterator end() const noexcept
void remove(const Name &identity)
Remove identity from the container.
Identity add(const Name &identity)
Add identity into the container.
bool isConsistent() const
Check if the container is consistent with the backend storage.
void reset()
Reset the state of the container.
Frontend handle for an identity in the PIB.
Definition: identity.hpp:44
Frontend to the Public Information Base.
Definition: pib.hpp:52
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:49