blob: 0e4749fc21ace824ac9151a5151e706893b8b6e4 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento3b101d02018-07-21 22:44:09 -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
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)
Davide Pesavento3b101d02018-07-21 22:44:09 -040084 : m_pibImpl(std::move(pibImpl))
Yingdi Yub8f8b342015-04-27 11:06:42 -070085{
Davide Pesavento3b101d02018-07-21 22:44:09 -040086 BOOST_ASSERT(m_pibImpl != nullptr);
87 m_identityNames = m_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);
Davide Pesavento3b101d02018-07-21 22:44:09 -0400119 m_identities[identityName] = make_shared<detail::IdentityImpl>(identityName, m_pibImpl, true);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800120 }
121 return get(identityName);
122}
123
124void
125IdentityContainer::remove(const Name& identityName)
126{
127 m_identityNames.erase(identityName);
128 m_identities.erase(identityName);
129 m_pibImpl->removeIdentity(identityName);
130}
131
132Identity
133IdentityContainer::get(const Name& identityName) const
134{
135 shared_ptr<detail::IdentityImpl> id;
136 auto it = m_identities.find(identityName);
137
138 if (it != m_identities.end()) {
139 id = it->second;
140 }
141 else {
Davide Pesavento3b101d02018-07-21 22:44:09 -0400142 id = make_shared<detail::IdentityImpl>(identityName, m_pibImpl, false);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800143 m_identities[identityName] = id;
144 }
145 return Identity(id);
146}
147
148void
149IdentityContainer::reset()
150{
151 m_identities.clear();
152 m_identityNames = m_pibImpl->getIdentities();
153}
154
155bool
156IdentityContainer::isConsistent() const
157{
158 return m_identityNames == m_pibImpl->getIdentities();
Yingdi Yub8f8b342015-04-27 11:06:42 -0700159}
160
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700161} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700162} // namespace security
163} // namespace ndn