blob: e26ec31ab87956a54e663171777a839eb2b784ec [file] [log] [blame]
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -08001/* -*- 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 <alexander.afanasyev@ucla.edu>
9 */
10
11#ifndef NDN_EXCLUDE_H
12#define NDN_EXCLUDE_H
13
14#include "name.hpp"
15
16#include <map>
17
18namespace ndn {
19
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080020/**
21 * @brief Class to represent Exclude component in NDN interests
22 */
23class Exclude
24{
25public:
Alexander Afanasyev993a0e12014-01-03 13:51:37 -080026 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
27
28
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080029 typedef std::map< Name::Component, bool /*any*/, std::greater<Name::Component> > exclude_type;
30
31 typedef exclude_type::iterator iterator;
32 typedef exclude_type::const_iterator const_iterator;
33 typedef exclude_type::reverse_iterator reverse_iterator;
34 typedef exclude_type::const_reverse_iterator const_reverse_iterator;
35
36 /**
37 * @brief Default constructor an empty exclude
38 */
39 Exclude ();
40
41 /**
42 * @brief Check if name component is excluded
43 * @param comp Name component to check against exclude filter
44 */
45 bool
46 isExcluded (const Name::Component &comp) const;
47
48 /**
49 * @brief Exclude specific name component
50 * @param comp component to exclude
51 * @returns *this to allow chaining
52 */
53 Exclude &
54 excludeOne (const Name::Component &comp);
55
56 /**
57 * @brief Exclude components from range [from, to]
58 * @param from first element of the range
59 * @param to last element of the range
60 * @returns *this to allow chaining
61 */
62 Exclude &
63 excludeRange (const Name::Component &from, const Name::Component &to);
64
65 /**
66 * @brief Exclude all components from range [/, to]
67 * @param to last element of the range
68 * @returns *this to allow chaining
69 */
70 inline Exclude &
71 excludeBefore (const Name::Component &to);
72
73 /**
74 * @brief Exclude all components from range [from, +Inf]
75 * @param to last element of the range
76 * @returns *this to allow chaining
77 */
78 Exclude &
79 excludeAfter (const Name::Component &from);
80
81 /**
82 * @brief Method to directly append exclude element
83 * @param name excluded name component
84 * @param any flag indicating if there is a postfix ANY component after the name
85 *
86 * This method is used during conversion from wire format of exclude filter
87 *
88 * If there is an error with ranges (e.g., order of components is wrong) an exception is thrown
89 */
Alexander Afanasyev993a0e12014-01-03 13:51:37 -080090 inline void
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080091 appendExclude (const Name::Component &name, bool any);
92
93 /**
94 * @brief Check if exclude filter is empty
95 */
96 inline bool
97 empty () const;
Alexander Afanasyev85480842014-01-06 14:46:54 -080098
99 /**
100 * @brief Clear the exclude filter
101 */
102 inline void
103 clear();
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800104
105 /**
106 * @brief Get number of exclude terms
107 */
108 inline size_t
109 size () const;
110
111 /**
112 * @brief Get begin iterator of the exclude terms
113 */
114 inline const_iterator
115 begin () const;
116
117 /**
118 * @brief Get end iterator of the exclude terms
119 */
120 inline const_iterator
121 end () const;
122
123 /**
124 * @brief Get begin iterator of the exclude terms
125 */
126 inline const_reverse_iterator
127 rbegin () const;
128
129 /**
130 * @brief Get end iterator of the exclude terms
131 */
132 inline const_reverse_iterator
133 rend () const;
134
135 /**
136 * @brief Get escaped string representation (e.g., for use in URI) of the exclude filter
137 */
138 inline std::string
139 toUri () const;
Alexander Afanasyev993a0e12014-01-03 13:51:37 -0800140
141 /**
142 * Encode this Interest for a particular wire format.
143 * @return The encoded byte array.
144 */
145 const Block&
146 wireEncode() const;
147
148 /**
149 * Decode the input using a particular wire format and update this Interest.
150 * @param input The input byte array to be decoded.
151 */
152 void
153 wireDecode(const Block &wire);
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800154
155private:
156 Exclude &
157 excludeRange (iterator fromLowerBound, iterator toLowerBound);
158
159private:
160 exclude_type m_exclude;
Alexander Afanasyev993a0e12014-01-03 13:51:37 -0800161
162 mutable Block wire_;
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800163};
164
165std::ostream&
166operator << (std::ostream &os, const Exclude &name);
167
168inline Exclude &
169Exclude::excludeBefore (const Name::Component &to)
170{
171 return excludeRange (Name::Component (), to);
172}
173
Alexander Afanasyev993a0e12014-01-03 13:51:37 -0800174inline void
175Exclude::appendExclude (const Name::Component &name, bool any)
176{
177 m_exclude[name] = any;
178}
179
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800180inline bool
181Exclude::empty () const
182{
183 return m_exclude.empty ();
184}
185
Alexander Afanasyev85480842014-01-06 14:46:54 -0800186inline void
187Exclude::clear ()
188{
189 m_exclude.clear ();
190}
191
192
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800193inline size_t
194Exclude::size () const
195{
196 return m_exclude.size ();
197}
198
199inline Exclude::const_iterator
200Exclude::begin () const
201{
202 return m_exclude.begin ();
203}
204
205inline Exclude::const_iterator
206Exclude::end () const
207{
208 return m_exclude.end ();
209}
210
211inline Exclude::const_reverse_iterator
212Exclude::rbegin () const
213{
214 return m_exclude.rbegin ();
215}
216
217inline Exclude::const_reverse_iterator
218Exclude::rend () const
219{
220 return m_exclude.rend ();
221}
222
223inline std::string
224Exclude::toUri () const
225{
226 std::ostringstream os;
227 os << *this;
228 return os.str();
229}
230
231} // ndn
232
233#endif // NDN_EXCLUDE_H