blob: bd6e02b92a18a939f9503ca06c62da308882820d [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompson47eecfc2013-07-07 22:56:46 -07002/**
Junxiao Shia6452ac2015-01-23 11:21:06 -07003 * Copyright (c) 2013-2015 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Jeff Thompson <jefft0@remap.ucla.edu>
22 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
23 * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070024 */
25
26#ifndef NDN_NAME_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070027#define NDN_NAME_HPP
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070028
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080029#include "common.hpp"
30#include "name-component.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070031
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080032#include <boost/iterator/reverse_iterator.hpp>
33
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070034namespace ndn {
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080035
Jeff Thompsonc7d65502013-11-06 17:22:26 -080036/**
37 * A Name holds an array of Name::Component and represents an NDN name.
38 */
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070039class Name : public enable_shared_from_this<Name>
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070040{
Jeff Thompson9c41dfe2013-06-27 12:10:25 -070041public:
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070042 /// @brief Error that can be thrown from Name
43 class Error : public name::Component::Error
44 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070045 public:
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070046 explicit
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070047 Error(const std::string& what)
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070048 : name::Component::Error(what)
49 {
50 }
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070051 };
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080052
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080053 typedef name::Component Component;
54
55 typedef std::vector<Component> component_container;
56
57 typedef Component value_type;
58 typedef void allocator_type;
59 typedef Component& reference;
60 typedef const Component const_reference;
61 typedef Component* pointer;
62 typedef const Component* const_pointer;
63 typedef Component* iterator;
64 typedef const Component* const_iterator;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070065
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080066 typedef boost::reverse_iterator<iterator> reverse_iterator;
67 typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
68
69 typedef component_container::difference_type difference_type;
70 typedef component_container::size_type size_type;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070071
Jeff Thompson443398d2013-07-02 19:45:46 -070072 /**
73 * Create a new Name with no components.
74 */
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080075 Name()
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060076 : m_nameBlock(tlv::Name)
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -080077 {
Jeff Thompson016ed642013-07-02 14:39:06 -070078 }
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080079
80 /**
81 * @brief Create Name object from wire block
82 *
83 * This is a more efficient equivalent for
84 * @code
85 * Name name;
86 * name.wireDecode(wire);
87 * @endcode
88 */
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080089 explicit
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070090 Name(const Block& wire)
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080091 {
92 m_nameBlock = wire;
93 m_nameBlock.parse();
94 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070095
Jeff Thompson3f2175b2013-07-31 17:12:47 -070096 /**
Jeff Thompson443398d2013-07-02 19:45:46 -070097 * Parse the uri according to the NDN URI Scheme and create the name with the components.
Jeff Thompson3f2175b2013-07-31 17:12:47 -070098 * @param uri The URI string.
Jeff Thompson443398d2013-07-02 19:45:46 -070099 */
Jeff Thompson3549ef32013-09-25 14:05:17 -0700100 Name(const char* uri)
Jeff Thompson67515bd2013-08-15 17:43:22 -0700101 {
102 set(uri);
103 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700104
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700105 /**
Jeff Thompson3549ef32013-09-25 14:05:17 -0700106 * Parse the uri according to the NDN URI Scheme and create the name with the components.
107 * @param uri The URI string.
108 */
109 Name(const std::string& uri)
110 {
111 set(uri.c_str());
112 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700113
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800114 /**
115 * @brief Fast encoding or block size estimation
116 */
Alexander Afanasyev74633892015-02-08 18:08:46 -0800117 template<encoding::Tag TAG>
Wentao Shang77949212014-02-01 23:42:24 -0800118 size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800119 wireEncode(EncodingImpl<TAG>& block) const;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700120
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700121 const Block&
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800122 wireEncode() const;
Alexander Afanasyev848c61a2014-01-03 13:52:04 -0800123
124 void
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700125 wireDecode(const Block& wire);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800126
127 /**
128 * @brief Check if already has wire
129 */
130 bool
131 hasWire() const;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700132
Jeff Thompsonb468c312013-07-01 17:50:14 -0700133 /**
Jeff Thompson67515bd2013-08-15 17:43:22 -0700134 * Parse the uri according to the NDN URI Scheme and set the name with the components.
Jeff Thompson7781b392013-12-17 11:45:59 -0800135 * @param uri The null-terminated URI string.
Jeff Thompson67515bd2013-08-15 17:43:22 -0700136 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700137 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700138 set(const char* uri);
Jeff Thompson67515bd2013-08-15 17:43:22 -0700139
140 /**
Jeff Thompson7781b392013-12-17 11:45:59 -0800141 * Parse the uri according to the NDN URI Scheme and set the name with the components.
142 * @param uri The URI string.
143 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700144 void
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800145 set(const std::string& uri)
146 {
147 set(uri.c_str());
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700148 }
149
Jeff Thompson7781b392013-12-17 11:45:59 -0800150 /**
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700151 * Append a new component, copying from value of length valueLength.
Jeff Thompson26b0d792013-09-23 16:19:01 -0700152 * @return This name so that you can chain calls to append.
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700153 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700154 Name&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700155 append(const uint8_t* value, size_t valueLength)
Jeff Thompson0f743452013-09-12 14:23:18 -0700156 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800157 m_nameBlock.push_back(Component(value, valueLength));
Jeff Thompson26b0d792013-09-23 16:19:01 -0700158 return *this;
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700159 }
Jeff Thompsonf72b1ac2013-08-16 16:44:41 -0700160
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800161 /**
Alexander Afanasyev74633892015-02-08 18:08:46 -0800162 * @brief Append a new component, copying from value frome the range [@p first, @p last) of bytes
163 * @param first Iterator pointing to the beginning of the buffer
164 * @param last Iterator pointing to the ending of the buffer
165 * @tparam Iterator iterator type satisfying at least InputIterator concept. Implementation
166 * is more optimal when the iterator type satisfies RandomAccessIterator concept.
167 * It is required that sizeof(std::iterator_traits<Iterator>::value_type) == 1.
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800168 * @return This name so that you can chain calls to append.
169 */
Alexander Afanasyev74633892015-02-08 18:08:46 -0800170 template<class Iterator>
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800171 Name&
Alexander Afanasyev74633892015-02-08 18:08:46 -0800172 append(Iterator first, Iterator last)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800173 {
Alexander Afanasyev74633892015-02-08 18:08:46 -0800174 m_nameBlock.push_back(Component(first, last));
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800175 return *this;
176 }
177
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700178 Name&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700179 append(const Component& value)
Jeff Thompson21eb7212013-09-26 09:05:40 -0700180 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800181 m_nameBlock.push_back(value);
Jeff Thompson21eb7212013-09-26 09:05:40 -0700182 return *this;
183 }
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800184
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800185 /**
186 * @brief Append name component that represented as a string
187 *
188 * Note that this method is necessary to ensure correctness and unambiguity of
189 * ``append("string")`` operations (both Component and Name can be implicitly
190 * converted from string, each having different outcomes
191 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700192 Name&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700193 append(const char* value)
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800194 {
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800195 m_nameBlock.push_back(Component(value));
Alexander Afanasyev594cdb22014-01-03 15:11:33 -0800196 return *this;
197 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700198
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800199 Name&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700200 append(const Block& value)
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800201 {
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600202 if (value.type() == tlv::NameComponent)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800203 m_nameBlock.push_back(value);
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800204 else
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600205 m_nameBlock.push_back(Block(tlv::NameComponent, value));
Alexander Afanasyev380420b2014-02-09 20:52:29 -0800206
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800207 return *this;
208 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700209
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700210 /**
Jeff Thompson26b0d792013-09-23 16:19:01 -0700211 * Append the components of the given name to this name.
212 * @param name The Name with components to append.
213 * @return This name so that you can chain calls to append.
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700214 */
Jeff Thompson26b0d792013-09-23 16:19:01 -0700215 Name&
216 append(const Name& name);
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700217
Jeff Thompson0aa66f22013-09-23 13:02:13 -0700218 /**
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700219 * Clear all the components.
220 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700221 void
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800222 clear()
223 {
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600224 m_nameBlock = Block(tlv::Name);
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700225 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700226
Jeff Thompsone5f839b2013-06-28 12:50:38 -0700227 /**
Jeff Thompsond0159d72013-09-23 13:34:15 -0700228 * Get a new name, constructed as a subset of components.
229 * @param iStartComponent The index if the first component to get.
230 * @param nComponents The number of components starting at iStartComponent.
Junxiao Shia6452ac2015-01-23 11:21:06 -0700231 * Use npos to get the sub Name until the end of this Name.
Jeff Thompsond0159d72013-09-23 13:34:15 -0700232 * @return A new name.
233 */
234 Name
Junxiao Shia6452ac2015-01-23 11:21:06 -0700235 getSubName(size_t iStartComponent, size_t nComponents = npos) const;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700236
Jeff Thompsond0159d72013-09-23 13:34:15 -0700237 /**
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700238 * @brief Return a new Name with the first nComponents components of this Name.
239 *
240 * @param nComponents The number of prefix components. If nComponents is -N then return
241 * the prefix up to name.size() - N. For example getPrefix(-1)
242 * returns the name without the final component.
Jeff Thompsond0159d72013-09-23 13:34:15 -0700243 * @return A new Name.
244 */
245 Name
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700246 getPrefix(ssize_t nComponents) const
Jeff Thompsond0159d72013-09-23 13:34:15 -0700247 {
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800248 if (nComponents < 0)
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800249 return getSubName(0, m_nameBlock.elements_size() + nComponents);
Jeff Thompsoneb0358f2013-12-17 10:59:53 -0800250 else
251 return getSubName(0, nComponents);
Jeff Thompsond0159d72013-09-23 13:34:15 -0700252 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700253
Jeff Thompsond0159d72013-09-23 13:34:15 -0700254 /**
Jeff Thompsone6063512013-07-01 15:11:28 -0700255 * Encode this name as a URI.
Jeff Thompson3f2175b2013-07-31 17:12:47 -0700256 * @return The encoded URI.
Jeff Thompsone6063512013-07-01 15:11:28 -0700257 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700258 std::string
Jeff Thompson0050abe2013-09-17 12:50:25 -0700259 toUri() const;
Jeff Thompsond129ac12013-10-11 14:30:12 -0700260
261 /**
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700262 * @brief Append a component with the number encoded as nonNegativeInteger
263 *
264 * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding
265 *
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600266 * @param number The non-negative number
267 * @return This name so that you can chain calls to append.
268 */
269 Name&
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700270 appendNumber(uint64_t number);
Steve DiBenedettoc145d492014-03-11 16:35:45 -0600271
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700272 /**
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700273 * @brief Create a component encoded as NameComponentWithMarker
274 *
275 * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf
276 *
277 * @param marker 1-byte marker octet
278 * @param number The non-negative number
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700279 */
280 Name&
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700281 appendNumberWithMarker(uint8_t marker, uint64_t number);
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700282
283 /**
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700284 * @brief Append version using NDN naming conventions
285 *
286 * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf
287 */
288 Name&
289 appendVersion(uint64_t version);
290
291 /**
292 * @brief Append version using NDN naming conventions based on current UNIX timestamp
293 * in milliseconds
294 *
295 * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700296 */
297 Name&
298 appendVersion();
299
300 /**
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700301 * @brief Append segment number (sequential) using NDN naming conventions
302 *
303 * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700304 */
305 Name&
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700306 appendSegment(uint64_t segmentNo);
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700307
308 /**
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700309 * @brief Append segment byte offset using NDN naming conventions
310 *
311 * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf
312 */
313 Name&
314 appendSegmentOffset(uint64_t offset);
315
316 /**
317 * @brief Append timestamp using NDN naming conventions
318 *
319 * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf
320 */
321 Name&
322 appendTimestamp(const time::system_clock::TimePoint& timePoint = time::system_clock::now());
323
324 /**
325 * @brief Append sequence number using NDN naming conventions
326 *
327 * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf
328 */
329 Name&
330 appendSequenceNumber(uint64_t seqNo);
331
332 /**
Alexander Afanasyev6486d522014-10-23 14:14:11 -0700333 * @brief Append ImplicitSha256Digest
334 */
335 Name&
336 appendImplicitSha256Digest(const ConstBufferPtr& digest);
337
338 /**
339 * @brief Append ImplicitSha256Digest
340 */
341 Name&
342 appendImplicitSha256Digest(const uint8_t* digest, size_t digestSize);
343
344 /**
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700345 * @brief Get the successor of a name
346 *
347 * The successor of a name is defined as follows:
348 *
349 * N represents the set of NDN Names, and X,Y ∈ N.
350 * Operator < is defined by canonical order on N.
351 * Y is the successor of X, if (a) X < Y, and (b) ∄ Z ∈ N s.t. X < Z < Y.
352 *
353 * In plain words, successor of a name is the same name, but with its last component
354 * advanced to a next possible value.
355 *
356 * Examples:
357 *
358 * - successor for / is /%00
359 * - successor for /%00%01/%01%02 is /%00%01/%01%03
360 * - successor for /%00%01/%01%FF is /%00%01/%02%00
361 * - successor for /%00%01/%FF%FF is /%00%01/%00%00%00
362 *
Shuo Chen5aa8c742014-06-22 15:00:02 +0800363 * @return a new name
364 */
365 Name
366 getSuccessor() const;
367
368 /**
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700369 * Check if this name has the same component count and components as the given name.
370 * @param name The Name to check.
371 * @return true if the names are equal, otherwise false.
372 */
373 bool
374 equals(const Name& name) const;
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700375
Jeff Thompson3c2ab012013-10-02 14:18:16 -0700376 /**
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700377 * @brief Check if the N components of this name are the same as the first N components
378 * of the given name.
379 *
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700380 * @param name The Name to check.
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700381 * @return true if this matches the given name, otherwise false. This always returns
382 * true if this name is empty.
Jeff Thompsonec7789a2013-08-21 11:08:36 -0700383 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700384 bool
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800385 isPrefixOf(const Name& name) const;
386
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700387 //
388 // vector equivalent interface.
389 //
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800390
391 /**
392 * @brief Check if name is emtpy
393 */
394 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700395 empty() const
396 {
397 return m_nameBlock.elements().empty();
398 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700399
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700400 /**
401 * Get the number of components.
402 * @return The number of components.
403 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700404 size_t
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700405 size() const
406 {
407 return m_nameBlock.elements_size();
408 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700409
410 /**
411 * Get the component at the given index.
412 * @param i The index of the component, starting from 0.
413 * @return The name component at the index.
414 */
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700415 const Component&
Alexander Afanasyev8f9aa8bed2013-12-30 15:58:57 -0800416 get(ssize_t i) const
417 {
418 if (i >= 0)
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800419 return reinterpret_cast<const Component&>(m_nameBlock.elements()[i]);
Alexander Afanasyev8f9aa8bed2013-12-30 15:58:57 -0800420 else
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800421 return reinterpret_cast<const Component&>(m_nameBlock.elements()[size() + i]);
422 }
423
424 const Component&
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700425 operator[](ssize_t i) const
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800426 {
427 return get(i);
Alexander Afanasyev8f9aa8bed2013-12-30 15:58:57 -0800428 }
Alexander Afanasyevc2344292014-03-02 00:08:00 +0000429
430 /**
431 * @brief Get component at the specified index
432 *
433 * Unlike get() and operator[] methods, at() checks for out of bounds
434 * and will throw Name::Error when it happens
435 *
436 * @throws Name::Error if index out of bounds
437 */
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800438 const Component&
439 at(ssize_t i) const
440 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700441 if ((i >= 0 && static_cast<size_t>(i) >= size()) ||
442 (i < 0 && static_cast<size_t>(-i) > size()))
Alexander Afanasyevc2344292014-03-02 00:08:00 +0000443 throw Error("Requested component does not exist (out of bounds)");
444
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800445 return get(i);
446 }
447
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800448 /**
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700449 * @brief Compare this to the other Name using NDN canonical ordering.
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800450 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700451 * If the first components of each name are not equal, this returns -1 if the first comes
452 * before the second using the NDN canonical ordering for name components, or 1 if it
453 * comes after. If they are equal, this compares the second components of each name, etc.
454 * If both names are the same up to the size of the shorter name, this returns -1 if the
455 * first name is shorter than the second or 1 if it is longer. For example, if you
456 * std::sort gives: /a/b/d /a/b/cc /c /c/a /bb . This is intuitive because all names with
457 * the prefix /a are next to each other. But it may be also be counter-intuitive because
458 * /c comes before /bb according to NDN canonical ordering since it is shorter. @param
Junxiao Shia6452ac2015-01-23 11:21:06 -0700459 * other The other Name to compare with.
460 *
461 * @retval 0 if they compare equal
462 * @retval -1 if *this comes before other in the canonical ordering
463 * @retval 1 if *this comes after other in the canonical ordering
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700464 *
465 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800466 */
467 int
Junxiao Shia6452ac2015-01-23 11:21:06 -0700468 compare(const Name& other) const
469 {
470 return this->compare(0, npos, other);
471 }
472
473 /** \brief compares [pos1, pos1+count1) components in this Name
474 * to [pos2, pos2+count2) components in \p other
475 *
476 * This is equivalent to this->getSubName(pos1, count1).compare(other.getSubName(pos2, count2));
477 */
478 int
479 compare(size_t pos1, size_t count1,
480 const Name& other, size_t pos2 = 0, size_t count2 = npos) const;
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700481
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700482 /**
483 * Append the component
484 * @param component The component of type T.
485 */
486 template<class T> void
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700487 push_back(const T& component)
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700488 {
489 append(component);
490 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700491
Jeff Thompson91737f52013-10-04 11:07:24 -0700492 /**
493 * Check if this name has the same component count and components as the given name.
494 * @param name The Name to check.
495 * @return true if the names are equal, otherwise false.
496 */
497 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700498 operator==(const Name& name) const
499 {
500 return equals(name);
501 }
Jeff Thompson91737f52013-10-04 11:07:24 -0700502
503 /**
504 * Check if this name has the same component count and components as the given name.
505 * @param name The Name to check.
506 * @return true if the names are not equal, otherwise false.
507 */
508 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700509 operator!=(const Name& name) const
510 {
511 return !equals(name);
512 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700513
Jeff Thompson82568ad2013-12-17 15:17:40 -0800514 /**
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800515 * Return true if this is less than or equal to the other Name in the NDN canonical ordering.
516 * @param other The other Name to compare with.
517 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700518 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Jeff Thompson82568ad2013-12-17 15:17:40 -0800519 */
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800520 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700521 operator<=(const Name& other) const
522 {
523 return compare(other) <= 0;
524 }
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800525
526 /**
527 * Return true if this is less than the other Name in the NDN canonical ordering.
528 * @param other The other Name to compare with.
529 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700530 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800531 */
532 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700533 operator<(const Name& other) const
534 {
535 return compare(other) < 0;
536 }
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800537
538 /**
539 * Return true if this is less than or equal to the other Name in the NDN canonical ordering.
540 * @param other The other Name to compare with.
541 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700542 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800543 */
544 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700545 operator>=(const Name& other) const
546 {
547 return compare(other) >= 0;
548 }
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800549
550 /**
551 * Return true if this is greater than the other Name in the NDN canonical ordering.
552 * @param other The other Name to compare with.
553 *
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700554 * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order
Jeff Thompsonafc45a92014-01-15 12:42:45 -0800555 */
556 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700557 operator>(const Name& other) const
558 {
559 return compare(other) > 0;
560 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700561
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700562 //
563 // Iterator interface to name components.
564 //
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700565
566 /**
567 * Begin iterator (const).
568 */
569 const_iterator
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800570 begin() const
571 {
572 return reinterpret_cast<const_iterator>(&*m_nameBlock.elements().begin());
573 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700574
575 /**
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700576 * End iterator (const).
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800577 *
578 * @todo Check if this crash when there are no elements in the buffer
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700579 */
580 const_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800581 end() const
582 {
583 return reinterpret_cast<const_iterator>(&*m_nameBlock.elements().end());
584 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700585
586 /**
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700587 * Reverse begin iterator (const).
588 */
589 const_reverse_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800590 rbegin() const
591 {
592 return const_reverse_iterator(end());
593 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700594
595 /**
596 * Reverse end iterator (const).
597 */
598 const_reverse_iterator
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800599 rend() const
600 {
601 return const_reverse_iterator(begin());
602 }
Jeff Thompsonec39fbd2013-10-04 10:56:23 -0700603
Junxiao Shia6452ac2015-01-23 11:21:06 -0700604public:
605 /** \brief indicates "until the end" in getSubName and compare
606 */
607 static const size_t npos;
608
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700609private:
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800610 mutable Block m_nameBlock;
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800611};
612
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700613std::ostream&
614operator<<(std::ostream& os, const Name& name);
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -0700615
Alexander Afanasyev15f67312014-07-22 15:11:09 -0700616std::istream&
617operator>>(std::istream& is, Name& name);
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800618
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800619inline bool
620Name::hasWire() const
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800621{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800622 return m_nameBlock.hasWire();
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -0800623}
624
Alexander Afanasyev95e8c2f2014-02-06 17:29:30 -0800625} // namespace ndn
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700626
Yingdi Yu90e23582014-11-06 14:21:04 -0800627namespace std {
628template<>
629struct hash<ndn::Name>
630{
631 size_t
632 operator()(const ndn::Name& name) const;
633};
634
635} // namespace std
636
Jeff Thompson9c41dfe2013-06-27 12:10:25 -0700637#endif