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-2022 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 
22 #ifndef NDN_CXX_NAME_HPP
23 #define NDN_CXX_NAME_HPP
24 
27 
28 #include <iterator>
29 
30 namespace ndn {
31 
32 class Name;
33 
37 using PartialName = Name;
38 
43 class Name
44 {
45 public: // nested types
47 
49  using component_container = std::vector<Component>;
50 
51  // Name appears as a container of name components
53  using allocator_type = void;
54  using reference = Component&;
55  using const_reference = const Component&;
56  using pointer = Component*;
57  using const_pointer = const Component*;
58  using iterator = const Component*; // disallow modifying via iterator
59  using const_iterator = const Component*;
60  using reverse_iterator = std::reverse_iterator<iterator>;
61  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
62  using difference_type = component_container::difference_type;
63  using size_type = component_container::size_type;
64 
65 public: // constructors, encoding, decoding
69  Name();
70 
80  explicit
81  Name(const Block& wire);
82 
87  Name(const char* uri);
88 
93  Name(std::string uri);
94 
98  void
99  toUri(std::ostream& os, name::UriFormat format = name::UriFormat::DEFAULT) const;
100 
106  std::string
108 
111  bool
112  hasWire() const noexcept
113  {
114  return m_wire.hasWire();
115  }
116 
119  template<encoding::Tag TAG>
120  size_t
121  wireEncode(EncodingImpl<TAG>& encoder) const;
122 
126  const Block&
127  wireEncode() const;
128 
133  void
134  wireDecode(const Block& wire);
135 
138  Name
139  deepCopy() const;
140 
141 public: // access
145  NDN_CXX_NODISCARD bool
146  empty() const noexcept
147  {
148  return m_wire.elements().empty();
149  }
150 
154  size_t
155  size() const noexcept
156  {
157  return m_wire.elements_size();
158  }
159 
166  const Component&
167  get(ssize_t i) const noexcept
168  {
169  if (i < 0) {
170  i += static_cast<ssize_t>(size());
171  }
172  return static_cast<const Component&>(m_wire.elements()[static_cast<size_t>(i)]);
173  }
174 
178  const Component&
179  operator[](ssize_t i) const noexcept
180  {
181  return get(i);
182  }
183 
191  const Component&
192  at(ssize_t i) const;
193 
207  getSubName(ssize_t iStartComponent, size_t nComponents = npos) const;
208 
216  getPrefix(ssize_t nComponents) const
217  {
218  if (nComponents < 0)
219  return getSubName(0, size() + nComponents);
220  else
221  return getSubName(0, nComponents);
222  }
223 
224 public: // iterators
228  begin() const noexcept
229  {
230  return reinterpret_cast<const_iterator>(m_wire.elements().data());
231  }
232 
236  end() const noexcept
237  {
238  return reinterpret_cast<const_iterator>(m_wire.elements().data() + m_wire.elements().size());
239  }
240 
244  rbegin() const noexcept
245  {
246  return const_reverse_iterator(end());
247  }
248 
252  rend() const noexcept
253  {
254  return const_reverse_iterator(begin());
255  }
256 
257 public: // modifiers
265  Name&
266  set(ssize_t i, const Component& component);
267 
275  Name&
276  set(ssize_t i, Component&& component);
277 
282  Name&
283  append(const Component& component)
284  {
285  m_wire.push_back(component);
286  return *this;
287  }
288 
293  Name&
294  append(Component&& component)
295  {
296  m_wire.push_back(std::move(component));
297  return *this;
298  }
299 
304  Name&
305  append(uint32_t type, span<const uint8_t> value)
306  {
307  return append(Component(type, value));
308  }
309 
314  Name&
315  append(span<const uint8_t> value)
316  {
318  }
319 
325  [[deprecated("use the overload that takes a span<>")]]
326  Name&
327  append(uint32_t type, const uint8_t* value, size_t count)
328  {
329  return append(type, make_span(value, count));
330  }
331 
337  [[deprecated("use the overload that takes a span<>")]]
338  Name&
339  append(const uint8_t* value, size_t count)
340  {
341  return append(make_span(value, count));
342  }
343 
352  template<class Iterator>
353  Name&
354  append(uint32_t type, Iterator first, Iterator last)
355  {
356  return append(Component(type, first, last));
357  }
358 
366  template<class Iterator>
367  Name&
368  append(Iterator first, Iterator last)
369  {
370  return append(Component(tlv::GenericNameComponent, first, last));
371  }
372 
378  Name&
379  append(const char* str)
380  {
381  return append(Component(str));
382  }
383 
388  Name&
389  append(const PartialName& name);
390 
395  Name&
396  appendNumber(uint64_t number)
397  {
398  return append(Component::fromNumber(number));
399  }
400 
411  Name&
412  appendNumberWithMarker(uint8_t marker, uint64_t number)
413  {
414  return append(Component::fromNumberWithMarker(marker, number));
415  }
416 
423  Name&
424  appendSegment(uint64_t segmentNo)
425  {
426  return append(Component::fromSegment(segmentNo));
427  }
428 
435  Name&
436  appendByteOffset(uint64_t offset)
437  {
438  return append(Component::fromByteOffset(offset));
439  }
440 
449  Name&
450  appendVersion(const optional<uint64_t>& version = nullopt);
451 
459  Name&
460  appendTimestamp(const optional<time::system_clock::time_point>& timestamp = nullopt);
461 
468  Name&
469  appendSequenceNumber(uint64_t seqNo)
470  {
471  return append(Component::fromSequenceNumber(seqNo));
472  }
473 
478  Name&
480  {
481  return append(Component(tlv::ImplicitSha256DigestComponent, std::move(digest)));
482  }
483 
488  Name&
489  appendImplicitSha256Digest(span<const uint8_t> digestBytes)
490  {
492  }
493 
498  Name&
500  {
501  return append(Component(tlv::ParametersSha256DigestComponent, std::move(digest)));
502  }
503 
508  Name&
509  appendParametersSha256Digest(span<const uint8_t> digestBytes)
510  {
512  }
513 
518  Name&
520 
525  Name&
526  appendKeyword(span<const uint8_t> keyword)
527  {
528  return append(Component(tlv::KeywordNameComponent, keyword));
529  }
530 
535  Name&
536  appendKeyword(const char* keyword)
537  {
538  return append(Component(tlv::KeywordNameComponent, {reinterpret_cast<const uint8_t*>(keyword),
539  std::char_traits<char>::length(keyword)}));
540  }
541 
546  template<class T>
547  void
548  push_back(const T& component)
549  {
550  append(component);
551  }
552 
559  void
560  erase(ssize_t i);
561 
566  void
567  clear();
568 
569 public: // algorithms
594  Name
595  getSuccessor() const;
596 
605  bool
606  isPrefixOf(const Name& other) const noexcept;
607 
613  bool
614  equals(const Name& other) const noexcept;
615 
637  int
638  compare(const Name& other) const
639  {
640  return this->compare(0, npos, other);
641  }
642 
648  int
649  compare(size_t pos1, size_t count1,
650  const Name& other, size_t pos2 = 0, size_t count2 = npos) const;
651 
652 private: // non-member operators
653  // NOTE: the following "hidden friend" operators are available via
654  // argument-dependent lookup only and must be defined inline.
655 
656  friend bool
657  operator==(const Name& lhs, const Name& rhs) noexcept
658  {
659  return lhs.equals(rhs);
660  }
661 
662  friend bool
663  operator!=(const Name& lhs, const Name& rhs) noexcept
664  {
665  return !lhs.equals(rhs);
666  }
667 
668  friend bool
669  operator<(const Name& lhs, const Name& rhs)
670  {
671  return lhs.compare(rhs) < 0;
672  }
673 
674  friend bool
675  operator<=(const Name& lhs, const Name& rhs)
676  {
677  return lhs.compare(rhs) <= 0;
678  }
679 
680  friend bool
681  operator>(const Name& lhs, const Name& rhs)
682  {
683  return lhs.compare(rhs) > 0;
684  }
685 
686  friend bool
687  operator>=(const Name& lhs, const Name& rhs)
688  {
689  return lhs.compare(rhs) >= 0;
690  }
691 
696  friend std::ostream&
697  operator<<(std::ostream& os, const Name& name)
698  {
699  name.toUri(os);
700  return os;
701  }
702 
703 public:
707  static const size_t npos;
708 
709 private:
710  mutable Block m_wire;
711 };
712 
714 
719 std::istream&
720 operator>>(std::istream& is, Name& name);
721 
722 } // namespace ndn
723 
724 namespace std {
725 
726 template<>
727 struct hash<ndn::Name>
728 {
729  size_t
730  operator()(const ndn::Name& name) const;
731 };
732 
733 } // namespace std
734 
735 #endif // NDN_CXX_NAME_HPP
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
size_t elements_size() const noexcept
Equivalent to elements().size().
Definition: block.hpp:453
const element_container & elements() const noexcept
Get container of sub-elements.
Definition: block.hpp:426
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:221
void push_back(const Block &element)
Append a sub-element.
Definition: block.cpp:455
Represents an absolute name.
Definition: name.hpp:44
bool equals(const Name &other) const noexcept
Check if this name equals another name.
Definition: name.cpp:316
friend bool operator<(const Name &lhs, const Name &rhs)
Definition: name.hpp:669
Name & appendKeyword(span< const uint8_t > keyword)
Append a keyword component.
Definition: name.hpp:526
Name & append(const uint8_t *value, size_t count)
Append a GenericNameComponent, copying count bytes at value as TLV-VALUE.
Definition: name.hpp:339
const_reverse_iterator rend() const noexcept
Reverse end iterator.
Definition: name.hpp:252
Name & appendByteOffset(uint64_t offset)
Append a byte offset component.
Definition: name.hpp:436
Name & append(span< const uint8_t > value)
Append a GenericNameComponent, copying the TLV-VALUE from value.
Definition: name.hpp:315
Name & appendParametersSha256Digest(span< const uint8_t > digestBytes)
Append a ParametersSha256Digest component.
Definition: name.hpp:509
friend bool operator>=(const Name &lhs, const Name &rhs)
Definition: name.hpp:687
name::Component Component
Definition: name.hpp:48
Name & set(ssize_t i, const Component &component)
Replace the component at the specified index.
Definition: name.cpp:206
Name & appendSequenceNumber(uint64_t seqNo)
Append a sequence number component.
Definition: name.hpp:469
void allocator_type
Definition: name.hpp:53
Name getSuccessor() const
Get the successor of a name.
Definition: name.cpp:289
Name & appendNumber(uint64_t number)
Append a component with a NonNegativeInteger.
Definition: name.hpp:396
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:354
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:216
friend bool operator<=(const Name &lhs, const Name &rhs)
Definition: name.hpp:675
std::reverse_iterator< iterator > reverse_iterator
Definition: name.hpp:60
Name & append(Iterator first, Iterator last)
Append a GenericNameComponent, copying TLV-VALUE from a range.
Definition: name.hpp:368
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:327
Name & appendTimestamp(const optional< time::system_clock::time_point > &timestamp=nullopt)
Append a timestamp component.
Definition: name.cpp:236
friend bool operator!=(const Name &lhs, const Name &rhs) noexcept
Definition: name.hpp:663
int compare(const Name &other) const
Compare this to the other Name using NDN canonical ordering.
Definition: name.hpp:638
Name & appendKeyword(const char *keyword)
Append a keyword component.
Definition: name.hpp:536
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:424
const Component & operator[](ssize_t i) const noexcept
Equivalent to get().
Definition: name.hpp:179
Name & appendNumberWithMarker(uint8_t marker, uint64_t number)
Append a component with a marked number.
Definition: name.hpp:412
const_reverse_iterator rbegin() const noexcept
Reverse begin iterator.
Definition: name.hpp:244
Name & appendParametersSha256DigestPlaceholder()
Append a placeholder for a ParametersSha256Digest component.
Definition: name.cpp:263
size_t size() const noexcept
Returns the number of components.
Definition: name.hpp:155
const_iterator begin() const noexcept
Begin iterator.
Definition: name.hpp:228
friend std::ostream & operator<<(std::ostream &os, const Name &name)
Print the URI representation of a name.
Definition: name.hpp:697
Name & appendImplicitSha256Digest(ConstBufferPtr digest)
Append an ImplicitSha256Digest component.
Definition: name.hpp:479
Name & appendVersion(const optional< uint64_t > &version=nullopt)
Append a version component.
Definition: name.cpp:230
bool hasWire() const noexcept
Check if this instance already has wire encoding.
Definition: name.hpp:112
bool empty() const noexcept
Checks if the name is empty, i.e., has no components.
Definition: name.hpp:146
Name & append(uint32_t type, span< const uint8_t > value)
Append a NameComponent of TLV-TYPE type, copying the TLV-VALUE from value.
Definition: name.hpp:305
void clear()
Remove all components.
Definition: name.cpp:281
component_container::size_type size_type
Definition: name.hpp:63
Name & appendImplicitSha256Digest(span< const uint8_t > digestBytes)
Append an ImplicitSha256Digest component.
Definition: name.hpp:489
Name & append(const Component &component)
Append a name component.
Definition: name.hpp:283
Name()
Create an empty name.
Definition: name.cpp:54
friend bool operator==(const Name &lhs, const Name &rhs) noexcept
Definition: name.hpp:657
const_iterator end() const noexcept
End iterator.
Definition: name.hpp:236
Name & appendParametersSha256Digest(ConstBufferPtr digest)
Append a ParametersSha256Digest component.
Definition: name.hpp:499
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 const size_t npos
Indicates "until the end" in getSubName() and compare().
Definition: name.hpp:707
Name deepCopy() const
Make a deep copy of the name, reallocating the underlying memory buffer.
Definition: name.cpp:160
const Block & wireEncode() const
Perform wire encoding, or return existing wire encoding.
Definition: name.cpp:132
Name & append(Component &&component)
Append a name component.
Definition: name.hpp:294
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: name.hpp:61
Name & append(const char *str)
Append a GenericNameComponent, copying TLV-VALUE from a null-terminated string.
Definition: name.hpp:379
PartialName getSubName(ssize_t iStartComponent, size_t nComponents=npos) const
Extracts some components as a sub-name (PartialName).
Definition: name.cpp:185
friend bool operator>(const Name &lhs, const Name &rhs)
Definition: name.hpp:681
void erase(ssize_t i)
Erase the component at the specified index.
Definition: name.cpp:270
std::vector< Component > component_container
Definition: name.hpp:49
const Component * const_iterator
Definition: name.hpp:59
void push_back(const T &component)
Append a name component.
Definition: name.hpp:548
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:349
bool isPrefixOf(const Name &other) const noexcept
Check if this name is a prefix of another name.
Definition: name.cpp:300
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:150
const Component & get(ssize_t i) const noexcept
Returns an immutable reference to the component at the specified index.
Definition: name.hpp:167
component_container::difference_type difference_type
Definition: name.hpp:62
Represents a name component.
static Component fromSegment(uint64_t segmentNo)
Create a segment number component using NDN naming conventions.
static Component fromNumber(uint64_t number, uint32_t type=tlv::GenericNameComponent)
Create a component encoded as NonNegativeInteger.
static Component fromSequenceNumber(uint64_t seqNo)
Create a sequence number component using NDN naming conventions.
static Component fromByteOffset(uint64_t offset)
Create a byte offset component using NDN naming conventions.
static Component fromNumberWithMarker(uint8_t marker, uint64_t number)
Create a component encoded as NameComponentWithMarker.
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
UriFormat
Format used for the URI representation of a name.
@ DEFAULT
Use the library's default format; currently equivalent to UriFormat::ENV_OR_ALTERNATE.
@ Name
Definition: tlv.hpp:71
@ GenericNameComponent
Definition: tlv.hpp:72
@ KeywordNameComponent
Definition: tlv.hpp:75
@ ParametersSha256DigestComponent
Definition: tlv.hpp:74
@ ImplicitSha256DigestComponent
Definition: tlv.hpp:73
Definition: data.cpp:25
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139
std::istream & operator>>(std::istream &is, Name &name)
Parse URI from stream as Name.
Definition: name.cpp:371