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 
100  std::string
101  toUri() const;
102 
105  bool
106  hasWire() const
107  {
108  return m_wire.hasWire();
109  }
110 
113  template<encoding::Tag TAG>
114  size_t
115  wireEncode(EncodingImpl<TAG>& encoder) const;
116 
120  const Block&
121  wireEncode() const;
122 
127  void
128  wireDecode(const Block& wire);
129 
132  Name
133  deepCopy() const;
134 
135 public: // access
138  bool
139  empty() const
140  {
141  return m_wire.elements().empty();
142  }
143 
146  size_t
147  size() const
148  {
149  return m_wire.elements_size();
150  }
151 
156  const Component&
157  get(ssize_t i) const
158  {
159  if (i < 0) {
160  i += size();
161  }
162  return reinterpret_cast<const Component&>(m_wire.elements()[i]);
163  }
164 
167  const Component&
168  operator[](ssize_t i) const
169  {
170  return get(i);
171  }
172 
177  const Component&
178  at(ssize_t i) const;
179 
193  getSubName(ssize_t iStartComponent, size_t nComponents = npos) const;
194 
203  getPrefix(ssize_t nComponents) const
204  {
205  if (nComponents < 0)
206  return getSubName(0, size() + nComponents);
207  else
208  return getSubName(0, nComponents);
209  }
210 
211 public: // iterators
215  begin() const
216  {
217  return reinterpret_cast<const_iterator>(m_wire.elements().data());
218  }
219 
223  end() const
224  {
225  return reinterpret_cast<const_iterator>(m_wire.elements().data() + m_wire.elements().size());
226  }
227 
231  rbegin() const
232  {
233  return const_reverse_iterator(end());
234  }
235 
239  rend() const
240  {
241  return const_reverse_iterator(begin());
242  }
243 
244 public: // modifiers
248  Name&
249  append(const Component& component)
250  {
251  m_wire.push_back(component);
252  return *this;
253  }
254 
259  Name&
260  append(uint32_t type, const uint8_t* value, size_t count)
261  {
262  return append(Component(type, value, count));
263  }
264 
268  Name&
269  append(const uint8_t* value, size_t count)
270  {
271  return append(Component(value, count));
272  }
273 
282  template<class Iterator>
283  Name&
284  append(uint32_t type, Iterator first, Iterator last)
285  {
286  return append(Component(type, first, last));
287  }
288 
296  template<class Iterator>
297  Name&
298  append(Iterator first, Iterator last)
299  {
300  return append(Component(first, last));
301  }
302 
308  Name&
309  append(const char* str)
310  {
311  return append(Component(str));
312  }
313 
319  Name&
320  append(const Block& value)
321  {
322  if (value.type() == tlv::GenericNameComponent) {
323  m_wire.push_back(value);
324  }
325  else {
326  m_wire.push_back(Block(tlv::GenericNameComponent, value));
327  }
328  return *this;
329  }
330 
336  Name&
337  appendNumber(uint64_t number)
338  {
339  return append(Component::fromNumber(number));
340  }
341 
351  Name&
352  appendNumberWithMarker(uint8_t marker, uint64_t number)
353  {
354  return append(Component::fromNumberWithMarker(marker, number));
355  }
356 
363  Name&
364  appendVersion(optional<uint64_t> version = nullopt);
365 
370  Name&
371  appendSegment(uint64_t segmentNo)
372  {
373  return append(Component::fromSegment(segmentNo));
374  }
375 
380  Name&
381  appendSegmentOffset(uint64_t offset)
382  {
383  return append(Component::fromSegmentOffset(offset));
384  }
385 
391  Name&
392  appendTimestamp(optional<time::system_clock::TimePoint> timestamp = nullopt);
393 
398  Name&
399  appendSequenceNumber(uint64_t seqNo)
400  {
401  return append(Component::fromSequenceNumber(seqNo));
402  }
403 
407  Name&
409  {
410  return append(Component::fromImplicitSha256Digest(std::move(digest)));
411  }
412 
416  Name&
417  appendImplicitSha256Digest(const uint8_t* digest, size_t digestSize)
418  {
419  return append(Component::fromImplicitSha256Digest(digest, digestSize));
420  }
421 
426  Name&
427  append(const PartialName& name);
428 
432  template<class T>
433  void
434  push_back(const T& component)
435  {
436  append(component);
437  }
438 
442  void
444  {
445  m_wire = Block(tlv::Name);
446  }
447 
448 public: // algorithms
473  Name
474  getSuccessor() const;
475 
484  bool
485  isPrefixOf(const Name& other) const;
486 
492  bool
493  equals(const Name& other) const;
494 
516  int
517  compare(const Name& other) const
518  {
519  return this->compare(0, npos, other);
520  }
521 
527  int
528  compare(size_t pos1, size_t count1,
529  const Name& other, size_t pos2 = 0, size_t count2 = npos) const;
530 
531 public:
534  static const size_t npos;
535 
536 private:
537  mutable Block m_wire;
538 };
539 
541 
542 inline bool
543 operator==(const Name& lhs, const Name& rhs)
544 {
545  return lhs.equals(rhs);
546 }
547 
548 inline bool
549 operator!=(const Name& lhs, const Name& rhs)
550 {
551  return !lhs.equals(rhs);
552 }
553 
554 inline bool
555 operator<=(const Name& lhs, const Name& rhs)
556 {
557  return lhs.compare(rhs) <= 0;
558 }
559 
560 inline bool
561 operator<(const Name& lhs, const Name& rhs)
562 {
563  return lhs.compare(rhs) < 0;
564 }
565 
566 inline bool
567 operator>=(const Name& lhs, const Name& rhs)
568 {
569  return lhs.compare(rhs) >= 0;
570 }
571 
572 inline bool
573 operator>(const Name& lhs, const Name& rhs)
574 {
575  return lhs.compare(rhs) > 0;
576 }
577 
581 std::ostream&
582 operator<<(std::ostream& os, const Name& name);
583 
587 std::istream&
588 operator>>(std::istream& is, Name& name);
589 
590 } // namespace ndn
591 
592 namespace std {
593 
594 template<>
595 struct hash<ndn::Name>
596 {
597  size_t
598  operator()(const ndn::Name& name) const;
599 };
600 
601 } // namespace std
602 
603 #endif // NDN_NAME_HPP
static Component fromNumber(uint64_t number)
Create a component encoded as nonNegativeInteger.
const Component * const_iterator
Definition: name.hpp:59
static Component fromSequenceNumber(uint64_t seqNo)
Create sequence number component using NDN naming conventions.
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix of the name.
Definition: name.hpp:203
void allocator_type
Definition: name.hpp:53
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:249
Copyright (c) 2011-2015 Regents of the University of California.
std::string toUri() const
Get URI representation of the name.
Definition: name.cpp:116
bool equals(const Name &other) const
Check if this name equals another name.
Definition: name.cpp:267
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:140
const_iterator end() const
End iterator.
Definition: name.hpp:223
const Component & get(ssize_t i) const
Get the component at the given index.
Definition: name.hpp:157
const Component & operator[](ssize_t i) const
Equivalent to get(i)
Definition: name.hpp:168
Name & appendImplicitSha256Digest(ConstBufferPtr digest)
Append an ImplicitSha256Digest component.
Definition: name.hpp:408
std::reverse_iterator< iterator > reverse_iterator
Definition: name.hpp:60
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:322
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:251
Name getSuccessor() const
Get the successor of a name.
Definition: name.cpp:241
const_reverse_iterator rend() const
Reverse end iterator.
Definition: name.hpp:239
static const size_t npos
indicates "until the end" in getSubName and compare
Definition: name.hpp:534
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
const_reverse_iterator rbegin() const
Reverse begin iterator.
Definition: name.hpp:231
bool operator<=(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.cpp:43
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:262
Name & appendNumber(uint64_t number)
Append a component with a nonNegativeInteger.
Definition: name.hpp:337
Name & append(const Component &component)
Append a component.
Definition: name.hpp:249
static Component fromSegmentOffset(uint64_t offset)
Create segment offset component using NDN naming conventions.
Name & appendVersion(optional< uint64_t > version=nullopt)
Append a version component.
Definition: name.cpp:214
const element_container & elements() const
Get container of sub elements.
Definition: block.hpp:361
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: name.hpp:61
std::size_t hash(T const &v)
Definition: variant.hpp:807
const Component & at(ssize_t i) const
Get the component at the given index.
Definition: name.cpp:179
static Component fromSegment(uint64_t segmentNo)
Create segment number component using NDN naming conventions.
int compare(const Name &other) const
Compare this to the other Name using NDN canonical ordering.
Definition: name.hpp:517
Name & appendSegmentOffset(uint64_t offset)
Append a segment byte offset component.
Definition: name.hpp:381
Name & append(const Block &value)
Append a GenericNameComponent from a TLV element.
Definition: name.hpp:320
component_container::size_type size_type
Definition: name.hpp:63
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:284
name::Component Component
Definition: name.hpp:48
Name & appendSequenceNumber(uint64_t seqNo)
Append a sequence number component.
Definition: name.hpp:399
std::istream & operator>>(std::istream &is, Name &name)
Parse URI from stream as Name.
Definition: name.cpp:315
Represents an absolute name.
Definition: name.hpp:43
void push_back(const Block &element)
Append a sub element.
Definition: block.cpp:465
Name & appendTimestamp(optional< time::system_clock::TimePoint > timestamp=nullopt)
Append a timestamp component.
Definition: name.cpp:220
Name & append(Iterator first, Iterator last)
Append a GenericNameComponent, copying TLV-VALUE from a range.
Definition: name.hpp:298
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:385
Name & append(const uint8_t *value, size_t count)
Append a GenericNameComponent, copying count bytes at value as TLV-VALUE.
Definition: name.hpp:269
const_iterator begin() const
Begin iterator.
Definition: name.hpp:215
void push_back(const T &component)
Append a component.
Definition: name.hpp:434
NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Exclude)
size_t size() const
Get number of components.
Definition: name.hpp:147
bool operator>(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.hpp:54
Represents a name component.
bool operator>=(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.hpp:60
bool empty() const
Check if name is empty.
Definition: name.hpp:139
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:313
Name & appendImplicitSha256Digest(const uint8_t *digest, size_t digestSize)
Append an ImplicitSha256Digest component.
Definition: name.hpp:417
Name & append(const char *str)
Append a GenericNameComponent, copying TLV-VALUE from a null-terminated string.
Definition: name.hpp:309
Name deepCopy() const
Make a deep copy of the name, reallocating the underlying memory buffer.
Definition: name.cpp:168
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:352
static Component fromImplicitSha256Digest(ConstBufferPtr digest)
Create ImplicitSha256DigestComponent component.
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:158
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:260
std::vector< Component > component_container
Definition: name.hpp:49
void clear()
Remove all components.
Definition: name.hpp:443
bool operator<(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.cpp:36
PartialName getSubName(ssize_t iStartComponent, size_t nComponents=npos) const
Extract some components as a sub-name (PartialName)
Definition: name.cpp:193
const nullopt_t nullopt((nullopt_t::init()))
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:249
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:371
Name()
Create an empty name.
Definition: name.cpp:54
bool hasWire() const
Check if this Name instance already has wire encoding.
Definition: name.hpp:106
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126