blob: 3a0cc52234b78e85c2626a13cf79ca8335974156 [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 Afanasyev2a7f7202014-04-23 14:25:29 -070026class Name : public ptr_lib::enable_shared_from_this<Name>
27{
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070028public:
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070029 /// @brief Error that can be thrown from Name
30 class Error : public name::Component::Error
31 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070032 public:
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070033 explicit
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070034 Error(const std::string& what)
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070035 : name::Component::Error(what)
36 {
37 }
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070038 };
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080039
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080040 typedef name::Component Component;
41
42 typedef std::vector<Component> component_container;
43
44 typedef Component value_type;
45 typedef void allocator_type;
46 typedef Component& reference;
47 typedef const Component const_reference;
48 typedef Component* pointer;
49 typedef const Component* const_pointer;
50 typedef Component* iterator;
51 typedef const Component* const_iterator;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070052
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080053 typedef boost::reverse_iterator<iterator> reverse_iterator;
54 typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
55
56 typedef component_container::difference_type difference_type;
57 typedef component_container::size_type size_type;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070058
Jeff Thompson443398d2013-07-02 19:45:46 -070059 /**
60 * Create a new Name with no components.
61 */
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080062 Name()
63 : m_nameBlock(Tlv::Name)
64 {
Jeff Thompson016ed642013-07-02 14:39:06 -070065 }
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080066
67 /**
68 * @brief Create Name object from wire block
69 *
70 * This is a more efficient equivalent for
71 * @code
72 * Name name;
73 * name.wireDecode(wire);
74 * @endcode
75 */
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080076 explicit
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070077 Name(const Block& wire)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080078 {
79 m_nameBlock = wire;
80 m_nameBlock.parse();
81 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070082
Jeff Thompson3f2175b2013-07-31 17:12:47 -070083 /**
Jeff Thompson443398d2013-07-02 19:45:46 -070084 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -070085 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -070086 */
Jeff Thompson3549ef32013-09-25 14:05:17 -070087 Name(const char* uri)
Jeff Thompson67515bd2013-08-15 17:43:22 -070088 {
89 set(uri);
90 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070091
Jeff Thompsone5f839b2013-06-28 12:50:38 -070092 /**
Jeff Thompson3549ef32013-09-25 14:05:17 -070093 * Parse the uri according to the NDN URI Scheme and create the name with the components.
94 * @param uri The URI string.
95 */
96 Name(const std::string& uri)
97 {
98 set(uri.c_str());
99 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700100
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800101 /**
102 * @brief Fast encoding or block size estimation
103 */
104 template<bool T>
Wentao Shang77949212014-02-01 23:42:24 -0800105 size_t
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700106 wireEncode(EncodingImpl<T>& block) const;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700107
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700108 const Block&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800109 wireEncode() const;
Alexander Afanasyev848c61a2014-01-03 13:52:04 -0800110
111 void
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700112 wireDecode(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800113
114 /**
115 * @brief Check if already has wire
116 */
117 bool
118 hasWire() const;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700119
Jeff Thompsonb468c312013-07-01 17:50:14 -0700120 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700121 * Parse the uri according to the NDN URI Scheme and set the name with the components.
Jeff Thompson7781b392013-12-17 11:45:59 -0800122 * @param uri The null-terminated URI string.
Jeff Thompson67515bd2013-08-15 17:43:22 -0700123 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700124 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700125 set(const char* uri);
Jeff Thompson67515bd2013-08-15 17:43:22 -0700126
127 /**
Jeff Thompson7781b392013-12-17 11:45:59 -0800128 * Parse the uri according to the NDN URI Scheme and set the name with the components.
129 * @param uri The URI string.
130 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700131 void
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800132 set(const std::string& uri)
133 {
134 set(uri.c_str());
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700135 }
136
Jeff Thompson7781b392013-12-17 11:45:59 -0800137 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700138 * Append a new component, copying from value of length valueLength.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700139 * @return This name so that you can chain calls to append.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700140 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700141 Name&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700142 append(const uint8_t* value, size_t valueLength)
Jeff Thompson0f743452013-09-12 14:23:18 -0700143 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800144 m_nameBlock.push_back(Component(value, valueLength));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700145 return *this;
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700146 }
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700147
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800148 /**
149 * Append a new component, copying from value of length valueLength.
150 * @return This name so that you can chain calls to append.
151 */
152 template<class InputIterator>
153 Name&
154 append(InputIterator begin, InputIterator end)
155 {
156 m_nameBlock.push_back(Component(begin, end));
157 return *this;
158 }
159
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700160 Name&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700161 append(const ConstBufferPtr& value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700162 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800163 m_nameBlock.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700164 return *this;
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700165 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700166
167 Name&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700168 append(const Component& value)
Jeff Thompson21eb7212013-09-26 09:05:40 -0700169 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800170 m_nameBlock.push_back(value);
Jeff Thompson21eb7212013-09-26 09:05:40 -0700171 return *this;
172 }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800173
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800174 /**
175 * @brief Append name component that represented as a string
176 *
177 * Note that this method is necessary to ensure correctness and unambiguity of
178 * ``append("string")`` operations (both Component and Name can be implicitly
179 * converted from string, each having different outcomes
180 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700181 Name&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700182 append(const char* value)
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800183 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800184 m_nameBlock.push_back(Component(value));
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800185 return *this;
186 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700187
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800188 Name&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700189 append(const Block& value)
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800190 {
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800191 if (value.type() == Tlv::NameComponent)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800192 m_nameBlock.push_back(value);
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800193 else
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800194 m_nameBlock.push_back(Block(Tlv::NameComponent, value));
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800195
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800196 return *this;
197 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700198
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700199 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700200 * Append the components of the given name to this name.
201 * @param name The Name with components to append.
202 * @return This name so that you can chain calls to append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700203 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700204 Name&
205 append(const Name& name);
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700206
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700207 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700208 * Clear all the components.
209 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700210 void
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800211 clear()
212 {
213 m_nameBlock = Block(Tlv::Name);
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700214 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700215
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700216 /**
Jeff Thompsond0159d72013-09-23 13:34:15 -0700217 * Get a new name, constructed as a subset of components.
218 * @param iStartComponent The index if the first component to get.
219 * @param nComponents The number of components starting at iStartComponent.
220 * @return A new name.
221 */
222 Name
223 getSubName(size_t iStartComponent, size_t nComponents) const;
224
225 /**
226 * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name.
227 * @param iStartComponent The index if the first component to get.
228 * @return A new name.
229 */
230 Name
231 getSubName(size_t iStartComponent) const;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700232
Jeff Thompsond0159d72013-09-23 13:34:15 -0700233 /**
234 * Return a new Name with the first nComponents components of this Name.
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800235 * @param nComponents The number of prefix components. If nComponents is -N then return the prefix up
236 * to name.size() - N. For example getPrefix(-1) returns the name without the final component.
Jeff Thompsond0159d72013-09-23 13:34:15 -0700237 * @return A new Name.
238 */
239 Name
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800240 getPrefix(int nComponents) const
Jeff Thompsond0159d72013-09-23 13:34:15 -0700241 {
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800242 if (nComponents < 0)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800243 return getSubName(0, m_nameBlock.elements_size() + nComponents);
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800244 else
245 return getSubName(0, nComponents);
Jeff Thompsond0159d72013-09-23 13:34:15 -0700246 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700247
Jeff Thompsond0159d72013-09-23 13:34:15 -0700248 /**
Jeff Thompsone6063512013-07-01 15:11:28 -0700249 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700250 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700251 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700252 std::string
Jeff Thompson0050abe2013-09-17 12:50:25 -0700253 toUri() const;
Jeff Thompsond129ac12013-10-11 14:30:12 -0700254
255 /**
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700256 * @brief Append a component with the number encoded as nonNegativeInteger
257 *
258 * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding
259 *
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600260 * @param number The non-negative number
261 * @return This name so that you can chain calls to append.
262 */
263 Name&
264 appendNumber(uint64_t number)
265 {
266 m_nameBlock.push_back(Component::fromNumber(number));
267 return *this;
268 }
269
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700270 /**
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700271 * @brief An alias for appendNumber(uint64_t)
272 */
273 Name&
274 appendVersion(uint64_t number)
275 {
276 return appendNumber(number);
277 }
278
279 /**
280 * @brief Append a component with the encoded version number (current UNIX timestamp
281 * in milliseconds)
282 */
283 Name&
284 appendVersion();
285
286 /**
287 * @brief An alias for appendNumber(uint64_t)
288 */
289 Name&
290 appendSegment(uint64_t number)
291 {
292 return appendNumber(number);
293 }
294
295 /**
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700296 * Check if this name has the same component count and components as the given name.
297 * @param name The Name to check.
298 * @return true if the names are equal, otherwise false.
299 */
300 bool
301 equals(const Name& name) const;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700302
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700303 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700304 * Check if the N components of this name are the same as the first N components of the given name.
305 * @param name The Name to check.
306 * @return true if this matches the given name, otherwise false. This always returns true if this name is empty.
307 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700308 bool
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800309 isPrefixOf(const Name& name) const;
310
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700311 //
312 // vector equivalent interface.
313 //
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800314
315 /**
316 * @brief Check if name is emtpy
317 */
318 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700319 empty() const
320 {
321 return m_nameBlock.elements().empty();
322 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700323
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700324 /**
325 * Get the number of components.
326 * @return The number of components.
327 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700328 size_t
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700329 size() const
330 {
331 return m_nameBlock.elements_size();
332 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700333
334 /**
335 * Get the component at the given index.
336 * @param i The index of the component, starting from 0.
337 * @return The name component at the index.
338 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700339 const Component&
Alexander Afanasyev8f9aa8bed2013-12-30 15:58:57 -0800340 get(ssize_t i) const
341 {
342 if (i >= 0)
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800343 return reinterpret_cast<const Component&>(m_nameBlock.elements()[i]);
Alexander Afanasyev8f9aa8bed2013-12-30 15:58:57 -0800344 else
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800345 return reinterpret_cast<const Component&>(m_nameBlock.elements()[size() + i]);
346 }
347
348 const Component&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700349 operator[](ssize_t i) const
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800350 {
351 return get(i);
Alexander Afanasyev8f9aa8bed2013-12-30 15:58:57 -0800352 }
Alexander Afanasyevc2344292014-03-02 00:08:00 +0000353
354 /**
355 * @brief Get component at the specified index
356 *
357 * Unlike get() and operator[] methods, at() checks for out of bounds
358 * and will throw Name::Error when it happens
359 *
360 * @throws Name::Error if index out of bounds
361 */
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800362 const Component&
363 at(ssize_t i) const
364 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700365 if ((i >= 0 && static_cast<size_t>(i) >= size()) ||
366 (i < 0 && static_cast<size_t>(-i) > size()))
Alexander Afanasyevc2344292014-03-02 00:08:00 +0000367 throw Error("Requested component does not exist (out of bounds)");
368
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800369 return get(i);
370 }
371
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800372 /**
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700373 * @brief Compare this to the other Name using NDN canonical ordering.
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800374 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700375 * If the first components of each name are not equal, this returns -1 if the first comes
376 * before the second using the NDN canonical ordering for name components, or 1 if it
377 * comes after. If they are equal, this compares the second components of each name, etc.
378 * If both names are the same up to the size of the shorter name, this returns -1 if the
379 * first name is shorter than the second or 1 if it is longer. For example, if you
380 * std::sort gives: /a/b/d /a/b/cc /c /c/a /bb . This is intuitive because all names with
381 * the prefix /a are next to each other. But it may be also be counter-intuitive because
382 * /c comes before /bb according to NDN canonical ordering since it is shorter. @param
383 * other The other Name to compare with. @return 0 If they compare equal, -1 if *this
384 * comes before other in the canonical ordering, or 1 if *this comes after other in the
385 * canonical ordering.
386 *
387 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800388 */
389 int
390 compare(const Name& other) const;
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700391
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700392 /**
393 * Append the component
394 * @param component The component of type T.
395 */
396 template<class T> void
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700397 push_back(const T& component)
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700398 {
399 append(component);
400 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700401
Jeff Thompson91737f52013-10-04 11:07:24 -0700402 /**
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 equal, otherwise false.
406 */
407 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700408 operator==(const Name& name) const
409 {
410 return equals(name);
411 }
Jeff Thompson91737f52013-10-04 11:07:24 -0700412
413 /**
414 * Check if this name has the same component count and components as the given name.
415 * @param name The Name to check.
416 * @return true if the names are not equal, otherwise false.
417 */
418 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700419 operator!=(const Name& name) const
420 {
421 return !equals(name);
422 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700423
Jeff Thompson82568ad2013-12-17 15:17:40 -0800424 /**
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800425 * Return true if this is less than or equal to the other Name in the NDN canonical ordering.
426 * @param other The other Name to compare with.
427 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700428 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Jeff Thompson82568ad2013-12-17 15:17:40 -0800429 */
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800430 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700431 operator<=(const Name& other) const
432 {
433 return compare(other) <= 0;
434 }
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800435
436 /**
437 * Return true if this is less than the other Name in the NDN canonical ordering.
438 * @param other The other Name to compare with.
439 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700440 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800441 */
442 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700443 operator<(const Name& other) const
444 {
445 return compare(other) < 0;
446 }
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800447
448 /**
449 * Return true if this is less than or equal to the other Name in the NDN canonical ordering.
450 * @param other The other Name to compare with.
451 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700452 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800453 */
454 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700455 operator>=(const Name& other) const
456 {
457 return compare(other) >= 0;
458 }
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800459
460 /**
461 * Return true if this is greater than the other Name in the NDN canonical ordering.
462 * @param other The other Name to compare with.
463 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700464 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800465 */
466 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700467 operator>(const Name& other) const
468 {
469 return compare(other) > 0;
470 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700471
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700472 //
473 // Iterator interface to name components.
474 //
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700475
476 /**
477 * Begin iterator (const).
478 */
479 const_iterator
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800480 begin() const
481 {
482 return reinterpret_cast<const_iterator>(&*m_nameBlock.elements().begin());
483 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700484
485 /**
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700486 * End iterator (const).
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800487 *
488 * @todo Check if this crash when there are no elements in the buffer
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700489 */
490 const_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800491 end() const
492 {
493 return reinterpret_cast<const_iterator>(&*m_nameBlock.elements().end());
494 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700495
496 /**
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700497 * Reverse begin iterator (const).
498 */
499 const_reverse_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800500 rbegin() const
501 {
502 return const_reverse_iterator(end());
503 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700504
505 /**
506 * Reverse end iterator (const).
507 */
508 const_reverse_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800509 rend() const
510 {
511 return const_reverse_iterator(begin());
512 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700513
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700514private:
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800515 mutable Block m_nameBlock;
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800516};
517
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700518inline std::ostream&
519operator<<(std::ostream& os, const Name& name)
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700520{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700521 if (name.empty())
522 {
523 os << "/";
524 }
525 else
526 {
527 for (Name::const_iterator i = name.begin(); i != name.end(); i++) {
528 os << "/";
529 i->toEscapedString(os);
530 }
531 }
532 return os;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700533}
534
535inline std::string
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800536Name::toUri() const
Jeff Thompson49e321a2013-10-04 17:35:59 -0700537{
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800538 std::ostringstream os;
539 os << *this;
540 return os.str();
Jeff Thompson49e321a2013-10-04 17:35:59 -0700541}
542
Alexander Afanasyev0df28362014-03-20 17:31:43 -0700543inline std::istream&
544operator>>(std::istream& is, Name& name)
545{
546 std::string inputString;
547 is >> inputString;
548 name.set(inputString);
549
550 return is;
551}
552
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700553
554inline void
555Name::set(const char* uri_cstr)
556{
557 clear();
558
559 std::string uri = uri_cstr;
560 trim(uri);
561 if (uri.size() == 0)
562 return;
563
564 size_t iColon = uri.find(':');
565 if (iColon != std::string::npos) {
566 // Make sure the colon came before a '/'.
567 size_t iFirstSlash = uri.find('/');
568 if (iFirstSlash == std::string::npos || iColon < iFirstSlash) {
569 // Omit the leading protocol such as ndn:
570 uri.erase(0, iColon + 1);
571 trim(uri);
572 }
573 }
574
575 // Trim the leading slash and possibly the authority.
576 if (uri[0] == '/') {
577 if (uri.size() >= 2 && uri[1] == '/') {
578 // Strip the authority following "//".
579 size_t iAfterAuthority = uri.find('/', 2);
580 if (iAfterAuthority == std::string::npos)
581 // Unusual case: there was only an authority.
582 return;
583 else {
584 uri.erase(0, iAfterAuthority + 1);
585 trim(uri);
586 }
587 }
588 else {
589 uri.erase(0, 1);
590 trim(uri);
591 }
592 }
593
594 size_t iComponentStart = 0;
595
596 // Unescape the components.
597 while (iComponentStart < uri.size()) {
598 size_t iComponentEnd = uri.find("/", iComponentStart);
599 if (iComponentEnd == std::string::npos)
600 iComponentEnd = uri.size();
601
602 Component component = Component::fromEscapedString(&uri[0], iComponentStart, iComponentEnd);
603 // Ignore illegal components. This also gets rid of a trailing '/'.
604 if (!component.empty())
605 append(Component(component));
606
607 iComponentStart = iComponentEnd + 1;
608 }
609}
610
611inline Name&
612Name::append(const Name& name)
613{
614 if (&name == this)
615 // Copying from this name, so need to make a copy first.
616 return append(Name(name));
617
618 for (size_t i = 0; i < name.size(); ++i)
619 append(name.at(i));
620
621 return *this;
622}
623
624inline Name&
625Name::appendVersion()
626{
627 appendNumber(time::toUnixTimestamp(time::system_clock::now()).count());
628 return *this;
629}
630
631inline Name
632Name::getSubName(size_t iStartComponent, size_t nComponents) const
633{
634 Name result;
635
636 size_t iEnd = iStartComponent + nComponents;
637 for (size_t i = iStartComponent; i < iEnd && i < size(); ++i)
638 result.append(at(i));
639
640 return result;
641}
642
643inline Name
644Name::getSubName(size_t iStartComponent) const
645{
646 Name result;
647
648 for (size_t i = iStartComponent; i < size(); ++i)
649 result.append(at(i));
650
651 return result;
652}
653
654inline bool
655Name::equals(const Name& name) const
656{
657 if (size() != name.size())
658 return false;
659
660 for (size_t i = 0; i < size(); ++i) {
661 if (at(i) != name.at(i))
662 return false;
663 }
664
665 return true;
666}
667
668inline bool
669Name::isPrefixOf(const Name& name) const
670{
671 // This name is longer than the name we are checking it against.
672 if (size() > name.size())
673 return false;
674
675 // Check if at least one of given components doesn't match.
676 for (size_t i = 0; i < size(); ++i) {
677 if (at(i) != name.at(i))
678 return false;
679 }
680
681 return true;
682}
683
684
685inline int
686Name::compare(const Name& other) const
687{
688 for (size_t i = 0; i < size() && i < other.size(); ++i) {
689 int comparison = at(i).compare(other.at(i));
690 if (comparison == 0)
691 // The components at this index are equal, so check the next components.
692 continue;
693
694 // Otherwise, the result is based on the components at this index.
695 return comparison;
696 }
697
698 // The components up to min(this.size(), other.size()) are equal, so the shorter name is less.
699 if (size() < other.size())
700 return -1;
701 else if (size() > other.size())
702 return 1;
703 else
704 return 0;
705}
706
707
708
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800709template<bool T>
710inline size_t
711Name::wireEncode(EncodingImpl<T>& blk) const
712{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700713 size_t totalLength = 0;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700714
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700715 for (const_reverse_iterator i = rbegin();
716 i != rend();
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800717 ++i)
718 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700719 totalLength += i->wireEncode(blk);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800720 }
721
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700722 totalLength += blk.prependVarNumber(totalLength);
723 totalLength += blk.prependVarNumber(Tlv::Name);
724 return totalLength;
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800725}
726
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700727inline const Block&
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800728Name::wireEncode() const
729{
730 if (m_nameBlock.hasWire())
731 return m_nameBlock;
732
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800733 EncodingEstimator estimator;
734 size_t estimatedSize = wireEncode(estimator);
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700735
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800736 EncodingBuffer buffer(estimatedSize, 0);
737 wireEncode(buffer);
738
739 m_nameBlock = buffer.block();
740 m_nameBlock.parse();
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700741
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800742 return m_nameBlock;
743}
744
745inline void
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700746Name::wireDecode(const Block& wire)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800747{
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800748 if (wire.type() != Tlv::Name)
749 throw Tlv::Error("Unexpected TLV type when decoding Name");
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700750
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800751 m_nameBlock = wire;
752 m_nameBlock.parse();
753}
754
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800755inline bool
756Name::hasWire() const
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800757{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800758 return m_nameBlock.hasWire();
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800759}
760
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800761} // namespace ndn
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700762
763#endif