Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 5 | * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 6 | * @author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 7 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #ifndef NDN_NAME_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 11 | #define NDN_NAME_HPP |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 12 | |
| 13 | #include <vector> |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 14 | #include <string> |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 15 | #include <sstream> |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 16 | #include <string.h> |
| 17 | #include "encoding/block.hpp" |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 18 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 19 | namespace ndn { |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 20 | |
Jeff Thompson | c7d6550 | 2013-11-06 17:22:26 -0800 | [diff] [blame] | 21 | /** |
| 22 | * A Name holds an array of Name::Component and represents an NDN name. |
| 23 | */ |
Alexander Afanasyev | b790d95 | 2014-01-24 12:07:53 -0800 | [diff] [blame] | 24 | class Name : public ptr_lib::enable_shared_from_this<Name> { |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 25 | public: |
Jeff Thompson | c1c12e4 | 2013-09-13 19:08:45 -0700 | [diff] [blame] | 26 | /** |
Jeff Thompson | c7d6550 | 2013-11-06 17:22:26 -0800 | [diff] [blame] | 27 | * A Name::Component holds a read-only name component value. |
Jeff Thompson | c1c12e4 | 2013-09-13 19:08:45 -0700 | [diff] [blame] | 28 | */ |
Alexander Afanasyev | 5874333 | 2014-01-12 13:24:42 -0800 | [diff] [blame] | 29 | class Component |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 30 | { |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 31 | public: |
| 32 | /** |
Jeff Thompson | 46411c9 | 2013-09-13 19:31:25 -0700 | [diff] [blame] | 33 | * Create a new Name::Component with a null value. |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 34 | */ |
| 35 | Component() |
| 36 | { |
| 37 | } |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 38 | |
| 39 | // copy constructor OK |
| 40 | |
| 41 | /** |
| 42 | * Create a new Name::Component, taking another pointer to the Blob value. |
| 43 | * @param value A blob with a pointer to an immutable array. The pointer is copied. |
| 44 | */ |
| 45 | Component(const ConstBufferPtr &buffer) |
Alexander Afanasyev | 5874333 | 2014-01-12 13:24:42 -0800 | [diff] [blame] | 46 | : value_ (buffer) |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 47 | { |
| 48 | } |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Create a new Name::Component, copying the given value. |
| 52 | * @param value The value byte array. |
| 53 | */ |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 54 | Component(const Buffer& value) |
Alexander Afanasyev | 5874333 | 2014-01-12 13:24:42 -0800 | [diff] [blame] | 55 | : value_ (new Buffer(value)) |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 56 | { |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Create a new Name::Component, copying the given value. |
| 61 | * @param value Pointer to the value byte array. |
| 62 | * @param valueLen Length of value. |
| 63 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 64 | Component(const uint8_t *value, size_t valueLen) |
Alexander Afanasyev | 5874333 | 2014-01-12 13:24:42 -0800 | [diff] [blame] | 65 | : value_ (new Buffer(value, valueLen)) |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 66 | { |
| 67 | } |
| 68 | |
| 69 | template<class InputIterator> |
| 70 | Component(InputIterator begin, InputIterator end) |
Alexander Afanasyev | 5874333 | 2014-01-12 13:24:42 -0800 | [diff] [blame] | 71 | : value_ (new Buffer(begin, end)) |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 72 | { |
| 73 | } |
Jeff Thompson | 0f74345 | 2013-09-12 14:23:18 -0700 | [diff] [blame] | 74 | |
Alexander Afanasyev | 848c61a | 2014-01-03 13:52:04 -0800 | [diff] [blame] | 75 | Component(const char *string) |
Alexander Afanasyev | 5874333 | 2014-01-12 13:24:42 -0800 | [diff] [blame] | 76 | : value_ (new Buffer(string, ::strlen(string))) |
Alexander Afanasyev | 848c61a | 2014-01-03 13:52:04 -0800 | [diff] [blame] | 77 | { |
| 78 | } |
| 79 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 80 | const Buffer& |
Alexander Afanasyev | 5874333 | 2014-01-12 13:24:42 -0800 | [diff] [blame] | 81 | getValue() const { return *value_; } |
Jeff Thompson | 6653b0b | 2013-09-23 12:32:39 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Write this component value to result, escaping characters according to the NDN URI Scheme. |
| 85 | * This also adds "..." to a value with zero or more ".". |
Jeff Thompson | 6653b0b | 2013-09-23 12:32:39 -0700 | [diff] [blame] | 86 | * @param result the string stream to write to. |
| 87 | */ |
| 88 | void |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 89 | toEscapedString(std::ostream& result) const |
Jeff Thompson | 6653b0b | 2013-09-23 12:32:39 -0700 | [diff] [blame] | 90 | { |
Alexander Afanasyev | 5874333 | 2014-01-12 13:24:42 -0800 | [diff] [blame] | 91 | Name::toEscapedString(*value_, result); |
Jeff Thompson | 6653b0b | 2013-09-23 12:32:39 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Convert this component value by escaping characters according to the NDN URI Scheme. |
| 96 | * This also adds "..." to a value with zero or more ".". |
| 97 | * @return The escaped string. |
| 98 | */ |
| 99 | std::string |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 100 | toEscapedString() const |
Jeff Thompson | 6653b0b | 2013-09-23 12:32:39 -0700 | [diff] [blame] | 101 | { |
Alexander Afanasyev | 5874333 | 2014-01-12 13:24:42 -0800 | [diff] [blame] | 102 | return Name::toEscapedString(*value_); |
Jeff Thompson | 6653b0b | 2013-09-23 12:32:39 -0700 | [diff] [blame] | 103 | } |
Jeff Thompson | 9bdb3b2 | 2013-09-12 12:42:13 -0700 | [diff] [blame] | 104 | |
Jeff Thompson | 50b3791 | 2013-10-08 13:39:33 -0700 | [diff] [blame] | 105 | /** |
| 106 | * Interpret this name component as a network-ordered number and return an integer. |
| 107 | * @return The integer number. |
| 108 | */ |
| 109 | uint64_t |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 110 | toNumber() const; |
Jeff Thompson | 27cae53 | 2013-10-08 12:52:41 -0700 | [diff] [blame] | 111 | |
Jeff Thompson | 50b3791 | 2013-10-08 13:39:33 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Interpret this name component as a network-ordered number with a marker and return an integer. |
| 114 | * @param marker The required first byte of the component. |
| 115 | * @return The integer number. |
| 116 | * @throw runtime_error If the first byte of the component does not equal the marker. |
| 117 | */ |
| 118 | uint64_t |
| 119 | toNumberWithMarker(uint8_t marker) const; |
| 120 | |
| 121 | /** |
| 122 | * Interpret this name component as a segment number according to NDN name conventions (a network-ordered number |
| 123 | * where the first byte is the marker 0x00). |
| 124 | * @return The integer segment number. |
| 125 | * @throw runtime_error If the first byte of the component is not the expected marker. |
| 126 | */ |
| 127 | uint64_t |
| 128 | toSegment() const |
| 129 | { |
| 130 | return toNumberWithMarker(0x00); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @deprecated Use toSegment. |
| 135 | */ |
| 136 | uint64_t |
| 137 | toSeqNum() const |
| 138 | { |
| 139 | return toSegment(); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Interpret this name component as a version number according to NDN name conventions (a network-ordered number |
| 144 | * where the first byte is the marker 0xFD). Note that this returns the exact number from the component |
| 145 | * without converting it to a time representation. |
| 146 | * @return The integer segment number. |
| 147 | * @throw runtime_error If the first byte of the component is not the expected marker. |
| 148 | */ |
| 149 | uint64_t |
| 150 | toVersion() const |
| 151 | { |
| 152 | return toNumberWithMarker(0xFD); |
| 153 | } |
Jeff Thompson | 27cae53 | 2013-10-08 12:52:41 -0700 | [diff] [blame] | 154 | |
Jeff Thompson | c1c12e4 | 2013-09-13 19:08:45 -0700 | [diff] [blame] | 155 | /** |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 156 | * Create a component whose value is the network-ordered encoding of the number. |
| 157 | * Note: if the number is zero, the result is empty. |
| 158 | * @param number The number to be encoded. |
| 159 | * @return The component value. |
| 160 | */ |
| 161 | static Component |
| 162 | fromNumber(uint64_t number); |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 163 | |
| 164 | /** |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 165 | * Create a component whose value is the marker appended with the network-ordered encoding of the number. |
| 166 | * Note: if the number is zero, no bytes are used for the number - the result will have only the marker. |
| 167 | * @param number The number to be encoded. |
| 168 | * @param marker The marker to use as the first byte of the component. |
| 169 | * @return The component value. |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 170 | */ |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 171 | static Component |
| 172 | fromNumberWithMarker(uint64_t number, uint8_t marker); |
Jeff Thompson | a98000c | 2013-12-16 14:40:09 -0800 | [diff] [blame] | 173 | |
| 174 | /** |
| 175 | * Check if this is the same component as other. |
| 176 | * @param other The other Component to compare with. |
| 177 | * @return true if the components are equal, otherwise false. |
| 178 | */ |
| 179 | bool |
| 180 | equals(const Component& other) const |
| 181 | { |
Alexander Afanasyev | 5874333 | 2014-01-12 13:24:42 -0800 | [diff] [blame] | 182 | return *value_ == *other.value_; |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | bool |
| 186 | empty() const |
| 187 | { |
Alexander Afanasyev | 5874333 | 2014-01-12 13:24:42 -0800 | [diff] [blame] | 188 | return !value_ || value_->empty(); |
Jeff Thompson | a98000c | 2013-12-16 14:40:09 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Check if this is the same component as other. |
| 193 | * @param other The other Component to compare with. |
| 194 | * @return true if the components are equal, otherwise false. |
| 195 | */ |
| 196 | bool |
| 197 | operator == (const Component& other) const { return equals(other); } |
| 198 | |
| 199 | /** |
| 200 | * Check if this is not the same component as other. |
| 201 | * @param other The other Component to compare with. |
| 202 | * @return true if the components are not equal, otherwise false. |
| 203 | */ |
| 204 | bool |
| 205 | operator != (const Component& other) const { return !equals(other); } |
| 206 | |
| 207 | /** |
| 208 | * Compare this to the other Component using NDN canonical ordering. |
| 209 | * @param other The other Component to compare with. |
| 210 | * @return 0 If they compare equal, -1 if *this comes before other in the canonical ordering, or |
| 211 | * 1 if *this comes after other in the canonical ordering. |
| 212 | * |
| 213 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 214 | */ |
| 215 | int |
| 216 | compare(const Component& other) const; |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 217 | |
Jeff Thompson | a98000c | 2013-12-16 14:40:09 -0800 | [diff] [blame] | 218 | /** |
| 219 | * Return true if this is less than or equal to the other Component in the NDN canonical ordering. |
| 220 | * @param other The other Component to compare with. |
| 221 | * |
| 222 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 223 | */ |
| 224 | bool |
| 225 | operator <= (const Component& other) const { return compare(other) <= 0; } |
| 226 | |
| 227 | /** |
| 228 | * Return true if this is less than the other Component in the NDN canonical ordering. |
| 229 | * @param other The other Component to compare with. |
| 230 | * |
| 231 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 232 | */ |
| 233 | bool |
| 234 | operator < (const Component& other) const { return compare(other) < 0; } |
| 235 | |
| 236 | /** |
| 237 | * Return true if this is less than or equal to the other Component in the NDN canonical ordering. |
| 238 | * @param other The other Component to compare with. |
| 239 | * |
| 240 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 241 | */ |
| 242 | bool |
| 243 | operator >= (const Component& other) const { return compare(other) >= 0; } |
| 244 | |
| 245 | /** |
| 246 | * Return true if this is greater than the other Component in the NDN canonical ordering. |
| 247 | * @param other The other Component to compare with. |
| 248 | * |
| 249 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 250 | */ |
| 251 | bool |
| 252 | operator > (const Component& other) const { return compare(other) > 0; } |
Alexander Afanasyev | 5874333 | 2014-01-12 13:24:42 -0800 | [diff] [blame] | 253 | private: |
| 254 | ConstBufferPtr value_; |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 255 | }; |
Jeff Thompson | a98000c | 2013-12-16 14:40:09 -0800 | [diff] [blame] | 256 | |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 257 | /** |
| 258 | * Create a new Name with no components. |
| 259 | */ |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 260 | Name() { |
| 261 | } |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 262 | |
| 263 | /** |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 264 | * Create a new Name, copying the name components. |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 265 | * @param components A vector of Component |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 266 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 267 | Name(const std::vector<Component>& components) |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 268 | : components_(components) |
| 269 | { |
| 270 | } |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 271 | |
| 272 | Name(const Block &name) |
| 273 | { |
| 274 | for (Block::element_const_iterator i = name.getAll().begin(); |
| 275 | i != name.getAll().end(); |
| 276 | ++i) |
| 277 | { |
| 278 | append(Component(i->value_begin(), i->value_end())); |
| 279 | } |
| 280 | } |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 281 | |
| 282 | /** |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 283 | * Parse the uri according to the NDN URI Scheme and create the name with the components. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 284 | * @param uri The URI string. |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 285 | */ |
Jeff Thompson | 3549ef3 | 2013-09-25 14:05:17 -0700 | [diff] [blame] | 286 | Name(const char* uri) |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 287 | { |
| 288 | set(uri); |
| 289 | } |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 290 | |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 291 | /** |
Jeff Thompson | 3549ef3 | 2013-09-25 14:05:17 -0700 | [diff] [blame] | 292 | * Parse the uri according to the NDN URI Scheme and create the name with the components. |
| 293 | * @param uri The URI string. |
| 294 | */ |
| 295 | Name(const std::string& uri) |
| 296 | { |
| 297 | set(uri.c_str()); |
| 298 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 299 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 300 | const Block & |
| 301 | wireEncode() const; |
Alexander Afanasyev | 848c61a | 2014-01-03 13:52:04 -0800 | [diff] [blame] | 302 | |
| 303 | void |
| 304 | wireDecode(const Block &wire); |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 305 | |
| 306 | /** |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 307 | * Parse the uri according to the NDN URI Scheme and set the name with the components. |
Jeff Thompson | 7781b39 | 2013-12-17 11:45:59 -0800 | [diff] [blame] | 308 | * @param uri The null-terminated URI string. |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 309 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 310 | void |
| 311 | set(const char *uri); |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 312 | |
| 313 | /** |
Jeff Thompson | 7781b39 | 2013-12-17 11:45:59 -0800 | [diff] [blame] | 314 | * Parse the uri according to the NDN URI Scheme and set the name with the components. |
| 315 | * @param uri The URI string. |
| 316 | */ |
| 317 | void |
| 318 | set(const std::string& uri) { set(uri.c_str()); } |
| 319 | |
| 320 | /** |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 321 | * Append a new component, copying from value of length valueLength. |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 322 | * @return This name so that you can chain calls to append. |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 323 | */ |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 324 | Name& |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 325 | append(const uint8_t *value, size_t valueLength) |
Jeff Thompson | 0f74345 | 2013-09-12 14:23:18 -0700 | [diff] [blame] | 326 | { |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 327 | components_.push_back(Component(value, valueLength)); |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 328 | return *this; |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 329 | } |
Jeff Thompson | f72b1ac | 2013-08-16 16:44:41 -0700 | [diff] [blame] | 330 | |
| 331 | /** |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 332 | * Append a new component, copying from value. |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 333 | * @return This name so that you can chain calls to append. |
Jeff Thompson | f72b1ac | 2013-08-16 16:44:41 -0700 | [diff] [blame] | 334 | */ |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 335 | Name& |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 336 | append(const Buffer& value) |
Jeff Thompson | 0f74345 | 2013-09-12 14:23:18 -0700 | [diff] [blame] | 337 | { |
| 338 | components_.push_back(value); |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 339 | return *this; |
Jeff Thompson | 0f74345 | 2013-09-12 14:23:18 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 342 | Name& |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 343 | append(const ConstBufferPtr &value) |
Jeff Thompson | 0f74345 | 2013-09-12 14:23:18 -0700 | [diff] [blame] | 344 | { |
Jeff Thompson | f72b1ac | 2013-08-16 16:44:41 -0700 | [diff] [blame] | 345 | components_.push_back(value); |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 346 | return *this; |
Jeff Thompson | f72b1ac | 2013-08-16 16:44:41 -0700 | [diff] [blame] | 347 | } |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 348 | |
Jeff Thompson | 21eb721 | 2013-09-26 09:05:40 -0700 | [diff] [blame] | 349 | Name& |
| 350 | append(const Component &value) |
| 351 | { |
| 352 | components_.push_back(value); |
| 353 | return *this; |
| 354 | } |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 355 | |
Alexander Afanasyev | 594cdb2 | 2014-01-03 15:11:33 -0800 | [diff] [blame] | 356 | /** |
| 357 | * @brief Append name component that represented as a string |
| 358 | * |
| 359 | * Note that this method is necessary to ensure correctness and unambiguity of |
| 360 | * ``append("string")`` operations (both Component and Name can be implicitly |
| 361 | * converted from string, each having different outcomes |
| 362 | */ |
| 363 | Name& |
| 364 | append(const char *value) |
| 365 | { |
| 366 | components_.push_back(Component(value)); |
| 367 | return *this; |
| 368 | } |
| 369 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 370 | Name& |
| 371 | append(const Block &value) |
| 372 | { |
| 373 | components_.push_back(Component(value.begin(), value.end())); |
| 374 | return *this; |
| 375 | } |
Jeff Thompson | 21eb721 | 2013-09-26 09:05:40 -0700 | [diff] [blame] | 376 | |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 377 | /** |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 378 | * Append the components of the given name to this name. |
| 379 | * @param name The Name with components to append. |
| 380 | * @return This name so that you can chain calls to append. |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 381 | */ |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 382 | Name& |
| 383 | append(const Name& name); |
| 384 | |
| 385 | /** |
| 386 | * @deprecated Use append. |
| 387 | */ |
| 388 | Name& |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 389 | appendComponent(const uint8_t *value, size_t valueLength) |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 390 | { |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 391 | return append(value, valueLength); |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /** |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 395 | * @deprecated Use append. |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 396 | */ |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 397 | Name& |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 398 | appendComponent(const Buffer& value) |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 399 | { |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 400 | return append(value); |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /** |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 404 | * @deprecated Use append. |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 405 | */ |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 406 | Name& |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 407 | appendComponent(const ConstBufferPtr &value) |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 408 | { |
| 409 | return append(value); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * @deprecated Use append. |
| 414 | */ |
| 415 | Name& |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 416 | addComponent(const uint8_t *value, size_t valueLength) |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 417 | { |
| 418 | return append(value, valueLength); |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * @deprecated Use append. |
| 423 | */ |
| 424 | Name& |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 425 | addComponent(const Buffer& value) |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 426 | { |
| 427 | return append(value); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * @deprecated Use append. |
| 432 | */ |
| 433 | Name& |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 434 | addComponent(const ConstBufferPtr &value) |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 435 | { |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 436 | return append(value); |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /** |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 440 | * Clear all the components. |
| 441 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 442 | void |
| 443 | clear() { |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 444 | components_.clear(); |
| 445 | } |
| 446 | |
| 447 | /** |
Jeff Thompson | eba62eb | 2013-10-30 13:24:22 -0700 | [diff] [blame] | 448 | * @deprecated use size(). |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 449 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 450 | size_t |
Jeff Thompson | eba62eb | 2013-10-30 13:24:22 -0700 | [diff] [blame] | 451 | getComponentCount() const { return size(); } |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 452 | |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 453 | /** |
Jeff Thompson | eba62eb | 2013-10-30 13:24:22 -0700 | [diff] [blame] | 454 | * @deprecated Use get(i). |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 455 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 456 | const Component& |
Jeff Thompson | eba62eb | 2013-10-30 13:24:22 -0700 | [diff] [blame] | 457 | getComponent(size_t i) const { return get(i); } |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 458 | |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 459 | /** |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 460 | * Get a new name, constructed as a subset of components. |
| 461 | * @param iStartComponent The index if the first component to get. |
| 462 | * @param nComponents The number of components starting at iStartComponent. |
| 463 | * @return A new name. |
| 464 | */ |
| 465 | Name |
| 466 | getSubName(size_t iStartComponent, size_t nComponents) const; |
| 467 | |
| 468 | /** |
| 469 | * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name. |
| 470 | * @param iStartComponent The index if the first component to get. |
| 471 | * @return A new name. |
| 472 | */ |
| 473 | Name |
| 474 | getSubName(size_t iStartComponent) const; |
| 475 | |
| 476 | /** |
| 477 | * Return a new Name with the first nComponents components of this Name. |
Jeff Thompson | eb0358f | 2013-12-17 10:59:53 -0800 | [diff] [blame] | 478 | * @param nComponents The number of prefix components. If nComponents is -N then return the prefix up |
| 479 | * to name.size() - N. For example getPrefix(-1) returns the name without the final component. |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 480 | * @return A new Name. |
| 481 | */ |
| 482 | Name |
Jeff Thompson | eb0358f | 2013-12-17 10:59:53 -0800 | [diff] [blame] | 483 | getPrefix(int nComponents) const |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 484 | { |
Jeff Thompson | eb0358f | 2013-12-17 10:59:53 -0800 | [diff] [blame] | 485 | if (nComponents < 0) |
| 486 | return getSubName(0, components_.size() + nComponents); |
| 487 | else |
| 488 | return getSubName(0, nComponents); |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /** |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 492 | * Encode this name as a URI. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 493 | * @return The encoded URI. |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 494 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 495 | std::string |
| 496 | toUri() const; |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 497 | |
Jeff Thompson | 21844fc | 2013-08-08 14:52:51 -0700 | [diff] [blame] | 498 | /** |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 499 | * Append a component with the encoded segment number. |
| 500 | * @param segment The segment number. |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 501 | * @return This name so that you can chain calls to append. |
| 502 | */ |
| 503 | Name& |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 504 | appendSegment(uint64_t segment) |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 505 | { |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 506 | components_.push_back(Component::fromNumberWithMarker(segment, 0x00)); |
| 507 | return *this; |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Append a component with the encoded version number. |
| 512 | * Note that this encodes the exact value of version without converting from a time representation. |
| 513 | * @param version The version number. |
| 514 | * @return This name so that you can chain calls to append. |
| 515 | */ |
| 516 | Name& |
| 517 | appendVersion(uint64_t version) |
| 518 | { |
| 519 | components_.push_back(Component::fromNumberWithMarker(version, 0xFD)); |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 520 | return *this; |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 521 | } |
Alexander Afanasyev | 594cdb2 | 2014-01-03 15:11:33 -0800 | [diff] [blame] | 522 | |
| 523 | /** |
| 524 | * @brief Append a component with the encoded version number. |
| 525 | * |
| 526 | * This version of the method creates version number based on the current timestamp |
| 527 | * @return This name so that you can chain calls to append. |
| 528 | */ |
| 529 | Name& |
| 530 | appendVersion(); |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 531 | |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 532 | /** |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 533 | * Check if this name has the same component count and components as the given name. |
| 534 | * @param name The Name to check. |
| 535 | * @return true if the names are equal, otherwise false. |
| 536 | */ |
| 537 | bool |
| 538 | equals(const Name& name) const; |
| 539 | |
| 540 | /** |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 541 | * Check if the N components of this name are the same as the first N components of the given name. |
| 542 | * @param name The Name to check. |
| 543 | * @return true if this matches the given name, otherwise false. This always returns true if this name is empty. |
| 544 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 545 | bool |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 546 | isPrefixOf(const Name& name) const; |
| 547 | |
| 548 | bool |
| 549 | match(const Name& name) const |
| 550 | { |
| 551 | return isPrefixOf(name); |
| 552 | } |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 553 | |
| 554 | /** |
Jeff Thompson | d8e53e6 | 2013-10-29 16:59:49 -0700 | [diff] [blame] | 555 | * Make a Blob value by decoding the escapedString between beginOffset and endOffset according to the NDN URI Scheme. |
Jeff Thompson | 0849359 | 2013-11-07 17:44:41 -0800 | [diff] [blame] | 556 | * If the escaped string is "", "." or ".." then return a Blob with a null pointer, |
| 557 | * which means the component should be skipped in a URI name. |
Jeff Thompson | d8e53e6 | 2013-10-29 16:59:49 -0700 | [diff] [blame] | 558 | * @param escapedString The escaped string. It does not need to be null-terminated because we only scan to endOffset. |
| 559 | * @param beginOffset The offset in escapedString of the beginning of the portion to decode. |
| 560 | * @param endOffset The offset in escapedString of the end of the portion to decode. |
| 561 | * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer. |
| 562 | */ |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 563 | static Component |
Jeff Thompson | d8e53e6 | 2013-10-29 16:59:49 -0700 | [diff] [blame] | 564 | fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset); |
| 565 | |
| 566 | /** |
| 567 | * Make a Blob value by decoding the escapedString according to the NDN URI Scheme. |
Jeff Thompson | 0849359 | 2013-11-07 17:44:41 -0800 | [diff] [blame] | 568 | * If the escaped string is "", "." or ".." then return a Blob with a null pointer, |
| 569 | * which means the component should be skipped in a URI name. |
Jeff Thompson | d8e53e6 | 2013-10-29 16:59:49 -0700 | [diff] [blame] | 570 | * @param escapedString The null-terminated escaped string. |
| 571 | * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer. |
| 572 | */ |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 573 | static Component |
Jeff Thompson | d8e53e6 | 2013-10-29 16:59:49 -0700 | [diff] [blame] | 574 | fromEscapedString(const char *escapedString); |
| 575 | |
| 576 | /** |
Jeff Thompson | 7781b39 | 2013-12-17 11:45:59 -0800 | [diff] [blame] | 577 | * Make a Blob value by decoding the escapedString according to the NDN URI Scheme. |
| 578 | * If the escaped string is "", "." or ".." then return a Blob with a null pointer, |
| 579 | * which means the component should be skipped in a URI name. |
| 580 | * @param escapedString The escaped string. |
| 581 | * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer. |
| 582 | */ |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 583 | static Component |
Jeff Thompson | 7781b39 | 2013-12-17 11:45:59 -0800 | [diff] [blame] | 584 | fromEscapedString(const std::string& escapedString) { return fromEscapedString(escapedString.c_str()); } |
| 585 | |
| 586 | /** |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 587 | * Write the value to result, escaping characters according to the NDN URI Scheme. |
| 588 | * This also adds "..." to a value with zero or more ".". |
| 589 | * @param value the buffer with the value to escape |
| 590 | * @param result the string stream to write to. |
| 591 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 592 | static void |
Alexander Afanasyev | 2a74276 | 2013-12-28 14:09:46 -0800 | [diff] [blame] | 593 | toEscapedString(const uint8_t *value, size_t valueSize, std::ostream& result); |
Jeff Thompson | 21844fc | 2013-08-08 14:52:51 -0700 | [diff] [blame] | 594 | |
Alexander Afanasyev | 2a74276 | 2013-12-28 14:09:46 -0800 | [diff] [blame] | 595 | inline static void |
| 596 | toEscapedString(const std::vector<uint8_t>& value, std::ostream& result) |
| 597 | { |
| 598 | toEscapedString(&*value.begin(), value.size(), result); |
| 599 | } |
| 600 | |
Jeff Thompson | 6653b0b | 2013-09-23 12:32:39 -0700 | [diff] [blame] | 601 | /** |
| 602 | * Convert the value by escaping characters according to the NDN URI Scheme. |
| 603 | * This also adds "..." to a value with zero or more ".". |
| 604 | * @param value the buffer with the value to escape |
| 605 | * @return The escaped string. |
| 606 | */ |
Alexander Afanasyev | 2a74276 | 2013-12-28 14:09:46 -0800 | [diff] [blame] | 607 | inline static std::string |
| 608 | toEscapedString(const uint8_t *value, size_t valueSize) |
| 609 | { |
| 610 | std::ostringstream result; |
| 611 | toEscapedString(value, valueSize, result); |
| 612 | return result.str(); |
| 613 | } |
Jeff Thompson | 6653b0b | 2013-09-23 12:32:39 -0700 | [diff] [blame] | 614 | |
Alexander Afanasyev | 2a74276 | 2013-12-28 14:09:46 -0800 | [diff] [blame] | 615 | static inline std::string |
| 616 | toEscapedString(const std::vector<uint8_t>& value) |
| 617 | { |
| 618 | return toEscapedString(&*value.begin(), value.size()); |
| 619 | } |
| 620 | |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 621 | // |
| 622 | // vector equivalent interface. |
| 623 | // |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 624 | |
| 625 | /** |
| 626 | * @brief Check if name is emtpy |
| 627 | */ |
| 628 | bool |
| 629 | empty() const { return components_.empty(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 630 | |
| 631 | /** |
| 632 | * Get the number of components. |
| 633 | * @return The number of components. |
| 634 | */ |
| 635 | size_t |
Jeff Thompson | eba62eb | 2013-10-30 13:24:22 -0700 | [diff] [blame] | 636 | size() const { return components_.size(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 637 | |
| 638 | /** |
| 639 | * Get the component at the given index. |
| 640 | * @param i The index of the component, starting from 0. |
| 641 | * @return The name component at the index. |
| 642 | */ |
| 643 | const Component& |
Alexander Afanasyev | 8f9aa8bed | 2013-12-30 15:58:57 -0800 | [diff] [blame] | 644 | get(ssize_t i) const |
| 645 | { |
| 646 | if (i >= 0) |
| 647 | return components_[i]; |
| 648 | else |
| 649 | return components_[size() + i]; |
| 650 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 651 | |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 652 | /** |
| 653 | * Compare this to the other Name using NDN canonical ordering. If the first components of each name are not equal, |
| 654 | * this returns -1 if the first comes before the second using the NDN canonical ordering for name components, or 1 if it comes after. |
| 655 | * If they are equal, this compares the second components of each name, etc. If both names are the same up to |
| 656 | * the size of the shorter name, this returns -1 if the first name is shorter than the second or 1 if it is longer. |
| 657 | * For example, if you std::sort gives: /a/b/d /a/b/cc /c /c/a /bb . This is intuitive because all names |
| 658 | * with the prefix /a are next to each other. But it may be also be counter-intuitive because /c comes before /bb |
| 659 | * according to NDN canonical ordering since it is shorter. |
| 660 | * @param other The other Name to compare with. |
| 661 | * @return 0 If they compare equal, -1 if *this comes before other in the canonical ordering, or |
| 662 | * 1 if *this comes after other in the canonical ordering. |
| 663 | * |
| 664 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 665 | */ |
| 666 | int |
| 667 | compare(const Name& other) const; |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 668 | |
| 669 | const Component& |
| 670 | operator [] (int i) const |
| 671 | { |
| 672 | return get(i); |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Append the component |
| 677 | * @param component The component of type T. |
| 678 | */ |
| 679 | template<class T> void |
| 680 | push_back(const T &component) |
| 681 | { |
| 682 | append(component); |
| 683 | } |
| 684 | |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 685 | /** |
| 686 | * Check if this name has the same component count and components as the given name. |
| 687 | * @param name The Name to check. |
| 688 | * @return true if the names are equal, otherwise false. |
| 689 | */ |
| 690 | bool |
| 691 | operator == (const Name &name) const { return equals(name); } |
| 692 | |
| 693 | /** |
| 694 | * Check if this name has the same component count and components as the given name. |
| 695 | * @param name The Name to check. |
| 696 | * @return true if the names are not equal, otherwise false. |
| 697 | */ |
| 698 | bool |
| 699 | operator != (const Name &name) const { return !equals(name); } |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 700 | |
| 701 | /** |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 702 | * Return true if this is less than or equal to the other Name in the NDN canonical ordering. |
| 703 | * @param other The other Name to compare with. |
| 704 | * |
| 705 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 706 | */ |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 707 | bool |
| 708 | operator <= (const Name& other) const { return compare(other) <= 0; } |
| 709 | |
| 710 | /** |
| 711 | * Return true if this is less than the other Name in the NDN canonical ordering. |
| 712 | * @param other The other Name to compare with. |
| 713 | * |
| 714 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 715 | */ |
| 716 | bool |
| 717 | operator < (const Name& other) const { return compare(other) < 0; } |
| 718 | |
| 719 | /** |
| 720 | * Return true if this is less than or equal to the other Name in the NDN canonical ordering. |
| 721 | * @param other The other Name to compare with. |
| 722 | * |
| 723 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 724 | */ |
| 725 | bool |
| 726 | operator >= (const Name& other) const { return compare(other) >= 0; } |
| 727 | |
| 728 | /** |
| 729 | * Return true if this is greater than the other Name in the NDN canonical ordering. |
| 730 | * @param other The other Name to compare with. |
| 731 | * |
| 732 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 733 | */ |
| 734 | bool |
| 735 | operator > (const Name& other) const { return compare(other) > 0; } |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 736 | |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 737 | // |
| 738 | // Iterator interface to name components. |
| 739 | // |
| 740 | typedef std::vector<Component>::iterator iterator; |
| 741 | typedef std::vector<Component>::const_iterator const_iterator; |
| 742 | typedef std::vector<Component>::reverse_iterator reverse_iterator; |
| 743 | typedef std::vector<Component>::const_reverse_iterator const_reverse_iterator; |
| 744 | typedef std::vector<Component>::reference reference; |
| 745 | typedef std::vector<Component>::const_reference const_reference; |
| 746 | |
Alexander Afanasyev | 5874333 | 2014-01-12 13:24:42 -0800 | [diff] [blame] | 747 | typedef std::vector<Component>::difference_type difference_type; |
| 748 | typedef std::vector<Component>::size_type size_type; |
| 749 | |
| 750 | typedef std::vector<Component>::value_type value_type; |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 751 | |
| 752 | /** |
| 753 | * Begin iterator (const). |
| 754 | */ |
| 755 | const_iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 756 | begin() const { return components_.begin(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 757 | |
| 758 | /** |
| 759 | * Begin iterator. |
| 760 | */ |
| 761 | iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 762 | begin() { return components_.begin(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 763 | |
| 764 | /** |
| 765 | * End iterator (const). |
| 766 | */ |
| 767 | const_iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 768 | end() const { return components_.end(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 769 | |
| 770 | /** |
| 771 | * End iterator. |
| 772 | */ |
| 773 | iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 774 | end() { return components_.end(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 775 | |
| 776 | /** |
| 777 | * Reverse begin iterator (const). |
| 778 | */ |
| 779 | const_reverse_iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 780 | rbegin() const { return components_.rbegin(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 781 | |
| 782 | /** |
| 783 | * Reverse begin iterator. |
| 784 | */ |
| 785 | reverse_iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 786 | rbegin() { return components_.rbegin(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 787 | |
| 788 | /** |
| 789 | * Reverse end iterator (const). |
| 790 | */ |
| 791 | const_reverse_iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 792 | rend() const { return components_.rend(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 793 | |
| 794 | /** |
| 795 | * Reverse end iterator. |
| 796 | */ |
| 797 | reverse_iterator |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 798 | rend() { return components_.rend(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 799 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 800 | private: |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 801 | std::vector<Component> components_; |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 802 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 803 | mutable Block wire_; |
| 804 | }; |
| 805 | |
| 806 | std::ostream & |
| 807 | operator << (std::ostream &os, const Name &name); |
| 808 | |
Alexander Afanasyev | 5874333 | 2014-01-12 13:24:42 -0800 | [diff] [blame] | 809 | inline std::ostream & |
| 810 | operator << (std::ostream &os, const Name::Component &component) |
| 811 | { |
| 812 | component.toEscapedString(os); |
| 813 | return os; |
| 814 | } |
| 815 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 816 | inline std::string |
| 817 | Name::toUri() const |
Jeff Thompson | 49e321a | 2013-10-04 17:35:59 -0700 | [diff] [blame] | 818 | { |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 819 | std::ostringstream os; |
| 820 | os << *this; |
| 821 | return os.str(); |
Jeff Thompson | 49e321a | 2013-10-04 17:35:59 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | #endif |
| 827 | |