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  appendByteOffset(uint64_t offset)
382  {
383  return append(Component::fromByteOffset(offset));
384  }
385 
387  Name&
388  appendSegmentOffset(uint64_t offset)
389  {
390  return appendByteOffset(offset);
391  }
392 
398  Name&
399  appendTimestamp(optional<time::system_clock::TimePoint> timestamp = nullopt);
400 
405  Name&
406  appendSequenceNumber(uint64_t seqNo)
407  {
408  return append(Component::fromSequenceNumber(seqNo));
409  }
410 
414  Name&
416  {
418  }
419 
423  Name&
424  appendImplicitSha256Digest(const uint8_t* digest, size_t digestSize)
425  {
426  return append(Component::fromImplicitSha256Digest(digest, digestSize));
427  }
428 
433  Name&
434  append(const PartialName& name);
435 
439  template<class T>
440  void
441  push_back(const T& component)
442  {
443  append(component);
444  }
445 
449  void
451  {
452  m_wire = Block(tlv::Name);
453  }
454 
455 public: // algorithms
480  Name
481  getSuccessor() const;
482 
491  bool
492  isPrefixOf(const Name& other) const;
493 
499  bool
500  equals(const Name& other) const;
501 
523  int
524  compare(const Name& other) const
525  {
526  return this->compare(0, npos, other);
527  }
528 
534  int
535  compare(size_t pos1, size_t count1,
536  const Name& other, size_t pos2 = 0, size_t count2 = npos) const;
537 
538 public:
541  static const size_t npos;
542 
543 private:
544  mutable Block m_wire;
545 };
546 
548 
549 inline bool
550 operator==(const Name& lhs, const Name& rhs)
551 {
552  return lhs.equals(rhs);
553 }
554 
555 inline bool
556 operator!=(const Name& lhs, const Name& rhs)
557 {
558  return !lhs.equals(rhs);
559 }
560 
561 inline bool
562 operator<=(const Name& lhs, const Name& rhs)
563 {
564  return lhs.compare(rhs) <= 0;
565 }
566 
567 inline bool
568 operator<(const Name& lhs, const Name& rhs)
569 {
570  return lhs.compare(rhs) < 0;
571 }
572 
573 inline bool
574 operator>=(const Name& lhs, const Name& rhs)
575 {
576  return lhs.compare(rhs) >= 0;
577 }
578 
579 inline bool
580 operator>(const Name& lhs, const Name& rhs)
581 {
582  return lhs.compare(rhs) > 0;
583 }
584 
588 std::ostream&
589 operator<<(std::ostream& os, const Name& name);
590 
594 std::istream&
595 operator>>(std::istream& is, Name& name);
596 
597 } // namespace ndn
598 
599 namespace std {
600 
601 template<>
602 struct hash<ndn::Name>
603 {
604  size_t
605  operator()(const ndn::Name& name) const;
606 };
607 
608 } // namespace std
609 
610 #endif // NDN_NAME_HPP
Name & appendByteOffset(uint64_t offset)
Append a byte offset component.
Definition: name.hpp:381
static Component fromSequenceNumber(uint64_t seqNo)
Create sequence number component using NDN naming conventions.
void allocator_type
Definition: name.hpp:53
Definition: data.cpp:26
const_iterator begin() const
Begin iterator.
Definition: name.hpp:215
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:391
static Component fromNumberWithMarker(uint8_t marker, uint64_t number)
Create a component encoded as NameComponentWithMarker.
Name getSuccessor() const
Get the successor of a name.
Definition: name.cpp:241
Name & appendImplicitSha256Digest(ConstBufferPtr digest)
Append an ImplicitSha256Digest component.
Definition: name.hpp:415
std::reverse_iterator< iterator > reverse_iterator
Definition: name.hpp:60
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:321
static const size_t npos
indicates "until the end" in getSubName and compare
Definition: name.hpp:541
STL namespace.
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
bool operator<=(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.cpp:43
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:230
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
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
const Component & operator[](ssize_t i) const
Equivalent to get(i)
Definition: name.hpp:168
Name & appendVersion(optional< uint64_t > version=nullopt)
Append a version component.
Definition: name.cpp:214
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: name.hpp:61
static Component fromSegment(uint64_t segmentNo)
Create segment number component using NDN naming conventions.
std::string toUri() const
Get URI representation of the name.
Definition: name.cpp:116
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
bool equals(const Name &other) const
Check if this name equals another name.
Definition: name.cpp:267
static Component fromByteOffset(uint64_t offset)
Create byte offset component using NDN naming conventions.
const_reverse_iterator rend() const
Reverse end iterator.
Definition: name.hpp:239
Name & appendSegmentOffset(uint64_t offset)
Definition: name.hpp:388
bool hasWire() const
Check if this Name instance already has wire encoding.
Definition: name.hpp:106
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
size_t size() const
Get number of components.
Definition: name.hpp:147
Name & appendSequenceNumber(uint64_t seqNo)
Append a sequence number component.
Definition: name.hpp:406
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:415
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
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:251
void push_back(const Block &element)
Append a sub-element.
Definition: block.cpp:456
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
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 Component & at(ssize_t i) const
Get the component at the given index.
Definition: name.cpp:179
void push_back(const T &component)
Append a component.
Definition: name.hpp:441
const_reverse_iterator rbegin() const
Reverse begin iterator.
Definition: name.hpp:231
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:274
bool operator>(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.hpp:54
Represents a name component.
int compare(const Name &other) const
Compare this to the other Name using NDN canonical ordering.
Definition: name.hpp:524
bool operator>=(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.hpp:60
Name deepCopy() const
Make a deep copy of the name, reallocating the underlying memory buffer.
Definition: name.cpp:168
static Component fromNumber(uint64_t number, uint32_t type=tlv::GenericNameComponent)
Create a component encoded as nonNegativeInteger.
bool empty() const
Check if name is empty.
Definition: name.hpp:139
PartialName getSubName(ssize_t iStartComponent, size_t nComponents=npos) const
Extract some components as a sub-name (PartialName)
Definition: name.cpp:193
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix of the name.
Definition: name.hpp:203
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:312
Name & appendImplicitSha256Digest(const uint8_t *digest, size_t digestSize)
Append an ImplicitSha256Digest component.
Definition: name.hpp:424
Name & append(const char *str)
Append a GenericNameComponent, copying TLV-VALUE from a null-terminated string.
Definition: name.hpp:309
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:450
bool operator<(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.cpp:36
const nullopt_t nullopt((nullopt_t::init()))
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:371
Name()
Create an empty name.
Definition: name.cpp:54
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126