All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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, Regents of the University of California
4  * Alexander Afanasyev
5  *
6  * BSD license, See the LICENSE file for more information
7  *
8  * Author: Alexander Afanasyev <[email protected]>
9  */
10 
11 #ifndef NDN_EXCLUDE_H
12 #define NDN_EXCLUDE_H
13 
14 #include "name-component.hpp"
15 
16 namespace ndn {
17 
21 class Exclude
22 {
23 public:
24  class Error : public Tlv::Error
25  {
26  public:
27  explicit
28  Error(const std::string& what)
29  : Tlv::Error(what)
30  {
31  }
32  };
33 
34  typedef std::map< name::Component, bool /*any*/, std::greater<name::Component> > exclude_type;
35 
36  typedef exclude_type::iterator iterator;
37  typedef exclude_type::const_iterator const_iterator;
38  typedef exclude_type::reverse_iterator reverse_iterator;
39  typedef exclude_type::const_reverse_iterator const_reverse_iterator;
40 
44  Exclude();
45 
49  explicit
50  Exclude(const Block& wire)
51  {
52  wireDecode(wire);
53  }
54 
58  template<bool T>
59  inline size_t
60  wireEncode(EncodingImpl<T>& block) const;
61 
65  inline const Block&
66  wireEncode() const;
67 
71  inline void
72  wireDecode(const Block& wire);
73 
75 
76 
81  bool
82  isExcluded(const name::Component& comp) const;
83 
89  Exclude&
90  excludeOne(const name::Component& comp);
91 
98  Exclude&
99  excludeRange(const name::Component& from, const name::Component& to);
100 
106  inline Exclude&
107  excludeBefore(const name::Component& to);
108 
114  Exclude&
115  excludeAfter(const name::Component& from);
116 
126  inline void
127  appendExclude(const name::Component& name, bool any);
128 
132  inline bool
133  empty() const;
134 
138  inline void
139  clear();
140 
144  inline size_t
145  size() const;
146 
150  inline const_iterator
151  begin() const;
152 
156  inline const_iterator
157  end() const;
158 
163  rbegin() const;
164 
169  rend() const;
170 
174  inline std::string
175  toUri() const;
176 
177 private:
178  Exclude&
179  excludeRange(iterator fromLowerBound, iterator toLowerBound);
180 
181 private:
182  exclude_type m_exclude;
183 
184  mutable Block m_wire;
185 };
186 
187 std::ostream&
188 operator<<(std::ostream& os, const Exclude& name);
189 
190 inline Exclude&
192 {
193  return excludeRange(name::Component(), to);
194 }
195 
196 inline void
198 {
199  m_exclude[name] = any;
200 }
201 
202 inline bool
204 {
205  return m_exclude.empty();
206 }
207 
208 inline void
210 {
211  m_exclude.clear();
212 }
213 
214 
215 inline size_t
217 {
218  return m_exclude.size();
219 }
220 
223 {
224  return m_exclude.begin();
225 }
226 
229 {
230  return m_exclude.end();
231 }
232 
235 {
236  return m_exclude.rbegin();
237 }
238 
241 {
242  return m_exclude.rend();
243 }
244 
245 inline std::string
247 {
248  std::ostringstream os;
249  os << *this;
250  return os.str();
251 }
252 
253 template<bool T>
254 inline size_t
256 {
257  size_t totalLength = 0;
258 
259  // Exclude ::= EXCLUDE-TYPE TLV-LENGTH Any? (NameComponent (Any)?)+
260  // Any ::= ANY-TYPE TLV-LENGTH(=0)
261 
262  for (Exclude::const_iterator i = m_exclude.begin(); i != m_exclude.end(); i++)
263  {
264  if (i->second)
265  {
266  totalLength += prependBooleanBlock(block, Tlv::Any);
267  }
268  if (!i->first.empty())
269  {
270  totalLength += i->first.wireEncode(block);
271  }
272  }
273 
274  totalLength += block.prependVarNumber(totalLength);
275  totalLength += block.prependVarNumber(Tlv::Exclude);
276  return totalLength;
277 }
278 
279 inline const Block&
281 {
282  if (m_wire.hasWire())
283  return m_wire;
284 
285  EncodingEstimator estimator;
286  size_t estimatedSize = wireEncode(estimator);
287 
288  EncodingBuffer buffer(estimatedSize, 0);
289  wireEncode(buffer);
290 
291  m_wire = buffer.block();
292  return m_wire;
293 }
294 
295 inline void
297 {
298  m_wire = wire;
299  m_wire.parse();
300 
301  // Exclude ::= EXCLUDE-TYPE TLV-LENGTH Any? (NameComponent (Any)?)+
302  // Any ::= ANY-TYPE TLV-LENGTH(=0)
303 
305  if (i->type() == Tlv::Any)
306  {
308  ++i;
309  }
310 
311  while (i != m_wire.elements_end())
312  {
313  if (i->type() != Tlv::NameComponent)
314  throw Error("Incorrect format of Exclude filter");
315 
316  name::Component excludedComponent(i->value(), i->value_size());
317  ++i;
318 
319  if (i != m_wire.elements_end())
320  {
321  if (i->type() == Tlv::Any)
322  {
323  appendExclude(excludedComponent, true);
324  ++i;
325  }
326  else
327  {
328  appendExclude(excludedComponent, false);
329  }
330  }
331  else
332  {
333  appendExclude(excludedComponent, false);
334  }
335  }
336 }
337 
338 
339 } // ndn
340 
341 #endif // NDN_EXCLUDE_H
void appendExclude(const name::Component &name, bool any)
Method to directly append exclude element.
Definition: exclude.hpp:197
const_reverse_iterator rend() const
Get end iterator of the exclude terms.
Definition: exclude.hpp:240
Exclude & excludeBefore(const name::Component &to)
Exclude all components from range [/, to].
Definition: exclude.hpp:191
Exclude(const Block &wire)
Create from wire encoding.
Definition: exclude.hpp:50
size_t size() const
Get number of exclude terms.
Definition: exclude.hpp:216
exclude_type::reverse_iterator reverse_iterator
Definition: exclude.hpp:38
exclude_type::const_iterator const_iterator
Definition: exclude.hpp:37
Class representing wire element of the NDN packet.
Definition: block.hpp:26
void clear()
Clear the exclude filter.
Definition: exclude.hpp:209
Exclude()
Default constructor an empty exclude.
Definition: exclude.cpp:16
element_const_iterator elements_end() const
Definition: block.hpp:476
element_const_iterator elements_begin() const
Definition: block.hpp:470
const Block & wireEncode() const
Encode to a wire format.
Definition: exclude.hpp:280
void wireDecode(const Block &wire)
Decode from the wire format.
Definition: exclude.hpp:296
Exclude & excludeOne(const name::Component &comp)
Exclude specific name component.
Definition: exclude.cpp:48
bool isExcluded(const name::Component &comp) const
Check if name component is excluded.
Definition: exclude.cpp:33
size_t prependBooleanBlock(EncodingImpl< P > &encoder, uint32_t type)
element_container::const_iterator element_const_iterator
Definition: block.hpp:31
const_iterator begin() const
Get begin iterator of the exclude terms.
Definition: exclude.hpp:222
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:265
Component holds a read-only name component value.
exclude_type::iterator iterator
Definition: exclude.hpp:36
bool empty() const
Check if exclude filter is empty.
Definition: exclude.hpp:203
exclude_type::const_reverse_iterator const_reverse_iterator
Definition: exclude.hpp:39
std::string toUri() const
Get escaped string representation (e.g., for use in URI) of the exclude filter.
Definition: exclude.hpp:246
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.hpp:288
Exclude & excludeAfter(const name::Component &from)
Exclude all components from range [from, +Inf].
Definition: exclude.cpp:122
Error(const std::string &what)
Definition: exclude.hpp:28
Exclude & excludeRange(const name::Component &from, const name::Component &to)
Exclude components from range [from, to].
Definition: exclude.cpp:86
const_reverse_iterator rbegin() const
Get begin iterator of the exclude terms.
Definition: exclude.hpp:234
const_iterator end() const
Get end iterator of the exclude terms.
Definition: exclude.hpp:228
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.hpp:523
std::map< name::Component, bool, std::greater< name::Component > > exclude_type
Definition: exclude.hpp:34
Class representing wire element of the NDN packet.
Class to represent Exclude component in NDN interests.
Definition: exclude.hpp:21