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 | |
| 20 | namespace error { |
| 21 | struct Exclude : public std::runtime_error { Exclude(const std::string &what) : std::runtime_error(what) {} }; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @brief Class to represent Exclude component in NDN interests |
| 26 | */ |
| 27 | class Exclude |
| 28 | { |
| 29 | public: |
| 30 | typedef std::map< Name::Component, bool /*any*/, std::greater<Name::Component> > exclude_type; |
| 31 | |
| 32 | typedef exclude_type::iterator iterator; |
| 33 | typedef exclude_type::const_iterator const_iterator; |
| 34 | typedef exclude_type::reverse_iterator reverse_iterator; |
| 35 | typedef exclude_type::const_reverse_iterator const_reverse_iterator; |
| 36 | |
| 37 | /** |
| 38 | * @brief Default constructor an empty exclude |
| 39 | */ |
| 40 | Exclude (); |
| 41 | |
| 42 | /** |
| 43 | * @brief Check if name component is excluded |
| 44 | * @param comp Name component to check against exclude filter |
| 45 | */ |
| 46 | bool |
| 47 | isExcluded (const Name::Component &comp) const; |
| 48 | |
| 49 | /** |
| 50 | * @brief Exclude specific name component |
| 51 | * @param comp component to exclude |
| 52 | * @returns *this to allow chaining |
| 53 | */ |
| 54 | Exclude & |
| 55 | excludeOne (const Name::Component &comp); |
| 56 | |
| 57 | /** |
| 58 | * @brief Exclude components from range [from, to] |
| 59 | * @param from first element of the range |
| 60 | * @param to last element of the range |
| 61 | * @returns *this to allow chaining |
| 62 | */ |
| 63 | Exclude & |
| 64 | excludeRange (const Name::Component &from, const Name::Component &to); |
| 65 | |
| 66 | /** |
| 67 | * @brief Exclude all components from range [/, to] |
| 68 | * @param to last element of the range |
| 69 | * @returns *this to allow chaining |
| 70 | */ |
| 71 | inline Exclude & |
| 72 | excludeBefore (const Name::Component &to); |
| 73 | |
| 74 | /** |
| 75 | * @brief Exclude all components from range [from, +Inf] |
| 76 | * @param to last element of the range |
| 77 | * @returns *this to allow chaining |
| 78 | */ |
| 79 | Exclude & |
| 80 | excludeAfter (const Name::Component &from); |
| 81 | |
| 82 | /** |
| 83 | * @brief Method to directly append exclude element |
| 84 | * @param name excluded name component |
| 85 | * @param any flag indicating if there is a postfix ANY component after the name |
| 86 | * |
| 87 | * This method is used during conversion from wire format of exclude filter |
| 88 | * |
| 89 | * If there is an error with ranges (e.g., order of components is wrong) an exception is thrown |
| 90 | */ |
| 91 | void |
| 92 | appendExclude (const Name::Component &name, bool any); |
| 93 | |
| 94 | /** |
| 95 | * @brief Check if exclude filter is empty |
| 96 | */ |
| 97 | inline bool |
| 98 | empty () const; |
| 99 | |
| 100 | /** |
| 101 | * @brief Get number of exclude terms |
| 102 | */ |
| 103 | inline size_t |
| 104 | size () const; |
| 105 | |
| 106 | /** |
| 107 | * @brief Get begin iterator of the exclude terms |
| 108 | */ |
| 109 | inline const_iterator |
| 110 | begin () const; |
| 111 | |
| 112 | /** |
| 113 | * @brief Get end iterator of the exclude terms |
| 114 | */ |
| 115 | inline const_iterator |
| 116 | end () const; |
| 117 | |
| 118 | /** |
| 119 | * @brief Get begin iterator of the exclude terms |
| 120 | */ |
| 121 | inline const_reverse_iterator |
| 122 | rbegin () const; |
| 123 | |
| 124 | /** |
| 125 | * @brief Get end iterator of the exclude terms |
| 126 | */ |
| 127 | inline const_reverse_iterator |
| 128 | rend () const; |
| 129 | |
| 130 | /** |
| 131 | * @brief Get escaped string representation (e.g., for use in URI) of the exclude filter |
| 132 | */ |
| 133 | inline std::string |
| 134 | toUri () const; |
| 135 | |
| 136 | private: |
| 137 | Exclude & |
| 138 | excludeRange (iterator fromLowerBound, iterator toLowerBound); |
| 139 | |
| 140 | private: |
| 141 | exclude_type m_exclude; |
| 142 | }; |
| 143 | |
| 144 | std::ostream& |
| 145 | operator << (std::ostream &os, const Exclude &name); |
| 146 | |
| 147 | inline Exclude & |
| 148 | Exclude::excludeBefore (const Name::Component &to) |
| 149 | { |
| 150 | return excludeRange (Name::Component (), to); |
| 151 | } |
| 152 | |
| 153 | inline bool |
| 154 | Exclude::empty () const |
| 155 | { |
| 156 | return m_exclude.empty (); |
| 157 | } |
| 158 | |
| 159 | inline size_t |
| 160 | Exclude::size () const |
| 161 | { |
| 162 | return m_exclude.size (); |
| 163 | } |
| 164 | |
| 165 | inline Exclude::const_iterator |
| 166 | Exclude::begin () const |
| 167 | { |
| 168 | return m_exclude.begin (); |
| 169 | } |
| 170 | |
| 171 | inline Exclude::const_iterator |
| 172 | Exclude::end () const |
| 173 | { |
| 174 | return m_exclude.end (); |
| 175 | } |
| 176 | |
| 177 | inline Exclude::const_reverse_iterator |
| 178 | Exclude::rbegin () const |
| 179 | { |
| 180 | return m_exclude.rbegin (); |
| 181 | } |
| 182 | |
| 183 | inline Exclude::const_reverse_iterator |
| 184 | Exclude::rend () const |
| 185 | { |
| 186 | return m_exclude.rend (); |
| 187 | } |
| 188 | |
| 189 | inline std::string |
| 190 | Exclude::toUri () const |
| 191 | { |
| 192 | std::ostringstream os; |
| 193 | os << *this; |
| 194 | return os.str(); |
| 195 | } |
| 196 | |
| 197 | } // ndn |
| 198 | |
| 199 | #endif // NDN_EXCLUDE_H |