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-2018 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 
29 #include "name-component.hpp"
30 #include <iterator>
31 
32 namespace ndn {
33 
34 class Name;
35 
38 using PartialName = Name;
39 
42 class Name
43 {
44 public: // nested types
46  {
47  public:
48  explicit
49  Error(const std::string& what)
50  : name::Component::Error(what)
51  {
52  }
53  };
54 
56  using component_container = std::vector<Component>;
57 
58  // Name appears as a container of name components
60  using allocator_type = void;
61  using reference = Component&;
62  using const_reference = const Component&;
63  using pointer = Component*;
64  using const_pointer = const Component*;
65  using iterator = const Component*; // disallow modifying via iterator
66  using const_iterator = const Component*;
67  using reverse_iterator = std::reverse_iterator<iterator>;
68  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
69  using difference_type = component_container::difference_type;
70  using size_type = component_container::size_type;
71 
72 public: // constructors, encoding, decoding
76  Name();
77 
87  explicit
88  Name(const Block& wire);
89 
94  Name(const char* uri);
95 
100  Name(std::string uri);
101 
107  std::string
108  toUri() const;
109 
112  bool
113  hasWire() const
114  {
115  return m_wire.hasWire();
116  }
117 
120  template<encoding::Tag TAG>
121  size_t
122  wireEncode(EncodingImpl<TAG>& encoder) const;
123 
127  const Block&
128  wireEncode() const;
129 
134  void
135  wireDecode(const Block& wire);
136 
139  Name
140  deepCopy() const;
141 
142 public: // access
145  bool
146  empty() const
147  {
148  return m_wire.elements().empty();
149  }
150 
153  size_t
154  size() const
155  {
156  return m_wire.elements_size();
157  }
158 
163  const Component&
164  get(ssize_t i) const
165  {
166  if (i < 0) {
167  i += 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 
184  const Component&
185  at(ssize_t i) const;
186 
200  getSubName(ssize_t iStartComponent, size_t nComponents = npos) const;
201 
210  getPrefix(ssize_t nComponents) const
211  {
212  if (nComponents < 0)
213  return getSubName(0, size() + nComponents);
214  else
215  return getSubName(0, nComponents);
216  }
217 
218 public: // iterators
222  begin() const
223  {
224  return reinterpret_cast<const_iterator>(m_wire.elements().data());
225  }
226 
230  end() const
231  {
232  return reinterpret_cast<const_iterator>(m_wire.elements().data() + m_wire.elements().size());
233  }
234 
238  rbegin() const
239  {
240  return const_reverse_iterator(end());
241  }
242 
246  rend() const
247  {
248  return const_reverse_iterator(begin());
249  }
250 
251 public: // modifiers
255  Name&
256  append(const Component& component)
257  {
258  m_wire.push_back(component);
259  return *this;
260  }
261 
266  Name&
267  append(uint32_t type, const uint8_t* value, size_t count)
268  {
269  return append(Component(type, value, count));
270  }
271 
275  Name&
276  append(const uint8_t* value, size_t count)
277  {
278  return append(Component(value, count));
279  }
280 
289  template<class Iterator>
290  Name&
291  append(uint32_t type, Iterator first, Iterator last)
292  {
293  return append(Component(type, first, last));
294  }
295 
303  template<class Iterator>
304  Name&
305  append(Iterator first, Iterator last)
306  {
307  return append(Component(first, last));
308  }
309 
315  Name&
316  append(const char* str)
317  {
318  return append(Component(str));
319  }
320 
326  Name&
327  append(const Block& value)
328  {
329  if (value.type() == tlv::GenericNameComponent) {
330  m_wire.push_back(value);
331  }
332  else {
333  m_wire.push_back(Block(tlv::GenericNameComponent, value));
334  }
335  return *this;
336  }
337 
343  Name&
344  appendNumber(uint64_t number)
345  {
346  return append(Component::fromNumber(number));
347  }
348 
358  Name&
359  appendNumberWithMarker(uint8_t marker, uint64_t number)
360  {
361  return append(Component::fromNumberWithMarker(marker, number));
362  }
363 
368  Name&
369  appendVersion(uint64_t version)
370  {
371  return append(Component::fromVersion(version));
372  }
373 
381  Name&
382  appendVersion();
383 
388  Name&
389  appendSegment(uint64_t segmentNo)
390  {
391  return append(Component::fromSegment(segmentNo));
392  }
393 
398  Name&
399  appendSegmentOffset(uint64_t offset)
400  {
401  return append(Component::fromSegmentOffset(offset));
402  }
403 
408  Name&
410  {
411  return append(Component::fromTimestamp(timePoint));
412  }
413 
418  Name&
419  appendTimestamp();
420 
425  Name&
426  appendSequenceNumber(uint64_t seqNo)
427  {
428  return append(Component::fromSequenceNumber(seqNo));
429  }
430 
434  Name&
436  {
438  }
439 
443  Name&
444  appendImplicitSha256Digest(const uint8_t* digest, size_t digestSize)
445  {
446  return append(Component::fromImplicitSha256Digest(digest, digestSize));
447  }
448 
453  Name&
454  append(const PartialName& name);
455 
459  template<class T>
460  void
461  push_back(const T& component)
462  {
463  append(component);
464  }
465 
469  void
471  {
472  m_wire = Block(tlv::Name);
473  }
474 
475 public: // algorithms
496  Name
497  getSuccessor() const;
498 
507  bool
508  isPrefixOf(const Name& other) const;
509 
515  bool
516  equals(const Name& other) const;
517 
539  int
540  compare(const Name& other) const
541  {
542  return this->compare(0, npos, other);
543  }
544 
550  int
551  compare(size_t pos1, size_t count1,
552  const Name& other, size_t pos2 = 0, size_t count2 = npos) const;
553 
554 public:
557  static const size_t npos;
558 
559 private:
560  mutable Block m_wire;
561 };
562 
564 
565 inline bool
566 operator==(const Name& lhs, const Name& rhs)
567 {
568  return lhs.equals(rhs);
569 }
570 
571 inline bool
572 operator!=(const Name& lhs, const Name& rhs)
573 {
574  return !lhs.equals(rhs);
575 }
576 
577 inline bool
578 operator<=(const Name& lhs, const Name& rhs)
579 {
580  return lhs.compare(rhs) <= 0;
581 }
582 
583 inline bool
584 operator<(const Name& lhs, const Name& rhs)
585 {
586  return lhs.compare(rhs) < 0;
587 }
588 
589 inline bool
590 operator>=(const Name& lhs, const Name& rhs)
591 {
592  return lhs.compare(rhs) >= 0;
593 }
594 
595 inline bool
596 operator>(const Name& lhs, const Name& rhs)
597 {
598  return lhs.compare(rhs) > 0;
599 }
600 
604 std::ostream&
605 operator<<(std::ostream& os, const Name& name);
606 
610 std::istream&
611 operator>>(std::istream& is, Name& name);
612 
613 } // namespace ndn
614 
615 namespace std {
616 
617 template<>
618 struct hash<ndn::Name>
619 {
620  size_t
621  operator()(const ndn::Name& name) const;
622 };
623 
624 } // namespace std
625 
626 #endif // NDN_NAME_HPP
static Component fromNumber(uint64_t number)
Create a component encoded as nonNegativeInteger.
const Component * const_iterator
Definition: name.hpp:66
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:210
void allocator_type
Definition: name.hpp:60
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:252
Copyright (c) 2011-2015 Regents of the University of California.
Name & appendTimestamp()
Append a timestamp component based on current time.
Definition: name.cpp:221
std::string toUri() const
Get URI representation of the name.
Definition: name.cpp:117
bool equals(const Name &other) const
Check if this name equals another name.
Definition: name.cpp:271
static Component fromNumberWithMarker(uint8_t marker, uint64_t number)
Create a component encoded as NameComponentWithMarker.
Name & appendTimestamp(const time::system_clock::TimePoint &timePoint)
Append a timestamp component.
Definition: name.hpp:409
const Block & wireEncode() const
Perform wire encoding, or return existing wire encoding.
Definition: name.cpp:141
const_iterator end() const
End iterator.
Definition: name.hpp:230
const Component & operator[](ssize_t i) const
Equivalent to get(i)
Definition: name.hpp:175
std::reverse_iterator< iterator > reverse_iterator
Definition: name.hpp:67
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:337
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:255
Name & appendVersion(uint64_t version)
Append a version component.
Definition: name.hpp:369
Name getSuccessor() const
Get the successor of a name.
Definition: name.cpp:242
const_reverse_iterator rend() const
Reverse end iterator.
Definition: name.hpp:246
static Component fromTimestamp(const time::system_clock::TimePoint &timePoint)
Create sequence number component using NDN naming conventions.
static const size_t npos
indicates "until the end" in getSubName and compare
Definition: name.hpp:557
STL namespace.
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
const_reverse_iterator rbegin() const
Reverse begin iterator.
Definition: name.hpp:238
bool operator<=(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.cpp:43
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:280
Name & appendNumber(uint64_t number)
Append a component with a nonNegativeInteger.
Definition: name.hpp:344
Name & append(const Component &component)
Append a component.
Definition: name.hpp:256
static Component fromSegmentOffset(uint64_t offset)
Create segment offset component using NDN naming conventions.
const element_container & elements() const
Get container of sub elements.
Definition: block.hpp:347
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: name.hpp:68
const Component & at(ssize_t i) const
Get the component at the given index.
Definition: name.cpp:180
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:540
Name & appendSegmentOffset(uint64_t offset)
Append a segment byte offset component.
Definition: name.hpp:399
Name & append(const Block &value)
Append a GenericNameComponent from a TLV element.
Definition: name.hpp:327
component_container::size_type size_type
Definition: name.hpp:70
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:291
name::Component Component
Definition: name.hpp:55
Name & appendSequenceNumber(uint64_t seqNo)
Append a sequence number component.
Definition: name.hpp:426
std::istream & operator>>(std::istream &is, Name &name)
Parse URI from stream as Name.
Definition: name.cpp:319
Represents an absolute name.
Definition: name.hpp:42
void push_back(const Block &element)
Append a sub element.
Definition: block.cpp:484
Name & append(Iterator first, Iterator last)
Append a GenericNameComponent, copying TLV-VALUE from a range.
Definition: name.hpp:305
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:371
Error(const std::string &what)
Definition: name.hpp:49
Name & append(const uint8_t *value, size_t count)
Append a GenericNameComponent, copying count bytes at value as TLV-VALUE.
Definition: name.hpp:276
const_iterator begin() const
Begin iterator.
Definition: name.hpp:222
void push_back(const T &component)
Append a component.
Definition: name.hpp:461
NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Exclude)
size_t size() const
Get number of components.
Definition: name.hpp:154
time_point TimePoint
Definition: time.hpp:196
bool operator>(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.hpp:54
Represents a name component.
static Component fromImplicitSha256Digest(const ConstBufferPtr &digest)
Create ImplicitSha256DigestComponent component.
bool operator>=(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.hpp:60
bool empty() const
Check if name is empty.
Definition: name.hpp:146
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:328
Name & appendImplicitSha256Digest(const uint8_t *digest, size_t digestSize)
Append an ImplicitSha256Digest component.
Definition: name.hpp:444
static Component fromVersion(uint64_t version)
Create version component using NDN naming conventions.
Name & append(const char *str)
Append a GenericNameComponent, copying TLV-VALUE from a null-terminated string.
Definition: name.hpp:316
Name deepCopy() const
Make a deep copy of the name, reallocating the underlying memory buffer.
Definition: name.cpp:169
component_container::difference_type difference_type
Definition: name.hpp:69
Name & appendNumberWithMarker(uint8_t marker, uint64_t number)
Append a component with a marked number.
Definition: name.hpp:359
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition: name.cpp:159
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:267
std::vector< Component > component_container
Definition: name.hpp:56
void clear()
Remove all components.
Definition: name.hpp:470
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:194
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:389
Name & appendImplicitSha256Digest(const ConstBufferPtr &digest)
Append an ImplicitSha256Digest component.
Definition: name.hpp:435
Name & appendVersion()
Append a version component based on current time.
Definition: name.cpp:215
Name()
Create an empty name.
Definition: name.cpp:55
bool hasWire() const
Check if this Name instance already has wire encoding.
Definition: name.hpp:113
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:89