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  NDN_CXX_NODISCARD 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: // non-member operators
219  // NOTE: the following "hidden friend" operators are available via
220  // argument-dependent lookup only and must be defined inline.
221 
226  friend bool
227  operator==(const DelegationList& lhs, const DelegationList& rhs)
228  {
229  return lhs.m_dels == rhs.m_dels;
230  }
231 
232  friend bool
233  operator!=(const DelegationList& lhs, const DelegationList& rhs)
234  {
235  return lhs.m_dels != rhs.m_dels;
236  }
237 
238 private:
239  bool m_isSorted;
240 
248  std::vector<Delegation> m_dels;
249 };
250 
251 #ifndef DOXYGEN
252 extern template size_t
253 DelegationList::wireEncode<encoding::EncoderTag>(EncodingBuffer&, uint32_t) const;
254 
255 extern template size_t
256 DelegationList::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, uint32_t) const;
257 #endif
258 
259 std::ostream&
260 operator<<(std::ostream& os, const DelegationList& dl);
261 
262 } // namespace ndn
263 
264 #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
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:383
friend bool operator!=(const DelegationList &lhs, const DelegationList &rhs)
const Delegation & operator[](size_t i) const
get the i-th delegation
Represents a TLV element of the NDN packet format.
Definition: block.hpp:42
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
uint64_t preference
Definition: delegation.hpp:83
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
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
size_t wireEncode(EncodingImpl< TAG > &encoder, uint32_t type=tlv::ForwardingHint) const
encode into wire format
size_t size() const noexcept
Represents an absolute name.
Definition: name.hpp:44
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
Represents a Delegation.
Definition: delegation.hpp:32
friend bool operator==(const DelegationList &lhs, const DelegationList &rhs)
Compare whether two DelegationLists are equal.
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
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
represents an error in TLV encoding or decoding
Definition: tlv.hpp:51
EncodingImpl< EstimatorTag > EncodingEstimator
const_iterator begin() const noexcept