blob: 9e45cf9d09f4d358c170884dcd27ac628b8d1161 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu6ee2d362015-07-16 21:48:05 -07003 * Copyright (c) 2013-2017 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
Yingdi Yucbe72b02015-11-25 17:35:37 -080027#include <set>
28#include <unordered_map>
29
Yingdi Yub8f8b342015-04-27 11:06:42 -070030namespace ndn {
31namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070032namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070033
34class PibImpl;
35
Yingdi Yucbe72b02015-11-25 17:35:37 -080036namespace detail {
37class IdentityImpl;
38} // namespace detail
39
40/**
41 * @brief Container of identities of a Pib
42 *
43 * The container is used to search/enumerate identities of a Pib.
44 * The container can be created only by Pib.
45 */
46class IdentityContainer : noncopyable
Yingdi Yub8f8b342015-04-27 11:06:42 -070047{
48public:
Yingdi Yucbe72b02015-11-25 17:35:37 -080049 class const_iterator : public std::iterator<std::forward_iterator_tag, const Identity>
Yingdi Yub8f8b342015-04-27 11:06:42 -070050 {
51 public:
Yingdi Yucbe72b02015-11-25 17:35:37 -080052 const_iterator();
Yingdi Yub8f8b342015-04-27 11:06:42 -070053
Yingdi Yub8f8b342015-04-27 11:06:42 -070054 Identity
55 operator*();
56
57 const_iterator&
58 operator++();
59
60 const_iterator
61 operator++(int);
62
63 bool
64 operator==(const const_iterator& other);
65
66 bool
67 operator!=(const const_iterator& other);
68
69 private:
Yingdi Yucbe72b02015-11-25 17:35:37 -080070 const_iterator(std::set<Name>::const_iterator it, const IdentityContainer& container);
Yingdi Yub8f8b342015-04-27 11:06:42 -070071
72 private:
Yingdi Yub8f8b342015-04-27 11:06:42 -070073 std::set<Name>::const_iterator m_it;
Yingdi Yucbe72b02015-11-25 17:35:37 -080074 const IdentityContainer* m_container;
75
76 friend class IdentityContainer;
Yingdi Yub8f8b342015-04-27 11:06:42 -070077 };
78
79 typedef const_iterator iterator;
80
81public:
Yingdi Yub8f8b342015-04-27 11:06:42 -070082 const_iterator
83 begin() const;
84
85 const_iterator
86 end() const;
87
88 const_iterator
89 find(const Name& keyId) const;
90
91 size_t
92 size() const;
93
Yingdi Yucbe72b02015-11-25 17:35:37 -080094 /**
95 * @brief Add @p identity into the container
96 */
97 Identity
98 add(const Name& identityName);
99
100 /**
101 * @brief Remove @p identity from the container
102 */
103 void
104 remove(const Name& identity);
105
106 /**
107 * @brief Get @p identity from the container
108 * @throw Pib::Error @p identity does not exist
109 */
110 Identity
111 get(const Name& identity) const;
112
113 /**
114 * @brief Reset state of the container
115 *
116 * This method removes all loaded identities and retrieves identity names from the PIB
117 * implementation.
118 */
119 void
120 reset();
121
122 /**
123 * @brief Check if the container is consistent with the backend storage
124 *
125 * @note this method is heavyweight and should be used in debugging mode only.
126 */
127 bool
128 isConsistent() const;
129
130NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
131 /**
132 * @brief Create identity container
133 * @param impl The PIB backend implementation.
134 */
135 explicit
136 IdentityContainer(shared_ptr<PibImpl> pibImpl);
137
138 const std::set<Name>&
139 getIdentityNames() const
140 {
141 return m_identityNames;
142 }
143
144 const std::unordered_map<Name, shared_ptr<detail::IdentityImpl>>&
145 getLoadedIdentities() const
146 {
147 return m_identities;
148 }
149
Yingdi Yub8f8b342015-04-27 11:06:42 -0700150private:
Yingdi Yucbe72b02015-11-25 17:35:37 -0800151 std::set<Name> m_identityNames;
152 /// @brief Cache of loaded detail::IdentityImpl.
153 mutable std::unordered_map<Name, shared_ptr<detail::IdentityImpl>> m_identities;
154
155 shared_ptr<PibImpl> m_pibImpl;
Yingdi Yufe4733a2015-10-22 14:24:12 -0700156
Yingdi Yucbe72b02015-11-25 17:35:37 -0800157 friend class Pib;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700158};
159
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700160} // namespace pib
161
162using pib::IdentityContainer;
163
Yingdi Yub8f8b342015-04-27 11:06:42 -0700164} // namespace security
165} // namespace ndn
166
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700167#endif // NDN_SECURITY_PIB_IDENTITY_CONTAINER_HPP