blob: 11a438d0d52891a3efb0899ceb8215c9cec01ba9 [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/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Jeff Thompson <jefft0@remap.ucla.edu>
13 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
14 * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070015 */
16
17#ifndef NDN_NAME_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070018#define NDN_NAME_HPP
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070019
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080020#include "common.hpp"
21#include "name-component.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070022#include "util/time.hpp"
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080023
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080024#include "encoding/block.hpp"
Wentao Shang77949212014-02-01 23:42:24 -080025#include "encoding/encoding-buffer.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070026
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080027#include <boost/iterator/reverse_iterator.hpp>
28
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070029namespace ndn {
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080030
Jeff Thompsonc7d65502013-11-06 17:22:26 -080031/**
32 * A Name holds an array of Name::Component and represents an NDN name.
33 */
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070034class Name : public enable_shared_from_this<Name>
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070035{
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070036public:
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070037 /// @brief Error that can be thrown from Name
38 class Error : public name::Component::Error
39 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070040 public:
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070041 explicit
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070042 Error(const std::string& what)
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070043 : name::Component::Error(what)
44 {
45 }
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070046 };
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080047
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080048 typedef name::Component Component;
49
50 typedef std::vector<Component> component_container;
51
52 typedef Component value_type;
53 typedef void allocator_type;
54 typedef Component& reference;
55 typedef const Component const_reference;
56 typedef Component* pointer;
57 typedef const Component* const_pointer;
58 typedef Component* iterator;
59 typedef const Component* const_iterator;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070060
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080061 typedef boost::reverse_iterator<iterator> reverse_iterator;
62 typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
63
64 typedef component_container::difference_type difference_type;
65 typedef component_container::size_type size_type;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070066
Jeff Thompson443398d2013-07-02 19:45:46 -070067 /**
68 * Create a new Name with no components.
69 */
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080070 Name()
71 : m_nameBlock(Tlv::Name)
72 {
Jeff Thompson016ed642013-07-02 14:39:06 -070073 }
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080074
75 /**
76 * @brief Create Name object from wire block
77 *
78 * This is a more efficient equivalent for
79 * @code
80 * Name name;
81 * name.wireDecode(wire);
82 * @endcode
83 */
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080084 explicit
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070085 Name(const Block& wire)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080086 {
87 m_nameBlock = wire;
88 m_nameBlock.parse();
89 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070090
Jeff Thompson3f2175b2013-07-31 17:12:47 -070091 /**
Jeff Thompson443398d2013-07-02 19:45:46 -070092 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -070093 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -070094 */
Jeff Thompson3549ef32013-09-25 14:05:17 -070095 Name(const char* uri)
Jeff Thompson67515bd2013-08-15 17:43:22 -070096 {
97 set(uri);
98 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070099
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700100 /**
Jeff Thompson3549ef32013-09-25 14:05:17 -0700101 * Parse the uri according to the NDN URI Scheme and create the name with the components.
102 * @param uri The URI string.
103 */
104 Name(const std::string& uri)
105 {
106 set(uri.c_str());
107 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700108
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800109 /**
110 * @brief Fast encoding or block size estimation
111 */
112 template<bool T>
Wentao Shang77949212014-02-01 23:42:24 -0800113 size_t
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700114 wireEncode(EncodingImpl<T>& block) const;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700115
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700116 const Block&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800117 wireEncode() const;
Alexander Afanasyev848c61a2014-01-03 13:52:04 -0800118
119 void
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700120 wireDecode(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800121
122 /**
123 * @brief Check if already has wire
124 */
125 bool
126 hasWire() const;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700127
Jeff Thompsonb468c312013-07-01 17:50:14 -0700128 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700129 * Parse the uri according to the NDN URI Scheme and set the name with the components.
Jeff Thompson7781b392013-12-17 11:45:59 -0800130 * @param uri The null-terminated URI string.
Jeff Thompson67515bd2013-08-15 17:43:22 -0700131 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700132 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700133 set(const char* uri);
Jeff Thompson67515bd2013-08-15 17:43:22 -0700134
135 /**
Jeff Thompson7781b392013-12-17 11:45:59 -0800136 * Parse the uri according to the NDN URI Scheme and set the name with the components.
137 * @param uri The URI string.
138 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700139 void
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800140 set(const std::string& uri)
141 {
142 set(uri.c_str());
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700143 }
144
Jeff Thompson7781b392013-12-17 11:45:59 -0800145 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700146 * Append a new component, copying from value of length valueLength.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700147 * @return This name so that you can chain calls to append.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700148 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700149 Name&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700150 append(const uint8_t* value, size_t valueLength)
Jeff Thompson0f743452013-09-12 14:23:18 -0700151 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800152 m_nameBlock.push_back(Component(value, valueLength));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700153 return *this;
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700154 }
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700155
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800156 /**
157 * Append a new component, copying from value of length valueLength.
158 * @return This name so that you can chain calls to append.
159 */
160 template<class InputIterator>
161 Name&
162 append(InputIterator begin, InputIterator end)
163 {
164 m_nameBlock.push_back(Component(begin, end));
165 return *this;
166 }
167
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700168 Name&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700169 append(const ConstBufferPtr& value)
Jeff Thompson0f743452013-09-12 14:23:18 -0700170 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800171 m_nameBlock.push_back(value);
Jeff Thompson26b0d792013-09-23 16:19:01 -0700172 return *this;
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700173 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700174
175 Name&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700176 append(const Component& value)
Jeff Thompson21eb7212013-09-26 09:05:40 -0700177 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800178 m_nameBlock.push_back(value);
Jeff Thompson21eb7212013-09-26 09:05:40 -0700179 return *this;
180 }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800181
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800182 /**
183 * @brief Append name component that represented as a string
184 *
185 * Note that this method is necessary to ensure correctness and unambiguity of
186 * ``append("string")`` operations (both Component and Name can be implicitly
187 * converted from string, each having different outcomes
188 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700189 Name&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700190 append(const char* value)
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800191 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800192 m_nameBlock.push_back(Component(value));
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800193 return *this;
194 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700195
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800196 Name&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700197 append(const Block& value)
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800198 {
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800199 if (value.type() == Tlv::NameComponent)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800200 m_nameBlock.push_back(value);
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800201 else
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800202 m_nameBlock.push_back(Block(Tlv::NameComponent, value));
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800203
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800204 return *this;
205 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700206
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700207 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700208 * Append the components of the given name to this name.
209 * @param name The Name with components to append.
210 * @return This name so that you can chain calls to append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700211 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700212 Name&
213 append(const Name& name);
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700214
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700215 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700216 * Clear all the components.
217 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700218 void
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800219 clear()
220 {
221 m_nameBlock = Block(Tlv::Name);
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700222 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700223
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700224 /**
Jeff Thompsond0159d72013-09-23 13:34:15 -0700225 * Get a new name, constructed as a subset of components.
226 * @param iStartComponent The index if the first component to get.
227 * @param nComponents The number of components starting at iStartComponent.
228 * @return A new name.
229 */
230 Name
231 getSubName(size_t iStartComponent, size_t nComponents) const;
232
233 /**
234 * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name.
235 * @param iStartComponent The index if the first component to get.
236 * @return A new name.
237 */
238 Name
239 getSubName(size_t iStartComponent) const;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700240
Jeff Thompsond0159d72013-09-23 13:34:15 -0700241 /**
242 * Return a new Name with the first nComponents components of this Name.
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800243 * @param nComponents The number of prefix components. If nComponents is -N then return the prefix up
244 * to name.size() - N. For example getPrefix(-1) returns the name without the final component.
Jeff Thompsond0159d72013-09-23 13:34:15 -0700245 * @return A new Name.
246 */
247 Name
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800248 getPrefix(int nComponents) const
Jeff Thompsond0159d72013-09-23 13:34:15 -0700249 {
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800250 if (nComponents < 0)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800251 return getSubName(0, m_nameBlock.elements_size() + nComponents);
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800252 else
253 return getSubName(0, nComponents);
Jeff Thompsond0159d72013-09-23 13:34:15 -0700254 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700255
Jeff Thompsond0159d72013-09-23 13:34:15 -0700256 /**
Jeff Thompsone6063512013-07-01 15:11:28 -0700257 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700258 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700259 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700260 std::string
Jeff Thompson0050abe2013-09-17 12:50:25 -0700261 toUri() const;
Jeff Thompsond129ac12013-10-11 14:30:12 -0700262
263 /**
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700264 * @brief Append a component with the number encoded as nonNegativeInteger
265 *
266 * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding
267 *
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600268 * @param number The non-negative number
269 * @return This name so that you can chain calls to append.
270 */
271 Name&
272 appendNumber(uint64_t number)
273 {
274 m_nameBlock.push_back(Component::fromNumber(number));
275 return *this;
276 }
277
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700278 /**
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700279 * @brief An alias for appendNumber(uint64_t)
280 */
281 Name&
282 appendVersion(uint64_t number)
283 {
284 return appendNumber(number);
285 }
286
287 /**
288 * @brief Append a component with the encoded version number (current UNIX timestamp
289 * in milliseconds)
290 */
291 Name&
292 appendVersion();
293
294 /**
295 * @brief An alias for appendNumber(uint64_t)
296 */
297 Name&
298 appendSegment(uint64_t number)
299 {
300 return appendNumber(number);
301 }
302
303 /**
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700304 * Check if this name has the same component count and components as the given name.
305 * @param name The Name to check.
306 * @return true if the names are equal, otherwise false.
307 */
308 bool
309 equals(const Name& name) const;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700310
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700311 /**
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700312 * Check if the N components of this name are the same as the first N components of the given name.
313 * @param name The Name to check.
314 * @return true if this matches the given name, otherwise false. This always returns true if this name is empty.
315 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700316 bool
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800317 isPrefixOf(const Name& name) const;
318
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700319 //
320 // vector equivalent interface.
321 //
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800322
323 /**
324 * @brief Check if name is emtpy
325 */
326 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700327 empty() const
328 {
329 return m_nameBlock.elements().empty();
330 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700331
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700332 /**
333 * Get the number of components.
334 * @return The number of components.
335 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700336 size_t
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700337 size() const
338 {
339 return m_nameBlock.elements_size();
340 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700341
342 /**
343 * Get the component at the given index.
344 * @param i The index of the component, starting from 0.
345 * @return The name component at the index.
346 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700347 const Component&
Alexander Afanasyev8f9aa8bed2013-12-30 15:58:57 -0800348 get(ssize_t i) const
349 {
350 if (i >= 0)
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800351 return reinterpret_cast<const Component&>(m_nameBlock.elements()[i]);
Alexander Afanasyev8f9aa8bed2013-12-30 15:58:57 -0800352 else
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800353 return reinterpret_cast<const Component&>(m_nameBlock.elements()[size() + i]);
354 }
355
356 const Component&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700357 operator[](ssize_t i) const
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800358 {
359 return get(i);
Alexander Afanasyev8f9aa8bed2013-12-30 15:58:57 -0800360 }
Alexander Afanasyevc2344292014-03-02 00:08:00 +0000361
362 /**
363 * @brief Get component at the specified index
364 *
365 * Unlike get() and operator[] methods, at() checks for out of bounds
366 * and will throw Name::Error when it happens
367 *
368 * @throws Name::Error if index out of bounds
369 */
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800370 const Component&
371 at(ssize_t i) const
372 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700373 if ((i >= 0 && static_cast<size_t>(i) >= size()) ||
374 (i < 0 && static_cast<size_t>(-i) > size()))
Alexander Afanasyevc2344292014-03-02 00:08:00 +0000375 throw Error("Requested component does not exist (out of bounds)");
376
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800377 return get(i);
378 }
379
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800380 /**
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700381 * @brief Compare this to the other Name using NDN canonical ordering.
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800382 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700383 * If the first components of each name are not equal, this returns -1 if the first comes
384 * before the second using the NDN canonical ordering for name components, or 1 if it
385 * comes after. If they are equal, this compares the second components of each name, etc.
386 * If both names are the same up to the size of the shorter name, this returns -1 if the
387 * first name is shorter than the second or 1 if it is longer. For example, if you
388 * std::sort gives: /a/b/d /a/b/cc /c /c/a /bb . This is intuitive because all names with
389 * the prefix /a are next to each other. But it may be also be counter-intuitive because
390 * /c comes before /bb according to NDN canonical ordering since it is shorter. @param
391 * other The other Name to compare with. @return 0 If they compare equal, -1 if *this
392 * comes before other in the canonical ordering, or 1 if *this comes after other in the
393 * canonical ordering.
394 *
395 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800396 */
397 int
398 compare(const Name& other) const;
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700399
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700400 /**
401 * Append the component
402 * @param component The component of type T.
403 */
404 template<class T> void
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700405 push_back(const T& component)
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700406 {
407 append(component);
408 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700409
Jeff Thompson91737f52013-10-04 11:07:24 -0700410 /**
411 * Check if this name has the same component count and components as the given name.
412 * @param name The Name to check.
413 * @return true if the names are equal, otherwise false.
414 */
415 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700416 operator==(const Name& name) const
417 {
418 return equals(name);
419 }
Jeff Thompson91737f52013-10-04 11:07:24 -0700420
421 /**
422 * Check if this name has the same component count and components as the given name.
423 * @param name The Name to check.
424 * @return true if the names are not equal, otherwise false.
425 */
426 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700427 operator!=(const Name& name) const
428 {
429 return !equals(name);
430 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700431
Jeff Thompson82568ad2013-12-17 15:17:40 -0800432 /**
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800433 * Return true if this is less than or equal to the other Name in the NDN canonical ordering.
434 * @param other The other Name to compare with.
435 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700436 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Jeff Thompson82568ad2013-12-17 15:17:40 -0800437 */
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800438 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700439 operator<=(const Name& other) const
440 {
441 return compare(other) <= 0;
442 }
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800443
444 /**
445 * Return true if this is less than the other Name in the NDN canonical ordering.
446 * @param other The other Name to compare with.
447 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700448 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800449 */
450 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700451 operator<(const Name& other) const
452 {
453 return compare(other) < 0;
454 }
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800455
456 /**
457 * Return true if this is less than or equal to the other Name in the NDN canonical ordering.
458 * @param other The other Name to compare with.
459 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700460 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800461 */
462 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700463 operator>=(const Name& other) const
464 {
465 return compare(other) >= 0;
466 }
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800467
468 /**
469 * Return true if this is greater than the other Name in the NDN canonical ordering.
470 * @param other The other Name to compare with.
471 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700472 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800473 */
474 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700475 operator>(const Name& other) const
476 {
477 return compare(other) > 0;
478 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700479
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700480 //
481 // Iterator interface to name components.
482 //
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700483
484 /**
485 * Begin iterator (const).
486 */
487 const_iterator
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800488 begin() const
489 {
490 return reinterpret_cast<const_iterator>(&*m_nameBlock.elements().begin());
491 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700492
493 /**
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700494 * End iterator (const).
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800495 *
496 * @todo Check if this crash when there are no elements in the buffer
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700497 */
498 const_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800499 end() const
500 {
501 return reinterpret_cast<const_iterator>(&*m_nameBlock.elements().end());
502 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700503
504 /**
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700505 * Reverse begin iterator (const).
506 */
507 const_reverse_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800508 rbegin() const
509 {
510 return const_reverse_iterator(end());
511 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700512
513 /**
514 * Reverse end iterator (const).
515 */
516 const_reverse_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800517 rend() const
518 {
519 return const_reverse_iterator(begin());
520 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700521
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700522private:
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800523 mutable Block m_nameBlock;
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800524};
525
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700526inline std::ostream&
527operator<<(std::ostream& os, const Name& name)
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700528{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700529 if (name.empty())
530 {
531 os << "/";
532 }
533 else
534 {
535 for (Name::const_iterator i = name.begin(); i != name.end(); i++) {
536 os << "/";
537 i->toEscapedString(os);
538 }
539 }
540 return os;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700541}
542
543inline std::string
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800544Name::toUri() const
Jeff Thompson49e321a2013-10-04 17:35:59 -0700545{
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800546 std::ostringstream os;
547 os << *this;
548 return os.str();
Jeff Thompson49e321a2013-10-04 17:35:59 -0700549}
550
Alexander Afanasyev0df28362014-03-20 17:31:43 -0700551inline std::istream&
552operator>>(std::istream& is, Name& name)
553{
554 std::string inputString;
555 is >> inputString;
556 name.set(inputString);
557
558 return is;
559}
560
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700561
562inline void
563Name::set(const char* uri_cstr)
564{
565 clear();
566
567 std::string uri = uri_cstr;
568 trim(uri);
569 if (uri.size() == 0)
570 return;
571
572 size_t iColon = uri.find(':');
573 if (iColon != std::string::npos) {
574 // Make sure the colon came before a '/'.
575 size_t iFirstSlash = uri.find('/');
576 if (iFirstSlash == std::string::npos || iColon < iFirstSlash) {
577 // Omit the leading protocol such as ndn:
578 uri.erase(0, iColon + 1);
579 trim(uri);
580 }
581 }
582
583 // Trim the leading slash and possibly the authority.
584 if (uri[0] == '/') {
585 if (uri.size() >= 2 && uri[1] == '/') {
586 // Strip the authority following "//".
587 size_t iAfterAuthority = uri.find('/', 2);
588 if (iAfterAuthority == std::string::npos)
589 // Unusual case: there was only an authority.
590 return;
591 else {
592 uri.erase(0, iAfterAuthority + 1);
593 trim(uri);
594 }
595 }
596 else {
597 uri.erase(0, 1);
598 trim(uri);
599 }
600 }
601
602 size_t iComponentStart = 0;
603
604 // Unescape the components.
605 while (iComponentStart < uri.size()) {
606 size_t iComponentEnd = uri.find("/", iComponentStart);
607 if (iComponentEnd == std::string::npos)
608 iComponentEnd = uri.size();
609
610 Component component = Component::fromEscapedString(&uri[0], iComponentStart, iComponentEnd);
611 // Ignore illegal components. This also gets rid of a trailing '/'.
612 if (!component.empty())
613 append(Component(component));
614
615 iComponentStart = iComponentEnd + 1;
616 }
617}
618
619inline Name&
620Name::append(const Name& name)
621{
622 if (&name == this)
623 // Copying from this name, so need to make a copy first.
624 return append(Name(name));
625
626 for (size_t i = 0; i < name.size(); ++i)
627 append(name.at(i));
628
629 return *this;
630}
631
632inline Name&
633Name::appendVersion()
634{
635 appendNumber(time::toUnixTimestamp(time::system_clock::now()).count());
636 return *this;
637}
638
639inline Name
640Name::getSubName(size_t iStartComponent, size_t nComponents) const
641{
642 Name result;
643
644 size_t iEnd = iStartComponent + nComponents;
645 for (size_t i = iStartComponent; i < iEnd && i < size(); ++i)
646 result.append(at(i));
647
648 return result;
649}
650
651inline Name
652Name::getSubName(size_t iStartComponent) const
653{
654 Name result;
655
656 for (size_t i = iStartComponent; i < size(); ++i)
657 result.append(at(i));
658
659 return result;
660}
661
662inline bool
663Name::equals(const Name& name) const
664{
665 if (size() != name.size())
666 return false;
667
668 for (size_t i = 0; i < size(); ++i) {
669 if (at(i) != name.at(i))
670 return false;
671 }
672
673 return true;
674}
675
676inline bool
677Name::isPrefixOf(const Name& name) const
678{
679 // This name is longer than the name we are checking it against.
680 if (size() > name.size())
681 return false;
682
683 // Check if at least one of given components doesn't match.
684 for (size_t i = 0; i < size(); ++i) {
685 if (at(i) != name.at(i))
686 return false;
687 }
688
689 return true;
690}
691
692
693inline int
694Name::compare(const Name& other) const
695{
696 for (size_t i = 0; i < size() && i < other.size(); ++i) {
697 int comparison = at(i).compare(other.at(i));
698 if (comparison == 0)
699 // The components at this index are equal, so check the next components.
700 continue;
701
702 // Otherwise, the result is based on the components at this index.
703 return comparison;
704 }
705
706 // The components up to min(this.size(), other.size()) are equal, so the shorter name is less.
707 if (size() < other.size())
708 return -1;
709 else if (size() > other.size())
710 return 1;
711 else
712 return 0;
713}
714
715
716
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800717template<bool T>
718inline size_t
719Name::wireEncode(EncodingImpl<T>& blk) const
720{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700721 size_t totalLength = 0;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700722
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700723 for (const_reverse_iterator i = rbegin();
724 i != rend();
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800725 ++i)
726 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700727 totalLength += i->wireEncode(blk);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800728 }
729
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700730 totalLength += blk.prependVarNumber(totalLength);
731 totalLength += blk.prependVarNumber(Tlv::Name);
732 return totalLength;
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800733}
734
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700735inline const Block&
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800736Name::wireEncode() const
737{
738 if (m_nameBlock.hasWire())
739 return m_nameBlock;
740
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800741 EncodingEstimator estimator;
742 size_t estimatedSize = wireEncode(estimator);
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700743
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800744 EncodingBuffer buffer(estimatedSize, 0);
745 wireEncode(buffer);
746
747 m_nameBlock = buffer.block();
748 m_nameBlock.parse();
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700749
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800750 return m_nameBlock;
751}
752
753inline void
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700754Name::wireDecode(const Block& wire)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800755{
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800756 if (wire.type() != Tlv::Name)
757 throw Tlv::Error("Unexpected TLV type when decoding Name");
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700758
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800759 m_nameBlock = wire;
760 m_nameBlock.parse();
761}
762
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800763inline bool
764Name::hasWire() const
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800765{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800766 return m_nameBlock.hasWire();
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800767}
768
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800769} // namespace ndn
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700770
771#endif