blob: 7c018cef957ba68d7ee8988d7385f55c5ff96473 [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
22#include "identity-container.hpp"
23#include "pib-impl.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080024#include "detail/identity-impl.hpp"
25#include "util/concepts.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070026
27namespace ndn {
28namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070029namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070030
Yingdi Yucbe72b02015-11-25 17:35:37 -080031NDN_CXX_ASSERT_FORWARD_ITERATOR(IdentityContainer::const_iterator);
32
33IdentityContainer::const_iterator::const_iterator()
34 : m_container(nullptr)
35{
36}
37
Yingdi Yub8f8b342015-04-27 11:06:42 -070038IdentityContainer::const_iterator::const_iterator(std::set<Name>::const_iterator it,
Yingdi Yucbe72b02015-11-25 17:35:37 -080039 const IdentityContainer& container)
Yingdi Yub8f8b342015-04-27 11:06:42 -070040 : m_it(it)
Yingdi Yucbe72b02015-11-25 17:35:37 -080041 , m_container(&container)
Yingdi Yub8f8b342015-04-27 11:06:42 -070042{
43}
44
45Identity
46IdentityContainer::const_iterator::operator*()
47{
Yingdi Yucbe72b02015-11-25 17:35:37 -080048 BOOST_ASSERT(m_container != nullptr);
49 return m_container->get(*m_it);
Yingdi Yub8f8b342015-04-27 11:06:42 -070050}
51
52IdentityContainer::const_iterator&
53IdentityContainer::const_iterator::operator++()
54{
55 ++m_it;
56 return *this;
57}
58
59IdentityContainer::const_iterator
60IdentityContainer::const_iterator::operator++(int)
61{
62 const_iterator it(*this);
63 ++m_it;
64 return it;
65}
66
67bool
68IdentityContainer::const_iterator::operator==(const const_iterator& other)
69{
Yingdi Yucbe72b02015-11-25 17:35:37 -080070 bool isThisEnd = m_container == nullptr || m_it == m_container->m_identityNames.end();
71 bool isOtherEnd = other.m_container == nullptr || other.m_it == other.m_container->m_identityNames.end();
72 return ((isThisEnd || isOtherEnd) ?
73 (isThisEnd == isOtherEnd) :
74 m_container->m_pibImpl == other.m_container->m_pibImpl && m_it == other.m_it);
Yingdi Yub8f8b342015-04-27 11:06:42 -070075}
76
77bool
78IdentityContainer::const_iterator::operator!=(const const_iterator& other)
79{
80 return !(*this == other);
81}
82
Yingdi Yucbe72b02015-11-25 17:35:37 -080083IdentityContainer::IdentityContainer(shared_ptr<PibImpl> pibImpl)
84 : m_pibImpl(pibImpl)
Yingdi Yub8f8b342015-04-27 11:06:42 -070085{
Yingdi Yucbe72b02015-11-25 17:35:37 -080086 BOOST_ASSERT(pibImpl != nullptr);
87 m_identityNames = pibImpl->getIdentities();
Yingdi Yub8f8b342015-04-27 11:06:42 -070088}
89
90IdentityContainer::const_iterator
91IdentityContainer::begin() const
92{
Yingdi Yucbe72b02015-11-25 17:35:37 -080093 return const_iterator(m_identityNames.begin(), *this);
Yingdi Yub8f8b342015-04-27 11:06:42 -070094}
95
96IdentityContainer::const_iterator
97IdentityContainer::end() const
98{
Yingdi Yucbe72b02015-11-25 17:35:37 -080099 return const_iterator();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700100}
101
102IdentityContainer::const_iterator
103IdentityContainer::find(const Name& identity) const
104{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800105 return const_iterator(m_identityNames.find(identity), *this);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700106}
107
108size_t
109IdentityContainer::size() const
110{
Yingdi Yucbe72b02015-11-25 17:35:37 -0800111 return m_identityNames.size();
112}
113
114Identity
115IdentityContainer::add(const Name& identityName)
116{
117 if (m_identityNames.count(identityName) == 0) {
118 m_identityNames.insert(identityName);
119 m_identities[identityName] =
120 shared_ptr<detail::IdentityImpl>(new detail::IdentityImpl(identityName, m_pibImpl, true));
121 }
122 return get(identityName);
123}
124
125void
126IdentityContainer::remove(const Name& identityName)
127{
128 m_identityNames.erase(identityName);
129 m_identities.erase(identityName);
130 m_pibImpl->removeIdentity(identityName);
131}
132
133Identity
134IdentityContainer::get(const Name& identityName) const
135{
136 shared_ptr<detail::IdentityImpl> id;
137 auto it = m_identities.find(identityName);
138
139 if (it != m_identities.end()) {
140 id = it->second;
141 }
142 else {
143 id = shared_ptr<detail::IdentityImpl>(new detail::IdentityImpl(identityName, m_pibImpl, false));
144 m_identities[identityName] = id;
145 }
146 return Identity(id);
147}
148
149void
150IdentityContainer::reset()
151{
152 m_identities.clear();
153 m_identityNames = m_pibImpl->getIdentities();
154}
155
156bool
157IdentityContainer::isConsistent() const
158{
159 return m_identityNames == m_pibImpl->getIdentities();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700160}
161
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700162} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700163} // namespace security
164} // namespace ndn