Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 18 | namespace ndn { |
| 19 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 20 | /** |
| 21 | * @brief Class to represent Exclude component in NDN interests |
| 22 | */ |
| 23 | class Exclude |
| 24 | { |
| 25 | public: |
Alexander Afanasyev | 993a0e1 | 2014-01-03 13:51:37 -0800 | [diff] [blame] | 26 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 27 | |
| 28 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 29 | 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 Afanasyev | 993a0e1 | 2014-01-03 13:51:37 -0800 | [diff] [blame] | 90 | inline void |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 91 | 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 Afanasyev | 8548084 | 2014-01-06 14:46:54 -0800 | [diff] [blame^] | 98 | |
| 99 | /** |
| 100 | * @brief Clear the exclude filter |
| 101 | */ |
| 102 | inline void |
| 103 | clear(); |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 104 | |
| 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 Afanasyev | 993a0e1 | 2014-01-03 13:51:37 -0800 | [diff] [blame] | 140 | |
| 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 Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 154 | |
| 155 | private: |
| 156 | Exclude & |
| 157 | excludeRange (iterator fromLowerBound, iterator toLowerBound); |
| 158 | |
| 159 | private: |
| 160 | exclude_type m_exclude; |
Alexander Afanasyev | 993a0e1 | 2014-01-03 13:51:37 -0800 | [diff] [blame] | 161 | |
| 162 | mutable Block wire_; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | std::ostream& |
| 166 | operator << (std::ostream &os, const Exclude &name); |
| 167 | |
| 168 | inline Exclude & |
| 169 | Exclude::excludeBefore (const Name::Component &to) |
| 170 | { |
| 171 | return excludeRange (Name::Component (), to); |
| 172 | } |
| 173 | |
Alexander Afanasyev | 993a0e1 | 2014-01-03 13:51:37 -0800 | [diff] [blame] | 174 | inline void |
| 175 | Exclude::appendExclude (const Name::Component &name, bool any) |
| 176 | { |
| 177 | m_exclude[name] = any; |
| 178 | } |
| 179 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 180 | inline bool |
| 181 | Exclude::empty () const |
| 182 | { |
| 183 | return m_exclude.empty (); |
| 184 | } |
| 185 | |
Alexander Afanasyev | 8548084 | 2014-01-06 14:46:54 -0800 | [diff] [blame^] | 186 | inline void |
| 187 | Exclude::clear () |
| 188 | { |
| 189 | m_exclude.clear (); |
| 190 | } |
| 191 | |
| 192 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 193 | inline size_t |
| 194 | Exclude::size () const |
| 195 | { |
| 196 | return m_exclude.size (); |
| 197 | } |
| 198 | |
| 199 | inline Exclude::const_iterator |
| 200 | Exclude::begin () const |
| 201 | { |
| 202 | return m_exclude.begin (); |
| 203 | } |
| 204 | |
| 205 | inline Exclude::const_iterator |
| 206 | Exclude::end () const |
| 207 | { |
| 208 | return m_exclude.end (); |
| 209 | } |
| 210 | |
| 211 | inline Exclude::const_reverse_iterator |
| 212 | Exclude::rbegin () const |
| 213 | { |
| 214 | return m_exclude.rbegin (); |
| 215 | } |
| 216 | |
| 217 | inline Exclude::const_reverse_iterator |
| 218 | Exclude::rend () const |
| 219 | { |
| 220 | return m_exclude.rend (); |
| 221 | } |
| 222 | |
| 223 | inline std::string |
| 224 | Exclude::toUri () const |
| 225 | { |
| 226 | std::ostringstream os; |
| 227 | os << *this; |
| 228 | return os.str(); |
| 229 | } |
| 230 | |
| 231 | } // ndn |
| 232 | |
| 233 | #endif // NDN_EXCLUDE_H |