name.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2020 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * 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.
20  *
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/>
24  */
25 
26 #ifndef NDN_CXX_NAME_HPP
27 #define NDN_CXX_NAME_HPP
28 
30 
31 #include <iterator>
32 
33 namespace ndn {
34 
35 class Name;
36 
39 using PartialName = Name;
40 
44 class Name
45 {
46 public: // nested types
48 
50  using component_container = std::vector<Component>;
51 
52  // Name appears as a container of name components
54  using allocator_type = void;
55  using reference = Component&;
56  using const_reference = const Component&;
57  using pointer = Component*;
58  using const_pointer = const Component*;
59  using iterator = const Component*; // disallow modifying via iterator
60  using const_iterator = const Component*;
61  using reverse_iterator = std::reverse_iterator<iterator>;
62  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
63  using difference_type = component_container::difference_type;
64  using size_type = component_container::size_type;
65 
66 public: // constructors, encoding, decoding
70  Name();
71 
81  explicit
82  Name(const Block& wire);
83 
88  Name(const char* uri);
89 
94  Name(std::string uri);
95 
99  void
100  toUri(std::ostream& os, name::UriFormat format = name::UriFormat::DEFAULT) const;
101 
107  std::string
109 
112  bool
113  hasWire() const noexcept
114  {
115  return m_wire.hasWire();
116  }
117 
120  template<encoding::Tag TAG>
121  size_t
122  wireEncode(EncodingImpl<TAG>& encoder) const;
123 
127  const Block&
128  wireEncode() const;
129 
134  void
135  wireDecode(const Block& wire);
136 
139  Name
140  deepCopy() const;
141 
142 public: // access
145  NDN_CXX_NODISCARD bool
146  empty() const
147  {
148  return m_wire.elements().empty();
149  }
150 
153  size_t
154  size() const
155  {
156  return m_wire.elements_size();
157  }
158 
164  const Component&
165  get(ssize_t i) const
166  {
167  if (i < 0) {
168  i += static_cast<ssize_t>(size());
169  }
170  return reinterpret_cast<const Component&>(m_wire.elements()[i]);
171  }
172 
175  const Component&
176  operator[](ssize_t i) const
177  {
178  return get(i);
179  }
180 
187  const Component&
188  at(ssize_t i) const;
189 
203  getSubName(ssize_t iStartComponent, size_t nComponents = npos) const;
204 
212  getPrefix(ssize_t nComponents) const
213  {
214  if (nComponents < 0)
215  return getSubName(0, size() + nComponents);
216  else
217  return getSubName(0, nComponents);
218  }
219 
220 public: // iterators
224  begin() const
225  {
226  return reinterpret_cast<const_iterator>(m_wire.elements().data());
227  }
228 
232  end() const
233  {
234  return reinterpret_cast<const_iterator>(m_wire.elements().data() + m_wire.elements().size());
235  }
236 
240  rbegin() const
241  {
242  return const_reverse_iterator(end());
243  }
244 
248  rend() const
249  {
250  return const_reverse_iterator(begin());
251  }
252 
253 public: // modifiers
261  Name&
262  set(ssize_t i, const Component& component);
263 
271  Name&
272  set(ssize_t i, Component&& component);
273 
277  Name&
278  append(const Component& component)
279  {
280  m_wire.push_back(component);
281  return *this;
282  }
283 
287  Name&
288  append(Component&& component)
289  {
290  m_wire.push_back(std::move(component));
291  return *this;
292  }
293 
298  Name&
299  append(uint32_t type, const uint8_t* value, size_t count)
300  {
301  return append(Component(type, value, count));
302  }
303 
307  Name&
308  append(const uint8_t* value, size_t count)
309  {
310  return append(Component(value, count));
311  }
312 
321  template<class Iterator>
322  Name&
323  append(uint32_t type, Iterator first, Iterator last)
324  {
325  return append(Component(type, first, last));
326  }
327 
335  template<class Iterator>
336  Name&
337  append(Iterator first, Iterator last)
338  {
339  return append(Component(first, last));
340  }
341 
347  Name&
348  append(const char* str)
349  {
350  return append(Component(str));
351  }
352 
358  Name&
359  append(Block value)
360  {
361  if (value.type() == tlv::GenericNameComponent) {
362  m_wire.push_back(std::move(value));
363  }
364  else {
365  m_wire.push_back(Block(tlv::GenericNameComponent, std::move(value)));
366  }
367  return *this;
368  }
369 
374  Name&
375  append(const PartialName& name);
376 
381  Name&
382  appendNumber(uint64_t number)
383  {
384  return append(Component::fromNumber(number));
385  }
386 
396  Name&
397  appendNumberWithMarker(uint8_t marker, uint64_t number)
398  {
399  return append(Component::fromNumberWithMarker(marker, number));
400  }
401 
408  Name&
409  appendVersion(optional<uint64_t> version = nullopt);
410 
415  Name&
416  appendSegment(uint64_t segmentNo)
417  {
418  return append(Component::fromSegment(segmentNo));
419  }
420 
425  Name&
426  appendByteOffset(uint64_t offset)
427  {
428  return append(Component::fromByteOffset(offset));
429  }
430 
436  Name&
437  appendTimestamp(optional<time::system_clock::TimePoint> timestamp = nullopt);
438 
443  Name&
444  appendSequenceNumber(uint64_t seqNo)
445  {
446  return append(Component::fromSequenceNumber(seqNo));
447  }
448 
452  Name&
454  {
455  return append(Component::fromImplicitSha256Digest(std::move(digest)));
456  }
457 
461  Name&
462  appendImplicitSha256Digest(const uint8_t* digest, size_t digestSize)
463  {
464  return append(Component::fromImplicitSha256Digest(digest, digestSize));
465  }
466 
470  Name&
472  {
473  return append(Component::fromParametersSha256Digest(std::move(digest)));
474  }
475 
479  Name&
480  appendParametersSha256Digest(const uint8_t* digest, size_t digestSize)
481  {
482  return append(Component::fromParametersSha256Digest(digest, digestSize));
483  }
484 
488  Name&
490 
494  template<class T>
495  void
496  push_back(const T& component)
497  {
498  append(component);
499  }
500 
506  void
507  erase(ssize_t i);
508 
512  void
513  clear();
514 
515 public: // algorithms
540  Name
541  getSuccessor() const;
542 
551  bool
552  isPrefixOf(const Name& other) const;
553 
559  bool
560  equals(const Name& other) const;
561 
583  int
584  compare(const Name& other) const
585  {
586  return this->compare(0, npos, other);
587  }
588 
594  int
595  compare(size_t pos1, size_t count1,
596  const Name& other, size_t pos2 = 0, size_t count2 = npos) const;
597 
598 private: // non-member operators
599  // NOTE: the following "hidden friend" operators are available via
600  // argument-dependent lookup only and must be defined inline.
601 
602  friend bool
603  operator==(const Name& lhs, const Name& rhs)
604  {
605  return lhs.equals(rhs);
606  }
607 
608  friend bool
609  operator!=(const Name& lhs, const Name& rhs)
610  {
611  return !lhs.equals(rhs);
612  }
613 
614  friend bool
615  operator<(const Name& lhs, const Name& rhs)
616  {
617  return lhs.compare(rhs) < 0;
618  }
619 
620  friend bool
621  operator<=(const Name& lhs, const Name& rhs)
622  {
623  return lhs.compare(rhs) <= 0;
624  }
625 
626  friend bool
627  operator>(const Name& lhs, const Name& rhs)
628  {
629  return lhs.compare(rhs) > 0;
630  }
631 
632  friend bool
633  operator>=(const Name& lhs, const Name& rhs)
634  {
635  return lhs.compare(rhs) >= 0;
636  }
637 
641  friend std::ostream&
642  operator<<(std::ostream& os, const Name& name)
643  {
644  name.toUri(os);
645  return os;
646  }
647 
648 public:
651  static const size_t npos;
652 
653 private:
654  mutable Block m_wire;
655 };
656 
658 
662 std::istream&
663 operator>>(std::istream& is, Name& name);
664 
665 } // namespace ndn
666 
667 namespace std {
668 
669 template<>
670 struct hash<ndn::Name>
671 {
672  size_t
673  operator()(const ndn::Name& name) const;
674 };
675 
676 } // namespace std
677 
678 #endif // NDN_CXX_NAME_HPP
static Component fromParametersSha256Digest(ConstBufferPtr digest)
Create ParametersSha256DigestComponent component.
friend bool operator==(const Name &lhs, const Name &rhs)
Definition: name.hpp:603
Name & appendByteOffset(uint64_t offset)
Append a byte offset component.
Definition: name.hpp:426
Name & appendParametersSha256Digest(const uint8_t *digest, size_t digestSize)
Append a ParametersSha256Digest component.
Definition: name.hpp:480
static Component fromSequenceNumber(uint64_t seqNo)
Create sequence number component using NDN naming conventions.
friend bool operator>=(const Name &lhs, const Name &rhs)
Definition: name.hpp:633
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:212
void allocator_type
Definition: name.hpp:54
Definition: data.cpp:26
Name & appendParametersSha256Digest(ConstBufferPtr digest)
Append a ParametersSha256Digest component.
Definition: name.hpp:471
bool equals(const Name &other) const
Check if this name equals another name.
Definition: name.cpp:315
static Component fromNumberWithMarker(uint8_t marker, uint64_t number)
Create a component encoded as NameComponentWithMarker.
const Block & wireEncode() const
Perform wire encoding, or return existing wire encoding.
Definition: name.cpp:132
UriFormat
Format used for the URI representation of a name.
const_iterator end() const
End iterator.
Definition: name.hpp:232
const Component & operator[](ssize_t i) const
Equivalent to get(i).
Definition: name.hpp:176
friend std::ostream & operator<<(std::ostream &os, const Name &name)
Print the URI representation of a name.
Definition: name.hpp:642
Name & appendImplicitSha256Digest(ConstBufferPtr digest)
Append an ImplicitSha256Digest component.
Definition: name.hpp:453
std::reverse_iterator< iterator > reverse_iterator
Definition: name.hpp:61
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:299
friend bool operator<(const Name &lhs, const Name &rhs)
Definition: name.hpp:615
Name getSuccessor() const
Get the successor of a name.
Definition: name.cpp:288
const_reverse_iterator rend() const
Reverse end iterator.
Definition: name.hpp:248
static const size_t npos
Indicates "until the end" in getSubName() and compare().
Definition: name.hpp:651
STL namespace.
friend bool operator>(const Name &lhs, const Name &rhs)
Definition: name.hpp:627
Represents a TLV element of the NDN packet format.
Definition: block.hpp:42
const_reverse_iterator rbegin() const
Reverse begin iterator.
Definition: name.hpp:240
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:217
Name & append(Component &&component)
Append a component.
Definition: name.hpp:288
Name & appendNumber(uint64_t number)
Append a component with a nonNegativeInteger.
Definition: name.hpp:382
Name & append(const Component &component)
Append a component.
Definition: name.hpp:278
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
friend bool operator!=(const Name &lhs, const Name &rhs)
Definition: name.hpp:609
Name & appendVersion(optional< uint64_t > version=nullopt)
Append a version component.
Definition: name.cpp:230
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:378
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: name.hpp:62
friend bool operator<=(const Name &lhs, const Name &rhs)
Definition: name.hpp:621
const Component & at(ssize_t i) const
Returns an immutable reference to the component at the specified index, with bounds checking...
Definition: name.cpp:171
static Component fromSegment(uint64_t segmentNo)
Create segment number component using NDN naming conventions.
Use the library&#39;s default format; currently equivalent to UriFormat::ENV_OR_ALTERNATE.
int compare(const Name &other) const
Compare this to the other Name using NDN canonical ordering.
Definition: name.hpp:584
static Component fromByteOffset(uint64_t offset)
Create byte offset component using NDN naming conventions.
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
component_container::size_type size_type
Definition: name.hpp:64
Name & append(Block value)
Append a GenericNameComponent from a TLV element.
Definition: name.hpp:359
Name & append(uint32_t type, Iterator first, Iterator last)
Append a NameComponent of TLV-TYPE type, copying TLV-VALUE from a range.
Definition: name.hpp:323
name::Component Component
Definition: name.hpp:49
Name & appendSequenceNumber(uint64_t seqNo)
Append a sequence number component.
Definition: name.hpp:444
std::istream & operator>>(std::istream &is, Name &name)
Parse URI from stream as Name.
Definition: name.cpp:370
Represents an absolute name.
Definition: name.hpp:44
void push_back(const Block &element)
Append a sub-element.
Definition: block.cpp:457
Name & appendTimestamp(optional< time::system_clock::TimePoint > timestamp=nullopt)
Append a timestamp component.
Definition: name.cpp:236
Name & append(Iterator first, Iterator last)
Append a GenericNameComponent, copying TLV-VALUE from a range.
Definition: name.hpp:337
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:402
Name & append(const uint8_t *value, size_t count)
Append a GenericNameComponent, copying count bytes at value as TLV-VALUE.
Definition: name.hpp:308
const_iterator begin() const
Begin iterator.
Definition: name.hpp:224
void push_back(const T &component)
Append a component.
Definition: name.hpp:496
size_t size() const
Returns the number of components.
Definition: name.hpp:154
Represents a name component.
void erase(ssize_t i)
Erase the component at the specified index.
Definition: name.cpp:270
static Component fromNumber(uint64_t number, uint32_t type=tlv::GenericNameComponent)
Create a component encoded as nonNegativeInteger.
void toUri(std::ostream &os, name::UriFormat format=name::UriFormat::DEFAULT) const
Write URI representation of the name to the output stream.
Definition: name.cpp:348
bool empty() const
Checks if the name is empty, i.e.
Definition: name.hpp:146
Name & appendImplicitSha256Digest(const uint8_t *digest, size_t digestSize)
Append an ImplicitSha256Digest component.
Definition: name.hpp:462
Name & append(const char *str)
Append a GenericNameComponent, copying TLV-VALUE from a null-terminated string.
Definition: name.hpp:348
bool hasWire() const noexcept
Check if this instance already has wire encoding.
Definition: name.hpp:113
Name deepCopy() const
Make a deep copy of the name, reallocating the underlying memory buffer.
Definition: name.cpp:160
component_container::difference_type difference_type
Definition: name.hpp:63
Name & appendNumberWithMarker(uint8_t marker, uint64_t number)
Append a component with a marked number.
Definition: name.hpp:397
static Component fromImplicitSha256Digest(ConstBufferPtr digest)
Create ImplicitSha256DigestComponent component.
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:150
Name & append(uint32_t type, const uint8_t *value, size_t count)
Append a NameComponent of TLV-TYPE type, copying count bytes at value as TLV-VALUE.
Definition: name.hpp:299
std::vector< Component > component_container
Definition: name.hpp:50
void clear()
Remove all components.
Definition: name.cpp:280
Name & appendParametersSha256DigestPlaceholder()
Append a placeholder for a ParametersSha256Digest component.
Definition: name.cpp:262
PartialName getSubName(ssize_t iStartComponent, size_t nComponents=npos) const
Extracts some components as a sub-name (PartialName).
Definition: name.cpp:185
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:261
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:416
Name()
Create an empty name.
Definition: name.cpp:54
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126