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-2022 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 
27 #include "ndn-cxx/util/time.hpp"
28 
29 namespace ndn {
30 namespace name {
31 
36 enum class UriFormat {
38  CANONICAL,
40  ALTERNATE,
47 };
48 
53 enum class Convention {
54  MARKER = 1 << 0,
55  TYPED = 1 << 1,
56  EITHER = MARKER | TYPED,
57 };
58 
63 enum : uint8_t {
69 };
70 
77 getConventionEncoding() noexcept;
78 
83 void
85 
92 getConventionDecoding() noexcept;
93 
99 void
101 
113 class Component : public Block
114 {
115 public:
116  class Error : public Block::Error
117  {
118  public:
119  using Block::Error::Error;
120  };
121 
122 public: // constructors
127  explicit
128  Component(uint32_t type = tlv::GenericNameComponent);
129 
134  explicit
135  Component(const Block& block);
136 
144  Component(uint32_t type, ConstBufferPtr buffer);
145 
153  explicit
155  : Component(tlv::GenericNameComponent, std::move(buffer))
156  {
157  }
158 
162  Component(uint32_t type, span<const uint8_t> value);
163 
167  explicit
168  Component(span<const uint8_t> buffer)
169  : Component(tlv::GenericNameComponent, buffer)
170  {
171  }
172 
178  [[deprecated("use the constructor that takes a span<>")]]
179  Component(uint32_t type, const uint8_t* value, size_t count)
180  : Component(type, {value, count})
181  {
182  }
183 
188  [[deprecated("use the constructor that takes a span<>")]]
189  Component(const uint8_t* value, size_t count)
190  : Component(tlv::GenericNameComponent, {value, count})
191  {
192  }
193 
202  template<class Iterator>
203  Component(uint32_t type, Iterator first, Iterator last)
204  : Block(makeBinaryBlock(type, first, last))
205  {
206  }
207 
211  template<class Iterator>
212  Component(Iterator first, Iterator last)
213  : Component(tlv::GenericNameComponent, first, last)
214  {
215  }
216 
222  explicit
223  Component(const char* str);
224 
230  explicit
231  Component(const std::string& str);
232 
233 public: // encoding and URI
237  template<encoding::Tag TAG>
238  size_t
239  wireEncode(EncodingImpl<TAG>& encoder) const;
240 
244  const Block&
245  wireEncode() const;
246 
250  void
251  wireDecode(const Block& wire);
252 
260  static Component
261  fromEscapedString(const char* input, size_t beginOffset, size_t endOffset)
262  {
263  return fromEscapedString(std::string(input + beginOffset, input + endOffset));
264  }
265 
270  static Component
271  fromEscapedString(const char* input)
272  {
273  return fromEscapedString(std::string(input));
274  }
275 
280  static Component
281  fromEscapedString(const std::string& input);
282 
287  void
288  toUri(std::ostream& os, UriFormat format = UriFormat::DEFAULT) const;
289 
294  std::string
295  toUri(UriFormat format = UriFormat::DEFAULT) const;
296 
297 public: // naming conventions
302  bool
303  isNumber() const noexcept;
304 
310  bool
311  isNumberWithMarker(uint8_t marker) const noexcept;
312 
317  bool
318  isSegment() const noexcept;
319 
324  bool
325  isByteOffset() const noexcept;
326 
331  bool
332  isVersion() const noexcept;
333 
338  bool
339  isTimestamp() const noexcept;
340 
345  bool
346  isSequenceNumber() const noexcept;
347 
353  uint64_t
354  toNumber() const;
355 
367  uint64_t
368  toNumberWithMarker(uint8_t marker) const;
369 
375  uint64_t
376  toSegment() const;
377 
383  uint64_t
384  toByteOffset() const;
385 
391  uint64_t
392  toVersion() const;
393 
399  time::system_clock::time_point
400  toTimestamp() const;
401 
407  uint64_t
408  toSequenceNumber() const;
409 
418  static Component
419  fromNumber(uint64_t number, uint32_t type = tlv::GenericNameComponent);
420 
438  static Component
439  fromNumberWithMarker(uint8_t marker, uint64_t number);
440 
445  static Component
446  fromSegment(uint64_t segmentNo);
447 
452  static Component
453  fromByteOffset(uint64_t offset);
454 
459  static Component
460  fromVersion(uint64_t version);
461 
466  static Component
467  fromTimestamp(const time::system_clock::time_point& timePoint);
468 
473  static Component
474  fromSequenceNumber(uint64_t seqNo);
475 
476 public: // commonly used TLV-TYPEs
481  bool
482  isGeneric() const noexcept
483  {
484  return type() == tlv::GenericNameComponent;
485  }
486 
492  bool
493  isImplicitSha256Digest() const noexcept;
494 
499  [[deprecated("use one of the name::Component constructors")]]
500  static Component
501  fromImplicitSha256Digest(ConstBufferPtr digest);
502 
507  [[deprecated("use one of the name::Component constructors")]]
508  static Component
509  fromImplicitSha256Digest(span<const uint8_t> digest);
510 
516  bool
517  isParametersSha256Digest() const noexcept;
518 
523  [[deprecated("use one of the name::Component constructors")]]
524  static Component
525  fromParametersSha256Digest(ConstBufferPtr digest);
526 
531  [[deprecated("use one of the name::Component constructors")]]
532  static Component
533  fromParametersSha256Digest(span<const uint8_t> digest);
534 
539  bool
540  isKeyword() const noexcept
541  {
542  return type() == tlv::KeywordNameComponent;
543  }
544 
545 public: // comparison
546  NDN_CXX_NODISCARD bool
547  empty() const noexcept
548  {
549  return value_size() == 0;
550  }
551 
558  bool
559  equals(const Component& other) const noexcept;
560 
571  int
572  compare(const Component& other) const;
573 
599  Component
600  getSuccessor() const;
601 
602 private:
606  void
607  ensureValid() const;
608 
609 private: // non-member operators
610  // NOTE: the following "hidden friend" operators are available via
611  // argument-dependent lookup only and must be defined inline.
612 
613  friend bool
614  operator==(const Component& lhs, const Component& rhs) noexcept
615  {
616  return lhs.equals(rhs);
617  }
618 
619  friend bool
620  operator!=(const Component& lhs, const Component& rhs) noexcept
621  {
622  return !lhs.equals(rhs);
623  }
624 
625  friend bool
626  operator<(const Component& lhs, const Component& rhs)
627  {
628  return lhs.compare(rhs) < 0;
629  }
630 
631  friend bool
632  operator<=(const Component& lhs, const Component& rhs)
633  {
634  return lhs.compare(rhs) <= 0;
635  }
636 
637  friend bool
638  operator>(const Component& lhs, const Component& rhs)
639  {
640  return lhs.compare(rhs) > 0;
641  }
642 
643  friend bool
644  operator>=(const Component& lhs, const Component& rhs)
645  {
646  return lhs.compare(rhs) >= 0;
647  }
648 
649  friend std::ostream&
650  operator<<(std::ostream& os, const Component& component)
651  {
652  component.toUri(os);
653  return os;
654  }
655 
656  // !!! NOTE TO IMPLEMENTOR !!!
657  //
658  // This class MUST NOT contain any data fields.
659  // Block can be reinterpret_cast'ed as Component type.
660 };
661 
663 
664 } // namespace name
665 } // namespace ndn
666 
667 #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
Represents a name component.
Component(ConstBufferPtr buffer)
Construct a GenericNameComponent, using TLV-VALUE from buffer.
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.
friend bool operator!=(const Component &lhs, const Component &rhs) noexcept
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 fromEscapedString(const char *input)
Decode NameComponent from a URI component.
friend bool operator>(const Component &lhs, const Component &rhs)
friend bool operator>=(const Component &lhs, const Component &rhs)
friend bool operator==(const Component &lhs, const Component &rhs) noexcept
Component(Iterator first, Iterator last)
Construct a GenericNameComponent, copying TLV-VALUE from a range.
Component(span< const uint8_t > buffer)
Construct a GenericNameComponent, copying the TLV-VALUE from buffer.
void toUri(std::ostream &os, UriFormat format=UriFormat::DEFAULT) const
Write *this to the output stream, escaping characters according to the NDN URI format.
bool empty() const noexcept
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.
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
#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)
@ SEQUENCE_NUMBER_MARKER
void setConventionDecoding(Convention convention)
Set which Naming Conventions style(s) to accept while decoding.
Convention getConventionEncoding() noexcept
Return which Naming Conventions style to use while encoding.
void setConventionEncoding(Convention convention)
Set which Naming Conventions style to use while encoding.
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'.
Convention getConventionDecoding() noexcept
Return which Naming Conventions style(s) to accept while decoding.
@ GenericNameComponent
Definition: tlv.hpp:72
@ KeywordNameComponent
Definition: tlv.hpp:75
Definition: data.cpp:25
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:374