blob: 04c89bc061f265fb4ceaaaf9a130cd28ab416fbe [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson47eecfc2013-07-07 22:56:46 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonec39fbd2013-10-04 10:56:23 -07005 * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
6 * @author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07007 * See COPYING for copyright and distribution information.
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07008 */
9
10#ifndef NDN_NAME_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070011#define NDN_NAME_HPP
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070012
13#include <vector>
Jeff Thompson443398d2013-07-02 19:45:46 -070014#include <string>
Jeff Thompson27b2bf92013-10-12 14:38:13 -070015#include <string.h>
Jeff Thompsonec7789a2013-08-21 11:08:36 -070016#include <sstream>
Jeff Thompson995aba52013-09-12 12:04:52 -070017#include "util/blob.hpp"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070018
Jeff Thompson25b4e612013-10-10 16:03:24 -070019struct ndn_NameComponent;
20struct ndn_Name;
21
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070022namespace ndn {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070023
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070024class Name {
25public:
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070026 /**
Jeff Thompson46411c92013-09-13 19:31:25 -070027 * A Name::Component is holds a read-only name component value.
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070028 */
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070029 class Component {
30 public:
31 /**
Jeff Thompson46411c92013-09-13 19:31:25 -070032 * Create a new Name::Component with a null value.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070033 */
34 Component()
35 {
36 }
37
38 /**
39 * Create a new Name::Component, copying the given value.
40 * @param value The value byte array.
41 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070042 Component(const std::vector<uint8_t>& value)
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070043 : value_(value)
44 {
45 }
46
47 /**
48 * Create a new Name::Component, copying the given value.
49 * @param value Pointer to the value byte array.
50 * @param valueLen Length of value.
51 */
Jeff Thompson97223af2013-09-24 17:01:27 -070052 Component(const uint8_t *value, size_t valueLen)
Jeff Thompson995aba52013-09-12 12:04:52 -070053 : value_(value, valueLen)
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070054 {
55 }
Jeff Thompson0f743452013-09-12 14:23:18 -070056
57 /**
58 * Create a new Name::Component, taking another pointer to the Blob value.
59 * @param value A blob with a pointer to an immutable array. The pointer is copied.
60 */
61 Component(const Blob &value)
62 : value_(value)
63 {
64 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070065
66 /**
67 * Set the componentStruct to point to this component, without copying any memory.
68 * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory.
69 * @param componentStruct The C ndn_NameComponent struct to receive the pointer.
70 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070071 void
Jeff Thompson25b4e612013-10-10 16:03:24 -070072 get(struct ndn_NameComponent& componentStruct) const;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070073
Jeff Thompson0050abe2013-09-17 12:50:25 -070074 const Blob&
75 getValue() const { return value_; }
Jeff Thompson6653b0b2013-09-23 12:32:39 -070076
77 /**
78 * Write this component value to result, escaping characters according to the NDN URI Scheme.
79 * This also adds "..." to a value with zero or more ".".
80 * @param value the buffer with the value to escape
81 * @param result the string stream to write to.
82 */
83 void
Jeff Thompsond0159d72013-09-23 13:34:15 -070084 toEscapedString(std::ostringstream& result) const
Jeff Thompson6653b0b2013-09-23 12:32:39 -070085 {
86 Name::toEscapedString(*value_, result);
87 }
88
89 /**
90 * Convert this component value by escaping characters according to the NDN URI Scheme.
91 * This also adds "..." to a value with zero or more ".".
92 * @return The escaped string.
93 */
94 std::string
Jeff Thompsond0159d72013-09-23 13:34:15 -070095 toEscapedString() const
Jeff Thompson6653b0b2013-09-23 12:32:39 -070096 {
97 return Name::toEscapedString(*value_);
98 }
Jeff Thompson9bdb3b22013-09-12 12:42:13 -070099
Jeff Thompson50b37912013-10-08 13:39:33 -0700100 /**
101 * Interpret this name component as a network-ordered number and return an integer.
102 * @return The integer number.
103 */
104 uint64_t
Jeff Thompson25b4e612013-10-10 16:03:24 -0700105 toNumber() const;
Jeff Thompson27cae532013-10-08 12:52:41 -0700106
Jeff Thompson50b37912013-10-08 13:39:33 -0700107 /**
108 * Interpret this name component as a network-ordered number with a marker and return an integer.
109 * @param marker The required first byte of the component.
110 * @return The integer number.
111 * @throw runtime_error If the first byte of the component does not equal the marker.
112 */
113 uint64_t
114 toNumberWithMarker(uint8_t marker) const;
115
116 /**
117 * Interpret this name component as a segment number according to NDN name conventions (a network-ordered number
118 * where the first byte is the marker 0x00).
119 * @return The integer segment number.
120 * @throw runtime_error If the first byte of the component is not the expected marker.
121 */
122 uint64_t
123 toSegment() const
124 {
125 return toNumberWithMarker(0x00);
126 }
127
128 /**
129 * @deprecated Use toSegment.
130 */
131 uint64_t
132 toSeqNum() const
133 {
134 return toSegment();
135 }
136
137 /**
138 * Interpret this name component as a version number according to NDN name conventions (a network-ordered number
139 * where the first byte is the marker 0xFD). Note that this returns the exact number from the component
140 * without converting it to a time representation.
141 * @return The integer segment number.
142 * @throw runtime_error If the first byte of the component is not the expected marker.
143 */
144 uint64_t
145 toVersion() const
146 {
147 return toNumberWithMarker(0xFD);
148 }
Jeff Thompson27cae532013-10-08 12:52:41 -0700149
Jeff Thompsonc1c12e42013-09-13 19:08:45 -0700150 /**
Jeff Thompsond129ac12013-10-11 14:30:12 -0700151 * Create a component whose value is the network-ordered encoding of the number.
152 * Note: if the number is zero, the result is empty.
153 * @param number The number to be encoded.
154 * @return The component value.
155 */
156 static Component
157 fromNumber(uint64_t number);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700158
159 /**
Jeff Thompsond129ac12013-10-11 14:30:12 -0700160 * Create a component whose value is the marker appended with the network-ordered encoding of the number.
161 * Note: if the number is zero, no bytes are used for the number - the result will have only the marker.
162 * @param number The number to be encoded.
163 * @param marker The marker to use as the first byte of the component.
164 * @return The component value.
Jeff Thompson8aac1992013-08-12 17:26:02 -0700165 */
Jeff Thompsond129ac12013-10-11 14:30:12 -0700166 static Component
167 fromNumberWithMarker(uint64_t number, uint8_t marker);
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700168
169 private:
Jeff Thompson995aba52013-09-12 12:04:52 -0700170 Blob value_;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700171 };
172
Jeff Thompson443398d2013-07-02 19:45:46 -0700173 /**
174 * Create a new Name with no components.
175 */
Jeff Thompson016ed642013-07-02 14:39:06 -0700176 Name() {
177 }
Jeff Thompson443398d2013-07-02 19:45:46 -0700178
179 /**
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700180 * Create a new Name, copying the name components.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700181 * @param components A vector of Component
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700182 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700183 Name(const std::vector<Component>& components)
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700184 : components_(components)
185 {
186 }
187
188 /**
Jeff Thompson443398d2013-07-02 19:45:46 -0700189 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700190 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -0700191 */
Jeff Thompson3549ef32013-09-25 14:05:17 -0700192 Name(const char* uri)
Jeff Thompson67515bd2013-08-15 17:43:22 -0700193 {
194 set(uri);
195 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700196
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700197 /**
Jeff Thompson3549ef32013-09-25 14:05:17 -0700198 * Parse the uri according to the NDN URI Scheme and create the name with the components.
199 * @param uri The URI string.
200 */
201 Name(const std::string& uri)
202 {
203 set(uri.c_str());
204 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700205
Jeff Thompson3549ef32013-09-25 14:05:17 -0700206 /**
Jeff Thompson016ed642013-07-02 14:39:06 -0700207 * Set the nameStruct to point to the components in this name, without copying any memory.
208 * WARNING: The resulting pointers in nameStruct are invalid after a further use of this object which could reallocate memory.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700209 * @param nameStruct A C ndn_Name struct where the components array is already allocated.
Jeff Thompson016ed642013-07-02 14:39:06 -0700210 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700211 void
212 get(struct ndn_Name& nameStruct) const;
Jeff Thompson016ed642013-07-02 14:39:06 -0700213
214 /**
Jeff Thompson8b27e3a2013-07-03 18:19:53 -0700215 * Clear this name, and set the components by copying from the name struct.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700216 * @param nameStruct A C ndn_Name struct
Jeff Thompsonb468c312013-07-01 17:50:14 -0700217 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700218 void
219 set(const struct ndn_Name& nameStruct);
Jeff Thompsonb468c312013-07-01 17:50:14 -0700220
221 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700222 * Parse the uri according to the NDN URI Scheme and set the name with the components.
223 * @param uri The URI string.
224 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700225 void
226 set(const char *uri);
Jeff Thompson67515bd2013-08-15 17:43:22 -0700227
228 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700229 * Append a new component, copying from value of length valueLength.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700230 * @return This name so that you can chain calls to append.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700231 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700232 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700233 append(const uint8_t *value, size_t valueLength)
Jeff Thompson0f743452013-09-12 14:23:18 -0700234 {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700235 components_.push_back(Component(value, valueLength));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700236 return *this;
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700237 }
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700238
239 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700240 * Append a new component, copying from value.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700241 * @return This name so that you can chain calls to append.
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700242 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700243 Name&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700244 append(const std::vector<uint8_t>& value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700245 {
246 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700247 return *this;
Jeff Thompson0f743452013-09-12 14:23:18 -0700248 }
249
Jeff Thompson26b0d792013-09-23 16:19:01 -0700250 Name&
251 append(const Blob &value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700252 {
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700253 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700254 return *this;
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700255 }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700256
Jeff Thompson21eb7212013-09-26 09:05:40 -0700257 Name&
258 append(const Component &value)
259 {
260 components_.push_back(value);
261 return *this;
262 }
263
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700264 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700265 * Append the components of the given name to this name.
266 * @param name The Name with components to append.
267 * @return This name so that you can chain calls to append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700268 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700269 Name&
270 append(const Name& name);
271
272 /**
273 * @deprecated Use append.
274 */
275 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700276 appendComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700277 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700278 return append(value, valueLength);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700279 }
280
281 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700282 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700283 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700284 Name&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700285 appendComponent(const std::vector<uint8_t>& value)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700286 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700287 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700288 }
289
290 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700291 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700292 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700293 Name&
294 appendComponent(const Blob &value)
295 {
296 return append(value);
297 }
298
299 /**
300 * @deprecated Use append.
301 */
302 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700303 addComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700304 {
305 return append(value, valueLength);
306 }
307
308 /**
309 * @deprecated Use append.
310 */
311 Name&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700312 addComponent(const std::vector<uint8_t>& value)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700313 {
314 return append(value);
315 }
316
317 /**
318 * @deprecated Use append.
319 */
320 Name&
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700321 addComponent(const Blob &value)
322 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700323 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700324 }
325
326 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700327 * Clear all the components.
328 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700329 void
330 clear() {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700331 components_.clear();
332 }
333
334 /**
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700335 * @deprecated use size().
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700336 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700337 size_t
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700338 getComponentCount() const { return size(); }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700339
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700340 /**
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700341 * @deprecated Use get(i).
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700342 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700343 const Component&
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700344 getComponent(size_t i) const { return get(i); }
Jeff Thompson443398d2013-07-02 19:45:46 -0700345
Jeff Thompsone6063512013-07-01 15:11:28 -0700346 /**
Jeff Thompsond0159d72013-09-23 13:34:15 -0700347 * Get a new name, constructed as a subset of components.
348 * @param iStartComponent The index if the first component to get.
349 * @param nComponents The number of components starting at iStartComponent.
350 * @return A new name.
351 */
352 Name
353 getSubName(size_t iStartComponent, size_t nComponents) const;
354
355 /**
356 * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name.
357 * @param iStartComponent The index if the first component to get.
358 * @return A new name.
359 */
360 Name
361 getSubName(size_t iStartComponent) const;
362
363 /**
364 * Return a new Name with the first nComponents components of this Name.
365 * @param nComponents The number of prefix components.
366 * @return A new Name.
367 */
368 Name
369 getPrefix(size_t nComponents) const
370 {
371 return getSubName(0, nComponents);
372 }
373
374 /**
Jeff Thompsone6063512013-07-01 15:11:28 -0700375 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700376 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700377 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700378 std::string
379 toUri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700380
Jeff Thompson21844fc2013-08-08 14:52:51 -0700381 /**
382 * @deprecated Use toUri().
383 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700384 std::string
385 to_uri() const
Jeff Thompson21844fc2013-08-08 14:52:51 -0700386 {
387 return toUri();
388 }
Jeff Thompson26b0d792013-09-23 16:19:01 -0700389
Jeff Thompson8aac1992013-08-12 17:26:02 -0700390 /**
391 * Append a component with the encoded segment number.
392 * @param segment The segment number.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700393 * @return This name so that you can chain calls to append.
394 */
395 Name&
Jeff Thompsond129ac12013-10-11 14:30:12 -0700396 appendSegment(uint64_t segment)
Jeff Thompson8aac1992013-08-12 17:26:02 -0700397 {
Jeff Thompsond129ac12013-10-11 14:30:12 -0700398 components_.push_back(Component::fromNumberWithMarker(segment, 0x00));
399 return *this;
400 }
401
402 /**
403 * Append a component with the encoded version number.
404 * Note that this encodes the exact value of version without converting from a time representation.
405 * @param version The version number.
406 * @return This name so that you can chain calls to append.
407 */
408 Name&
409 appendVersion(uint64_t version)
410 {
411 components_.push_back(Component::fromNumberWithMarker(version, 0xFD));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700412 return *this;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700413 }
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700414
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700415 /**
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700416 * Check if this name has the same component count and components as the given name.
417 * @param name The Name to check.
418 * @return true if the names are equal, otherwise false.
419 */
420 bool
421 equals(const Name& name) const;
422
423 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700424 * Check if the N components of this name are the same as the first N components of the given name.
425 * @param name The Name to check.
426 * @return true if this matches the given name, otherwise false. This always returns true if this name is empty.
427 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700428 bool
429 match(const Name& name) const;
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700430
431 /**
Jeff Thompsond8e53e62013-10-29 16:59:49 -0700432 * Make a Blob value by decoding the escapedString between beginOffset and endOffset according to the NDN URI Scheme.
433 * If the escaped string is "", "." or ".." then return a Blob with a null pointer, which means this component value was not changed, and
434 * the component should be skipped in a URI name.
435 * @param escapedString The escaped string. It does not need to be null-terminated because we only scan to endOffset.
436 * @param beginOffset The offset in escapedString of the beginning of the portion to decode.
437 * @param endOffset The offset in escapedString of the end of the portion to decode.
438 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
439 */
440 static Blob
441 fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset);
442
443 /**
444 * Make a Blob value by decoding the escapedString according to the NDN URI Scheme.
445 * If the escaped string is "", "." or ".." then return a Blob with a null pointer, which means this component value was not changed, and
446 * the component should be skipped in a URI name.
447 * @param escapedString The null-terminated escaped string.
448 * @return The Blob value. If the escapedString is not a valid escaped component, then the Blob is a null pointer.
449 */
450 static Blob
451 fromEscapedString(const char *escapedString);
452
453 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700454 * Write the value to result, escaping characters according to the NDN URI Scheme.
455 * This also adds "..." to a value with zero or more ".".
456 * @param value the buffer with the value to escape
457 * @param result the string stream to write to.
458 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700459 static void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700460 toEscapedString(const std::vector<uint8_t>& value, std::ostringstream& result);
Jeff Thompson21844fc2013-08-08 14:52:51 -0700461
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700462 /**
463 * Convert the value by escaping characters according to the NDN URI Scheme.
464 * This also adds "..." to a value with zero or more ".".
465 * @param value the buffer with the value to escape
466 * @return The escaped string.
467 */
468 static std::string
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700469 toEscapedString(const std::vector<uint8_t>& value);
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700470
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700471 //
472 // vector equivalent interface.
473 //
474
475 /**
476 * Get the number of components.
477 * @return The number of components.
478 */
479 size_t
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700480 size() const { return components_.size(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700481
482 /**
483 * Get the component at the given index.
484 * @param i The index of the component, starting from 0.
485 * @return The name component at the index.
486 */
487 const Component&
Jeff Thompsoneba62eb2013-10-30 13:24:22 -0700488 get(size_t i) const { return components_[i]; }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700489
490
491 const Component&
492 operator [] (int i) const
493 {
494 return get(i);
495 }
496
497 /**
498 * Append the component
499 * @param component The component of type T.
500 */
501 template<class T> void
502 push_back(const T &component)
503 {
504 append(component);
505 }
506
Jeff Thompson91737f52013-10-04 11:07:24 -0700507 /**
508 * Check if this name has the same component count and components as the given name.
509 * @param name The Name to check.
510 * @return true if the names are equal, otherwise false.
511 */
512 bool
513 operator == (const Name &name) const { return equals(name); }
514
515 /**
516 * Check if this name has the same component count and components as the given name.
517 * @param name The Name to check.
518 * @return true if the names are not equal, otherwise false.
519 */
520 bool
521 operator != (const Name &name) const { return !equals(name); }
522
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700523 //
524 // Iterator interface to name components.
525 //
526 typedef std::vector<Component>::iterator iterator;
527 typedef std::vector<Component>::const_iterator const_iterator;
528 typedef std::vector<Component>::reverse_iterator reverse_iterator;
529 typedef std::vector<Component>::const_reverse_iterator const_reverse_iterator;
530 typedef std::vector<Component>::reference reference;
531 typedef std::vector<Component>::const_reference const_reference;
532
533 typedef Component partial_type;
534
535 /**
536 * Begin iterator (const).
537 */
538 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700539 begin() const { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700540
541 /**
542 * Begin iterator.
543 */
544 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700545 begin() { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700546
547 /**
548 * End iterator (const).
549 */
550 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700551 end() const { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700552
553 /**
554 * End iterator.
555 */
556 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700557 end() { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700558
559 /**
560 * Reverse begin iterator (const).
561 */
562 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700563 rbegin() const { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700564
565 /**
566 * Reverse begin iterator.
567 */
568 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700569 rbegin() { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700570
571 /**
572 * Reverse end iterator (const).
573 */
574 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700575 rend() const { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700576
577 /**
578 * Reverse end iterator.
579 */
580 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700581 rend() { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700582
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700583private:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700584 std::vector<Component> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700585};
586
Jeff Thompson49e321a2013-10-04 17:35:59 -0700587inline std::ostream&
588operator << (std::ostream& os, const Name& name)
589{
590 os << name.toUri();
591 return os;
592}
593
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700594}
595
596#endif
597