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-2022 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 <iterator>
28 #include <set>
29 #include <unordered_map>
30 
31 namespace ndn {
32 namespace security {
33 namespace pib {
34 
35 class PibImpl;
36 
37 namespace detail {
38 class IdentityImpl;
39 class KeyImpl;
40 } // namespace detail
41 
50 class KeyContainer : noncopyable
51 {
52 private:
53  using NameSet = std::set<Name>;
54 
55 public:
57  {
58  public:
59  using iterator_category = std::forward_iterator_tag;
60  using value_type = const Key;
61  using difference_type = std::ptrdiff_t;
62  using pointer = value_type*;
64 
65  const_iterator() = default;
66 
67  Key
68  operator*();
69 
72  {
73  ++m_it;
74  return *this;
75  }
76 
79  {
80  const_iterator it(*this);
81  ++m_it;
82  return it;
83  }
84 
85  bool
86  operator==(const const_iterator& other) const;
87 
88  bool
89  operator!=(const const_iterator& other) const
90  {
91  return !this->operator==(other);
92  }
93 
94  private:
95  const_iterator(NameSet::const_iterator it, const KeyContainer& container) noexcept;
96 
97  private:
98  NameSet::const_iterator m_it;
99  const KeyContainer* m_container = nullptr;
100 
101  friend KeyContainer;
102  };
103 
105 
106 public:
108  begin() const noexcept
109  {
110  return {m_keyNames.begin(), *this};
111  }
112 
113  const_iterator
114  end() const noexcept
115  {
116  return {};
117  }
118 
119  const_iterator
120  find(const Name& keyName) const;
121 
125  NDN_CXX_NODISCARD bool
126  empty() const noexcept
127  {
128  return m_keyNames.empty();
129  }
130 
134  size_t
135  size() const noexcept
136  {
137  return m_keyNames.size();
138  }
139 
147  Key
148  add(span<const uint8_t> key, const Name& keyName);
149 
154  void
155  remove(const Name& keyName);
156 
162  Key
163  get(const Name& keyName) const;
164 
169  bool
170  isConsistent() const;
171 
172 NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // private interface for IdentityImpl
177  KeyContainer(const Name& identity, shared_ptr<PibImpl> pibImpl);
178 
180  // cache of loaded KeyImpl
181  mutable std::unordered_map<Name, shared_ptr<detail::KeyImpl>> m_keys;
182 
183 private:
184  NameSet m_keyNames;
185  const Name m_identity;
186  const shared_ptr<PibImpl> m_pib;
187 
188  friend detail::IdentityImpl;
189 };
190 
191 } // namespace pib
192 } // namespace security
193 } // namespace ndn
194 
195 #endif // NDN_CXX_SECURITY_PIB_KEY_CONTAINER_HPP
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
Represents an absolute name.
Definition: name.hpp:44
bool operator!=(const const_iterator &other) const
bool operator==(const const_iterator &other) const
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:51
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:48
Definition: data.cpp:25