delegation-list.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2018 Regents of the University of California.
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 #ifndef NDN_DELEGATION_LIST_HPP
23 #define NDN_DELEGATION_LIST_HPP
24 
25 #include "delegation.hpp"
26 #include <initializer_list>
27 
28 namespace ndn {
29 
37 {
38 public:
39  class Error : public tlv::Error
40  {
41  public:
42  using tlv::Error::Error;
43 
44  Error(const std::string& what, const std::exception& innerException);
45  };
46 
50 
56  DelegationList(std::initializer_list<Delegation> dels);
57 
61  explicit
62  DelegationList(const Block& block, bool wantSort = true);
63 
70  template<encoding::Tag TAG>
71  size_t
72  wireEncode(EncodingImpl<TAG>& encoder, uint32_t type = tlv::ForwardingHint) const;
73 
79  void
80  wireDecode(const Block& block, bool wantSort = true);
81 
82  bool
83  isSorted() const noexcept
84  {
85  return m_isSorted;
86  }
87 
88  using const_iterator = std::vector<Delegation>::const_iterator;
89 
91  begin() const noexcept
92  {
93  return m_dels.begin();
94  }
95 
97  end() const noexcept
98  {
99  return m_dels.end();
100  }
101 
102  bool
103  empty() const noexcept
104  {
105  return m_dels.empty();
106  }
107 
108  size_t
109  size() const noexcept
110  {
111  return m_dels.size();
112  }
113 
117  const Delegation&
118  operator[](size_t i) const
119  {
120  BOOST_ASSERT(i < size());
121  return m_dels[i];
122  }
123 
127  const Delegation&
128  at(size_t i) const
129  {
130  return m_dels.at(i);
131  }
132 
133 public: // modifiers
146  void
147  sort();
148 
155 
160 
164  };
165 
169  bool
170  insert(uint64_t preference, const Name& name,
172 
176  bool
178  {
179  return this->insert(del.preference, del.name, onConflict);
180  }
181 
185  size_t
186  erase(uint64_t preference, const Name& name)
187  {
188  return this->eraseImpl(preference, name);
189  }
190 
194  size_t
195  erase(const Delegation& del)
196  {
197  return this->eraseImpl(del.preference, del.name);
198  }
199 
203  size_t
204  erase(const Name& name)
205  {
206  return this->eraseImpl(nullopt, name);
207  }
208 
209 private:
210  static bool
211  isValidTlvType(uint32_t type);
212 
213  void
214  insertImpl(uint64_t preference, const Name& name);
215 
216  size_t
217  eraseImpl(optional<uint64_t> preference, const Name& name);
218 
219 private:
220  bool m_isSorted;
221 
229  std::vector<Delegation> m_dels;
230 
231  friend bool operator==(const DelegationList&, const DelegationList&);
232 };
233 
234 #ifndef DOXYGEN
235 extern template size_t
236 DelegationList::wireEncode<encoding::EncoderTag>(EncodingBuffer&, uint32_t) const;
237 
238 extern template size_t
239 DelegationList::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, uint32_t) const;
240 #endif
241 
246 bool
247 operator==(const DelegationList& lhs, const DelegationList& rhs);
248 
249 inline bool
250 operator!=(const DelegationList& lhs, const DelegationList& rhs)
251 {
252  return !(lhs == rhs);
253 }
254 
255 std::ostream&
256 operator<<(std::ostream& os, const DelegationList& dl);
257 
258 } // namespace ndn
259 
260 #endif // NDN_DELEGATION_LIST_HPP
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:65
existing delegation(s) with the same name are replaced with the new delegation
const Delegation & at(size_t i) const
get the i-th delegation
new delegation is not inserted if an existing delegation has the same name
DelegationList()
construct an empty DelegationList
uint64_t preference
Definition: delegation.hpp:34
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:337
friend bool operator==(const DelegationList &, const DelegationList &)
compare whether two DelegationLists are equal
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
Error(const std::string &what, const std::exception &innerException)
represents a delegation
Definition: delegation.hpp:32
std::vector< Delegation >::const_iterator const_iterator
size_t erase(uint64_t preference, const Name &name)
delete Delegation(s) with specified preference and name
size_t erase(const Name &name)
erase Delegation(s) with specified name
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:276
void wireDecode(const Block &block, bool wantSort=true)
decode a DelegationList
const_iterator end() const noexcept
bool insert(const Delegation &del, InsertConflictResolution onConflict=INS_REPLACE)
insert Delegation
bool isSorted() const noexcept
size_t size() const noexcept
Represents an absolute name.
Definition: name.hpp:42
bool insert(uint64_t preference, const Name &name, InsertConflictResolution onConflict=INS_REPLACE)
insert Delegation
InsertConflictResolution
what to do when inserting a duplicate name
size_t erase(const Delegation &del)
delete Delegation(s) with matching preference and name
const Delegation & operator[](size_t i) const
get the i-th delegation
void sort()
sort the delegation list
bool empty() const noexcept
represents a list of Delegations
multiple delegations with the same name are kept in the DelegationList
EncodingImpl< EncoderTag > EncodingBuffer
const nullopt_t nullopt((nullopt_t::init()))
size_t wireEncode(EncodingImpl< TAG > &encoder, uint32_t type=tlv::ForwardingHint) const
encode into wire format
represents an error in TLV encoding or decoding
EncodingImpl< EstimatorTag > EncodingEstimator
const_iterator begin() const noexcept