exclude.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  * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
22  */
23 
24 #ifndef NDN_EXCLUDE_HPP
25 #define NDN_EXCLUDE_HPP
26 
29 
30 #include <iterator>
31 #include <map>
32 
33 namespace ndn {
34 
43 class Exclude
44 {
45 public:
46  class Error : public tlv::Error
47  {
48  public:
49  using tlv::Error::Error;
50  };
51 
55  Exclude();
56 
60  explicit
61  Exclude(const Block& wire);
62 
66  template<encoding::Tag TAG>
67  size_t
68  wireEncode(EncodingImpl<TAG>& encoder) const;
69 
73  const Block&
74  wireEncode() const;
75 
79  void
80  wireDecode(const Block& wire);
81 
85  std::string
86  toUri() const;
87 
88 public: // high-level API
93  bool
94  isExcluded(const name::Component& comp) const;
95 
101  Exclude&
102  excludeOne(const name::Component& comp);
103 
111  Exclude&
112  excludeRange(const name::Component& from, const name::Component& to);
113 
119  Exclude&
120  excludeBefore(const name::Component& to);
121 
127  Exclude&
128  excludeAfter(const name::Component& from);
129 
130 public: // EqualityComparable concept
131  bool
132  operator==(const Exclude& other) const;
133 
134  bool
135  operator!=(const Exclude& other) const;
136 
137 public: // internal storage
142  {
143  public:
148  ExcludeComponent(const name::Component& component);
149 
154  explicit
155  ExcludeComponent(bool isNegInf);
156 
157  public:
158  bool isNegInf;
160  };
161 
171  typedef std::map<ExcludeComponent, bool, std::greater<ExcludeComponent>> ExcludeMap;
172  typedef ExcludeMap::value_type Entry;
173 
174 public: // enumeration API
178  class Range
179  {
180  public:
181  Range();
182 
183  Range(bool fromInfinity, const name::Component& from, bool toInfinity, const name::Component& to);
184 
189  bool
190  isSingular() const;
191 
192  bool
193  operator==(const Exclude::Range& other) const;
194 
195  bool
196  operator!=(const Exclude::Range& other) const;
197 
198  public:
203 
209 
214 
220  };
221 
223  {
224  public:
225  using iterator_category = std::forward_iterator_tag;
226  using value_type = const Range;
227  using difference_type = std::ptrdiff_t;
228  using pointer = value_type*;
230 
231  const_iterator() = default;
232 
233  const_iterator(ExcludeMap::const_reverse_iterator it, ExcludeMap::const_reverse_iterator rend);
234 
235  reference
236  operator*() const;
237 
238  pointer
239  operator->() const;
240 
242  operator++();
243 
245  operator++(int);
246 
247  bool
248  operator==(const const_iterator& other) const;
249 
250  bool
251  operator!=(const const_iterator& other) const;
252 
253  private:
254  void
255  update();
256 
257  private:
258  ExcludeMap::const_reverse_iterator m_it;
259  ExcludeMap::const_reverse_iterator m_rend;
260  Range m_range;
261  friend class Exclude;
262  };
263 
265  begin() const;
266 
268  end() const;
269 
270  bool
271  empty() const;
272 
273  size_t
274  size() const;
275 
277 
278  void
279  clear();
280 
281 private:
288  template<typename T>
289  void
290  appendEntry(const T& component, bool hasAny);
291 
292  Exclude&
293  excludeRange(const ExcludeComponent& from, const name::Component& to);
294 
295 private:
296  ExcludeMap m_entries;
297  mutable Block m_wire;
298 
299  friend std::ostream&
300  operator<<(std::ostream& os, const Exclude& name);
301 };
302 
304 
305 bool
307 
308 bool
310 
311 std::ostream&
312 operator<<(std::ostream& os, const Exclude::Range& range);
313 
314 std::ostream&
315 operator<<(std::ostream& os, const Exclude& name);
316 
319 {
320  return const_iterator(m_entries.rbegin(), m_entries.rend());
321 }
322 
325 {
326  return const_iterator(m_entries.rend(), m_entries.rend());
327 }
328 
329 inline bool
331 {
332  return m_entries.empty();
333 }
334 
335 inline bool
336 Exclude::operator!=(const Exclude& other) const
337 {
338  return !(*this == other);
339 }
340 
341 inline bool
343 {
344  return !this->fromInfinity && !this->toInfinity && this->from == this->to;
345 }
346 
347 inline bool
349 {
350  return !this->operator==(other);
351 }
352 
355 {
356  BOOST_ASSERT(m_it != m_rend);
357  return m_range;
358 }
359 
362 {
363  BOOST_ASSERT(m_it != m_rend);
364  return &m_range;
365 }
366 
367 inline bool
369 {
370  return m_it == other.m_it;
371 }
372 
373 inline bool
375 {
376  return !this->operator==(other);
377 }
378 
379 } // namespace ndn
380 
381 #endif // NDN_EXCLUDE_HPP
Definition: data.cpp:26
Exclude & excludeBefore(const name::Component &to)
Exclude all components in range (-Inf, to].
Definition: exclude.cpp:250
friend std::ostream & operator<<(std::ostream &os, const Exclude &name)
Definition: exclude.cpp:332
either a name::Component or "negative infinity"
Definition: exclude.hpp:141
represent an excluded component or range
Definition: exclude.hpp:178
size_t size() const
Definition: exclude.cpp:361
name::Component from
from component (inclusive)
Definition: exclude.hpp:208
bool toInfinity
to positive infinity?
Definition: exclude.hpp:213
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
reference operator*() const
Definition: exclude.hpp:354
void clear()
Definition: exclude.cpp:367
Exclude()
Constructs an empty Exclude.
bool operator==(const const_iterator &other) const
Definition: exclude.hpp:368
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
bool isSingular() const
Definition: exclude.hpp:342
bool operator!=(const const_iterator &other) const
Definition: exclude.hpp:374
const Block & wireEncode() const
Encode to a wire format.
Definition: exclude.cpp:150
void wireDecode(const Block &wire)
Decode from the wire format.
Definition: exclude.cpp:166
Exclude & excludeOne(const name::Component &comp)
Exclude specific name component.
Definition: exclude.cpp:240
bool isExcluded(const name::Component &comp) const
Check if name component is excluded.
Definition: exclude.cpp:231
ExcludeMap::value_type Entry
Definition: exclude.hpp:172
std::ptrdiff_t difference_type
Definition: exclude.hpp:227
std::map< ExcludeComponent, bool, std::greater< ExcludeComponent > > ExcludeMap
a map of exclude entries
Definition: exclude.hpp:171
const_iterator begin() const
Definition: exclude.hpp:318
bool fromInfinity
from negative infinity?
Definition: exclude.hpp:202
name::Component to
to component (inclusive)
Definition: exclude.hpp:219
std::forward_iterator_tag iterator_category
Definition: exclude.hpp:225
bool operator>(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.hpp:54
Represents a name component.
bool empty() const
Definition: exclude.hpp:330
std::string toUri() const
Get escaped string representation (e.g., for use in URI) of the exclude filter.
Definition: exclude.cpp:347
bool operator!=(const Exclude::Range &other) const
Definition: exclude.hpp:348
bool operator!=(const Exclude &other) const
Definition: exclude.hpp:336
Exclude & excludeAfter(const name::Component &from)
Exclude all components in range [from, +Inf)
Definition: exclude.cpp:310
pointer operator->() const
Definition: exclude.hpp:361
Exclude & excludeRange(const name::Component &from, const name::Component &to)
Exclude components in range [from, to].
Definition: exclude.cpp:256
const_iterator end() const
Definition: exclude.hpp:324
bool operator==(const Exclude &other) const
Definition: exclude.cpp:355
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
Represents Exclude selector in NDN Interest.
Definition: exclude.hpp:43