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-2019 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_NAME_HPP
27 #define NDN_NAME_HPP
28 
30 
31 #include <iterator>
32 
33 namespace ndn {
34 
35 class Name;
36 
39 using PartialName = Name;
40 
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
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
144  NDN_CXX_NODISCARD bool
145  empty() const
146  {
147  return m_wire.elements().empty();
148  }
149 
152  size_t
153  size() const
154  {
155  return m_wire.elements_size();
156  }
157 
163  const Component&
164  get(ssize_t i) const
165  {
166  if (i < 0) {
167  i += static_cast<ssize_t>(size());
168  }
169  return reinterpret_cast<const Component&>(m_wire.elements()[i]);
170  }
171 
174  const Component&
175  operator[](ssize_t i) const
176  {
177  return get(i);
178  }
179 
186  const Component&
187  at(ssize_t i) const;
188 
202  getSubName(ssize_t iStartComponent, size_t nComponents = npos) const;
203 
211  getPrefix(ssize_t nComponents) const
212  {
213  if (nComponents < 0)
214  return getSubName(0, size() + nComponents);
215  else
216  return getSubName(0, nComponents);
217  }
218 
219 public: // iterators
223  begin() const
224  {
225  return reinterpret_cast<const_iterator>(m_wire.elements().data());
226  }
227 
231  end() const
232  {
233  return reinterpret_cast<const_iterator>(m_wire.elements().data() + m_wire.elements().size());
234  }
235 
239  rbegin() const
240  {
241  return const_reverse_iterator(end());
242  }
243 
247  rend() const
248  {
249  return const_reverse_iterator(begin());
250  }
251 
252 public: // modifiers
260  Name&
261  set(ssize_t i, const Component& component);
262 
270  Name&
271  set(ssize_t i, Component&& component);
272 
276  Name&
277  append(const Component& component)
278  {
279  m_wire.push_back(component);
280  return *this;
281  }
282 
286  Name&
287  append(Component&& component)
288  {
289  m_wire.push_back(std::move(component));
290  return *this;
291  }
292 
297  Name&
298  append(uint32_t type, const uint8_t* value, size_t count)
299  {
300  return append(Component(type, value, count));
301  }
302 
306  Name&
307  append(const uint8_t* value, size_t count)
308  {
309  return append(Component(value, count));
310  }
311 
320  template<class Iterator>
321  Name&
322  append(uint32_t type, Iterator first, Iterator last)
323  {
324  return append(Component(type, first, last));
325  }
326 
334  template<class Iterator>
335  Name&
336  append(Iterator first, Iterator last)
337  {
338  return append(Component(first, last));
339  }
340 
346  Name&
347  append(const char* str)
348  {
349  return append(Component(str));
350  }
351 
357  Name&
358  append(Block value)
359  {
360  if (value.type() == tlv::GenericNameComponent) {
361  m_wire.push_back(std::move(value));
362  }
363  else {
365  }
366  return *this;
367  }
368 
373  Name&
374  append(const PartialName& name);
375 
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 
432  Name&
433  appendSegmentOffset(uint64_t offset)
434  {
435  return appendByteOffset(offset);
436  }
437 
443  Name&
444  appendTimestamp(optional<time::system_clock::TimePoint> timestamp = nullopt);
445 
450  Name&
451  appendSequenceNumber(uint64_t seqNo)
452  {
453  return append(Component::fromSequenceNumber(seqNo));
454  }
455 
459  Name&
461  {
463  }
464 
468  Name&
469  appendImplicitSha256Digest(const uint8_t* digest, size_t digestSize)
470  {
471  return append(Component::fromImplicitSha256Digest(digest, digestSize));
472  }
473 
477  Name&
479  {
481  }
482 
486  Name&
487  appendParametersSha256Digest(const uint8_t* digest, size_t digestSize)
488  {
489  return append(Component::fromParametersSha256Digest(digest, digestSize));
490  }
491 
495  Name&
497 
501  template<class T>
502  void
503  push_back(const T& component)
504  {
505  append(component);
506  }
507 
513  void
514  erase(ssize_t i);
515 
519  void
520  clear();
521 
522 public: // algorithms
547  Name
548  getSuccessor() const;
549 
558  bool
559  isPrefixOf(const Name& other) const;
560 
566  bool
567  equals(const Name& other) const;
568 
590  int
591  compare(const Name& other) const
592  {
593  return this->compare(0, npos, other);
594  }
595 
601  int
602  compare(size_t pos1, size_t count1,
603  const Name& other, size_t pos2 = 0, size_t count2 = npos) const;
604 
605 private: // non-member operators
606  // NOTE: the following "hidden friend" operators are available via
607  // argument-dependent lookup only and must be defined inline.
608 
609  friend bool
610  operator==(const Name& lhs, const Name& rhs)
611  {
612  return lhs.equals(rhs);
613  }
614 
615  friend bool
616  operator!=(const Name& lhs, const Name& rhs)
617  {
618  return !lhs.equals(rhs);
619  }
620 
621  friend bool
622  operator<(const Name& lhs, const Name& rhs)
623  {
624  return lhs.compare(rhs) < 0;
625  }
626 
627  friend bool
628  operator<=(const Name& lhs, const Name& rhs)
629  {
630  return lhs.compare(rhs) <= 0;
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 
648  friend std::ostream&
649  operator<<(std::ostream& os, const Name& name)
650  {
651  name.toUri(os);
652  return os;
653  }
654 
655 public:
658  static const size_t npos;
659 
660 private:
661  mutable Block m_wire;
662 };
663 
665 
669 std::istream&
670 operator>>(std::istream& is, Name& name);
671 
672 } // namespace ndn
673 
674 namespace std {
675 
676 template<>
677 struct hash<ndn::Name>
678 {
679  size_t
680  operator()(const ndn::Name& name) const;
681 };
682 
683 } // namespace std
684 
685 #endif // NDN_NAME_HPP
static Component fromParametersSha256Digest(ConstBufferPtr digest)
Create ParametersSha256DigestComponent component.
friend bool operator==(const Name &lhs, const Name &rhs)
Definition: name.hpp:610
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:487
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:640
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:211
void allocator_type
Definition: name.hpp:53
Definition: data.cpp:26
Name & appendParametersSha256Digest(ConstBufferPtr digest)
Append a ParametersSha256Digest component.
Definition: name.hpp:478
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
Identify a format of URI representation.
const_iterator end() const
End iterator.
Definition: name.hpp:231
const Component & operator[](ssize_t i) const
Equivalent to get(i).
Definition: name.hpp:175
friend std::ostream & operator<<(std::ostream &os, const Name &name)
Print URI representation of a name.
Definition: name.hpp:649
Name & appendImplicitSha256Digest(ConstBufferPtr digest)
Append an ImplicitSha256Digest component.
Definition: name.hpp:460
std::reverse_iterator< iterator > reverse_iterator
Definition: name.hpp:60
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:622
Name getSuccessor() const
Get the successor of a name.
Definition: name.cpp:288
const_reverse_iterator rend() const
Reverse end iterator.
Definition: name.hpp:247
static const size_t npos
Indicates "until the end" in getSubName() and compare().
Definition: name.hpp:658
STL namespace.
friend bool operator>(const Name &lhs, const Name &rhs)
Definition: name.hpp:634
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
const_reverse_iterator rbegin() const
Reverse begin iterator.
Definition: name.hpp:239
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:230
Name & append(Component &&component)
Append a component.
Definition: name.hpp:287
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:277
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
friend bool operator!=(const Name &lhs, const Name &rhs)
Definition: name.hpp:616
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:391
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: name.hpp:61
friend bool operator<=(const Name &lhs, const Name &rhs)
Definition: name.hpp:628
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.
ALTERNATE, unless NDN_NAME_ALT_URI environment variable is set to &#39;0&#39;.
int compare(const Name &other) const
Compare this to the other Name using NDN canonical ordering.
Definition: name.hpp:591
static Component fromByteOffset(uint64_t offset)
Create byte offset component using NDN naming conventions.
Name & appendSegmentOffset(uint64_t offset)
Definition: name.hpp:433
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
component_container::size_type size_type
Definition: name.hpp:63
Name & append(Block value)
Append a GenericNameComponent from a TLV element.
Definition: name.hpp:358
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:322
name::Component Component
Definition: name.hpp:48
Name & appendSequenceNumber(uint64_t seqNo)
Append a sequence number component.
Definition: name.hpp:451
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:43
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:336
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:415
Name & append(const uint8_t *value, size_t count)
Append a GenericNameComponent, copying count bytes at value as TLV-VALUE.
Definition: name.hpp:307
const_iterator begin() const
Begin iterator.
Definition: name.hpp:223
void push_back(const T &component)
Append a component.
Definition: name.hpp:503
size_t size() const
Returns the number of components.
Definition: name.hpp:153
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:145
Name & appendImplicitSha256Digest(const uint8_t *digest, size_t digestSize)
Append an ImplicitSha256Digest component.
Definition: name.hpp:469
Name & append(const char *str)
Append a GenericNameComponent, copying TLV-VALUE from a null-terminated string.
Definition: name.hpp:347
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:62
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:298
std::vector< Component > component_container
Definition: name.hpp:49
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
const nullopt_t nullopt((nullopt_t::init()))
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:274
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:416
Name()
Create an empty name.
Definition: name.cpp:54
bool hasWire() const
Check if this Name instance already has wire encoding.
Definition: name.hpp:112
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126