blob: 7f6ff4f19c3b92ea7b1e0ea1864e8521b2089f8b [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyev74633892015-02-08 18:08:46 -08003 * Copyright (c) 2013-2015 Regents of the University of California.
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -08004 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -08006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080022 */
23
24#ifndef NDN_EXCLUDE_H
25#define NDN_EXCLUDE_H
26
Alexander Afanasyevc348f832014-02-17 16:35:17 -080027#include "name-component.hpp"
Alexander Afanasyev15f67312014-07-22 15:11:09 -070028#include "encoding/encoding-buffer.hpp"
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080029
Alexander Afanasyev15f67312014-07-22 15:11:09 -070030#include <sstream>
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070031#include <map>
32
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080033namespace ndn {
34
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080035/**
36 * @brief Class to represent Exclude component in NDN interests
37 */
38class Exclude
39{
40public:
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060041 class Error : public tlv::Error
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070042 {
43 public:
44 explicit
45 Error(const std::string& what)
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060046 : tlv::Error(what)
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070047 {
48 }
49 };
Alexander Afanasyev993a0e12014-01-03 13:51:37 -080050
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080051 /**
52 * @brief Default constructor an empty exclude
53 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070054 Exclude();
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080055
56 /**
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070057 * @brief Create from wire encoding
58 */
59 explicit
Junxiao Shi75203022014-09-11 10:01:50 -070060 Exclude(const Block& wire);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070061
62 /**
Alexander Afanasyevc348f832014-02-17 16:35:17 -080063 * @brief Fast encoding or block size estimation
64 */
Alexander Afanasyev74633892015-02-08 18:08:46 -080065 template<encoding::Tag TAG>
Junxiao Shi75203022014-09-11 10:01:50 -070066 size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070067 wireEncode(EncodingImpl<TAG>& encoder) const;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070068
Alexander Afanasyevc348f832014-02-17 16:35:17 -080069 /**
70 * @brief Encode to a wire format
71 */
Junxiao Shi75203022014-09-11 10:01:50 -070072 const Block&
Alexander Afanasyevc348f832014-02-17 16:35:17 -080073 wireEncode() const;
74
75 /**
76 * @brief Decode from the wire format
77 */
Junxiao Shi75203022014-09-11 10:01:50 -070078 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070079 wireDecode(const Block& wire);
80
Junxiao Shi75203022014-09-11 10:01:50 -070081 /**
82 * @brief Get escaped string representation (e.g., for use in URI) of the exclude filter
83 */
84 std::string
85 toUri() const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -080086
Junxiao Shi75203022014-09-11 10:01:50 -070087public: // high-level API
Alexander Afanasyevc348f832014-02-17 16:35:17 -080088 /**
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080089 * @brief Check if name component is excluded
90 * @param comp Name component to check against exclude filter
91 */
92 bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070093 isExcluded(const name::Component& comp) const;
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -080094
95 /**
96 * @brief Exclude specific name component
97 * @param comp component to exclude
98 * @returns *this to allow chaining
99 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700100 Exclude&
101 excludeOne(const name::Component& comp);
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800102
103 /**
104 * @brief Exclude components from range [from, to]
105 * @param from first element of the range
106 * @param to last element of the range
107 * @returns *this to allow chaining
108 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700109 Exclude&
110 excludeRange(const name::Component& from, const name::Component& to);
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800111
112 /**
113 * @brief Exclude all components from range [/, to]
114 * @param to last element of the range
115 * @returns *this to allow chaining
116 */
Junxiao Shi75203022014-09-11 10:01:50 -0700117 Exclude&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700118 excludeBefore(const name::Component& to);
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800119
120 /**
121 * @brief Exclude all components from range [from, +Inf]
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700122 * @param from the first element of the range
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800123 * @returns *this to allow chaining
124 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700125 Exclude&
126 excludeAfter(const name::Component& from);
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800127
128 /**
Junxiao Shi75203022014-09-11 10:01:50 -0700129 * @brief Check if exclude filter is empty
130 */
131 bool
132 empty() const;
133
134 /**
135 * @brief Clear the exclude filter
136 */
137 void
138 clear();
139
Alexander Afanasyevba096052014-09-19 15:36:37 -0700140public: // EqualityComparable concept
141 bool
142 operator==(const Exclude& other) const;
143
144 bool
145 operator!=(const Exclude& other) const;
146
Junxiao Shi75203022014-09-11 10:01:50 -0700147public: // low-level exclude element API
148 typedef std::map< name::Component, bool /*any*/, std::greater<name::Component> > exclude_type;
149
150 typedef exclude_type::iterator iterator;
151 typedef exclude_type::const_iterator const_iterator;
152 typedef exclude_type::reverse_iterator reverse_iterator;
153 typedef exclude_type::const_reverse_iterator const_reverse_iterator;
154
155 /**
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800156 * @brief Method to directly append exclude element
157 * @param name excluded name component
158 * @param any flag indicating if there is a postfix ANY component after the name
159 *
160 * This method is used during conversion from wire format of exclude filter
161 *
162 * If there is an error with ranges (e.g., order of components is wrong) an exception is thrown
163 */
Junxiao Shi75203022014-09-11 10:01:50 -0700164 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700165 appendExclude(const name::Component& name, bool any);
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800166
167 /**
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800168 * @brief Get number of exclude terms
169 */
Junxiao Shi75203022014-09-11 10:01:50 -0700170 size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700171 size() const;
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800172
173 /**
174 * @brief Get begin iterator of the exclude terms
175 */
Junxiao Shi75203022014-09-11 10:01:50 -0700176 const_iterator
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700177 begin() const;
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800178
179 /**
180 * @brief Get end iterator of the exclude terms
181 */
Junxiao Shi75203022014-09-11 10:01:50 -0700182 const_iterator
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700183 end() const;
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800184
185 /**
186 * @brief Get begin iterator of the exclude terms
187 */
Junxiao Shi75203022014-09-11 10:01:50 -0700188 const_reverse_iterator
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700189 rbegin() const;
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800190
191 /**
192 * @brief Get end iterator of the exclude terms
193 */
Junxiao Shi75203022014-09-11 10:01:50 -0700194 const_reverse_iterator
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700195 rend() const;
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800196
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800197private:
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700198 Exclude&
199 excludeRange(iterator fromLowerBound, iterator toLowerBound);
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800200
201private:
202 exclude_type m_exclude;
Alexander Afanasyev993a0e12014-01-03 13:51:37 -0800203
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700204 mutable Block m_wire;
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800205};
206
207std::ostream&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700208operator<<(std::ostream& os, const Exclude& name);
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800209
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700210inline Exclude&
211Exclude::excludeBefore(const name::Component& to)
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800212{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700213 return excludeRange(name::Component(), to);
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800214}
215
Alexander Afanasyev993a0e12014-01-03 13:51:37 -0800216inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700217Exclude::appendExclude(const name::Component& name, bool any)
Alexander Afanasyev993a0e12014-01-03 13:51:37 -0800218{
219 m_exclude[name] = any;
220}
221
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800222inline bool
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700223Exclude::empty() const
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800224{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700225 return m_exclude.empty();
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800226}
227
Alexander Afanasyev85480842014-01-06 14:46:54 -0800228inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700229Exclude::clear()
Alexander Afanasyev85480842014-01-06 14:46:54 -0800230{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700231 m_exclude.clear();
Alexander Afanasyevba096052014-09-19 15:36:37 -0700232 m_wire.reset();
Alexander Afanasyev85480842014-01-06 14:46:54 -0800233}
234
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800235inline size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700236Exclude::size() const
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800237{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700238 return m_exclude.size();
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800239}
240
241inline Exclude::const_iterator
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700242Exclude::begin() const
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800243{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700244 return m_exclude.begin();
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800245}
246
247inline Exclude::const_iterator
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700248Exclude::end() const
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800249{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700250 return m_exclude.end();
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800251}
252
253inline Exclude::const_reverse_iterator
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700254Exclude::rbegin() const
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800255{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700256 return m_exclude.rbegin();
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800257}
258
259inline Exclude::const_reverse_iterator
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700260Exclude::rend() const
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800261{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700262 return m_exclude.rend();
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800263}
264
Alexander Afanasyevba096052014-09-19 15:36:37 -0700265inline bool
266Exclude::operator!=(const Exclude& other) const
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800267{
Alexander Afanasyevba096052014-09-19 15:36:37 -0700268 return !(*this == other);
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800269}
270
Alexander Afanasyevb3a6af42014-01-03 13:08:28 -0800271} // ndn
272
273#endif // NDN_EXCLUDE_H