blob: 6d7582f24159f0b378ace25b97df735855afdcfb [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
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080013#include "common.hpp"
14#include "name-component.hpp"
15
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080016#include "encoding/block.hpp"
Wentao Shang77949212014-02-01 23:42:24 -080017#include "encoding/encoding-buffer.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070018
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080019#include <boost/iterator/reverse_iterator.hpp>
20
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070021namespace ndn {
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080022
Jeff Thompsonc7d65502013-11-06 17:22:26 -080023/**
24 * A Name holds an array of Name::Component and represents an NDN name.
25 */
Alexander Afanasyevb790d952014-01-24 12:07:53 -080026class Name : public ptr_lib::enable_shared_from_this<Name> {
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070027public:
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080028 /// @brief Error that can be thrown from the block
29 struct Error : public name::Component::Error { Error(const std::string &what) : name::Component::Error(what) {} };
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080030
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080031 typedef name::Component Component;
32
33 typedef std::vector<Component> component_container;
34
35 typedef Component value_type;
36 typedef void allocator_type;
37 typedef Component& reference;
38 typedef const Component const_reference;
39 typedef Component* pointer;
40 typedef const Component* const_pointer;
41 typedef Component* iterator;
42 typedef const Component* const_iterator;
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080043
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080044 typedef boost::reverse_iterator<iterator> reverse_iterator;
45 typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
46
47 typedef component_container::difference_type difference_type;
48 typedef component_container::size_type size_type;
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070049
Jeff Thompson443398d2013-07-02 19:45:46 -070050 /**
51 * Create a new Name with no components.
52 */
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080053 Name()
54 : m_nameBlock(Tlv::Name)
55 {
Jeff Thompson016ed642013-07-02 14:39:06 -070056 }
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080057
58 /**
59 * @brief Create Name object from wire block
60 *
61 * This is a more efficient equivalent for
62 * @code
63 * Name name;
64 * name.wireDecode(wire);
65 * @endcode
66 */
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080067 explicit
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080068 Name(const Block &wire)
69 {
70 m_nameBlock = wire;
71 m_nameBlock.parse();
72 }
Jeff Thompson3f2175b2013-07-31 17:12:47 -070073
74 /**
Jeff Thompson443398d2013-07-02 19:45:46 -070075 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -070076 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -070077 */
Jeff Thompson3549ef32013-09-25 14:05:17 -070078 Name(const char* uri)
Jeff Thompson67515bd2013-08-15 17:43:22 -070079 {
80 set(uri);
81 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070082
Jeff Thompsone5f839b2013-06-28 12:50:38 -070083 /**
Jeff Thompson3549ef32013-09-25 14:05:17 -070084 * Parse the uri according to the NDN URI Scheme and create the name with the components.
85 * @param uri The URI string.
86 */
87 Name(const std::string& uri)
88 {
89 set(uri.c_str());
90 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -070091
Alexander Afanasyev380420b2014-02-09 20:52:29 -080092 /**
93 * @brief Fast encoding or block size estimation
94 */
95 template<bool T>
Wentao Shang77949212014-02-01 23:42:24 -080096 size_t
Alexander Afanasyev380420b2014-02-09 20:52:29 -080097 wireEncode(EncodingImpl<T> &block) const;
Wentao Shang77949212014-02-01 23:42:24 -080098
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080099 const Block &
100 wireEncode() const;
Alexander Afanasyev848c61a2014-01-03 13:52:04 -0800101
102 void
103 wireDecode(const Block &wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800104
105 /**
106 * @brief Check if already has wire
107 */
108 bool
109 hasWire() const;
110
Jeff Thompsonb468c312013-07-01 17:50:14 -0700111 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700112 * Parse the uri according to the NDN URI Scheme and set the name with the components.
Jeff Thompson7781b392013-12-17 11:45:59 -0800113 * @param uri The null-terminated URI string.
Jeff Thompson67515bd2013-08-15 17:43:22 -0700114 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700115 void
116 set(const char *uri);
Jeff Thompson67515bd2013-08-15 17:43:22 -0700117
118 /**
Jeff Thompson7781b392013-12-17 11:45:59 -0800119 * Parse the uri according to the NDN URI Scheme and set the name with the components.
120 * @param uri The URI string.
121 */
122 void
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800123 set(const std::string& uri)
124 {
125 set(uri.c_str());
126 }
Jeff Thompson7781b392013-12-17 11:45:59 -0800127
128 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700129 * Append a new component, copying from value of length valueLength.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700130 * @return This name so that you can chain calls to append.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700131 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700132 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700133 append(const uint8_t *value, size_t valueLength)
Jeff Thompson0f743452013-09-12 14:23:18 -0700134 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800135 m_nameBlock.push_back(Component(value, valueLength));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700136 return *this;
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700137 }
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700138
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800139 /**
140 * Append a new component, copying from value of length valueLength.
141 * @return This name so that you can chain calls to append.
142 */
143 template<class InputIterator>
144 Name&
145 append(InputIterator begin, InputIterator end)
146 {
147 m_nameBlock.push_back(Component(begin, end));
148 return *this;
149 }
150
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800151 // /**
152 // * Append a new component, copying from value.
153 // * @return This name so that you can chain calls to append.
154 // */
155 // Name&
156 // append(const Buffer& value)
157 // {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800158 // m_nameBlock.push_back(value);
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800159 // return *this;
160 // }
Jeff Thompson0f743452013-09-12 14:23:18 -0700161
Jeff Thompson26b0d792013-09-23 16:19:01 -0700162 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800163 append(const ConstBufferPtr &value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700164 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800165 m_nameBlock.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700166 return *this;
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700167 }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700168
Jeff Thompson21eb7212013-09-26 09:05:40 -0700169 Name&
170 append(const Component &value)
171 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800172 m_nameBlock.push_back(value);
Jeff Thompson21eb7212013-09-26 09:05:40 -0700173 return *this;
174 }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800175
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800176 /**
177 * @brief Append name component that represented as a string
178 *
179 * Note that this method is necessary to ensure correctness and unambiguity of
180 * ``append("string")`` operations (both Component and Name can be implicitly
181 * converted from string, each having different outcomes
182 */
183 Name&
184 append(const char *value)
185 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800186 m_nameBlock.push_back(Component(value));
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800187 return *this;
188 }
189
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800190 Name&
191 append(const Block &value)
192 {
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800193 if (value.type() == Tlv::NameComponent)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800194 m_nameBlock.push_back(value);
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800195 else
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800196 m_nameBlock.push_back(Block(Tlv::NameComponent, value));
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800197
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800198 return *this;
199 }
Jeff Thompson21eb7212013-09-26 09:05:40 -0700200
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700201 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700202 * Append the components of the given name to this name.
203 * @param name The Name with components to append.
204 * @return This name so that you can chain calls to append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700205 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700206 Name&
207 append(const Name& name);
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800208
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700209 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700210 * Clear all the components.
211 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700212 void
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800213 clear()
214 {
215 m_nameBlock = Block(Tlv::Name);
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700216 }
217
218 /**
Jeff Thompsond0159d72013-09-23 13:34:15 -0700219 * Get a new name, constructed as a subset of components.
220 * @param iStartComponent The index if the first component to get.
221 * @param nComponents The number of components starting at iStartComponent.
222 * @return A new name.
223 */
224 Name
225 getSubName(size_t iStartComponent, size_t nComponents) const;
226
227 /**
228 * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name.
229 * @param iStartComponent The index if the first component to get.
230 * @return A new name.
231 */
232 Name
233 getSubName(size_t iStartComponent) const;
234
235 /**
236 * Return a new Name with the first nComponents components of this Name.
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800237 * @param nComponents The number of prefix components. If nComponents is -N then return the prefix up
238 * to name.size() - N. For example getPrefix(-1) returns the name without the final component.
Jeff Thompsond0159d72013-09-23 13:34:15 -0700239 * @return A new Name.
240 */
241 Name
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800242 getPrefix(int nComponents) const
Jeff Thompsond0159d72013-09-23 13:34:15 -0700243 {
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800244 if (nComponents < 0)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800245 return getSubName(0, m_nameBlock.elements_size() + nComponents);
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800246 else
247 return getSubName(0, nComponents);
Jeff Thompsond0159d72013-09-23 13:34:15 -0700248 }
249
250 /**
Jeff Thompsone6063512013-07-01 15:11:28 -0700251 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700252 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700253 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700254 std::string
255 toUri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700256
Jeff Thompson21844fc2013-08-08 14:52:51 -0700257 /**
Jeff Thompson8aac1992013-08-12 17:26:02 -0700258 * Append a component with the encoded segment number.
259 * @param segment The segment number.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700260 * @return This name so that you can chain calls to append.
261 */
262 Name&
Jeff Thompsond129ac12013-10-11 14:30:12 -0700263 appendSegment(uint64_t segment)
Jeff Thompson8aac1992013-08-12 17:26:02 -0700264 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800265 m_nameBlock.push_back(Component::fromNumberWithMarker(segment, 0x00));
Jeff Thompsond129ac12013-10-11 14:30:12 -0700266 return *this;
267 }
268
269 /**
270 * Append a component with the encoded version number.
271 * Note that this encodes the exact value of version without converting from a time representation.
272 * @param version The version number.
273 * @return This name so that you can chain calls to append.
274 */
275 Name&
276 appendVersion(uint64_t version)
277 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800278 m_nameBlock.push_back(Component::fromNumberWithMarker(version, 0xFD));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700279 return *this;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700280 }
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800281
282 /**
283 * @brief Append a component with the encoded version number.
284 *
285 * This version of the method creates version number based on the current timestamp
286 * @return This name so that you can chain calls to append.
287 */
288 Name&
289 appendVersion();
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700290
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700291 /**
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700292 * Check if this name has the same component count and components as the given name.
293 * @param name The Name to check.
294 * @return true if the names are equal, otherwise false.
295 */
296 bool
297 equals(const Name& name) const;
298
299 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700300 * Check if the N components of this name are the same as the first N components of the given name.
301 * @param name The Name to check.
302 * @return true if this matches the given name, otherwise false. This always returns true if this name is empty.
303 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700304 bool
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800305 isPrefixOf(const Name& name) const;
306
307 bool
308 match(const Name& name) const
309 {
310 return isPrefixOf(name);
311 }
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700312
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700313 //
314 // vector equivalent interface.
315 //
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800316
317 /**
318 * @brief Check if name is emtpy
319 */
320 bool
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800321 empty() const { return m_nameBlock.elements().empty(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700322
323 /**
324 * Get the number of components.
325 * @return The number of components.
326 */
327 size_t
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800328 size() const { return m_nameBlock.elements_size(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700329
330 /**
331 * Get the component at the given index.
332 * @param i The index of the component, starting from 0.
333 * @return The name component at the index.
334 */
335 const Component&
Alexander Afanasyev8f9aa8bed2013-12-30 15:58:57 -0800336 get(ssize_t i) const
337 {
338 if (i >= 0)
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800339 return reinterpret_cast<const Component&>(m_nameBlock.elements()[i]);
Alexander Afanasyev8f9aa8bed2013-12-30 15:58:57 -0800340 else
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800341 return reinterpret_cast<const Component&>(m_nameBlock.elements()[size() + i]);
342 }
343
344 const Component&
345 operator [] (ssize_t i) const
346 {
347 return get(i);
Alexander Afanasyev8f9aa8bed2013-12-30 15:58:57 -0800348 }
Alexander Afanasyevc2344292014-03-02 00:08:00 +0000349
350 /**
351 * @brief Get component at the specified index
352 *
353 * Unlike get() and operator[] methods, at() checks for out of bounds
354 * and will throw Name::Error when it happens
355 *
356 * @throws Name::Error if index out of bounds
357 */
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800358 const Component&
359 at(ssize_t i) const
360 {
Alexander Afanasyevc2344292014-03-02 00:08:00 +0000361 if ((i >= 0 && i >= size()) || (i < 0 && i < -size()))
362 throw Error("Requested component does not exist (out of bounds)");
363
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800364 return get(i);
365 }
366
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800367 /**
368 * Compare this to the other Name using NDN canonical ordering. If the first components of each name are not equal,
369 * this returns -1 if the first comes before the second using the NDN canonical ordering for name components, or 1 if it comes after.
370 * If they are equal, this compares the second components of each name, etc. If both names are the same up to
371 * the size of the shorter name, this returns -1 if the first name is shorter than the second or 1 if it is longer.
372 * For example, if you std::sort gives: /a/b/d /a/b/cc /c /c/a /bb . This is intuitive because all names
373 * with the prefix /a are next to each other. But it may be also be counter-intuitive because /c comes before /bb
374 * according to NDN canonical ordering since it is shorter.
375 * @param other The other Name to compare with.
376 * @return 0 If they compare equal, -1 if *this comes before other in the canonical ordering, or
377 * 1 if *this comes after other in the canonical ordering.
378 *
379 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
380 */
381 int
382 compare(const Name& other) const;
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700383
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700384 /**
385 * Append the component
386 * @param component The component of type T.
387 */
388 template<class T> void
389 push_back(const T &component)
390 {
391 append(component);
392 }
393
Jeff Thompson91737f52013-10-04 11:07:24 -0700394 /**
395 * Check if this name has the same component count and components as the given name.
396 * @param name The Name to check.
397 * @return true if the names are equal, otherwise false.
398 */
399 bool
400 operator == (const Name &name) const { return equals(name); }
401
402 /**
403 * Check if this name has the same component count and components as the given name.
404 * @param name The Name to check.
405 * @return true if the names are not equal, otherwise false.
406 */
407 bool
408 operator != (const Name &name) const { return !equals(name); }
Jeff Thompson82568ad2013-12-17 15:17:40 -0800409
410 /**
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800411 * Return true if this is less than or equal to the other Name in the NDN canonical ordering.
412 * @param other The other Name to compare with.
413 *
414 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
Jeff Thompson82568ad2013-12-17 15:17:40 -0800415 */
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800416 bool
417 operator <= (const Name& other) const { return compare(other) <= 0; }
418
419 /**
420 * Return true if this is less than the other Name in the NDN canonical ordering.
421 * @param other The other Name to compare with.
422 *
423 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
424 */
425 bool
426 operator < (const Name& other) const { return compare(other) < 0; }
427
428 /**
429 * Return true if this is less than or equal to the other Name in the NDN canonical ordering.
430 * @param other The other Name to compare with.
431 *
432 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
433 */
434 bool
435 operator >= (const Name& other) const { return compare(other) >= 0; }
436
437 /**
438 * Return true if this is greater than the other Name in the NDN canonical ordering.
439 * @param other The other Name to compare with.
440 *
441 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
442 */
443 bool
444 operator > (const Name& other) const { return compare(other) > 0; }
Jeff Thompson82568ad2013-12-17 15:17:40 -0800445
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700446 //
447 // Iterator interface to name components.
448 //
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700449
450 /**
451 * Begin iterator (const).
452 */
453 const_iterator
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800454 begin() const
455 {
456 return reinterpret_cast<const_iterator>(&*m_nameBlock.elements().begin());
457 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700458
459 /**
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700460 * End iterator (const).
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800461 *
462 * @todo Check if this crash when there are no elements in the buffer
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700463 */
464 const_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800465 end() const
466 {
467 return reinterpret_cast<const_iterator>(&*m_nameBlock.elements().end());
468 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700469
470 /**
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700471 * Reverse begin iterator (const).
472 */
473 const_reverse_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800474 rbegin() const
475 {
476 return const_reverse_iterator(end());
477 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700478
479 /**
480 * Reverse end iterator (const).
481 */
482 const_reverse_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800483 rend() const
484 {
485 return const_reverse_iterator(begin());
486 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700487
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700488private:
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800489 mutable Block m_nameBlock;
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800490};
491
492std::ostream &
493operator << (std::ostream &os, const Name &name);
494
495inline std::string
496Name::toUri() const
Jeff Thompson49e321a2013-10-04 17:35:59 -0700497{
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800498 std::ostringstream os;
499 os << *this;
500 return os.str();
Jeff Thompson49e321a2013-10-04 17:35:59 -0700501}
502
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800503template<bool T>
504inline size_t
505Name::wireEncode(EncodingImpl<T>& blk) const
506{
507 size_t total_len = 0;
508
509 for (const_reverse_iterator i = rbegin ();
510 i != rend ();
511 ++i)
512 {
513 total_len += i->wireEncode (blk);
514 }
515
516 total_len += blk.prependVarNumber (total_len);
517 total_len += blk.prependVarNumber (Tlv::Name);
518 return total_len;
519}
520
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800521inline const Block &
522Name::wireEncode() const
523{
524 if (m_nameBlock.hasWire())
525 return m_nameBlock;
526
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800527 EncodingEstimator estimator;
528 size_t estimatedSize = wireEncode(estimator);
529
530 EncodingBuffer buffer(estimatedSize, 0);
531 wireEncode(buffer);
532
533 m_nameBlock = buffer.block();
534 m_nameBlock.parse();
535
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800536 return m_nameBlock;
537}
538
539inline void
540Name::wireDecode(const Block &wire)
541{
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800542 if (wire.type() != Tlv::Name)
543 throw Tlv::Error("Unexpected TLV type when decoding Name");
544
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800545 m_nameBlock = wire;
546 m_nameBlock.parse();
547}
548
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800549inline bool
550Name::hasWire() const
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800551{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800552 return m_nameBlock.hasWire();
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800553}
554
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800555} // namespace ndn
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700556
557#endif
558