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; |
| 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 Afanasyev | 993a0e1 | 2014-01-03 13:51:37 -0800 | [diff] [blame] | 134 | |
| 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 Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 148 | |
| 149 | private: |
| 150 | Exclude & |
| 151 | excludeRange (iterator fromLowerBound, iterator toLowerBound); |
| 152 | |
| 153 | private: |
| 154 | exclude_type m_exclude; |
Alexander Afanasyev | 993a0e1 | 2014-01-03 13:51:37 -0800 | [diff] [blame] | 155 | |
| 156 | mutable Block wire_; |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | std::ostream& |
| 160 | operator << (std::ostream &os, const Exclude &name); |
| 161 | |
| 162 | inline Exclude & |
| 163 | Exclude::excludeBefore (const Name::Component &to) |
| 164 | { |
| 165 | return excludeRange (Name::Component (), to); |
| 166 | } |
| 167 | |
Alexander Afanasyev | 993a0e1 | 2014-01-03 13:51:37 -0800 | [diff] [blame] | 168 | inline void |
| 169 | Exclude::appendExclude (const Name::Component &name, bool any) |
| 170 | { |
| 171 | m_exclude[name] = any; |
| 172 | } |
| 173 | |
Alexander Afanasyev | b3a6af4 | 2014-01-03 13:08:28 -0800 | [diff] [blame] | 174 | inline bool |
| 175 | Exclude::empty () const |
| 176 | { |
| 177 | return m_exclude.empty (); |
| 178 | } |
| 179 | |
| 180 | inline size_t |
| 181 | Exclude::size () const |
| 182 | { |
| 183 | return m_exclude.size (); |
| 184 | } |
| 185 | |
| 186 | inline Exclude::const_iterator |
| 187 | Exclude::begin () const |
| 188 | { |
| 189 | return m_exclude.begin (); |
| 190 | } |
| 191 | |
| 192 | inline Exclude::const_iterator |
| 193 | Exclude::end () const |
| 194 | { |
| 195 | return m_exclude.end (); |
| 196 | } |
| 197 | |
| 198 | inline Exclude::const_reverse_iterator |
| 199 | Exclude::rbegin () const |
| 200 | { |
| 201 | return m_exclude.rbegin (); |
| 202 | } |
| 203 | |
| 204 | inline Exclude::const_reverse_iterator |
| 205 | Exclude::rend () const |
| 206 | { |
| 207 | return m_exclude.rend (); |
| 208 | } |
| 209 | |
| 210 | inline std::string |
| 211 | Exclude::toUri () const |
| 212 | { |
| 213 | std::ostringstream os; |
| 214 | os << *this; |
| 215 | return os.str(); |
| 216 | } |
| 217 | |
| 218 | } // ndn |
| 219 | |
| 220 | #endif // NDN_EXCLUDE_H |