blob: 0bfbdba15047bb795cbe2bb91b9d3768f8b19880 [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 */
67 Name(const Block &wire)
68 {
69 m_nameBlock = wire;
70 m_nameBlock.parse();
71 }
Jeff Thompson3f2175b2013-07-31 17:12:47 -070072
73 /**
Jeff Thompson443398d2013-07-02 19:45:46 -070074 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -070075 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -070076 */
Jeff Thompson3549ef32013-09-25 14:05:17 -070077 Name(const char* uri)
Jeff Thompson67515bd2013-08-15 17:43:22 -070078 {
79 set(uri);
80 }
Jeff Thompson5a6b5ab2013-08-05 15:43:47 -070081
Jeff Thompsone5f839b2013-06-28 12:50:38 -070082 /**
Jeff Thompson3549ef32013-09-25 14:05:17 -070083 * Parse the uri according to the NDN URI Scheme and create the name with the components.
84 * @param uri The URI string.
85 */
86 Name(const std::string& uri)
87 {
88 set(uri.c_str());
89 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -070090
Alexander Afanasyev380420b2014-02-09 20:52:29 -080091 /**
92 * @brief Fast encoding or block size estimation
93 */
94 template<bool T>
Wentao Shang77949212014-02-01 23:42:24 -080095 size_t
Alexander Afanasyev380420b2014-02-09 20:52:29 -080096 wireEncode(EncodingImpl<T> &block) const;
Wentao Shang77949212014-02-01 23:42:24 -080097
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080098 const Block &
99 wireEncode() const;
Alexander Afanasyev848c61a2014-01-03 13:52:04 -0800100
101 void
102 wireDecode(const Block &wire);
Jeff Thompsonb468c312013-07-01 17:50:14 -0700103
104 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700105 * Parse the uri according to the NDN URI Scheme and set the name with the components.
Jeff Thompson7781b392013-12-17 11:45:59 -0800106 * @param uri The null-terminated URI string.
Jeff Thompson67515bd2013-08-15 17:43:22 -0700107 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700108 void
109 set(const char *uri);
Jeff Thompson67515bd2013-08-15 17:43:22 -0700110
111 /**
Jeff Thompson7781b392013-12-17 11:45:59 -0800112 * Parse the uri according to the NDN URI Scheme and set the name with the components.
113 * @param uri The URI string.
114 */
115 void
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800116 set(const std::string& uri)
117 {
118 set(uri.c_str());
119 }
Jeff Thompson7781b392013-12-17 11:45:59 -0800120
121 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700122 * Append a new component, copying from value of length valueLength.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700123 * @return This name so that you can chain calls to append.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700124 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700125 Name&
Jeff Thompson97223af2013-09-24 17:01:27 -0700126 append(const uint8_t *value, size_t valueLength)
Jeff Thompson0f743452013-09-12 14:23:18 -0700127 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800128 m_nameBlock.push_back(Component(value, valueLength));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700129 return *this;
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700130 }
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700131
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800132 /**
133 * Append a new component, copying from value of length valueLength.
134 * @return This name so that you can chain calls to append.
135 */
136 template<class InputIterator>
137 Name&
138 append(InputIterator begin, InputIterator end)
139 {
140 m_nameBlock.push_back(Component(begin, end));
141 return *this;
142 }
143
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800144 // /**
145 // * Append a new component, copying from value.
146 // * @return This name so that you can chain calls to append.
147 // */
148 // Name&
149 // append(const Buffer& value)
150 // {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800151 // m_nameBlock.push_back(value);
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800152 // return *this;
153 // }
Jeff Thompson0f743452013-09-12 14:23:18 -0700154
Jeff Thompson26b0d792013-09-23 16:19:01 -0700155 Name&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800156 append(const ConstBufferPtr &value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700157 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800158 m_nameBlock.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700159 return *this;
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700160 }
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700161
Jeff Thompson21eb7212013-09-26 09:05:40 -0700162 Name&
163 append(const Component &value)
164 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800165 m_nameBlock.push_back(value);
Jeff Thompson21eb7212013-09-26 09:05:40 -0700166 return *this;
167 }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800168
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800169 /**
170 * @brief Append name component that represented as a string
171 *
172 * Note that this method is necessary to ensure correctness and unambiguity of
173 * ``append("string")`` operations (both Component and Name can be implicitly
174 * converted from string, each having different outcomes
175 */
176 Name&
177 append(const char *value)
178 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800179 m_nameBlock.push_back(Component(value));
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800180 return *this;
181 }
182
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800183 Name&
184 append(const Block &value)
185 {
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800186 if (value.type() == Tlv::NameComponent)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800187 m_nameBlock.push_back(value);
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800188 else
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800189 m_nameBlock.push_back(Block(Tlv::NameComponent, value));
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800190
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800191 return *this;
192 }
Jeff Thompson21eb7212013-09-26 09:05:40 -0700193
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700194 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700195 * Append the components of the given name to this name.
196 * @param name The Name with components to append.
197 * @return This name so that you can chain calls to append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700198 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700199 Name&
200 append(const Name& name);
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800201
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700202 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700203 * Clear all the components.
204 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700205 void
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800206 clear()
207 {
208 m_nameBlock = Block(Tlv::Name);
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700209 }
210
211 /**
Jeff Thompsond0159d72013-09-23 13:34:15 -0700212 * Get a new name, constructed as a subset of components.
213 * @param iStartComponent The index if the first component to get.
214 * @param nComponents The number of components starting at iStartComponent.
215 * @return A new name.
216 */
217 Name
218 getSubName(size_t iStartComponent, size_t nComponents) const;
219
220 /**
221 * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name.
222 * @param iStartComponent The index if the first component to get.
223 * @return A new name.
224 */
225 Name
226 getSubName(size_t iStartComponent) const;
227
228 /**
229 * Return a new Name with the first nComponents components of this Name.
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800230 * @param nComponents The number of prefix components. If nComponents is -N then return the prefix up
231 * to name.size() - N. For example getPrefix(-1) returns the name without the final component.
Jeff Thompsond0159d72013-09-23 13:34:15 -0700232 * @return A new Name.
233 */
234 Name
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800235 getPrefix(int nComponents) const
Jeff Thompsond0159d72013-09-23 13:34:15 -0700236 {
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800237 if (nComponents < 0)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800238 return getSubName(0, m_nameBlock.elements_size() + nComponents);
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800239 else
240 return getSubName(0, nComponents);
Jeff Thompsond0159d72013-09-23 13:34:15 -0700241 }
242
243 /**
Jeff Thompsone6063512013-07-01 15:11:28 -0700244 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700245 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700246 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700247 std::string
248 toUri() const;
Jeff Thompsone6063512013-07-01 15:11:28 -0700249
Jeff Thompson21844fc2013-08-08 14:52:51 -0700250 /**
Jeff Thompson8aac1992013-08-12 17:26:02 -0700251 * Append a component with the encoded segment number.
252 * @param segment The segment number.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700253 * @return This name so that you can chain calls to append.
254 */
255 Name&
Jeff Thompsond129ac12013-10-11 14:30:12 -0700256 appendSegment(uint64_t segment)
Jeff Thompson8aac1992013-08-12 17:26:02 -0700257 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800258 m_nameBlock.push_back(Component::fromNumberWithMarker(segment, 0x00));
Jeff Thompsond129ac12013-10-11 14:30:12 -0700259 return *this;
260 }
261
262 /**
263 * Append a component with the encoded version number.
264 * Note that this encodes the exact value of version without converting from a time representation.
265 * @param version The version number.
266 * @return This name so that you can chain calls to append.
267 */
268 Name&
269 appendVersion(uint64_t version)
270 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800271 m_nameBlock.push_back(Component::fromNumberWithMarker(version, 0xFD));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700272 return *this;
Jeff Thompson8aac1992013-08-12 17:26:02 -0700273 }
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800274
275 /**
276 * @brief Append a component with the encoded version number.
277 *
278 * This version of the method creates version number based on the current timestamp
279 * @return This name so that you can chain calls to append.
280 */
281 Name&
282 appendVersion();
Jeff Thompsoncc35cd42013-08-20 12:23:14 -0700283
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700284 /**
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700285 * Check if this name has the same component count and components as the given name.
286 * @param name The Name to check.
287 * @return true if the names are equal, otherwise false.
288 */
289 bool
290 equals(const Name& name) const;
291
292 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700293 * Check if the N components of this name are the same as the first N components of the given name.
294 * @param name The Name to check.
295 * @return true if this matches the given name, otherwise false. This always returns true if this name is empty.
296 */
Jeff Thompson0050abe2013-09-17 12:50:25 -0700297 bool
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800298 isPrefixOf(const Name& name) const;
299
300 bool
301 match(const Name& name) const
302 {
303 return isPrefixOf(name);
304 }
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700305
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700306 //
307 // vector equivalent interface.
308 //
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800309
310 /**
311 * @brief Check if name is emtpy
312 */
313 bool
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800314 empty() const { return m_nameBlock.elements().empty(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700315
316 /**
317 * Get the number of components.
318 * @return The number of components.
319 */
320 size_t
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800321 size() const { return m_nameBlock.elements_size(); }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700322
323 /**
324 * Get the component at the given index.
325 * @param i The index of the component, starting from 0.
326 * @return The name component at the index.
327 */
328 const Component&
Alexander Afanasyev8f9aa8b2013-12-30 15:58:57 -0800329 get(ssize_t i) const
330 {
331 if (i >= 0)
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800332 return reinterpret_cast<const Component&>(m_nameBlock.elements()[i]);
Alexander Afanasyev8f9aa8b2013-12-30 15:58:57 -0800333 else
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800334 return reinterpret_cast<const Component&>(m_nameBlock.elements()[size() + i]);
335 }
336
337 const Component&
338 operator [] (ssize_t i) const
339 {
340 return get(i);
Alexander Afanasyev8f9aa8b2013-12-30 15:58:57 -0800341 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700342
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800343 const Component&
344 at(ssize_t i) const
345 {
346 return get(i);
347 }
348
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800349 /**
350 * Compare this to the other Name using NDN canonical ordering. If the first components of each name are not equal,
351 * this returns -1 if the first comes before the second using the NDN canonical ordering for name components, or 1 if it comes after.
352 * If they are equal, this compares the second components of each name, etc. If both names are the same up to
353 * the size of the shorter name, this returns -1 if the first name is shorter than the second or 1 if it is longer.
354 * For example, if you std::sort gives: /a/b/d /a/b/cc /c /c/a /bb . This is intuitive because all names
355 * with the prefix /a are next to each other. But it may be also be counter-intuitive because /c comes before /bb
356 * according to NDN canonical ordering since it is shorter.
357 * @param other The other Name to compare with.
358 * @return 0 If they compare equal, -1 if *this comes before other in the canonical ordering, or
359 * 1 if *this comes after other in the canonical ordering.
360 *
361 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
362 */
363 int
364 compare(const Name& other) const;
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700365
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700366 /**
367 * Append the component
368 * @param component The component of type T.
369 */
370 template<class T> void
371 push_back(const T &component)
372 {
373 append(component);
374 }
375
Jeff Thompson91737f52013-10-04 11:07:24 -0700376 /**
377 * Check if this name has the same component count and components as the given name.
378 * @param name The Name to check.
379 * @return true if the names are equal, otherwise false.
380 */
381 bool
382 operator == (const Name &name) const { return equals(name); }
383
384 /**
385 * Check if this name has the same component count and components as the given name.
386 * @param name The Name to check.
387 * @return true if the names are not equal, otherwise false.
388 */
389 bool
390 operator != (const Name &name) const { return !equals(name); }
Jeff Thompson82568ad2013-12-17 15:17:40 -0800391
392 /**
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800393 * Return true if this is less than or equal to the other Name in the NDN canonical ordering.
394 * @param other The other Name to compare with.
395 *
396 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
Jeff Thompson82568ad2013-12-17 15:17:40 -0800397 */
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800398 bool
399 operator <= (const Name& other) const { return compare(other) <= 0; }
400
401 /**
402 * Return true if this is less than the other Name in the NDN canonical ordering.
403 * @param other The other Name to compare with.
404 *
405 * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html
406 */
407 bool
408 operator < (const Name& other) const { return compare(other) < 0; }
409
410 /**
411 * 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
415 */
416 bool
417 operator >= (const Name& other) const { return compare(other) >= 0; }
418
419 /**
420 * Return true if this is greater 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; }
Jeff Thompson82568ad2013-12-17 15:17:40 -0800427
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700428 //
429 // Iterator interface to name components.
430 //
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700431
432 /**
433 * Begin iterator (const).
434 */
435 const_iterator
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800436 begin() const
437 {
438 return reinterpret_cast<const_iterator>(&*m_nameBlock.elements().begin());
439 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700440
441 /**
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700442 * End iterator (const).
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800443 *
444 * @todo Check if this crash when there are no elements in the buffer
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700445 */
446 const_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800447 end() const
448 {
449 return reinterpret_cast<const_iterator>(&*m_nameBlock.elements().end());
450 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700451
452 /**
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700453 * Reverse begin iterator (const).
454 */
455 const_reverse_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800456 rbegin() const
457 {
458 return const_reverse_iterator(end());
459 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700460
461 /**
462 * Reverse end iterator (const).
463 */
464 const_reverse_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800465 rend() const
466 {
467 return const_reverse_iterator(begin());
468 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700469
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700470private:
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800471 mutable Block m_nameBlock;
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800472};
473
474std::ostream &
475operator << (std::ostream &os, const Name &name);
476
477inline std::string
478Name::toUri() const
Jeff Thompson49e321a2013-10-04 17:35:59 -0700479{
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800480 std::ostringstream os;
481 os << *this;
482 return os.str();
Jeff Thompson49e321a2013-10-04 17:35:59 -0700483}
484
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800485inline const Block &
486Name::wireEncode() const
487{
488 if (m_nameBlock.hasWire())
489 return m_nameBlock;
490
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800491 EncodingEstimator estimator;
492 size_t estimatedSize = wireEncode(estimator);
493
494 EncodingBuffer buffer(estimatedSize, 0);
495 wireEncode(buffer);
496
497 m_nameBlock = buffer.block();
498 m_nameBlock.parse();
499
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800500 return m_nameBlock;
501}
502
503inline void
504Name::wireDecode(const Block &wire)
505{
506 m_nameBlock = wire;
507 m_nameBlock.parse();
508}
509
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800510template<bool T>
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800511inline size_t
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800512Name::wireEncode(EncodingImpl<T>& blk) const
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800513{
514 size_t total_len = 0;
515
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800516 for (const_reverse_iterator i = rbegin ();
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800517 i != rend ();
518 ++i)
519 {
520 total_len += i->wireEncode (blk);
521 }
522
523 total_len += blk.prependVarNumber (total_len);
524 total_len += blk.prependVarNumber (Tlv::Name);
525 return total_len;
526}
527
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800528template
529size_t
530Name::wireEncode<true>(EncodingBuffer& block) const;
531
532template
533size_t
534Name::wireEncode<false>(EncodingEstimator& block) const;
535
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800536
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800537} // namespace ndn
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700538
539#endif
540