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