NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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
ndn::Block::elements
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:391
ndn::operator>>
std::istream & operator>>(std::istream &is, Name &name)
Parse URI from stream as Name.
Definition: name.cpp:370
ndn::Name::appendImplicitSha256Digest
Name & appendImplicitSha256Digest(const uint8_t *digest, size_t digestSize)
Append an ImplicitSha256Digest component.
Definition: name.hpp:469
ndn::name::Component::fromNumber
static Component fromNumber(uint64_t number, uint32_t type=tlv::GenericNameComponent)
Create a component encoded as nonNegativeInteger.
Definition: name-component.cpp:341
ndn::Name::append
Name & append(const char *str)
Append a GenericNameComponent, copying TLV-VALUE from a null-terminated string.
Definition: name.hpp:347
ndn::name::Component::fromByteOffset
static Component fromByteOffset(uint64_t offset)
Create byte offset component using NDN naming conventions.
Definition: name-component.cpp:383
ndn::Name::hasWire
bool hasWire() const
Check if this Name instance already has wire encoding.
Definition: name.hpp:112
ndn::name::UriFormat::DEFAULT
@ DEFAULT
ALTERNATE, unless NDN_NAME_ALT_URI environment variable is set to '0'.
ndn::Name::rbegin
const_reverse_iterator rbegin() const
Reverse begin iterator.
Definition: name.hpp:239
ndn::name::Component::fromNumberWithMarker
static Component fromNumberWithMarker(uint8_t marker, uint64_t number)
Create a component encoded as NameComponentWithMarker.
Definition: name-component.cpp:347
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
ndn::Name::appendByteOffset
Name & appendByteOffset(uint64_t offset)
Append a byte offset component.
Definition: name.hpp:426
ndn::Name::erase
void erase(ssize_t i)
Erase the component at the specified index.
Definition: name.cpp:270
ndn::Name::compare
int compare(const Name &other) const
Compare this to the other Name using NDN canonical ordering.
Definition: name.hpp:591
ndn::Name::appendSequenceNumber
Name & appendSequenceNumber(uint64_t seqNo)
Append a sequence number component.
Definition: name.hpp:451
ndn::Name::isPrefixOf
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:299
ndn::Name::Name
Name()
Create an empty name.
Definition: name.cpp:54
ndn::Name::size
size_t size() const
Returns the number of components.
Definition: name.hpp:153
ndn::NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS
NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Interest)
ndn::Name::difference_type
component_container::difference_type difference_type
Definition: name.hpp:62
ndn::Name::operator!=
friend bool operator!=(const Name &lhs, const Name &rhs)
Definition: name.hpp:616
ndn::Name::allocator_type
void allocator_type
Definition: name.hpp:53
ndn::Name::component_container
std::vector< Component > component_container
Definition: name.hpp:49
ndn::Name::empty
NDN_CXX_NODISCARD bool empty() const
Checks if the name is empty, i.e.
Definition: name.hpp:145
ndn::Name::npos
static const size_t npos
Indicates "until the end" in getSubName() and compare().
Definition: name.hpp:658
ndn::Name::appendParametersSha256Digest
Name & appendParametersSha256Digest(ConstBufferPtr digest)
Append a ParametersSha256Digest component.
Definition: name.hpp:478
ndn::Block::push_back
void push_back(const Block &element)
Append a sub-element.
Definition: block.cpp:457
ndn::name::Component::fromParametersSha256Digest
static Component fromParametersSha256Digest(ConstBufferPtr digest)
Create ParametersSha256DigestComponent component.
Definition: name-component.cpp:440
ndn::Name::getSuccessor
Name getSuccessor() const
Get the successor of a name.
Definition: name.cpp:288
ndn::Name::Component
name::Component Component
Definition: name.hpp:48
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::Name::wireDecode
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:150
ns3::ndn::Name
Name
Definition: ndn-common.cpp:25
ndn::Name::appendParametersSha256Digest
Name & appendParametersSha256Digest(const uint8_t *digest, size_t digestSize)
Append a ParametersSha256Digest component.
Definition: name.hpp:487
ndn::Name::getSubName
PartialName getSubName(ssize_t iStartComponent, size_t nComponents=npos) const
Extracts some components as a sub-name (PartialName).
Definition: name.cpp:185
ndn::Name::rend
const_reverse_iterator rend() const
Reverse end iterator.
Definition: name.hpp:247
NDN_CXX_NODISCARD
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
ndn::Block::type
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:274
ndn::Name::appendVersion
Name & appendVersion(optional< uint64_t > version=nullopt)
Append a version component.
Definition: name.cpp:230
ndn::Name::operator<=
friend bool operator<=(const Name &lhs, const Name &rhs)
Definition: name.hpp:628
ndn::Name::set
Name & set(ssize_t i, const Component &component)
Replace the component at the specified index.
Definition: name.cpp:206
ndn::Name::operator<
friend bool operator<(const Name &lhs, const Name &rhs)
Definition: name.hpp:622
ndn::Name::append
Name & append(const Component &component)
Append a component.
Definition: name.hpp:277
ndn::Name::getPrefix
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:211
ndn::name::Component::fromSegment
static Component fromSegment(uint64_t segmentNo)
Create segment number component using NDN naming conventions.
Definition: name-component.cpp:375
ndn::Name::wireEncode
const Block & wireEncode() const
Perform wire encoding, or return existing wire encoding.
Definition: name.cpp:132
ndn::name::Component::fromSequenceNumber
static Component fromSequenceNumber(uint64_t seqNo)
Create sequence number component using NDN naming conventions.
Definition: name-component.cpp:400
ndn::Name::append
Name & append(const uint8_t *value, size_t count)
Append a GenericNameComponent, copying count bytes at value as TLV-VALUE.
Definition: name.hpp:307
nonstd::optional_lite::nullopt
const nullopt_t nullopt((nullopt_t::init()))
ndn::name::Component::Error
Definition: name-component.hpp:97
ndn::tlv::GenericNameComponent
@ GenericNameComponent
Definition: tlv.hpp:68
ndn::Name::toUri
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
ndn::Name::append
Name & append(Block value)
Append a GenericNameComponent from a TLV element.
Definition: name.hpp:358
ndn::Name::operator>=
friend bool operator>=(const Name &lhs, const Name &rhs)
Definition: name.hpp:640
ndn::Name::end
const_iterator end() const
End iterator.
Definition: name.hpp:231
ndn::Name::at
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
ndn::Name::operator>
friend bool operator>(const Name &lhs, const Name &rhs)
Definition: name.hpp:634
ndn::encoding::EncodingImpl
Definition: encoding-buffer-fwd.hpp:36
ndn::Name::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: name.hpp:60
ndn::Name::appendParametersSha256DigestPlaceholder
Name & appendParametersSha256DigestPlaceholder()
Append a placeholder for a ParametersSha256Digest component.
Definition: name.cpp:262
ndn::name::Component
Represents a name component.
Definition: name-component.hpp:94
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::Block::elements_size
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:415
ndn::Name::operator[]
const Component & operator[](ssize_t i) const
Equivalent to get(i).
Definition: name.hpp:175
ndn::Name::operator<<
friend std::ostream & operator<<(std::ostream &os, const Name &name)
Print URI representation of a name.
Definition: name.hpp:649
ndn::Name::size_type
component_container::size_type size_type
Definition: name.hpp:63
ndn::Name::append
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
ndn::name
Definition: name-component-types.hpp:33
ndn::Name::clear
void clear()
Remove all components.
Definition: name.cpp:280
ndn::Name::deepCopy
Name deepCopy() const
Make a deep copy of the name, reallocating the underlying memory buffer.
Definition: name.cpp:160
ndn::Block::hasWire
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:230
ndn::name::Component::fromImplicitSha256Digest
static Component fromImplicitSha256Digest(ConstBufferPtr digest)
Create ImplicitSha256DigestComponent component.
Definition: name-component.cpp:422
ndn::Name::appendTimestamp
Name & appendTimestamp(optional< time::system_clock::TimePoint > timestamp=nullopt)
Append a timestamp component.
Definition: name.cpp:236
ndn::Name::appendNumber
Name & appendNumber(uint64_t number)
Append a component with a nonNegativeInteger.
Definition: name.hpp:382
ndn::Name::appendSegmentOffset
Name & appendSegmentOffset(uint64_t offset)
Definition: name.hpp:433
ndn::Name::const_iterator
const Component * const_iterator
Definition: name.hpp:59
ndn::Name::push_back
void push_back(const T &component)
Append a component.
Definition: name.hpp:503
ndn::Name::appendSegment
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:416
ndn::Name::begin
const_iterator begin() const
Begin iterator.
Definition: name.hpp:223
ndn::Name::get
const Component & get(ssize_t i) const
Returns an immutable reference to the component at the specified index.
Definition: name.hpp:164
ndn::Name::appendNumberWithMarker
Name & appendNumberWithMarker(uint8_t marker, uint64_t number)
Append a component with a marked number.
Definition: name.hpp:397
ndn::Name::append
Name & append(Component &&component)
Append a component.
Definition: name.hpp:287
name-component.hpp
ndn::Name::appendImplicitSha256Digest
Name & appendImplicitSha256Digest(ConstBufferPtr digest)
Append an ImplicitSha256Digest component.
Definition: name.hpp:460
ndn::ConstBufferPtr
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126
ndn::Name::append
Name & append(Iterator first, Iterator last)
Append a GenericNameComponent, copying TLV-VALUE from a range.
Definition: name.hpp:336
ndn::Name::operator==
friend bool operator==(const Name &lhs, const Name &rhs)
Definition: name.hpp:610
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::Name::equals
bool equals(const Name &other) const
Check if this name equals another name.
Definition: name.cpp:315
ndn::Name::append
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
ndn::name::UriFormat
UriFormat
Identify a format of URI representation.
Definition: name-component.hpp:35
ndn::Name::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: name.hpp:61