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-2017 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 
27 #include "name-component.hpp"
29 
30 #include <map>
31 
32 namespace ndn {
33 
37 class Exclude
38 {
39 public:
40  class Error : public tlv::Error
41  {
42  public:
43  explicit
44  Error(const std::string& what)
45  : tlv::Error(what)
46  {
47  }
48  };
49 
53  Exclude();
54 
58  explicit
59  Exclude(const Block& wire);
60 
64  template<encoding::Tag TAG>
65  size_t
66  wireEncode(EncodingImpl<TAG>& encoder) const;
67 
71  const Block&
72  wireEncode() const;
73 
77  void
78  wireDecode(const Block& wire);
79 
83  std::string
84  toUri() const;
85 
86 public: // high-level API
91  bool
92  isExcluded(const name::Component& comp) const;
93 
99  Exclude&
100  excludeOne(const name::Component& comp);
101 
109  Exclude&
110  excludeRange(const name::Component& from, const name::Component& to);
111 
117  Exclude&
118  excludeBefore(const name::Component& to);
119 
125  Exclude&
126  excludeAfter(const name::Component& from);
127 
128 public: // EqualityComparable concept
129  bool
130  operator==(const Exclude& other) const;
131 
132  bool
133  operator!=(const Exclude& other) const;
134 
135 public: // internal storage
140  {
141  public:
146  ExcludeComponent(const name::Component& component);
147 
152  explicit
153  ExcludeComponent(bool isNegInf);
154 
155  public:
156  bool isNegInf;
158  };
159 
169  typedef std::map<ExcludeComponent, bool, std::greater<ExcludeComponent>> ExcludeMap;
170  typedef ExcludeMap::value_type Entry;
171 
172 public: // enumeration API
176  class Range
177  {
178  public:
179  Range();
180 
181  Range(bool fromInfinity, const name::Component& from, bool toInfinity, const name::Component& to);
182 
187  bool
188  isSingular() const;
189 
190  bool
191  operator==(const Exclude::Range& other) const;
192 
193  bool
194  operator!=(const Exclude::Range& other) const;
195 
196  public:
201 
207 
212 
218  };
219 
220  class const_iterator : public std::iterator<std::forward_iterator_tag, const Range>
221  {
222  public:
223  const_iterator() = default;
224 
225  const_iterator(ExcludeMap::const_reverse_iterator it, ExcludeMap::const_reverse_iterator rend);
226 
227  const Range&
228  operator*() const;
229 
230  const Range*
231  operator->() const;
232 
234  operator++();
235 
237  operator++(int);
238 
239  bool
240  operator==(const const_iterator& other) const;
241 
242  bool
243  operator!=(const const_iterator& other) const;
244 
245  private:
246  void
247  update();
248 
249  private:
250  ExcludeMap::const_reverse_iterator m_it;
251  ExcludeMap::const_reverse_iterator m_rend;
252  Range m_range;
253  friend class Exclude;
254  };
255 
257  begin() const;
258 
260  end() const;
261 
262  bool
263  empty() const;
264 
265  size_t
266  size() const;
267 
269 
270  void
271  clear();
272 
273 private:
280  template<typename T>
281  void
282  appendEntry(const T& component, bool hasAny);
283 
284  Exclude&
285  excludeRange(const ExcludeComponent& from, const name::Component& to);
286 
287 private:
288  ExcludeMap m_entries;
289  mutable Block m_wire;
290 
291  friend std::ostream&
292  operator<<(std::ostream& os, const Exclude& name);
293 };
294 
296 
297 bool
299 
300 bool
302 
303 std::ostream&
304 operator<<(std::ostream& os, const Exclude::Range& range);
305 
306 std::ostream&
307 operator<<(std::ostream& os, const Exclude& name);
308 
311 {
312  return const_iterator(m_entries.rbegin(), m_entries.rend());
313 }
314 
317 {
318  return const_iterator(m_entries.rend(), m_entries.rend());
319 }
320 
321 inline bool
323 {
324  return m_entries.empty();
325 }
326 
327 inline bool
328 Exclude::operator!=(const Exclude& other) const
329 {
330  return !(*this == other);
331 }
332 
333 inline bool
335 {
336  return !this->fromInfinity && !this->toInfinity && this->from == this->to;
337 }
338 
339 inline bool
341 {
342  return !this->operator==(other);
343 }
344 
345 inline const Exclude::Range&
347 {
348  BOOST_ASSERT(m_it != m_rend);
349  return m_range;
350 }
351 
352 inline const Exclude::Range*
354 {
355  BOOST_ASSERT(m_it != m_rend);
356  return &m_range;
357 }
358 
359 inline bool
361 {
362  return m_it == other.m_it;
363 }
364 
365 inline bool
367 {
368  return !this->operator==(other);
369 }
370 
371 } // namespace ndn
372 
373 #endif // NDN_EXCLUDE_HPP
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
Exclude & excludeBefore(const name::Component &to)
Exclude all components in range (-Inf, to].
Definition: exclude.cpp:247
friend std::ostream & operator<<(std::ostream &os, const Exclude &name)
Definition: exclude.cpp:329
either a name::Component or "negative infinity"
Definition: exclude.hpp:139
represent an excluded component or range
Definition: exclude.hpp:176
size_t size() const
Definition: exclude.cpp:358
name::Component from
from component (inclusive)
Definition: exclude.hpp:206
bool toInfinity
to positive infinity?
Definition: exclude.hpp:211
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
void clear()
Definition: exclude.cpp:364
Exclude()
Constructs an empty Exclude.
bool operator==(const const_iterator &other) const
Definition: exclude.hpp:360
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
bool isSingular() const
Definition: exclude.hpp:334
const Range & operator*() const
Definition: exclude.hpp:346
bool operator!=(const const_iterator &other) const
Definition: exclude.hpp:366
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:237
bool isExcluded(const name::Component &comp) const
Check if name component is excluded.
Definition: exclude.cpp:228
ExcludeMap::value_type Entry
Definition: exclude.hpp:170
std::map< ExcludeComponent, bool, std::greater< ExcludeComponent > > ExcludeMap
a map of exclude entries
Definition: exclude.hpp:169
const_iterator begin() const
Definition: exclude.hpp:310
const Range * operator->() const
Definition: exclude.hpp:353
bool fromInfinity
from negative infinity?
Definition: exclude.hpp:200
name::Component to
to component (inclusive)
Definition: exclude.hpp:217
bool operator>(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.hpp:54
Component holds a read-only name component value.
bool empty() const
Definition: exclude.hpp:322
std::string toUri() const
Get escaped string representation (e.g., for use in URI) of the exclude filter.
Definition: exclude.cpp:344
bool operator!=(const Exclude::Range &other) const
Definition: exclude.hpp:340
bool operator!=(const Exclude &other) const
Definition: exclude.hpp:328
Exclude & excludeAfter(const name::Component &from)
Exclude all components in range [from, +Inf)
Definition: exclude.cpp:307
Error(const std::string &what)
Definition: exclude.hpp:44
Exclude & excludeRange(const name::Component &from, const name::Component &to)
Exclude components in range [from, to].
Definition: exclude.cpp:253
const_iterator end() const
Definition: exclude.hpp:316
bool operator==(const Exclude &other) const
Definition: exclude.cpp:352
represents an error in TLV encoding or decoding
Represents Exclude selector in NDN Interest.
Definition: exclude.hpp:37