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-2021 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_CXX_NAME_COMPONENT_HPP
23 #define NDN_CXX_NAME_COMPONENT_HPP
24 
28 #include "ndn-cxx/util/time.hpp"
29 
30 namespace ndn {
31 namespace name {
32 
36 enum class UriFormat {
38  CANONICAL,
40  ALTERNATE,
47 };
48 
52 enum class Convention {
53  MARKER = 1 << 0,
54  TYPED = 1 << 1,
55  EITHER = MARKER | TYPED,
56 };
57 
61 enum : uint8_t {
67 };
68 
75 
79 void
81 
88 
93 void
95 
107 class Component : public Block
108 {
109 public:
110  class Error : public Block::Error
111  {
112  public:
113  using Block::Error::Error;
114  };
115 
116 public: // constructors
121  explicit
123 
130  Component(const Block& wire);
131 
139  Component(uint32_t type, ConstBufferPtr buffer);
140 
148  explicit
150  : Component(tlv::GenericNameComponent, std::move(buffer))
151  {
152  }
153 
157  Component(uint32_t type, span<const uint8_t> buffer);
158 
162  explicit
163  Component(span<const uint8_t> buffer)
164  : Component(tlv::GenericNameComponent, buffer)
165  {
166  }
167 
172  Component(uint32_t type, const uint8_t* value, size_t count)
173  : Component(type, {value, count})
174  {
175  }
176 
180  Component(const uint8_t* value, size_t count)
181  : Component(tlv::GenericNameComponent, {value, count})
182  {
183  }
184 
193  template<class Iterator>
194  Component(uint32_t type, Iterator first, Iterator last)
195  : Block(makeBinaryBlock(type, first, last))
196  {
197  }
198 
202  template<class Iterator>
203  Component(Iterator first, Iterator last)
204  : Component(tlv::GenericNameComponent, first, last)
205  {
206  }
207 
213  explicit
214  Component(const char* str);
215 
221  explicit
222  Component(const std::string& str);
223 
224 public: // encoding and URI
228  template<encoding::Tag TAG>
229  size_t
230  wireEncode(EncodingImpl<TAG>& encoder) const;
231 
235  const Block&
236  wireEncode() const;
237 
241  void
242  wireDecode(const Block& wire);
243 
251  static Component
252  fromEscapedString(const char* input, size_t beginOffset, size_t endOffset)
253  {
254  return fromEscapedString(std::string(input + beginOffset, input + endOffset));
255  }
256 
261  static Component
262  fromEscapedString(const char* input)
263  {
264  return fromEscapedString(std::string(input));
265  }
266 
271  static Component
272  fromEscapedString(const std::string& input);
273 
278  void
279  toUri(std::ostream& os, UriFormat format = UriFormat::DEFAULT) const;
280 
285  std::string
286  toUri(UriFormat format = UriFormat::DEFAULT) const;
287 
288 public: // naming conventions
293  bool
294  isNumber() const;
295 
301  bool
302  isNumberWithMarker(uint8_t marker) const;
303 
308  bool
309  isVersion() const;
310 
315  bool
316  isSegment() const;
317 
322  bool
323  isByteOffset() const;
324 
329  bool
330  isTimestamp() const;
331 
336  bool
337  isSequenceNumber() const;
338 
346  uint64_t
347  toNumber() const;
348 
360  uint64_t
361  toNumberWithMarker(uint8_t marker) const;
362 
370  uint64_t
371  toVersion() const;
372 
380  uint64_t
381  toSegment() const;
382 
390  uint64_t
391  toByteOffset() const;
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 
473  static Component
475 
481  static Component
482  fromSequenceNumber(uint64_t seqNo);
483 
484 public: // commonly used TLV-TYPEs
488  bool
489  isGeneric() const;
490 
494  bool
495  isImplicitSha256Digest() const;
496 
500  static Component
502 
506  static Component
507  fromImplicitSha256Digest(span<const uint8_t> digest);
508 
512  bool
513  isParametersSha256Digest() const;
514 
518  static Component
520 
524  static Component
525  fromParametersSha256Digest(span<const uint8_t> digest);
526 
527 public: // comparison
528  NDN_CXX_NODISCARD bool
529  empty() const
530  {
531  return value_size() == 0;
532  }
533 
540  bool
541  equals(const Component& other) const;
542 
553  int
554  compare(const Component& other) const;
555 
581  Component
582  getSuccessor() const;
583 
584 private:
588  void
589  ensureValid() const;
590 
591 private: // non-member operators
592  // NOTE: the following "hidden friend" operators are available via
593  // argument-dependent lookup only and must be defined inline.
594 
595  friend bool
596  operator==(const Component& lhs, const Component& rhs)
597  {
598  return lhs.equals(rhs);
599  }
600 
601  friend bool
602  operator!=(const Component& lhs, const Component& rhs)
603  {
604  return !lhs.equals(rhs);
605  }
606 
607  friend bool
608  operator<(const Component& lhs, const Component& rhs)
609  {
610  return lhs.compare(rhs) < 0;
611  }
612 
613  friend bool
614  operator<=(const Component& lhs, const Component& rhs)
615  {
616  return lhs.compare(rhs) <= 0;
617  }
618 
619  friend bool
620  operator>(const Component& lhs, const Component& rhs)
621  {
622  return lhs.compare(rhs) > 0;
623  }
624 
625  friend bool
626  operator>=(const Component& lhs, const Component& rhs)
627  {
628  return lhs.compare(rhs) >= 0;
629  }
630 
631  friend std::ostream&
632  operator<<(std::ostream& os, const Component& component)
633  {
634  component.toUri(os);
635  return os;
636  }
637 
638  // !!! NOTE TO IMPLEMENTOR !!!
639  //
640  // This class MUST NOT contain any data fields.
641  // Block can be reinterpret_cast'ed as Component type.
642 };
643 
645 
646 } // namespace name
647 } // namespace ndn
648 
649 #endif // NDN_CXX_NAME_COMPONENT_HPP
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
Definition: block.cpp:296
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:285
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH.
Definition: block.cpp:323
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
Definition: block.cpp:317
Represents a name component.
uint64_t toSegment() const
Interpret as segment number component using NDN naming conventions.
Component(ConstBufferPtr buffer)
Construct a GenericNameComponent, using TLV-VALUE from buffer.
static Component fromSegment(uint64_t segmentNo)
Create a segment number component using NDN naming conventions.
Component(uint32_t type, const uint8_t *value, size_t count)
Construct a NameComponent of TLV-TYPE type, copying count bytes at value as TLV-VALUE.
const Block & wireEncode() const
Encode to a wire format.
Component getSuccessor() const
Get the successor of this name component.
bool isGeneric() const
Check if the component is GenericNameComponent.
static Component fromEscapedString(const char *input, size_t beginOffset, size_t endOffset)
Decode NameComponent from a URI component.
Component(const uint8_t *value, size_t count)
Construct a GenericNameComponent, copying count bytes at value as TLV-VALUE.
static Component fromNumber(uint64_t number, uint32_t type=tlv::GenericNameComponent)
Create a component encoded as NonNegativeInteger.
static Component fromTimestamp(const time::system_clock::time_point &timePoint)
Create a timestamp component using NDN naming conventions.
bool isByteOffset() const
Check if the component is a byte offset per NDN naming conventions.
static Component fromEscapedString(const char *input)
Decode NameComponent from a URI component.
bool isSegment() const
Check if the component is a segment number per NDN naming conventions.
static Component fromParametersSha256Digest(ConstBufferPtr digest)
Create ParametersSha256DigestComponent component.
uint64_t toNumberWithMarker(uint8_t marker) const
Interpret this name component as NameComponentWithMarker.
friend bool operator>(const Component &lhs, const Component &rhs)
friend bool operator>=(const Component &lhs, const Component &rhs)
bool isTimestamp() const
Check if the component is a timestamp per NDN naming conventions.
bool isParametersSha256Digest() const
Check if the component is ParametersSha256DigestComponent.
static Component fromSequenceNumber(uint64_t seqNo)
Create a sequence number component using NDN naming conventions.
uint64_t toByteOffset() const
Interpret as byte offset component using NDN naming conventions.
static Component fromVersion(uint64_t version)
Create a version component using NDN naming conventions.
uint64_t toSequenceNumber() const
Interpret as sequence number component using NDN naming conventions.
bool isSequenceNumber() const
Check if the component is a sequence number per NDN naming conventions.
Component(Iterator first, Iterator last)
Construct a GenericNameComponent, copying TLV-VALUE from a range.
Component(uint32_t type=tlv::GenericNameComponent)
Construct a NameComponent of TLV-TYPE type and with empty TLV-VALUE.
friend std::ostream & operator<<(std::ostream &os, const Component &component)
Component(span< const uint8_t > buffer)
Construct a GenericNameComponent, copying TLV-VALUE from buffer.
static Component fromByteOffset(uint64_t offset)
Create a byte offset component using NDN naming conventions.
bool isVersion() const
Check if the component is a version per NDN naming conventions.
bool isNumberWithMarker(uint8_t marker) const
Check if the component is a NameComponentWithMarker per NDN naming conventions rev1.
friend bool operator<(const Component &lhs, const Component &rhs)
bool isNumber() const
Check if the component is a NonNegativeInteger.
static Component fromNumberWithMarker(uint8_t marker, uint64_t number)
Create a component encoded as NameComponentWithMarker.
void toUri(std::ostream &os, UriFormat format=UriFormat::DEFAULT) const
Write *this to the output stream, escaping characters according to the NDN URI format.
void wireDecode(const Block &wire)
Decode from the wire format.
time::system_clock::time_point toTimestamp() const
Interpret as timestamp component using NDN naming conventions.
friend bool operator!=(const Component &lhs, const Component &rhs)
static Component fromImplicitSha256Digest(ConstBufferPtr digest)
Create ImplicitSha256DigestComponent component.
bool equals(const Component &other) const
Check if this is the same component as other.
friend bool operator<=(const Component &lhs, const Component &rhs)
Component(uint32_t type, Iterator first, Iterator last)
Construct a NameComponent of TLV-TYPE type, copying TLV-VALUE from a range.
int compare(const Component &other) const
Compare this to the other Component using NDN canonical ordering.
uint64_t toNumber() const
Interpret this name component as NonNegativeInteger.
bool isImplicitSha256Digest() const
Check if the component is ImplicitSha256DigestComponent.
friend bool operator==(const Component &lhs, const Component &rhs)
uint64_t toVersion() const
Interpret as version component using NDN naming conventions.
boost::chrono::time_point< system_clock > time_point
Definition: time.hpp:200
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
Common includes and macros used throughout the library.
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
Block makeBinaryBlock(uint32_t type, span< const uint8_t > value)
Create a TLV block copying the TLV-VALUE from a byte range.
Convention
Identify a style of NDN Naming Conventions.
@ MARKER
Component markers (revision 1)
@ TYPED
Typed name components (revision 3)
void setConventionDecoding(Convention convention)
Set which Naming Conventions style(s) to accept while decoding.
Convention getConventionEncoding()
Return which Naming Conventions style to use while encoding.
@ SEQUENCE_NUMBER_MARKER
void setConventionEncoding(Convention convention)
Set which Naming Conventions style to use while encoding.
Convention getConventionDecoding()
Return which Naming Conventions style(s) to accept while decoding.
UriFormat
Format used for the URI representation of a name.
@ CANONICAL
Always use <type-number>=<percent-encoded-value> format.
@ DEFAULT
Use the library's default format; currently equivalent to UriFormat::ENV_OR_ALTERNATE.
@ ALTERNATE
Always prefer the alternate format when available.
@ ENV_OR_ALTERNATE
Same as UriFormat::ALTERNATE, unless NDN_NAME_ALT_URI environment variable is set to '0'.
@ ENV_OR_CANONICAL
Same as UriFormat::CANONICAL, unless NDN_NAME_ALT_URI environment variable is set to '1'.
@ GenericNameComponent
Definition: tlv.hpp:68
Definition: data.cpp:25
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139