blob: 7d533d0939f80cb8b13cc18932f7c2cdb8f3cbd1 [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 Thompsonec7789a2013-08-21 11:08:36 -070015#include <sstream>
Jeff Thompson995aba52013-09-12 12:04:52 -070016#include "util/blob.hpp"
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070017
Jeff Thompson25b4e612013-10-10 16:03:24 -070018struct ndn_NameComponent;
19struct ndn_Name;
20
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070021namespace ndn {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070022
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070023class Name {
24public:
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070025 /**
Jeff Thompson46411c92013-09-13 19:31:25 -070026 * A Name::Component is holds a read-only name component value.
Jeff Thompsonc1c12e42013-09-13 19:08:45 -070027 */
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070028 class Component {
29 public:
30 /**
Jeff Thompson46411c92013-09-13 19:31:25 -070031 * Create a new Name::Component with a null value.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070032 */
33 Component()
34 {
35 }
36
37 /**
38 * Create a new Name::Component, copying the given value.
39 * @param value The value byte array.
40 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070041 Component(const std::vector<uint8_t>& value)
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070042 : value_(value)
43 {
44 }
45
46 /**
47 * Create a new Name::Component, copying the given value.
48 * @param value Pointer to the value byte array.
49 * @param valueLen Length of value.
50 */
Jeff Thompson97223af2013-09-24 17:01:27 -070051 Component(const uint8_t *value, size_t valueLen)
Jeff Thompson995aba52013-09-12 12:04:52 -070052 : value_(value, valueLen)
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070053 {
54 }
Jeff Thompson0f743452013-09-12 14:23:18 -070055
56 /**
57 * Create a new Name::Component, taking another pointer to the Blob value.
58 * @param value A blob with a pointer to an immutable array. The pointer is copied.
59 */
60 Component(const Blob &value)
61 : value_(value)
62 {
63 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070064
65 /**
66 * Set the componentStruct to point to this component, without copying any memory.
67 * WARNING: The resulting pointer in componentStruct is invalid after a further use of this object which could reallocate memory.
68 * @param componentStruct The C ndn_NameComponent struct to receive the pointer.
69 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070070 void
Jeff Thompson25b4e612013-10-10 16:03:24 -070071 get(struct ndn_NameComponent& componentStruct) const;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070072
Jeff Thompson0050abe2013-09-17 12:50:25 -070073 const Blob&
74 getValue() const { return value_; }
Jeff Thompson6653b0b2013-09-23 12:32:39 -070075
76 /**
77 * Write this component value to result, escaping characters according to the NDN URI Scheme.
78 * This also adds "..." to a value with zero or more ".".
79 * @param value the buffer with the value to escape
80 * @param result the string stream to write to.
81 */
82 void
Jeff Thompsond0159d72013-09-23 13:34:15 -070083 toEscapedString(std::ostringstream& result) const
Jeff Thompson6653b0b2013-09-23 12:32:39 -070084 {
85 Name::toEscapedString(*value_, result);
86 }
87
88 /**
89 * Convert this component value by escaping characters according to the NDN URI Scheme.
90 * This also adds "..." to a value with zero or more ".".
91 * @return The escaped string.
92 */
93 std::string
Jeff Thompsond0159d72013-09-23 13:34:15 -070094 toEscapedString() const
Jeff Thompson6653b0b2013-09-23 12:32:39 -070095 {
96 return Name::toEscapedString(*value_);
97 }
Jeff Thompson9bdb3b22013-09-12 12:42:13 -070098
Jeff Thompson50b37912013-10-08 13:39:33 -070099 /**
100 * Interpret this name component as a network-ordered number and return an integer.
101 * @return The integer number.
102 */
103 uint64_t
Jeff Thompson25b4e612013-10-10 16:03:24 -0700104 toNumber() const;
Jeff Thompson27cae532013-10-08 12:52:41 -0700105
Jeff Thompson50b37912013-10-08 13:39:33 -0700106 /**
107 * Interpret this name component as a network-ordered number with a marker and return an integer.
108 * @param marker The required first byte of the component.
109 * @return The integer number.
110 * @throw runtime_error If the first byte of the component does not equal the marker.
111 */
112 uint64_t
113 toNumberWithMarker(uint8_t marker) const;
114
115 /**
116 * Interpret this name component as a segment number according to NDN name conventions (a network-ordered number
117 * where the first byte is the marker 0x00).
118 * @return The integer segment number.
119 * @throw runtime_error If the first byte of the component is not the expected marker.
120 */
121 uint64_t
122 toSegment() const
123 {
124 return toNumberWithMarker(0x00);
125 }
126
127 /**
128 * @deprecated Use toSegment.
129 */
130 uint64_t
131 toSeqNum() const
132 {
133 return toSegment();
134 }
135
136 /**
137 * Interpret this name component as a version number according to NDN name conventions (a network-ordered number
138 * where the first byte is the marker 0xFD). Note that this returns the exact number from the component
139 * without converting it to a time representation.
140 * @return The integer segment number.
141 * @throw runtime_error If the first byte of the component is not the expected marker.
142 */
143 uint64_t
144 toVersion() const
145 {
146 return toNumberWithMarker(0xFD);
147 }
Jeff Thompson27cae532013-10-08 12:52:41 -0700148
Jeff Thompsonc1c12e42013-09-13 19:08:45 -0700149 /**
Jeff Thompson46411c92013-09-13 19:31:25 -0700150 * Make a component value by decoding the escapedString between beginOffset and endOffset according to the NDN URI Scheme.
151 * If the escaped string is "", "." or ".." then return a Blob with a null pointer, which means this component value was not changed, and
Jeff Thompsonc1c12e42013-09-13 19:08:45 -0700152 * the component should be skipped in a URI name.
153 * @param escapedString The escaped string. It does not need to be null-terminated because we only scan to endOffset.
154 * @param beginOffset The offset in escapedString of the beginning of the portion to decode.
155 * @param endOffset The offset in escapedString of the end of the portion to decode.
Jeff Thompsond129ac12013-10-11 14:30:12 -0700156 * @return The component value. If the escapedString is not a valid escaped component, then the component value is a null pointer.
Jeff Thompsonc1c12e42013-09-13 19:08:45 -0700157 */
Jeff Thompsond129ac12013-10-11 14:30:12 -0700158 static Component
159 fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset);
160
161 /**
162 * Create a component whose value is the network-ordered encoding of the number.
163 * Note: if the number is zero, the result is empty.
164 * @param number The number to be encoded.
165 * @return The component value.
166 */
167 static Component
168 fromNumber(uint64_t number);
Jeff Thompson8aac1992013-08-12 17:26:02 -0700169
170 /**
Jeff Thompsond129ac12013-10-11 14:30:12 -0700171 * Create a component whose value is the marker appended with the network-ordered encoding of the number.
172 * Note: if the number is zero, no bytes are used for the number - the result will have only the marker.
173 * @param number The number to be encoded.
174 * @param marker The marker to use as the first byte of the component.
175 * @return The component value.
Jeff Thompson8aac1992013-08-12 17:26:02 -0700176 */
Jeff Thompsond129ac12013-10-11 14:30:12 -0700177 static Component
178 fromNumberWithMarker(uint64_t number, uint8_t marker);
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700179
180 private:
Jeff Thompson995aba52013-09-12 12:04:52 -0700181 Blob value_;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700182 };
183
Jeff Thompson443398d2013-07-02 19:45:46 -0700184 /**
185 * Create a new Name with no components.
186 */
Jeff Thompson016ed642013-07-02 14:39:06 -0700187 Name() {
188 }
Jeff Thompson443398d2013-07-02 19:45:46 -0700189
190 /**
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700191 * Create a new Name, copying the name components.
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700192 * @param components A vector of Component
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700193 */
Jeff Thompson1656e6a2013-08-29 18:01:48 -0700194 Name(const std::vector<Component>& components)
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700195 : components_(components)
196 {
197 }
198
199 /**
Jeff Thompson443398d2013-07-02 19:45:46 -0700200 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700201 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -0700202 */
Jeff Thompson3549ef32013-09-25 14:05:17 -0700203 Name(const char* uri)
Jeff Thompson67515bd2013-08-15 17:43:22 -0700204 {
205 set(uri);
206 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700207
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700208 /**
Jeff Thompson3549ef32013-09-25 14:05:17 -0700209 * Parse the uri according to the NDN URI Scheme and create the name with the components.
210 * @param uri The URI string.
211 */
212 Name(const std::string& uri)
213 {
214 set(uri.c_str());
215 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700216
Jeff Thompson3549ef32013-09-25 14:05:17 -0700217 /**
Jeff Thompson016ed642013-07-02 14:39:06 -0700218 * Set the nameStruct to point to the components in this name, without copying any memory.
219 * 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 -0700220 * @param nameStruct A C ndn_Name struct where the components array is already allocated.
Jeff Thompson016ed642013-07-02 14:39:06 -0700221 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700222 void
223 get(struct ndn_Name& nameStruct) const;
Jeff Thompson016ed642013-07-02 14:39:06 -0700224
225 /**
Jeff Thompson8b27e3a2013-07-03 18:19:53 -0700226 * Clear this name, and set the components by copying from the name struct.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700227 * @param nameStruct A C ndn_Name struct
Jeff Thompsonb468c312013-07-01 17:50:14 -0700228 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700229 void
230 set(const struct ndn_Name& nameStruct);
Jeff Thompsonb468c312013-07-01 17:50:14 -0700231
232 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700233 * Parse the uri according to the NDN URI Scheme and set the name with the components.
234 * @param uri The URI string.
235 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700236 void
237 set(const char *uri);
Jeff Thompson67515bd2013-08-15 17:43:22 -0700238
239 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700240 * Append a new component, copying from value of length valueLength.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700241 * @return This name so that you can chain calls to append.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700242 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700243 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700244 append(const uint8_t *value, size_t valueLength)
Jeff Thompson0f743452013-09-12 14:23:18 -0700245 {
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700246 components_.push_back(Component(value, valueLength));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700247 return *this;
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700248 }
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700249
250 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700251 * Append a new component, copying from value.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700252 * @return This name so that you can chain calls to append.
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700253 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700254 Name&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700255 append(const std::vector<uint8_t>& value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700256 {
257 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700258 return *this;
Jeff Thompson0f743452013-09-12 14:23:18 -0700259 }
260
Jeff Thompson26b0d792013-09-23 16:19:01 -0700261 Name&
262 append(const Blob &value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700263 {
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700264 components_.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700265 return *this;
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700266 }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700267
Jeff Thompson21eb7212013-09-26 09:05:40 -0700268 Name&
269 append(const Component &value)
270 {
271 components_.push_back(value);
272 return *this;
273 }
274
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700275 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700276 * Append the components of the given name to this name.
277 * @param name The Name with components to append.
278 * @return This name so that you can chain calls to append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700279 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700280 Name&
281 append(const Name& name);
282
283 /**
284 * @deprecated Use append.
285 */
286 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700287 appendComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700288 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700289 return append(value, valueLength);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700290 }
291
292 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700293 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700294 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700295 Name&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700296 appendComponent(const std::vector<uint8_t>& value)
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700297 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700298 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700299 }
300
301 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700302 * @deprecated Use append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700303 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700304 Name&
305 appendComponent(const Blob &value)
306 {
307 return append(value);
308 }
309
310 /**
311 * @deprecated Use append.
312 */
313 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700314 addComponent(const uint8_t *value, size_t valueLength)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700315 {
316 return append(value, valueLength);
317 }
318
319 /**
320 * @deprecated Use append.
321 */
322 Name&
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700323 addComponent(const std::vector<uint8_t>& value)
Jeff Thompson26b0d792013-09-23 16:19:01 -0700324 {
325 return append(value);
326 }
327
328 /**
329 * @deprecated Use append.
330 */
331 Name&
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700332 addComponent(const Blob &value)
333 {
Jeff Thompson26b0d792013-09-23 16:19:01 -0700334 return append(value);
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700335 }
336
337 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700338 * Clear all the components.
339 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700340 void
341 clear() {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700342 components_.clear();
343 }
344
345 /**
346 * Get the number of components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700347 * @return The number of components.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700348 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700349 size_t
Jeff Thompson0050abe2013-09-17 12:50:25 -0700350 getComponentCount() const {
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700351 return components_.size();
352 }
353
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700354 /**
355 * Get the component at the given index.
356 * @param i The index of the component, starting from 0.
357 * @return The name component at the index.
358 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700359 const Component&
Jeff Thompson97223af2013-09-24 17:01:27 -0700360 getComponent(size_t i) const { return components_[i]; }
Jeff Thompson443398d2013-07-02 19:45:46 -0700361
Jeff Thompsone6063512013-07-01 15:11:28 -0700362 /**
Jeff Thompsond0159d72013-09-23 13:34:15 -0700363 * Get a new name, constructed as a subset of components.
364 * @param iStartComponent The index if the first component to get.
365 * @param nComponents The number of components starting at iStartComponent.
366 * @return A new name.
367 */
368 Name
369 getSubName(size_t iStartComponent, size_t nComponents) const;
370
371 /**
372 * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name.
373 * @param iStartComponent The index if the first component to get.
374 * @return A new name.
375 */
376 Name
377 getSubName(size_t iStartComponent) const;
378
379 /**
380 * Return a new Name with the first nComponents components of this Name.
381 * @param nComponents The number of prefix components.
382 * @return A new Name.
383 */
384 Name
385 getPrefix(size_t nComponents) const
386 {
387 return getSubName(0, nComponents);
388 }
389
390 /**
Jeff Thompsone6063512013-07-01 15:11:28 -0700391 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700392 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700393 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700394 std::string
395 toUri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700396
Jeff Thompson21844fc2013-08-08 14:52:51 -0700397 /**
398 * @deprecated Use toUri().
399 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700400 std::string
401 to_uri() const
Jeff Thompson21844fc2013-08-08 14:52:51 -0700402 {
403 return toUri();
404 }
Jeff Thompson26b0d792013-09-23 16:19:01 -0700405
Jeff Thompson8aac1992013-08-12 17:26:02 -0700406 /**
407 * Append a component with the encoded segment number.
408 * @param segment The segment number.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700409 * @return This name so that you can chain calls to append.
410 */
411 Name&
Jeff Thompsond129ac12013-10-11 14:30:12 -0700412 appendSegment(uint64_t segment)
Jeff Thompson8aac1992013-08-12 17:26:02 -0700413 {
Jeff Thompsond129ac12013-10-11 14:30:12 -0700414 components_.push_back(Component::fromNumberWithMarker(segment, 0x00));
415 return *this;
416 }
417
418 /**
419 * Append a component with the encoded version number.
420 * Note that this encodes the exact value of version without converting from a time representation.
421 * @param version The version number.
422 * @return This name so that you can chain calls to append.
423 */
424 Name&
425 appendVersion(uint64_t version)
426 {
427 components_.push_back(Component::fromNumberWithMarker(version, 0xFD));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700428 return *this;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700429 }
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700430
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700431 /**
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700432 * Check if this name has the same component count and components as the given name.
433 * @param name The Name to check.
434 * @return true if the names are equal, otherwise false.
435 */
436 bool
437 equals(const Name& name) const;
438
439 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700440 * Check if the N components of this name are the same as the first N components of the given name.
441 * @param name The Name to check.
442 * @return true if this matches the given name, otherwise false. This always returns true if this name is empty.
443 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700444 bool
445 match(const Name& name) const;
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700446
447 /**
448 * Write the value to result, escaping characters according to the NDN URI Scheme.
449 * This also adds "..." to a value with zero or more ".".
450 * @param value the buffer with the value to escape
451 * @param result the string stream to write to.
452 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700453 static void
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700454 toEscapedString(const std::vector<uint8_t>& value, std::ostringstream& result);
Jeff Thompson21844fc2013-08-08 14:52:51 -0700455
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700456 /**
457 * Convert the value by escaping characters according to the NDN URI Scheme.
458 * This also adds "..." to a value with zero or more ".".
459 * @param value the buffer with the value to escape
460 * @return The escaped string.
461 */
462 static std::string
Jeff Thompson10ad12a2013-09-24 16:19:11 -0700463 toEscapedString(const std::vector<uint8_t>& value);
Jeff Thompson6653b0b2013-09-23 12:32:39 -0700464
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700465 //
466 // vector equivalent interface.
467 //
468
469 /**
470 * Get the number of components.
471 * @return The number of components.
472 */
473 size_t
474 size() const {
475 return getComponentCount();
476 }
477
478 /**
479 * Get the component at the given index.
480 * @param i The index of the component, starting from 0.
481 * @return The name component at the index.
482 */
483 const Component&
484 get(size_t i) const { return getComponent(i); }
485
486
487 const Component&
488 operator [] (int i) const
489 {
490 return get(i);
491 }
492
493 /**
494 * Append the component
495 * @param component The component of type T.
496 */
497 template<class T> void
498 push_back(const T &component)
499 {
500 append(component);
501 }
502
Jeff Thompson91737f52013-10-04 11:07:24 -0700503 /**
504 * Check if this name has the same component count and components as the given name.
505 * @param name The Name to check.
506 * @return true if the names are equal, otherwise false.
507 */
508 bool
509 operator == (const Name &name) const { return equals(name); }
510
511 /**
512 * Check if this name has the same component count and components as the given name.
513 * @param name The Name to check.
514 * @return true if the names are not equal, otherwise false.
515 */
516 bool
517 operator != (const Name &name) const { return !equals(name); }
518
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700519 //
520 // Iterator interface to name components.
521 //
522 typedef std::vector<Component>::iterator iterator;
523 typedef std::vector<Component>::const_iterator const_iterator;
524 typedef std::vector<Component>::reverse_iterator reverse_iterator;
525 typedef std::vector<Component>::const_reverse_iterator const_reverse_iterator;
526 typedef std::vector<Component>::reference reference;
527 typedef std::vector<Component>::const_reference const_reference;
528
529 typedef Component partial_type;
530
531 /**
532 * Begin iterator (const).
533 */
534 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700535 begin() const { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700536
537 /**
538 * Begin iterator.
539 */
540 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700541 begin() { return components_.begin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700542
543 /**
544 * End iterator (const).
545 */
546 const_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700547 end() const { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700548
549 /**
550 * End iterator.
551 */
552 iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700553 end() { return components_.end(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700554
555 /**
556 * Reverse begin iterator (const).
557 */
558 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700559 rbegin() const { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700560
561 /**
562 * Reverse begin iterator.
563 */
564 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700565 rbegin() { return components_.rbegin(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700566
567 /**
568 * Reverse end iterator (const).
569 */
570 const_reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700571 rend() const { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700572
573 /**
574 * Reverse end iterator.
575 */
576 reverse_iterator
Jeff Thompson91737f52013-10-04 11:07:24 -0700577 rend() { return components_.rend(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700578
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700579private:
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -0700580 std::vector<Component> components_;
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700581};
582
Jeff Thompson49e321a2013-10-04 17:35:59 -0700583inline std::ostream&
584operator << (std::ostream& os, const Name& name)
585{
586 os << name.toUri();
587 return os;
588}
589
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700590}
591
592#endif
593