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-2020 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 
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 
102 class Component : public Block
103 {
104 public:
105  class Error : public Block::Error
106  {
107  public:
108  using Block::Error::Error;
109  };
110 
111 public: // constructors
116  explicit
117  Component(uint32_t type = tlv::GenericNameComponent);
118 
125  Component(const Block& wire);
126 
134  Component(uint32_t type, ConstBufferPtr buffer);
135 
143  explicit
145  : Component(tlv::GenericNameComponent, std::move(buffer))
146  {
147  }
148 
152  Component(uint32_t type, const Buffer& buffer)
153  : Component(type, buffer.data(), buffer.size())
154  {
155  }
156 
160  explicit
161  Component(const Buffer& buffer)
162  : Component(tlv::GenericNameComponent, buffer)
163  {
164  }
165 
170  Component(uint32_t type, const uint8_t* value, size_t count);
171 
175  Component(const uint8_t* value, size_t count)
176  : Component(tlv::GenericNameComponent, value, count)
177  {
178  }
179 
188  template<class Iterator>
189  Component(uint32_t type, Iterator first, Iterator last)
190  : Block(makeBinaryBlock(type, first, last))
191  {
192  }
193 
197  template<class Iterator>
198  Component(Iterator first, Iterator last)
199  : Component(tlv::GenericNameComponent, first, last)
200  {
201  }
202 
208  explicit
209  Component(const char* str);
210 
216  explicit
217  Component(const std::string& str);
218 
219 public: // encoding and URI
223  template<encoding::Tag TAG>
224  size_t
225  wireEncode(EncodingImpl<TAG>& encoder) const;
226 
230  const Block&
231  wireEncode() const;
232 
236  void
237  wireDecode(const Block& wire);
238 
246  static Component
247  fromEscapedString(const char* input, size_t beginOffset, size_t endOffset)
248  {
249  return fromEscapedString(std::string(input + beginOffset, input + endOffset));
250  }
251 
256  static Component
257  fromEscapedString(const char* input)
258  {
259  return fromEscapedString(std::string(input));
260  }
261 
266  static Component
267  fromEscapedString(const std::string& input);
268 
273  void
274  toUri(std::ostream& os, UriFormat format = UriFormat::DEFAULT) const;
275 
280  std::string
281  toUri(UriFormat format = UriFormat::DEFAULT) const;
282 
283 public: // naming conventions
288  bool
289  isNumber() const;
290 
296  bool
297  isNumberWithMarker(uint8_t marker) const;
298 
303  bool
304  isVersion() const;
305 
310  bool
311  isSegment() const;
312 
317  bool
318  isByteOffset() const;
319 
324  bool
325  isTimestamp() const;
326 
331  bool
332  isSequenceNumber() const;
333 
341  uint64_t
342  toNumber() const;
343 
355  uint64_t
356  toNumberWithMarker(uint8_t marker) const;
357 
365  uint64_t
366  toVersion() const;
367 
375  uint64_t
376  toSegment() const;
377 
385  uint64_t
386  toByteOffset() const;
387 
396  toTimestamp() const;
397 
405  uint64_t
406  toSequenceNumber() const;
407 
416  static Component
417  fromNumber(uint64_t number, uint32_t type = tlv::GenericNameComponent);
418 
436  static Component
437  fromNumberWithMarker(uint8_t marker, uint64_t number);
438 
444  static Component
445  fromVersion(uint64_t version);
446 
452  static Component
453  fromSegment(uint64_t segmentNo);
454 
460  static Component
461  fromByteOffset(uint64_t offset);
462 
468  static Component
469  fromTimestamp(const time::system_clock::TimePoint& timePoint);
470 
476  static Component
477  fromSequenceNumber(uint64_t seqNo);
478 
479 public: // commonly used TLV-TYPEs
483  bool
484  isGeneric() const;
485 
489  bool
490  isImplicitSha256Digest() const;
491 
495  static Component
496  fromImplicitSha256Digest(ConstBufferPtr digest);
497 
501  static Component
502  fromImplicitSha256Digest(const uint8_t* digest, size_t digestSize);
503 
507  bool
508  isParametersSha256Digest() const;
509 
513  static Component
514  fromParametersSha256Digest(ConstBufferPtr digest);
515 
519  static Component
520  fromParametersSha256Digest(const uint8_t* digest, size_t digestSize);
521 
522 public: // comparison
523  NDN_CXX_NODISCARD bool
524  empty() const
525  {
526  return value_size() == 0;
527  }
528 
535  bool
536  equals(const Component& other) const;
537 
548  int
549  compare(const Component& other) const;
550 
576  Component
577  getSuccessor() const;
578 
579 private:
587  void
588  ensureValid() const;
589 
590 private: // non-member operators
591  // NOTE: the following "hidden friend" operators are available via
592  // argument-dependent lookup only and must be defined inline.
593 
594  friend bool
595  operator==(const Component& lhs, const Component& rhs)
596  {
597  return lhs.equals(rhs);
598  }
599 
600  friend bool
601  operator!=(const Component& lhs, const Component& rhs)
602  {
603  return !lhs.equals(rhs);
604  }
605 
606  friend bool
607  operator<(const Component& lhs, const Component& rhs)
608  {
609  return lhs.compare(rhs) < 0;
610  }
611 
612  friend bool
613  operator<=(const Component& lhs, const Component& rhs)
614  {
615  return lhs.compare(rhs) <= 0;
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 std::ostream&
631  operator<<(std::ostream& os, const Component& component)
632  {
633  component.toUri(os);
634  return os;
635  }
636 
637  // !!! NOTE TO IMPLEMENTOR !!!
638  //
639  // This class MUST NOT contain any data fields.
640  // Block can be reinterpret_cast'ed as Component type.
641 };
642 
644 
645 } // namespace name
646 } // namespace ndn
647 
648 #endif // NDN_NAME_COMPONENT_HPP
void setConventionDecoding(Convention convention)
Set which Naming Conventions style(s) to accept while decoding.
Same as UriFormat::CANONICAL, unless NDN_NAME_ALT_URI environment variable is set to &#39;1&#39;...
Definition: data.cpp:26
UriFormat
Format used for the URI representation of a name.
Component(uint32_t type, Iterator first, Iterator last)
Construct a NameComponent of TLV-TYPE type, copying TLV-VALUE from a range.
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:383
Same as UriFormat::ALTERNATE, unless NDN_NAME_ALT_URI environment variable is set to &#39;0&#39;...
STL namespace.
void setConventionEncoding(Convention convention)
Set which Naming Conventions style to use while encoding.
Represents a TLV element of the NDN packet format.
Definition: block.hpp:42
int compare(const Component &other) const
Compare this to the other Component using NDN canonical ordering.
static Component fromEscapedString(const char *input)
Decode NameComponent from a URI component.
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
Component(uint32_t type, const Buffer &buffer)
Construct a NameComponent of TLV-TYPE type, copying TLV-VALUE from buffer.
Typed name components (revision 2)
Use the library&#39;s default format; currently equivalent to UriFormat::ENV_OR_ALTERNATE.
Convention getConventionDecoding()
Return which Naming Conventions style(s) to accept while decoding.
friend bool operator>=(const Component &lhs, const Component &rhs)
friend bool operator>(const Component &lhs, const Component &rhs)
static Component fromEscapedString(const char *input, size_t beginOffset, size_t endOffset)
Decode NameComponent from a URI component.
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block copying TLV-VALUE from raw buffer.
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
Common includes and macros used throughout the library.
Component(const Buffer &buffer)
Construct a GenericNameComponent, copying TLV-VALUE from buffer.
bool equals(const Component &other) const
Check if this is the same component as other.
friend bool operator!=(const Component &lhs, const Component &rhs)
Always prefer the alternate format when available.
Component(ConstBufferPtr buffer)
Construct a GenericNameComponent, using TLV-VALUE from buffer.
time_point TimePoint
Definition: time.hpp:195
Represents a name component.
Component(Iterator first, Iterator last)
Construct a GenericNameComponent, copying TLV-VALUE from a range.
Always use <type-number>=<percent-encoded-value> format.
Component(const uint8_t *value, size_t count)
Construct a GenericNameComponent, copying count bytes at value as TLV-VALUE.
Component markers (revision 1)
Convention getConventionEncoding()
Return which Naming Conventions style to use while encoding.
Convention
Identify a style of NDN Naming Conventions.
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:40
friend bool operator==(const Component &lhs, const Component &rhs)
void toUri(std::ostream &os, UriFormat format=UriFormat::DEFAULT) const
Write *this to the output stream, escaping characters according to the NDN URI format.
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126