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 
48  using component_container = std::vector<Component>;
49 
50  // Name appears as a container of name components
52  using allocator_type = void;
53  using reference = Component&;
54  using const_reference = const Component&;
55  using pointer = Component*;
56  using const_pointer = const Component*;
57  using iterator = const Component*; // disallow modifying via iterator
58  using const_iterator = const Component*;
59  using reverse_iterator = std::reverse_iterator<iterator>;
60  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
61  using difference_type = component_container::difference_type;
62  using size_type = component_container::size_type;
63 
64 public: // constructors, encoding, decoding
68  Name();
69 
79  explicit
80  Name(const Block& wire);
81 
86  Name(const char* uri);
87 
92  Name(std::string uri);
93 
99  std::string
100  toUri() const;
101 
104  bool
105  hasWire() const
106  {
107  return m_wire.hasWire();
108  }
109 
112  template<encoding::Tag TAG>
113  size_t
114  wireEncode(EncodingImpl<TAG>& encoder) const;
115 
119  const Block&
120  wireEncode() const;
121 
126  void
127  wireDecode(const Block& wire);
128 
131  Name
132  deepCopy() const;
133 
134 public: // access
137  bool
138  empty() const
139  {
140  return m_wire.elements().empty();
141  }
142 
145  size_t
146  size() const
147  {
148  return m_wire.elements_size();
149  }
150 
155  const Component&
156  get(ssize_t i) const
157  {
158  if (i < 0) {
159  i += size();
160  }
161  return reinterpret_cast<const Component&>(m_wire.elements()[i]);
162  }
163 
166  const Component&
167  operator[](ssize_t i) const
168  {
169  return get(i);
170  }
171 
176  const Component&
177  at(ssize_t i) const;
178 
192  getSubName(ssize_t iStartComponent, size_t nComponents = npos) const;
193 
202  getPrefix(ssize_t nComponents) const
203  {
204  if (nComponents < 0)
205  return getSubName(0, size() + nComponents);
206  else
207  return getSubName(0, nComponents);
208  }
209 
210 public: // iterators
214  begin() const
215  {
216  return reinterpret_cast<const_iterator>(m_wire.elements().data());
217  }
218 
222  end() const
223  {
224  return reinterpret_cast<const_iterator>(m_wire.elements().data() + m_wire.elements().size());
225  }
226 
230  rbegin() const
231  {
232  return const_reverse_iterator(end());
233  }
234 
238  rend() const
239  {
240  return const_reverse_iterator(begin());
241  }
242 
243 public: // modifiers
247  Name&
248  append(const Component& component)
249  {
250  m_wire.push_back(component);
251  return *this;
252  }
253 
258  Name&
259  append(uint32_t type, const uint8_t* value, size_t count)
260  {
261  return append(Component(type, value, count));
262  }
263 
267  Name&
268  append(const uint8_t* value, size_t count)
269  {
270  return append(Component(value, count));
271  }
272 
281  template<class Iterator>
282  Name&
283  append(uint32_t type, Iterator first, Iterator last)
284  {
285  return append(Component(type, first, last));
286  }
287 
295  template<class Iterator>
296  Name&
297  append(Iterator first, Iterator last)
298  {
299  return append(Component(first, last));
300  }
301 
307  Name&
308  append(const char* str)
309  {
310  return append(Component(str));
311  }
312 
318  Name&
319  append(const Block& value)
320  {
321  if (value.type() == tlv::GenericNameComponent) {
322  m_wire.push_back(value);
323  }
324  else {
325  m_wire.push_back(Block(tlv::GenericNameComponent, value));
326  }
327  return *this;
328  }
329 
335  Name&
336  appendNumber(uint64_t number)
337  {
338  return append(Component::fromNumber(number));
339  }
340 
350  Name&
351  appendNumberWithMarker(uint8_t marker, uint64_t number)
352  {
353  return append(Component::fromNumberWithMarker(marker, number));
354  }
355 
360  Name&
361  appendVersion(uint64_t version)
362  {
363  return append(Component::fromVersion(version));
364  }
365 
373  Name&
374  appendVersion();
375 
380  Name&
381  appendSegment(uint64_t segmentNo)
382  {
383  return append(Component::fromSegment(segmentNo));
384  }
385 
390  Name&
391  appendSegmentOffset(uint64_t offset)
392  {
393  return append(Component::fromSegmentOffset(offset));
394  }
395 
400  Name&
402  {
403  return append(Component::fromTimestamp(timePoint));
404  }
405 
410  Name&
411  appendTimestamp();
412 
417  Name&
418  appendSequenceNumber(uint64_t seqNo)
419  {
420  return append(Component::fromSequenceNumber(seqNo));
421  }
422 
426  Name&
428  {
429  return append(Component::fromImplicitSha256Digest(std::move(digest)));
430  }
431 
435  Name&
436  appendImplicitSha256Digest(const uint8_t* digest, size_t digestSize)
437  {
438  return append(Component::fromImplicitSha256Digest(digest, digestSize));
439  }
440 
445  Name&
446  append(const PartialName& name);
447 
451  template<class T>
452  void
453  push_back(const T& component)
454  {
455  append(component);
456  }
457 
461  void
463  {
464  m_wire = Block(tlv::Name);
465  }
466 
467 public: // algorithms
492  Name
493  getSuccessor() const;
494 
503  bool
504  isPrefixOf(const Name& other) const;
505 
511  bool
512  equals(const Name& other) const;
513 
535  int
536  compare(const Name& other) const
537  {
538  return this->compare(0, npos, other);
539  }
540 
546  int
547  compare(size_t pos1, size_t count1,
548  const Name& other, size_t pos2 = 0, size_t count2 = npos) const;
549 
550 public:
553  static const size_t npos;
554 
555 private:
556  mutable Block m_wire;
557 };
558 
560 
561 inline bool
562 operator==(const Name& lhs, const Name& rhs)
563 {
564  return lhs.equals(rhs);
565 }
566 
567 inline bool
568 operator!=(const Name& lhs, const Name& rhs)
569 {
570  return !lhs.equals(rhs);
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 
585 inline bool
586 operator>=(const Name& lhs, const Name& rhs)
587 {
588  return lhs.compare(rhs) >= 0;
589 }
590 
591 inline bool
592 operator>(const Name& lhs, const Name& rhs)
593 {
594  return lhs.compare(rhs) > 0;
595 }
596 
600 std::ostream&
601 operator<<(std::ostream& os, const Name& name);
602 
606 std::istream&
607 operator>>(std::istream& is, Name& name);
608 
609 } // namespace ndn
610 
611 namespace std {
612 
613 template<>
614 struct hash<ndn::Name>
615 {
616  size_t
617  operator()(const ndn::Name& name) const;
618 };
619 
620 } // namespace std
621 
622 #endif // NDN_NAME_HPP
static Component fromNumber(uint64_t number)
Create a component encoded as nonNegativeInteger.
static Component fromSequenceNumber(uint64_t seqNo)
Create sequence number component using NDN naming conventions.
void allocator_type
Definition: name.hpp:52
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:65
Name & appendTimestamp()
Append a timestamp component based on current time.
Definition: name.cpp:221
const_iterator begin() const
Begin iterator.
Definition: name.hpp:214
const element_container & elements() const
Get container of sub elements.
Definition: block.hpp:361
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:401
Name getSuccessor() const
Get the successor of a name.
Definition: name.cpp:242
Name & appendImplicitSha256Digest(ConstBufferPtr digest)
Append an ImplicitSha256Digest component.
Definition: name.hpp:427
std::reverse_iterator< iterator > reverse_iterator
Definition: name.hpp:59
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:337
Name & appendVersion(uint64_t version)
Append a version component.
Definition: name.hpp:361
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:553
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 operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:276
Name & appendNumber(uint64_t number)
Append a component with a nonNegativeInteger.
Definition: name.hpp:336
Name & append(const Component &component)
Append a component.
Definition: name.hpp:248
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
static Component fromSegmentOffset(uint64_t offset)
Create segment offset component using NDN naming conventions.
const Component & operator[](ssize_t i) const
Equivalent to get(i)
Definition: name.hpp:167
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: name.hpp:60
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:117
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:222
bool equals(const Name &other) const
Check if this name equals another name.
Definition: name.cpp:268
const_reverse_iterator rend() const
Reverse end iterator.
Definition: name.hpp:238
Name & appendSegmentOffset(uint64_t offset)
Append a segment byte offset component.
Definition: name.hpp:391
bool hasWire() const
Check if this Name instance already has wire encoding.
Definition: name.hpp:105
Name & append(const Block &value)
Append a GenericNameComponent from a TLV element.
Definition: name.hpp:319
component_container::size_type size_type
Definition: name.hpp:62
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:283
name::Component Component
Definition: name.hpp:47
size_t size() const
Get number of components.
Definition: name.hpp:146
Name & appendSequenceNumber(uint64_t seqNo)
Append a sequence number component.
Definition: name.hpp:418
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:385
std::istream & operator>>(std::istream &is, Name &name)
Parse URI from stream as Name.
Definition: name.cpp:316
Represents an absolute name.
Definition: name.hpp:42
bool isPrefixOf(const Name &other) const
Check if this name is a prefix of another name.
Definition: name.cpp:252
void push_back(const Block &element)
Append a sub element.
Definition: block.cpp:465
Name & append(Iterator first, Iterator last)
Append a GenericNameComponent, copying TLV-VALUE from a range.
Definition: name.hpp:297
Name & append(const uint8_t *value, size_t count)
Append a GenericNameComponent, copying count bytes at value as TLV-VALUE.
Definition: name.hpp:268
const Component & at(ssize_t i) const
Get the component at the given index.
Definition: name.cpp:180
void push_back(const T &component)
Append a component.
Definition: name.hpp:453
const_reverse_iterator rbegin() const
Reverse begin iterator.
Definition: name.hpp:230
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:249
time_point TimePoint
Definition: time.hpp:195
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:536
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:169
bool empty() const
Check if name is empty.
Definition: name.hpp:138
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:249
PartialName getSubName(ssize_t iStartComponent, size_t nComponents=npos) const
Extract some components as a sub-name (PartialName)
Definition: name.cpp:194
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix of the name.
Definition: name.hpp:202
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:436
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:308
component_container::difference_type difference_type
Definition: name.hpp:61
Name & appendNumberWithMarker(uint8_t marker, uint64_t number)
Append a component with a marked number.
Definition: name.hpp:351
static Component fromImplicitSha256Digest(ConstBufferPtr digest)
Create ImplicitSha256DigestComponent component.
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:259
std::vector< Component > component_container
Definition: name.hpp:48
void clear()
Remove all components.
Definition: name.hpp:462
bool operator<(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.cpp:36
Name & appendSegment(uint64_t segmentNo)
Append a segment number (sequential) component.
Definition: name.hpp:381
Name & appendVersion()
Append a version component based on current time.
Definition: name.cpp:215
Name()
Create an empty name.
Definition: name.cpp:55
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126