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