blob: 276aa5bb0fc2dcd7a20413b89f0e33e245b764a1 [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;
98
99 /**
100 * @brief Get number of exclude terms
101 */
102 inline size_t
103 size () const;
104
105 /**
106 * @brief Get begin iterator of the exclude terms
107 */
108 inline const_iterator
109 begin () const;
110
111 /**
112 * @brief Get end iterator of the exclude terms
113 */
114 inline const_iterator
115 end () const;
116
117 /**
118 * @brief Get begin iterator of the exclude terms
119 */
120 inline const_reverse_iterator
121 rbegin () const;
122
123 /**
124 * @brief Get end iterator of the exclude terms
125 */
126 inline const_reverse_iterator
127 rend () const;
128
129 /**
130 * @brief Get escaped string representation (e.g., for use in URI) of the exclude filter
131 */
132 inline std::string
133 toUri () const;
Alexander Afanasyev993a0e12014-01-03 13:51:37 -0800134
135 /**
136 * Encode this Interest for a particular wire format.
137 * @return The encoded byte array.
138 */
139 const Block&
140 wireEncode() const;
141
142 /**
143 * Decode the input using a particular wire format and update this Interest.
144 * @param input The input byte array to be decoded.
145 */
146 void
147 wireDecode(const Block &wire);
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800148
149private:
150 Exclude &
151 excludeRange (iterator fromLowerBound, iterator toLowerBound);
152
153private:
154 exclude_type m_exclude;
Alexander Afanasyev993a0e12014-01-03 13:51:37 -0800155
156 mutable Block wire_;
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800157};
158
159std::ostream&
160operator << (std::ostream &os, const Exclude &name);
161
162inline Exclude &
163Exclude::excludeBefore (const Name::Component &to)
164{
165 return excludeRange (Name::Component (), to);
166}
167
Alexander Afanasyev993a0e12014-01-03 13:51:37 -0800168inline void
169Exclude::appendExclude (const Name::Component &name, bool any)
170{
171 m_exclude[name] = any;
172}
173
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800174inline bool
175Exclude::empty () const
176{
177 return m_exclude.empty ();
178}
179
180inline size_t
181Exclude::size () const
182{
183 return m_exclude.size ();
184}
185
186inline Exclude::const_iterator
187Exclude::begin () const
188{
189 return m_exclude.begin ();
190}
191
192inline Exclude::const_iterator
193Exclude::end () const
194{
195 return m_exclude.end ();
196}
197
198inline Exclude::const_reverse_iterator
199Exclude::rbegin () const
200{
201 return m_exclude.rbegin ();
202}
203
204inline Exclude::const_reverse_iterator
205Exclude::rend () const
206{
207 return m_exclude.rend ();
208}
209
210inline std::string
211Exclude::toUri () const
212{
213 std::ostringstream os;
214 os << *this;
215 return os.str();
216}
217
218} // ndn
219
220#endif // NDN_EXCLUDE_H