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-2021 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 
31 
32 #include <iterator>
33 
34 namespace ndn {
35 
36 class Name;
37 
40 using PartialName = Name;
41 
45 class Name
46 {
47 public: // nested types
49 
51  using component_container = std::vector<Component>;
52 
53  // Name appears as a container of name components
55  using allocator_type = void;
56  using reference = Component&;
57  using const_reference = const Component&;
58  using pointer = Component*;
59  using const_pointer = const Component*;
60  using iterator = const Component*; // disallow modifying via iterator
61  using const_iterator = const Component*;
62  using reverse_iterator = std::reverse_iterator<iterator>;
63  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
64  using difference_type = component_container::difference_type;
65  using size_type = component_container::size_type;
66 
67 public: // constructors, encoding, decoding
71  Name();
72 
82  explicit
83  Name(const Block& wire);
84 
89  Name(const char* uri);
90 
95  Name(std::string uri);
96 
100  void
101  toUri(std::ostream& os, name::UriFormat format = name::UriFormat::DEFAULT) const;
102 
108  std::string
110 
113  bool
114  hasWire() const noexcept
115  {
116  return m_wire.hasWire();
117  }
118 
121  template<encoding::Tag TAG>
122  size_t
123  wireEncode(EncodingImpl<TAG>& encoder) const;
124 
128  const Block&
129  wireEncode() const;
130 
135  void
136  wireDecode(const Block& wire);
137 
140  Name
141  deepCopy() const;
142 
143 public: // access
146  NDN_CXX_NODISCARD bool
147  empty() const
148  {
149  return m_wire.elements().empty();
150  }
151 
154  size_t
155  size() const
156  {
157  return m_wire.elements_size();
158  }
159 
165  const Component&
166  get(ssize_t i) const
167  {
168  if (i < 0) {
169  i += static_cast<ssize_t>(size());
170  }
171  return reinterpret_cast<const Component&>(m_wire.elements()[i]);
172  }
173 
176  const Component&
177  operator[](ssize_t i) const
178  {
179  return get(i);
180  }
181 
188  const Component&
189  at(ssize_t i) const;
190 
204  getSubName(ssize_t iStartComponent, size_t nComponents = npos) const;
205 
213  getPrefix(ssize_t nComponents) const
214  {
215  if (nComponents < 0)
216  return getSubName(0, size() + nComponents);
217  else
218  return getSubName(0, nComponents);
219  }
220 
221 public: // iterators
225  begin() const
226  {
227  return reinterpret_cast<const_iterator>(m_wire.elements().data());
228  }
229 
233  end() const
234  {
235  return reinterpret_cast<const_iterator>(m_wire.elements().data() + m_wire.elements().size());
236  }
237 
241  rbegin() const
242  {
243  return const_reverse_iterator(end());
244  }
245 
249  rend() const
250  {
251  return const_reverse_iterator(begin());
252  }
253 
254 public: // modifiers
262  Name&
263  set(ssize_t i, const Component& component);
264 
272  Name&
273  set(ssize_t i, Component&& component);
274 
278  Name&
279  append(const Component& component)
280  {
281  m_wire.push_back(component);
282  return *this;
283  }
284 
288  Name&
289  append(Component&& component)
290  {
291  m_wire.push_back(std::move(component));
292  return *this;
293  }
294 
299  Name&
300  append(uint32_t type, const uint8_t* value, size_t count)
301  {
302  return append(Component(type, value, count));
303  }
304 
308  Name&
309  append(const uint8_t* value, size_t count)
310  {
311  return append(Component(value, count));
312  }
313 
322  template<class Iterator>
323  Name&
324  append(uint32_t type, Iterator first, Iterator last)
325  {
326  return append(Component(type, first, last));
327  }
328 
336  template<class Iterator>
337  Name&
338  append(Iterator first, Iterator last)
339  {
340  return append(Component(first, last));
341  }
342 
348  Name&
349  append(const char* str)
350  {
351  return append(Component(str));
352  }
353 
360  [[deprecated]]
361  Name&
362  append(Block value)
363  {
364  if (value.type() == tlv::GenericNameComponent) {
365  m_wire.push_back(std::move(value));
366  }
367  else {
368  m_wire.push_back(Block(tlv::GenericNameComponent, std::move(value)));
369  }
370  return *this;
371  }
372 
377  Name&
378  append(const PartialName& name);
379 
384  Name&
385  appendNumber(uint64_t number)
386  {
387  return append(Component::fromNumber(number));
388  }
389 
400  Name&
401  appendNumberWithMarker(uint8_t marker, uint64_t number)
402  {
403  return append(Component::fromNumberWithMarker(marker, number));
404  }
405 
414  Name&
415  appendVersion(const optional<uint64_t>& version = nullopt);
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 
448  Name&
449  appendTimestamp(const optional<time::system_clock::time_point>& timestamp = nullopt);
450 
457  Name&
458  appendSequenceNumber(uint64_t seqNo)
459  {
460  return append(Component::fromSequenceNumber(seqNo));
461  }
462 
467  Name&
469  {
470  return append(Component::fromImplicitSha256Digest(std::move(digest)));
471  }
472 
477  Name&
478  appendImplicitSha256Digest(span<const uint8_t> digestBytes)
479  {
480  return append(Component::fromImplicitSha256Digest(digestBytes));
481  }
482 
487  Name&
489  {
490  return append(Component::fromParametersSha256Digest(std::move(digest)));
491  }
492 
497  Name&
498  appendParametersSha256Digest(span<const uint8_t> digestBytes)
499  {
500  return append(Component::fromParametersSha256Digest(digestBytes));
501  }
502 
507  Name&
509 
513  template<class T>
514  void
515  push_back(const T& component)
516  {
517  append(component);
518  }
519 
525  void
526  erase(ssize_t i);
527 
531  void
532  clear();
533 
534 public: // algorithms
559  Name
560  getSuccessor() const;
561 
570  bool
571  isPrefixOf(const Name& other) const;
572 
578  bool
579  equals(const Name& other) const;
580 
602  int
603  compare(const Name& other) const
604  {
605  return this->compare(0, npos, other);
606  }
607 
613  int
614  compare(size_t pos1, size_t count1,
615  const Name& other, size_t pos2 = 0, size_t count2 = npos) const;
616 
617 private: // non-member operators
618  // NOTE: the following "hidden friend" operators are available via
619  // argument-dependent lookup only and must be defined inline.
620 
621  friend bool
622  operator==(const Name& lhs, const Name& rhs)
623  {
624  return lhs.equals(rhs);
625  }
626 
627  friend bool
628  operator!=(const Name& lhs, const Name& rhs)
629  {
630  return !lhs.equals(rhs);
631  }
632 
633  friend bool
634  operator<(const Name& lhs, const Name& rhs)
635  {
636  return lhs.compare(rhs) < 0;
637  }
638 
639  friend bool
640  operator<=(const Name& lhs, const Name& rhs)
641  {
642  return lhs.compare(rhs) <= 0;
643  }
644 
645  friend bool
646  operator>(const Name& lhs, const Name& rhs)
647  {
648  return lhs.compare(rhs) > 0;
649  }
650 
651  friend bool
652  operator>=(const Name& lhs, const Name& rhs)
653  {
654  return lhs.compare(rhs) >= 0;
655  }
656 
660  friend std::ostream&
661  operator<<(std::ostream& os, const Name& name)
662  {
663  name.toUri(os);
664  return os;
665  }
666 
667 public:
670  static const size_t npos;
671 
672 private:
673  mutable Block m_wire;
674 };
675 
677 
681 std::istream&
682 operator>>(std::istream& is, Name& name);
683 
684 } // namespace ndn
685 
686 namespace std {
687 
688 template<>
689 struct hash<ndn::Name>
690 {
691  size_t
692  operator()(const ndn::Name& name) const;
693 };
694 
695 } // namespace std
696 
697 #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
Equivalent to elements().size()
Definition: block.hpp:426
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:285
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:241
void push_back(const Block &element)
Append a sub-element.
Definition: block.cpp:472
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:402
Represents an absolute name.
Definition: name.hpp:46
friend bool operator<(const Name &lhs, const Name &rhs)
Definition: name.hpp:634
friend bool operator==(const Name &lhs, const Name &rhs)
Definition: name.hpp:622
Name & append(const uint8_t *value, size_t count)
Append a GenericNameComponent, copying count bytes at value as TLV-VALUE.
Definition: name.hpp:309
Name & appendByteOffset(uint64_t offset)
Append a byte offset component.
Definition: name.hpp:436
Name & appendParametersSha256Digest(span< const uint8_t > digestBytes)
Append a ParametersSha256Digest component.
Definition: name.hpp:498
friend bool operator>=(const Name &lhs, const Name &rhs)
Definition: name.hpp:652
bool empty() const
Checks if the name is empty, i.e.
Definition: name.hpp:147
name::Component Component
Definition: name.hpp:50
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:458
void allocator_type
Definition: name.hpp:55
Name getSuccessor() const
Get the successor of a name.
Definition: name.cpp:288
Name & appendNumber(uint64_t number)
Append a component with a NonNegativeInteger.
Definition: name.hpp:385
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:324
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:213
friend bool operator<=(const Name &lhs, const Name &rhs)
Definition: name.hpp:640
std::reverse_iterator< iterator > reverse_iterator
Definition: name.hpp:62
Name & append(Iterator first, Iterator last)
Append a GenericNameComponent, copying TLV-VALUE from a range.
Definition: name.hpp:338
const_reverse_iterator rbegin() const
Reverse begin iterator.
Definition: name.hpp:241
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:300
Name & appendTimestamp(const optional< time::system_clock::time_point > &timestamp=nullopt)
Append a timestamp component.
Definition: name.cpp:236
int compare(const Name &other) const
Compare this to the other Name using NDN canonical ordering.
Definition: name.hpp:603
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:424
Name & appendNumberWithMarker(uint8_t marker, uint64_t number)
Append a component with a marked number.
Definition: name.hpp:401
Name & appendParametersSha256DigestPlaceholder()
Append a placeholder for a ParametersSha256Digest component.
Definition: name.cpp:262
friend bool operator!=(const Name &lhs, const Name &rhs)
Definition: name.hpp:628
friend std::ostream & operator<<(std::ostream &os, const Name &name)
Print the URI representation of a name.
Definition: name.hpp:661
Name & appendImplicitSha256Digest(ConstBufferPtr digest)
Append an ImplicitSha256Digest component.
Definition: name.hpp:468
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:114
void clear()
Remove all components.
Definition: name.cpp:280
component_container::size_type size_type
Definition: name.hpp:65
Name & appendImplicitSha256Digest(span< const uint8_t > digestBytes)
Append an ImplicitSha256Digest component.
Definition: name.hpp:478
Name & append(const Component &component)
Append a component.
Definition: name.hpp:279
const Component & operator[](ssize_t i) const
Equivalent to get(i).
Definition: name.hpp:177
const_reverse_iterator rend() const
Reverse end iterator.
Definition: name.hpp:249
Name()
Create an empty name.
Definition: name.cpp:54
bool equals(const Name &other) const
Check if this name equals another name.
Definition: name.cpp:315
Name & appendParametersSha256Digest(ConstBufferPtr digest)
Append a ParametersSha256Digest component.
Definition: name.hpp:488
const_iterator end() const
End iterator.
Definition: name.hpp:233
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:670
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
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:299
Name & append(Component &&component)
Append a component.
Definition: name.hpp:289
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: name.hpp:63
Name & append(const char *str)
Append a GenericNameComponent, copying TLV-VALUE from a null-terminated string.
Definition: name.hpp:349
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:646
void erase(ssize_t i)
Erase the component at the specified index.
Definition: name.cpp:270
std::vector< Component > component_container
Definition: name.hpp:51
size_t size() const
Returns the number of components.
Definition: name.hpp:155
Name & append(Block value)
Append a GenericNameComponent from a TLV element.
Definition: name.hpp:362
const Component * const_iterator
Definition: name.hpp:61
void push_back(const T &component)
Append a component.
Definition: name.hpp:515
const Component & get(ssize_t i) const
Returns an immutable reference to the component at the specified index.
Definition: name.hpp:166
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
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:150
component_container::difference_type difference_type
Definition: name.hpp:64
const_iterator begin() const
Begin iterator.
Definition: name.hpp:225
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 fromParametersSha256Digest(ConstBufferPtr digest)
Create ParametersSha256DigestComponent component.
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.
static Component fromImplicitSha256Digest(ConstBufferPtr digest)
Create ImplicitSha256DigestComponent component.
#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:67
@ GenericNameComponent
Definition: tlv.hpp:68
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:370