Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 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 | #include "identity-container.hpp" |
| 23 | #include "pib-impl.hpp" |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 24 | #include "detail/identity-impl.hpp" |
| 25 | #include "util/concepts.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 29 | namespace pib { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 30 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 31 | NDN_CXX_ASSERT_FORWARD_ITERATOR(IdentityContainer::const_iterator); |
| 32 | |
| 33 | IdentityContainer::const_iterator::const_iterator() |
| 34 | : m_container(nullptr) |
| 35 | { |
| 36 | } |
| 37 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 38 | IdentityContainer::const_iterator::const_iterator(std::set<Name>::const_iterator it, |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 39 | const IdentityContainer& container) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 40 | : m_it(it) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 41 | , m_container(&container) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
| 45 | Identity |
| 46 | IdentityContainer::const_iterator::operator*() |
| 47 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 48 | BOOST_ASSERT(m_container != nullptr); |
| 49 | return m_container->get(*m_it); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | IdentityContainer::const_iterator& |
| 53 | IdentityContainer::const_iterator::operator++() |
| 54 | { |
| 55 | ++m_it; |
| 56 | return *this; |
| 57 | } |
| 58 | |
| 59 | IdentityContainer::const_iterator |
| 60 | IdentityContainer::const_iterator::operator++(int) |
| 61 | { |
| 62 | const_iterator it(*this); |
| 63 | ++m_it; |
| 64 | return it; |
| 65 | } |
| 66 | |
| 67 | bool |
| 68 | IdentityContainer::const_iterator::operator==(const const_iterator& other) |
| 69 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 70 | 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 Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | bool |
| 78 | IdentityContainer::const_iterator::operator!=(const const_iterator& other) |
| 79 | { |
| 80 | return !(*this == other); |
| 81 | } |
| 82 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 83 | IdentityContainer::IdentityContainer(shared_ptr<PibImpl> pibImpl) |
| 84 | : m_pibImpl(pibImpl) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 85 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 86 | BOOST_ASSERT(pibImpl != nullptr); |
| 87 | m_identityNames = pibImpl->getIdentities(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | IdentityContainer::const_iterator |
| 91 | IdentityContainer::begin() const |
| 92 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 93 | return const_iterator(m_identityNames.begin(), *this); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | IdentityContainer::const_iterator |
| 97 | IdentityContainer::end() const |
| 98 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 99 | return const_iterator(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | IdentityContainer::const_iterator |
| 103 | IdentityContainer::find(const Name& identity) const |
| 104 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 105 | return const_iterator(m_identityNames.find(identity), *this); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | size_t |
| 109 | IdentityContainer::size() const |
| 110 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 111 | return m_identityNames.size(); |
| 112 | } |
| 113 | |
| 114 | Identity |
| 115 | IdentityContainer::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 | |
| 125 | void |
| 126 | IdentityContainer::remove(const Name& identityName) |
| 127 | { |
| 128 | m_identityNames.erase(identityName); |
| 129 | m_identities.erase(identityName); |
| 130 | m_pibImpl->removeIdentity(identityName); |
| 131 | } |
| 132 | |
| 133 | Identity |
| 134 | IdentityContainer::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 | |
| 149 | void |
| 150 | IdentityContainer::reset() |
| 151 | { |
| 152 | m_identities.clear(); |
| 153 | m_identityNames = m_pibImpl->getIdentities(); |
| 154 | } |
| 155 | |
| 156 | bool |
| 157 | IdentityContainer::isConsistent() const |
| 158 | { |
| 159 | return m_identityNames == m_pibImpl->getIdentities(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 162 | } // namespace pib |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 163 | } // namespace security |
| 164 | } // namespace ndn |