NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
name-component.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 
22 #ifndef NDN_NAME_COMPONENT_HPP
23 #define NDN_NAME_COMPONENT_HPP
24 
28 #include "ndn-cxx/util/time.hpp"
29 
30 namespace ndn {
31 namespace name {
32 
35 enum class UriFormat {
36  DEFAULT,
37  CANONICAL,
38  ALTERNATE,
39 };
40 
44 enum class Convention {
45  MARKER = 1 << 0,
46  TYPED = 1 << 1,
47  EITHER = MARKER | TYPED,
48 };
49 
52 enum : uint8_t {
58 };
59 
66 
70 void
72 
79 
84 void
86 
93 class Component : public Block
94 {
95 public:
96  class Error : public Block::Error
97  {
98  public:
99  using Block::Error::Error;
100  };
101 
102 public: // constructors
107  explicit
109 
116  Component(const Block& wire);
117 
125  Component(uint32_t type, ConstBufferPtr buffer);
126 
134  explicit
136  : Component(tlv::GenericNameComponent, std::move(buffer))
137  {
138  }
139 
143  Component(uint32_t type, const Buffer& buffer)
144  : Component(type, buffer.data(), buffer.size())
145  {
146  }
147 
151  explicit
152  Component(const Buffer& buffer)
153  : Component(tlv::GenericNameComponent, buffer)
154  {
155  }
156 
161  Component(uint32_t type, const uint8_t* value, size_t count);
162 
166  Component(const uint8_t* value, size_t count)
167  : Component(tlv::GenericNameComponent, value, count)
168  {
169  }
170 
179  template<class Iterator>
180  Component(uint32_t type, Iterator first, Iterator last)
181  : Block(makeBinaryBlock(type, first, last))
182  {
183  }
184 
188  template<class Iterator>
189  Component(Iterator first, Iterator last)
190  : Component(tlv::GenericNameComponent, first, last)
191  {
192  }
193 
199  explicit
200  Component(const char* str);
201 
207  explicit
208  Component(const std::string& str);
209 
210 public: // encoding and URI
214  template<encoding::Tag TAG>
215  size_t
216  wireEncode(EncodingImpl<TAG>& encoder) const;
217 
221  const Block&
222  wireEncode() const;
223 
227  void
228  wireDecode(const Block& wire);
229 
237  static Component
238  fromEscapedString(const char* input, size_t beginOffset, size_t endOffset)
239  {
240  return fromEscapedString(std::string(input + beginOffset, input + endOffset));
241  }
242 
247  static Component
248  fromEscapedString(const char* input)
249  {
250  return fromEscapedString(std::string(input));
251  }
252 
257  static Component
258  fromEscapedString(const std::string& input);
259 
264  void
265  toUri(std::ostream& os, UriFormat format = UriFormat::DEFAULT) const;
266 
271  std::string
272  toUri(UriFormat format = UriFormat::DEFAULT) const;
273 
274 public: // naming conventions
279  bool
280  isNumber() const;
281 
287  bool
288  isNumberWithMarker(uint8_t marker) const;
289 
294  bool
295  isVersion() const;
296 
301  bool
302  isSegment() const;
303 
308  bool
309  isByteOffset() const;
310 
312  bool
314  {
315  return isByteOffset();
316  }
317 
322  bool
323  isTimestamp() const;
324 
329  bool
330  isSequenceNumber() const;
331 
339  uint64_t
340  toNumber() const;
341 
353  uint64_t
354  toNumberWithMarker(uint8_t marker) const;
355 
363  uint64_t
364  toVersion() const;
365 
373  uint64_t
374  toSegment() const;
375 
383  uint64_t
384  toByteOffset() const;
385 
387  uint64_t
389  {
390  return toByteOffset();
391  }
392 
401  toTimestamp() const;
402 
410  uint64_t
411  toSequenceNumber() const;
412 
421  static Component
422  fromNumber(uint64_t number, uint32_t type = tlv::GenericNameComponent);
423 
441  static Component
442  fromNumberWithMarker(uint8_t marker, uint64_t number);
443 
449  static Component
450  fromVersion(uint64_t version);
451 
457  static Component
458  fromSegment(uint64_t segmentNo);
459 
465  static Component
466  fromByteOffset(uint64_t offset);
467 
469  static Component
470  fromSegmentOffset(uint64_t offset)
471  {
472  return fromByteOffset(offset);
473  }
474 
480  static Component
482 
488  static Component
489  fromSequenceNumber(uint64_t seqNo);
490 
491 public: // commonly used TLV-TYPEs
495  bool
496  isGeneric() const;
497 
501  bool
502  isImplicitSha256Digest() const;
503 
507  static Component
509 
513  static Component
514  fromImplicitSha256Digest(const uint8_t* digest, size_t digestSize);
515 
519  bool
520  isParametersSha256Digest() const;
521 
525  static Component
527 
531  static Component
532  fromParametersSha256Digest(const uint8_t* digest, size_t digestSize);
533 
534 public: // comparison
535  NDN_CXX_NODISCARD bool
536  empty() const
537  {
538  return value_size() == 0;
539  }
540 
547  bool
548  equals(const Component& other) const;
549 
560  int
561  compare(const Component& other) const;
562 
588  Component
589  getSuccessor() const;
590 
591 private:
599  void
600  ensureValid() const;
601 
602 private: // non-member operators
603  // NOTE: the following "hidden friend" operators are available via
604  // argument-dependent lookup only and must be defined inline.
605 
606  friend bool
607  operator==(const Component& lhs, const Component& rhs)
608  {
609  return lhs.equals(rhs);
610  }
611 
612  friend bool
613  operator!=(const Component& lhs, const Component& rhs)
614  {
615  return !lhs.equals(rhs);
616  }
617 
618  friend bool
619  operator<(const Component& lhs, const Component& rhs)
620  {
621  return lhs.compare(rhs) < 0;
622  }
623 
624  friend bool
625  operator<=(const Component& lhs, const Component& rhs)
626  {
627  return lhs.compare(rhs) <= 0;
628  }
629 
630  friend bool
631  operator>(const Component& lhs, const Component& rhs)
632  {
633  return lhs.compare(rhs) > 0;
634  }
635 
636  friend bool
637  operator>=(const Component& lhs, const Component& rhs)
638  {
639  return lhs.compare(rhs) >= 0;
640  }
641 
642  friend std::ostream&
643  operator<<(std::ostream& os, const Component& component)
644  {
645  component.toUri(os);
646  return os;
647  }
648 
649  // !!! NOTE TO IMPLEMENTOR !!!
650  //
651  // This class MUST NOT contain any data fields.
652  // Block can be reinterpret_cast'ed as Component type.
653 };
654 
656 
657 } // namespace name
658 } // namespace ndn
659 
660 #endif // NDN_NAME_COMPONENT_HPP
ndn::name::SEGMENT_OFFSET_MARKER
@ SEGMENT_OFFSET_MARKER
Definition: name-component.hpp:54
common.hpp
Common includes and macros used throughout the library.
ndn::name::Component::equals
bool equals(const Component &other) const
Check if this is the same component as other.
Definition: name-component.cpp:454
ndn::name::Component::fromTimestamp
static Component fromTimestamp(const time::system_clock::TimePoint &timePoint)
Create sequence number component using NDN naming conventions.
Definition: name-component.cpp:391
ndn::name::Component::isByteOffset
bool isByteOffset() const
Check if the component is a byte offset per NDN naming conventions.
Definition: name-component.cpp:232
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::Component::isNumber
bool isNumber() const
Check if the component is a nonNegativeInteger.
Definition: name-component.cpp:204
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::getConventionEncoding
Convention getConventionEncoding()
Return which Naming Conventions style to use while encoding.
Definition: name-component.cpp:47
ndn::name::UriFormat::DEFAULT
@ DEFAULT
ALTERNATE, unless NDN_NAME_ALT_URI environment variable is set to '0'.
ndn::time::system_clock::TimePoint
time_point TimePoint
Definition: time.hpp:195
ndn::name::Component::isSegment
bool isSegment() const
Check if the component is a segment number per NDN naming conventions.
Definition: name-component.cpp:225
ndn::name::Component::isSegmentOffset
bool isSegmentOffset() const
Definition: name-component.hpp:313
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::Component::isGeneric
bool isGeneric() const
Check if the component is GenericComponent.
Definition: name-component.cpp:410
ndn::name::Component::Component
Component(ConstBufferPtr buffer)
Construct a GenericNameComponent, using TLV-VALUE from buffer.
Definition: name-component.hpp:135
ndn::Block::value_size
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH.
Definition: block.cpp:308
ndn::name::Component::toNumberWithMarker
uint64_t toNumberWithMarker(uint8_t marker) const
Interpret this name component as NameComponentWithMarker.
Definition: name-component.cpp:264
ndn::name::NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS
NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Component)
ndn::name::Component::isSequenceNumber
bool isSequenceNumber() const
Check if the component is a sequence number per NDN naming conventions.
Definition: name-component.cpp:246
ndn::name::TIMESTAMP_MARKER
@ TIMESTAMP_MARKER
Definition: name-component.hpp:56
ndn::name::Convention::EITHER
@ EITHER
ndn::Buffer
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:41
ndn::name::setConventionEncoding
void setConventionEncoding(Convention convention)
Set which Naming Conventions style to use while encoding.
Definition: name-component.cpp:53
block.hpp
ndn::name::Component::toUri
void toUri(std::ostream &os, UriFormat format=UriFormat::DEFAULT) const
Write *this to the output stream, escaping characters according to the NDN URI format.
Definition: name-component.cpp:183
ndn::name::Component::compare
int compare(const Component &other) const
Compare this to the other Component using NDN canonical ordering.
Definition: name-component.cpp:463
ndn::name::VERSION_MARKER
@ VERSION_MARKER
Definition: name-component.hpp:55
ndn::name::Component::toSegmentOffset
uint64_t toSegmentOffset() const
Definition: name-component.hpp:388
ndn::name::Component::toByteOffset
uint64_t toByteOffset() const
Interpret as byte offset component using NDN naming conventions.
Definition: name-component.cpp:299
ndn::name::Component::operator!=
friend bool operator!=(const Component &lhs, const Component &rhs)
Definition: name-component.hpp:613
ndn::name::Component::toVersion
uint64_t toVersion() const
Interpret as version component using NDN naming conventions.
Definition: name-component.cpp:275
ndn::name::Component::toNumber
uint64_t toNumber() const
Interpret this name component as nonNegativeInteger.
Definition: name-component.cpp:255
ndn::name::Component::operator<=
friend bool operator<=(const Component &lhs, const Component &rhs)
Definition: name-component.hpp:625
ndn::name::setConventionDecoding
void setConventionDecoding(Convention convention)
Set which Naming Conventions style(s) to accept while decoding.
Definition: name-component.cpp:72
ndn::name::Component::fromParametersSha256Digest
static Component fromParametersSha256Digest(ConstBufferPtr digest)
Create ParametersSha256DigestComponent component.
Definition: name-component.cpp:440
ndn::name::Component::wireDecode
void wireDecode(const Block &wire)
Decode from the wire format.
Definition: name-component.cpp:534
ndn::name::Component::getSuccessor
Component getSuccessor() const
Get the successor of this name component.
Definition: name-component.cpp:488
ndn::name::Component::operator==
friend bool operator==(const Component &lhs, const Component &rhs)
Definition: name-component.hpp:607
ndn::name::Component::toSequenceNumber
uint64_t toSequenceNumber() const
Interpret as sequence number component using NDN naming conventions.
Definition: name-component.cpp:327
NDN_CXX_NODISCARD
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
ndn::name::Component::fromVersion
static Component fromVersion(uint64_t version)
Create version component using NDN naming conventions.
Definition: name-component.cpp:367
ndn::Block::type
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:274
ndn::name::Component::Component
Component(uint32_t type, Iterator first, Iterator last)
Construct a NameComponent of TLV-TYPE type, copying TLV-VALUE from a range.
Definition: name-component.hpp:180
ndn::name::Component::isVersion
bool isVersion() const
Check if the component is a version per NDN naming conventions.
Definition: name-component.cpp:218
ndn::name::Component::Component
Component(Iterator first, Iterator last)
Construct a GenericNameComponent, copying TLV-VALUE from a range.
Definition: name-component.hpp:189
ndn::name::Component::isNumberWithMarker
bool isNumberWithMarker(uint8_t marker) const
Check if the component is a NameComponentWithMarker per NDN naming conventions rev1.
Definition: name-component.cpp:211
ndn::name::Component::toSegment
uint64_t toSegment() const
Interpret as segment number component using NDN naming conventions.
Definition: name-component.cpp:287
ndn::name::Convention
Convention
Identify a style of NDN Naming Conventions.
Definition: name-component.hpp:44
ndn::name::Component::isTimestamp
bool isTimestamp() const
Check if the component is a timestamp per NDN naming conventions.
Definition: name-component.cpp:239
ndn::name::Component::isImplicitSha256Digest
bool isImplicitSha256Digest() const
Check if the component is ImplicitSha256DigestComponent.
Definition: name-component.cpp:416
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::Component::fromSequenceNumber
static Component fromSequenceNumber(uint64_t seqNo)
Create sequence number component using NDN naming conventions.
Definition: name-component.cpp:400
ndn::name::Component::operator<
friend bool operator<(const Component &lhs, const Component &rhs)
Definition: name-component.hpp:619
ndn::name::Component::empty
NDN_CXX_NODISCARD bool empty() const
Definition: name-component.hpp:536
ndn::name::Component::operator>
friend bool operator>(const Component &lhs, const Component &rhs)
Definition: name-component.hpp:631
ndn::name::Component::fromSegmentOffset
static Component fromSegmentOffset(uint64_t offset)
Definition: name-component.hpp:470
ndn::name::Component::Error
Definition: name-component.hpp:97
ndn::name::UriFormat::CANONICAL
@ CANONICAL
always use <type-number>=<percent-encoded-value> format
ndn::tlv::GenericNameComponent
@ GenericNameComponent
Definition: tlv.hpp:68
ndn::name::Component::operator<<
friend std::ostream & operator<<(std::ostream &os, const Component &component)
Definition: name-component.hpp:643
ndn::encoding::EncodingImpl
Definition: encoding-buffer-fwd.hpp:36
ndn::name::Component::wireEncode
const Block & wireEncode() const
Encode to a wire format.
Definition: name-component.cpp:518
ndn::name::SEGMENT_MARKER
@ SEGMENT_MARKER
Definition: name-component.hpp:53
block-helpers.hpp
ndn::Block::Error
Definition: block.hpp:50
ndn::name::UriFormat::ALTERNATE
@ ALTERNATE
prefer alternate format when available
ndn::name::Component::toTimestamp
time::system_clock::TimePoint toTimestamp() const
Interpret as timestamp component using NDN naming conventions.
Definition: name-component.cpp:311
ndn::Block::value
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
Definition: block.cpp:302
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::name::Component::fromEscapedString
static Component fromEscapedString(const char *input)
Decode NameComponent from a URI component.
Definition: name-component.hpp:248
ndn::Block::size
size_t size() const
Return the size of the encoded wire, i.e.
Definition: block.cpp:290
ndn::name::Convention::TYPED
@ TYPED
typed name components (revision 2)
ndn::name
Definition: name-component-types.hpp:33
ndn::name::Convention::MARKER
@ MARKER
component markers (revision 1)
ndn::name::Component::Component
Component(uint32_t type, const Buffer &buffer)
Construct a NameComponent of TLV-TYPE type, copying TLV-VALUE from buffer.
Definition: name-component.hpp:143
ndn::name::Component::fromImplicitSha256Digest
static Component fromImplicitSha256Digest(ConstBufferPtr digest)
Create ImplicitSha256DigestComponent component.
Definition: name-component.cpp:422
ndn::name::SEQUENCE_NUMBER_MARKER
@ SEQUENCE_NUMBER_MARKER
Definition: name-component.hpp:57
ndn::name::Component::operator>=
friend bool operator>=(const Component &lhs, const Component &rhs)
Definition: name-component.hpp:637
ndn::name::Component::Component
Component(const uint8_t *value, size_t count)
Construct a GenericNameComponent, copying count bytes at value as TLV-VALUE.
Definition: name-component.hpp:166
time.hpp
ndn::name::Component::fromEscapedString
static Component fromEscapedString(const char *input, size_t beginOffset, size_t endOffset)
Decode NameComponent from a URI component.
Definition: name-component.hpp:238
ndn::name::Component::Component
Component(uint32_t type=tlv::GenericNameComponent)
Construct a NameComponent of TLV-TYPE type, using empty TLV-VALUE.
Definition: name-component.cpp:109
ndn::Block::wire
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
Definition: block.cpp:281
ndn::tlv::Error::Error
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
ndn::name::Component::isParametersSha256Digest
bool isParametersSha256Digest() const
Check if the component is ParametersSha256DigestComponent.
Definition: name-component.cpp:434
ndn::encoding::makeBinaryBlock
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block copying TLV-VALUE from raw buffer.
Definition: block-helpers.cpp:181
ndn::ConstBufferPtr
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126
ndn::name::Component::Component
Component(const Buffer &buffer)
Construct a GenericNameComponent, copying TLV-VALUE from buffer.
Definition: name-component.hpp:152
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::name::UriFormat
UriFormat
Identify a format of URI representation.
Definition: name-component.hpp:35
ndn::name::getConventionDecoding
Convention getConventionDecoding()
Return which Naming Conventions style(s) to accept while decoding.
Definition: name-component.cpp:66