Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -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 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
| 5 | * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 6 | * @author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 7 | * See COPYING for copyright and distribution information. |
| 8 | */ |
| 9 | |
| 10 | #ifndef NDN_NAME_COMPONENT_HPP |
| 11 | #define NDN_NAME_COMPONENT_HPP |
| 12 | |
| 13 | #include "common.hpp" |
| 14 | #include "encoding/block.hpp" |
| 15 | #include "encoding/encoding-buffer.hpp" |
| 16 | |
| 17 | namespace ndn { |
| 18 | namespace name { |
| 19 | |
| 20 | /** |
| 21 | * A Name::Component holds a read-only name component value. |
| 22 | */ |
| 23 | class Component : public Block |
| 24 | { |
| 25 | public: |
| 26 | /// @brief Error that can be thrown from the block |
| 27 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 28 | |
| 29 | /** |
| 30 | * Create a new Name::Component with a null value. |
| 31 | */ |
| 32 | Component() |
| 33 | : Block(Tlv::NameComponent) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | // copy constructor OK |
| 38 | |
| 39 | /** |
| 40 | * Create a new Name::Component, taking another pointer to the Blob value. |
| 41 | * @param value A blob with a pointer to an immutable array. The pointer is copied. |
| 42 | */ |
| 43 | Component(const ConstBufferPtr &buffer) |
| 44 | : Block (Tlv::NameComponent, buffer) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Create a new Name::Component, copying the given value. |
| 50 | * @param value The value byte array. |
| 51 | */ |
| 52 | Component(const Buffer& value) |
| 53 | : Block (Tlv::NameComponent, ConstBufferPtr(new Buffer(value))) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Create a new Name::Component, copying the given value. |
| 59 | * @param value Pointer to the value byte array. |
| 60 | * @param valueLen Length of value. |
| 61 | */ |
| 62 | Component(const uint8_t *value, size_t valueLen) |
| 63 | : Block (Tlv::NameComponent, ConstBufferPtr(new Buffer(value, valueLen))) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | template<class InputIterator> |
| 68 | Component(InputIterator begin, InputIterator end) |
| 69 | : Block (Tlv::NameComponent, ConstBufferPtr(new Buffer(begin, end))) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | Component(const char *string) |
| 74 | : Block (Tlv::NameComponent, ConstBufferPtr(new Buffer(string, ::strlen(string)))) |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @brief Fast encoding or block size estimation |
| 80 | */ |
| 81 | template<bool T> |
| 82 | size_t |
| 83 | wireEncode(EncodingImpl<T> &block) const; |
| 84 | |
| 85 | /** |
| 86 | * Make a Blob value by decoding the escapedString between beginOffset and endOffset according to the NDN URI Scheme. |
| 87 | * If the escaped string is "", "." or ".." then return a Blob with a null pointer, |
| 88 | * which means the component should be skipped in a URI name. |
| 89 | * @param escapedString The escaped string. It does not need to be null-terminated because we only scan to endOffset. |
| 90 | * @param beginOffset The offset in escapedString of the beginning of the portion to decode. |
| 91 | * @param endOffset The offset in escapedString of the end of the portion to decode. |
| 92 | * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer. |
| 93 | */ |
| 94 | static Component |
| 95 | fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset); |
| 96 | |
| 97 | /** |
| 98 | * Make a Blob value by decoding the escapedString according to the NDN URI Scheme. |
| 99 | * If the escaped string is "", "." or ".." then return a Blob with a null pointer, |
| 100 | * which means the component should be skipped in a URI name. |
| 101 | * @param escapedString The null-terminated escaped string. |
| 102 | * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer. |
| 103 | */ |
| 104 | static Component |
| 105 | fromEscapedString(const char *escapedString) |
| 106 | { |
| 107 | return fromEscapedString(escapedString, 0, ::strlen(escapedString)); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Make a Blob value by decoding the escapedString according to the NDN URI Scheme. |
| 112 | * If the escaped string is "", "." or ".." then return a Blob with a null pointer, |
| 113 | * which means the component should be skipped in a URI name. |
| 114 | * @param escapedString The escaped string. |
| 115 | * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer. |
| 116 | */ |
| 117 | static Component |
| 118 | fromEscapedString(const std::string& escapedString) |
| 119 | { |
| 120 | return fromEscapedString(escapedString.c_str()); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Write the value to result, escaping characters according to the NDN URI Scheme. |
| 125 | * This also adds "..." to a value with zero or more ".". |
| 126 | * @param value the buffer with the value to escape |
| 127 | * @param result the string stream to write to. |
| 128 | */ |
| 129 | void |
| 130 | toEscapedString(std::ostream& result) const; |
| 131 | |
| 132 | /** |
| 133 | * Convert the value by escaping characters according to the NDN URI Scheme. |
| 134 | * This also adds "..." to a value with zero or more ".". |
| 135 | * @param value the buffer with the value to escape |
| 136 | * @return The escaped string. |
| 137 | */ |
| 138 | inline std::string |
| 139 | toEscapedString() const |
| 140 | { |
| 141 | std::ostringstream result; |
| 142 | toEscapedString(result); |
| 143 | return result.str(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Interpret this name component as a network-ordered number and return an integer. |
| 148 | * @return The integer number. |
| 149 | */ |
| 150 | uint64_t |
| 151 | toNumber() const; |
| 152 | |
| 153 | /** |
| 154 | * Interpret this name component as a network-ordered number with a marker and return an integer. |
| 155 | * @param marker The required first byte of the component. |
| 156 | * @return The integer number. |
| 157 | * @throw runtime_error If the first byte of the component does not equal the marker. |
| 158 | */ |
| 159 | uint64_t |
| 160 | toNumberWithMarker(uint8_t marker) const; |
| 161 | |
| 162 | /** |
| 163 | * Interpret this name component as a segment number according to NDN name conventions (a network-ordered number |
| 164 | * where the first byte is the marker 0x00). |
| 165 | * @return The integer segment number. |
| 166 | * @throw runtime_error If the first byte of the component is not the expected marker. |
| 167 | */ |
| 168 | uint64_t |
| 169 | toSegment() const |
| 170 | { |
| 171 | return toNumberWithMarker(0x00); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @deprecated Use toSegment. |
| 176 | */ |
| 177 | uint64_t |
| 178 | toSeqNum() const |
| 179 | { |
| 180 | return toSegment(); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Interpret this name component as a version number according to NDN name conventions (a network-ordered number |
| 185 | * where the first byte is the marker 0xFD). Note that this returns the exact number from the component |
| 186 | * without converting it to a time representation. |
| 187 | * @return The integer segment number. |
| 188 | * @throw runtime_error If the first byte of the component is not the expected marker. |
| 189 | */ |
| 190 | uint64_t |
| 191 | toVersion() const |
| 192 | { |
| 193 | return toNumberWithMarker(0xFD); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Create a component whose value is the network-ordered encoding of the number. |
| 198 | * Note: if the number is zero, the result is empty. |
| 199 | * @param number The number to be encoded. |
| 200 | * @return The component value. |
| 201 | */ |
| 202 | static Component |
| 203 | fromNumber(uint64_t number); |
| 204 | |
| 205 | /** |
| 206 | * Create a component whose value is the marker appended with the network-ordered encoding of the number. |
| 207 | * Note: if the number is zero, no bytes are used for the number - the result will have only the marker. |
| 208 | * @param number The number to be encoded. |
| 209 | * @param marker The marker to use as the first byte of the component. |
| 210 | * @return The component value. |
| 211 | */ |
| 212 | static Component |
| 213 | fromNumberWithMarker(uint64_t number, uint8_t marker); |
| 214 | |
| 215 | /** |
| 216 | * Check if this is the same component as other. |
| 217 | * @param other The other Component to compare with. |
| 218 | * @return true if the components are equal, otherwise false. |
| 219 | */ |
| 220 | bool |
| 221 | equals(const Component& other) const |
| 222 | { |
| 223 | if (value_size() != other.value_size()) |
| 224 | return false; |
| 225 | |
| 226 | return std::equal(value_begin(), value_end(), other.value_begin()); |
| 227 | } |
| 228 | |
| 229 | bool |
| 230 | empty() const |
| 231 | { |
| 232 | return !hasValue(); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Check if this is the same component as other. |
| 237 | * @param other The other Component to compare with. |
| 238 | * @return true if the components are equal, otherwise false. |
| 239 | */ |
| 240 | bool |
| 241 | operator == (const Component& other) const { return equals(other); } |
| 242 | |
| 243 | /** |
| 244 | * Check if this is not the same component as other. |
| 245 | * @param other The other Component to compare with. |
| 246 | * @return true if the components are not equal, otherwise false. |
| 247 | */ |
| 248 | bool |
| 249 | operator != (const Component& other) const { return !equals(other); } |
| 250 | |
| 251 | /** |
| 252 | * Compare this to the other Component using NDN canonical ordering. |
| 253 | * @param other The other Component to compare with. |
| 254 | * @return 0 If they compare equal, -1 if *this comes before other in the canonical ordering, or |
| 255 | * 1 if *this comes after other in the canonical ordering. |
| 256 | * |
| 257 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 258 | */ |
| 259 | int |
| 260 | compare(const Component& other) const; |
| 261 | |
| 262 | /** |
| 263 | * Return true if this is less than or equal to the other Component in the NDN canonical ordering. |
| 264 | * @param other The other Component to compare with. |
| 265 | * |
| 266 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 267 | */ |
| 268 | bool |
| 269 | operator <= (const Component& other) const { return compare(other) <= 0; } |
| 270 | |
| 271 | /** |
| 272 | * Return true if this is less than the other Component in the NDN canonical ordering. |
| 273 | * @param other The other Component to compare with. |
| 274 | * |
| 275 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 276 | */ |
| 277 | bool |
| 278 | operator < (const Component& other) const { return compare(other) < 0; } |
| 279 | |
| 280 | /** |
| 281 | * Return true if this is less than or equal to the other Component in the NDN canonical ordering. |
| 282 | * @param other The other Component to compare with. |
| 283 | * |
| 284 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 285 | */ |
| 286 | bool |
| 287 | operator >= (const Component& other) const { return compare(other) >= 0; } |
| 288 | |
| 289 | /** |
| 290 | * Return true if this is greater than the other Component in the NDN canonical ordering. |
| 291 | * @param other The other Component to compare with. |
| 292 | * |
| 293 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 294 | */ |
| 295 | bool |
| 296 | operator > (const Component& other) const { return compare(other) > 0; } |
| 297 | |
| 298 | // |
| 299 | // !!! MUST NOT INCLUDE ANY DATA HERE !!! |
| 300 | // |
| 301 | // This class is just a helper and is directly reinterpret_cast'ed from Block |
| 302 | }; |
| 303 | |
| 304 | inline std::ostream & |
| 305 | operator << (std::ostream &os, const Component &component) |
| 306 | { |
| 307 | component.toEscapedString(os); |
| 308 | return os; |
| 309 | } |
| 310 | |
| 311 | template<bool T> |
| 312 | inline size_t |
| 313 | Component::wireEncode(EncodingImpl<T>& block) const |
| 314 | { |
| 315 | size_t total_len = 0; |
| 316 | total_len += block.prependByteArray (value(), value_size()); |
| 317 | total_len += block.prependVarNumber (value_size()); |
| 318 | total_len += block.prependVarNumber (Tlv::NameComponent); |
| 319 | return total_len; |
| 320 | } |
| 321 | |
| 322 | template |
| 323 | size_t |
| 324 | Component::wireEncode<true>(EncodingBuffer& block) const; |
| 325 | |
| 326 | template |
| 327 | size_t |
| 328 | Component::wireEncode<false>(EncodingEstimator& block) const; |
| 329 | |
| 330 | |
| 331 | } // namespace name |
| 332 | } // namespace ndn |
| 333 | |
| 334 | #endif // NDN_NAME_COMPONENT_HPP |