key-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_KEY_CONTAINER_HPP
23 #define NDN_CXX_SECURITY_PIB_KEY_CONTAINER_HPP
24 
26 
27 #include <set>
28 #include <unordered_map>
29 
30 namespace ndn::security::pib {
31 
32 class PibImpl;
33 
42 class KeyContainer : 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 Key>
49  {
50  public:
51  const_iterator() = default;
52 
53  Key
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 KeyContainer& 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 KeyContainer* m_container = nullptr;
86 
87  friend KeyContainer;
88  };
89 
91 
92 public:
94  begin() const noexcept
95  {
96  return {m_keyNames.begin(), *this};
97  }
98 
99  const_iterator
100  end() const noexcept
101  {
102  return {};
103  }
104 
105  const_iterator
106  find(const Name& keyName) const;
107 
111  [[nodiscard]] bool
112  empty() const noexcept
113  {
114  return m_keyNames.empty();
115  }
116 
120  size_t
121  size() const noexcept
122  {
123  return m_keyNames.size();
124  }
125 
133  Key
134  add(span<const uint8_t> key, const Name& keyName);
135 
140  void
141  remove(const Name& keyName);
142 
148  Key
149  get(const Name& keyName) const;
150 
155  bool
156  isConsistent() const;
157 
158 NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // private interface for IdentityImpl
163  KeyContainer(const Name& identity, shared_ptr<PibImpl> pibImpl);
164 
166  // cache of loaded KeyImpl
167  mutable std::unordered_map<Name, shared_ptr<KeyImpl>> m_keys;
168 
169 private:
170  NameSet m_keyNames;
171  const Name m_identity;
172  const shared_ptr<PibImpl> m_pib;
173 
174  friend class IdentityImpl;
175 };
176 
177 } // namespace ndn::security::pib
178 
179 #endif // NDN_CXX_SECURITY_PIB_KEY_CONTAINER_HPP
Represents an absolute name.
Definition: name.hpp:45
friend bool operator==(const const_iterator &lhs, const const_iterator &rhs) noexcept
Container of keys of an identity.
const_iterator end() const noexcept
size_t size() const noexcept
Return the number of keys in the container.
Key add(span< const uint8_t > key, const Name &keyName)
Add key with name keyName into the container.
Key get(const Name &keyName) const
Return a key by name.
const_iterator find(const Name &keyName) const
bool isConsistent() const
Check if the container is consistent with the backend storage.
const_iterator begin() const noexcept
bool empty() const noexcept
Check whether the container is empty.
void remove(const Name &keyName)
Remove a key with keyName from the container.
Frontend handle for a key in the PIB.
Definition: key.hpp:45
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:49