Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #ifndef NDN_EXCLUDE_H |
| 25 | #define NDN_EXCLUDE_H |
| 26 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 27 | #include "name-component.hpp" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 28 | #include "encoding/encoding-buffer.hpp" |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 30 | #include <sstream> |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 31 | #include <map> |
| 32 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 33 | namespace ndn { |
| 34 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 35 | /** |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 36 | * @brief Represents Exclude selector in NDN Interest |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 37 | */ |
| 38 | class Exclude |
| 39 | { |
| 40 | public: |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 41 | class Error : public tlv::Error |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 42 | { |
| 43 | public: |
| 44 | explicit |
| 45 | Error(const std::string& what) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 46 | : tlv::Error(what) |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | }; |
Alexander Afanasyev | 993a0e1 | 2014-01-03 13:51:37 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 51 | /** |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 52 | * @brief Constructs an empty Exclude |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 53 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 54 | Exclude(); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 55 | |
| 56 | /** |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 57 | * @brief Create from wire encoding |
| 58 | */ |
| 59 | explicit |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 60 | Exclude(const Block& wire); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 63 | * @brief Fast encoding or block size estimation |
| 64 | */ |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 65 | template<encoding::Tag TAG> |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 66 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 67 | wireEncode(EncodingImpl<TAG>& encoder) const; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 68 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 69 | /** |
| 70 | * @brief Encode to a wire format |
| 71 | */ |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 72 | const Block& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 73 | wireEncode() const; |
| 74 | |
| 75 | /** |
| 76 | * @brief Decode from the wire format |
| 77 | */ |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 78 | void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 79 | wireDecode(const Block& wire); |
| 80 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 81 | /** |
| 82 | * @brief Get escaped string representation (e.g., for use in URI) of the exclude filter |
| 83 | */ |
| 84 | std::string |
| 85 | toUri() const; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 86 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 87 | public: // high-level API |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 88 | /** |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 89 | * @brief Check if name component is excluded |
| 90 | * @param comp Name component to check against exclude filter |
| 91 | */ |
| 92 | bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 93 | isExcluded(const name::Component& comp) const; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * @brief Exclude specific name component |
| 97 | * @param comp component to exclude |
| 98 | * @returns *this to allow chaining |
| 99 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 100 | Exclude& |
| 101 | excludeOne(const name::Component& comp); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 102 | |
| 103 | /** |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 104 | * @brief Exclude components in range [from, to] |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 105 | * @param from first element of the range |
| 106 | * @param to last element of the range |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 107 | * @throw Error \p from equals or comes after \p to in canonical ordering |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 108 | * @returns *this to allow chaining |
| 109 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 110 | Exclude& |
| 111 | excludeRange(const name::Component& from, const name::Component& to); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 112 | |
| 113 | /** |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 114 | * @brief Exclude all components in range (-Inf, to] |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 115 | * @param to last element of the range |
| 116 | * @returns *this to allow chaining |
| 117 | */ |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 118 | Exclude& |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 119 | excludeBefore(const name::Component& to); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 120 | |
| 121 | /** |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 122 | * @brief Exclude all components in range [from, +Inf) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 123 | * @param from the first element of the range |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 124 | * @returns *this to allow chaining |
| 125 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 126 | Exclude& |
| 127 | excludeAfter(const name::Component& from); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 128 | |
| 129 | /** |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 130 | * @brief Check if exclude filter is empty |
| 131 | */ |
| 132 | bool |
| 133 | empty() const; |
| 134 | |
| 135 | /** |
| 136 | * @brief Clear the exclude filter |
| 137 | */ |
| 138 | void |
| 139 | clear(); |
| 140 | |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame] | 141 | public: // EqualityComparable concept |
| 142 | bool |
| 143 | operator==(const Exclude& other) const; |
| 144 | |
| 145 | bool |
| 146 | operator!=(const Exclude& other) const; |
| 147 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 148 | public: // low-level exclude entry API |
| 149 | /** |
| 150 | * @brief either a name::Component or "negative infinity" |
| 151 | */ |
| 152 | class ExcludeComponent |
| 153 | { |
| 154 | public: |
| 155 | /** |
| 156 | * @brief implicitly construct a regular infinity ExcludeComponent |
| 157 | * @param component a name component which is excluded |
| 158 | */ |
| 159 | ExcludeComponent(const name::Component& component); |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 160 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 161 | /** |
| 162 | * @brief construct a negative infinity ExcludeComponent |
| 163 | * @param isNegInf must be true |
| 164 | */ |
| 165 | explicit |
| 166 | ExcludeComponent(bool isNegInf); |
| 167 | |
| 168 | public: |
| 169 | bool isNegInf; |
| 170 | name::Component component; |
| 171 | }; |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 172 | |
| 173 | /** |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 174 | * @brief a map of exclude entries |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 175 | * |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 176 | * Each key, except "negative infinity", is a name component that is excluded. |
| 177 | * The mapped boolean indicates whether the range between a key and the next greater key |
| 178 | * is also excluded. If true, the wire encoding shall have an ANY element. |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 179 | * |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 180 | * The map is ordered in descending order to simplify \p isExcluded. |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 181 | */ |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 182 | typedef std::map<ExcludeComponent, bool, std::greater<ExcludeComponent>> ExcludeType; |
| 183 | typedef ExcludeType::value_type Entry; |
| 184 | typedef ExcludeType::iterator iterator; |
| 185 | typedef ExcludeType::const_iterator const_iterator; |
| 186 | typedef ExcludeType::reverse_iterator reverse_iterator; |
| 187 | typedef ExcludeType::const_reverse_iterator const_reverse_iterator; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 188 | |
| 189 | /** |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 190 | * @brief Get number of exclude terms |
| 191 | */ |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 192 | size_t |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 193 | size() const; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 194 | |
| 195 | /** |
| 196 | * @brief Get begin iterator of the exclude terms |
| 197 | */ |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 198 | const_iterator |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 199 | begin() const; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 200 | |
| 201 | /** |
| 202 | * @brief Get end iterator of the exclude terms |
| 203 | */ |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 204 | const_iterator |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 205 | end() const; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 206 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 207 | private: |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 208 | /** |
| 209 | * @brief directly append exclude element |
| 210 | * @tparam T either name::Component or bool |
| 211 | * |
| 212 | * This method is used during conversion from wire format of exclude filter |
| 213 | */ |
| 214 | template<typename T> |
| 215 | void |
| 216 | appendEntry(const T& component, bool hasAny); |
| 217 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 218 | Exclude& |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 219 | excludeRange(const ExcludeComponent& from, const name::Component& to); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 220 | |
| 221 | private: |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 222 | ExcludeType m_entries; |
Alexander Afanasyev | 993a0e1 | 2014-01-03 13:51:37 -0800 | [diff] [blame] | 223 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 224 | mutable Block m_wire; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | std::ostream& |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 228 | operator<<(std::ostream& os, const Exclude& name); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 229 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 230 | bool |
| 231 | operator>(const Exclude::ExcludeComponent& a, const Exclude::ExcludeComponent& b); |
Alexander Afanasyev | 993a0e1 | 2014-01-03 13:51:37 -0800 | [diff] [blame] | 232 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 233 | inline bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 234 | Exclude::empty() const |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 235 | { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 236 | return m_entries.empty(); |
Alexander Afanasyev | 8548084 | 2014-01-06 14:46:54 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 239 | inline size_t |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 240 | Exclude::size() const |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 241 | { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 242 | return m_entries.size(); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | inline Exclude::const_iterator |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 246 | Exclude::begin() const |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 247 | { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 248 | return m_entries.begin(); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | inline Exclude::const_iterator |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 252 | Exclude::end() const |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 253 | { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 254 | return m_entries.end(); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame] | 257 | inline bool |
| 258 | Exclude::operator!=(const Exclude& other) const |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 259 | { |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame] | 260 | return !(*this == other); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame^] | 263 | } // namespace ndn |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 264 | |
| 265 | #endif // NDN_EXCLUDE_H |