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-2019 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 "ndn-cxx/delegation.hpp"
26 
27 #include <initializer_list>
28 
29 namespace ndn {
30 
38 {
39 public:
40  class Error : public tlv::Error
41  {
42  public:
43  using tlv::Error::Error;
44  };
45 
49 
55  DelegationList(std::initializer_list<Delegation> dels);
56 
60  explicit
61  DelegationList(const Block& block, bool wantSort = true);
62 
69  template<encoding::Tag TAG>
70  size_t
71  wireEncode(EncodingImpl<TAG>& encoder, uint32_t type = tlv::ForwardingHint) const;
72 
78  void
79  wireDecode(const Block& block, bool wantSort = true);
80 
81  bool
82  isSorted() const noexcept
83  {
84  return m_isSorted;
85  }
86 
87  using const_iterator = std::vector<Delegation>::const_iterator;
88 
90  begin() const noexcept
91  {
92  return m_dels.begin();
93  }
94 
96  end() const noexcept
97  {
98  return m_dels.end();
99  }
100 
101  bool
102  empty() const noexcept
103  {
104  return m_dels.empty();
105  }
106 
107  size_t
108  size() const noexcept
109  {
110  return m_dels.size();
111  }
112 
116  const Delegation&
117  operator[](size_t i) const
118  {
119  BOOST_ASSERT(i < size());
120  return m_dels[i];
121  }
122 
126  const Delegation&
127  at(size_t i) const
128  {
129  return m_dels.at(i);
130  }
131 
132 public: // modifiers
145  void
146  sort();
147 
154 
159 
163  };
164 
168  bool
169  insert(uint64_t preference, const Name& name,
171 
175  bool
177  {
178  return this->insert(del.preference, del.name, onConflict);
179  }
180 
184  size_t
185  erase(uint64_t preference, const Name& name)
186  {
187  return this->eraseImpl(preference, name);
188  }
189 
193  size_t
194  erase(const Delegation& del)
195  {
196  return this->eraseImpl(del.preference, del.name);
197  }
198 
202  size_t
203  erase(const Name& name)
204  {
205  return this->eraseImpl(nullopt, name);
206  }
207 
208 private:
209  static bool
210  isValidTlvType(uint32_t type);
211 
212  void
213  insertImpl(uint64_t preference, const Name& name);
214 
215  size_t
216  eraseImpl(optional<uint64_t> preference, const Name& name);
217 
218 private:
219  bool m_isSorted;
220 
228  std::vector<Delegation> m_dels;
229 
230  friend bool operator==(const DelegationList&, const DelegationList&);
231 };
232 
233 #ifndef DOXYGEN
234 extern template size_t
235 DelegationList::wireEncode<encoding::EncoderTag>(EncodingBuffer&, uint32_t) const;
236 
237 extern template size_t
238 DelegationList::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, uint32_t) const;
239 #endif
240 
245 bool
246 operator==(const DelegationList& lhs, const DelegationList& rhs);
247 
248 inline bool
249 operator!=(const DelegationList& lhs, const DelegationList& rhs)
250 {
251  return !(lhs == rhs);
252 }
253 
254 std::ostream&
255 operator<<(std::ostream& os, const DelegationList& dl);
256 
257 } // namespace ndn
258 
259 #endif // NDN_DELEGATION_LIST_HPP
Definition: data.cpp:26
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:321
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
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:262
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:43
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()))
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
size_t wireEncode(EncodingImpl< TAG > &encoder, uint32_t type=tlv::ForwardingHint) const
encode into wire format
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
EncodingImpl< EstimatorTag > EncodingEstimator
const_iterator begin() const noexcept