blob: 24bd5c3c88fea5be85b2fc965fece2a41ea29f97 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventodb4da5e2018-06-15 11:37:52 -04002/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
Yingdi Yub8f8b342015-04-27 11:06:42 -07004 *
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
Alexander Afanasyev97709c02016-08-25 19:58:30 -070022#ifndef NDN_SECURITY_PIB_IDENTITY_CONTAINER_HPP
23#define NDN_SECURITY_PIB_IDENTITY_CONTAINER_HPP
Yingdi Yub8f8b342015-04-27 11:06:42 -070024
Yingdi Yub8f8b342015-04-27 11:06:42 -070025#include "identity.hpp"
26
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040027#include <iterator>
Yingdi Yucbe72b02015-11-25 17:35:37 -080028#include <set>
29#include <unordered_map>
30
Yingdi Yub8f8b342015-04-27 11:06:42 -070031namespace ndn {
32namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070033namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070034
35class PibImpl;
36
Yingdi Yucbe72b02015-11-25 17:35:37 -080037namespace detail {
38class IdentityImpl;
39} // namespace detail
40
41/**
42 * @brief Container of identities of a Pib
43 *
44 * The container is used to search/enumerate identities of a Pib.
45 * The container can be created only by Pib.
46 */
47class IdentityContainer : noncopyable
Yingdi Yub8f8b342015-04-27 11:06:42 -070048{
49public:
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040050 class const_iterator
Yingdi Yub8f8b342015-04-27 11:06:42 -070051 {
52 public:
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040053 using iterator_category = std::forward_iterator_tag;
54 using value_type = const Identity;
55 using difference_type = std::ptrdiff_t;
56 using pointer = value_type*;
57 using reference = value_type&;
58
Yingdi Yucbe72b02015-11-25 17:35:37 -080059 const_iterator();
Yingdi Yub8f8b342015-04-27 11:06:42 -070060
Yingdi Yub8f8b342015-04-27 11:06:42 -070061 Identity
62 operator*();
63
64 const_iterator&
65 operator++();
66
67 const_iterator
68 operator++(int);
69
70 bool
71 operator==(const const_iterator& other);
72
73 bool
74 operator!=(const const_iterator& other);
75
76 private:
Yingdi Yucbe72b02015-11-25 17:35:37 -080077 const_iterator(std::set<Name>::const_iterator it, const IdentityContainer& container);
Yingdi Yub8f8b342015-04-27 11:06:42 -070078
79 private:
Yingdi Yub8f8b342015-04-27 11:06:42 -070080 std::set<Name>::const_iterator m_it;
Yingdi Yucbe72b02015-11-25 17:35:37 -080081 const IdentityContainer* m_container;
82
83 friend class IdentityContainer;
Yingdi Yub8f8b342015-04-27 11:06:42 -070084 };
85
86 typedef const_iterator iterator;
87
88public:
Yingdi Yub8f8b342015-04-27 11:06:42 -070089 const_iterator
90 begin() const;
91
92 const_iterator
93 end() const;
94
95 const_iterator
96 find(const Name& keyId) const;
97
98 size_t
99 size() const;
100
Yingdi Yucbe72b02015-11-25 17:35:37 -0800101 /**
102 * @brief Add @p identity into the container
103 */
104 Identity
105 add(const Name& identityName);
106
107 /**
108 * @brief Remove @p identity from the container
109 */
110 void
111 remove(const Name& identity);
112
113 /**
114 * @brief Get @p identity from the container
115 * @throw Pib::Error @p identity does not exist
116 */
117 Identity
118 get(const Name& identity) const;
119
120 /**
121 * @brief Reset state of the container
122 *
123 * This method removes all loaded identities and retrieves identity names from the PIB
124 * implementation.
125 */
126 void
127 reset();
128
129 /**
130 * @brief Check if the container is consistent with the backend storage
131 *
132 * @note this method is heavyweight and should be used in debugging mode only.
133 */
134 bool
135 isConsistent() const;
136
137NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
138 /**
139 * @brief Create identity container
140 * @param impl The PIB backend implementation.
141 */
142 explicit
143 IdentityContainer(shared_ptr<PibImpl> pibImpl);
144
145 const std::set<Name>&
146 getIdentityNames() const
147 {
148 return m_identityNames;
149 }
150
151 const std::unordered_map<Name, shared_ptr<detail::IdentityImpl>>&
152 getLoadedIdentities() const
153 {
154 return m_identities;
155 }
156
Yingdi Yub8f8b342015-04-27 11:06:42 -0700157private:
Yingdi Yucbe72b02015-11-25 17:35:37 -0800158 std::set<Name> m_identityNames;
159 /// @brief Cache of loaded detail::IdentityImpl.
160 mutable std::unordered_map<Name, shared_ptr<detail::IdentityImpl>> m_identities;
161
162 shared_ptr<PibImpl> m_pibImpl;
Yingdi Yufe4733a2015-10-22 14:24:12 -0700163
Yingdi Yucbe72b02015-11-25 17:35:37 -0800164 friend class Pib;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700165};
166
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700167} // namespace pib
168
169using pib::IdentityContainer;
170
Yingdi Yub8f8b342015-04-27 11:06:42 -0700171} // namespace security
172} // namespace ndn
173
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700174#endif // NDN_SECURITY_PIB_IDENTITY_CONTAINER_HPP