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
108  Component(uint32_t type = tlv::GenericNameComponent);
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
481  fromTimestamp(const time::system_clock::TimePoint& timePoint);
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
508  fromImplicitSha256Digest(ConstBufferPtr digest);
509 
513  static Component
514  fromImplicitSha256Digest(const uint8_t* digest, size_t digestSize);
515 
519  bool
520  isParametersSha256Digest() const;
521 
525  static Component
526  fromParametersSha256Digest(ConstBufferPtr digest);
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
void setConventionDecoding(Convention convention)
Set which Naming Conventions style(s) to accept while decoding.
Definition: data.cpp:26
UriFormat
Identify a format of URI representation.
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:322
STL namespace.
void setConventionEncoding(Convention convention)
Set which Naming Conventions style to use while encoding.
Represents a TLV element of 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)
bool isSegmentOffset() const
ALTERNATE, unless NDN_NAME_ALT_URI environment variable is set to &#39;0&#39;.
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.
uint64_t toSegmentOffset() const
Component(const Buffer &buffer)
Construct a GenericNameComponent, copying TLV-VALUE from buffer.
bool operator<(optional< T > const &x, optional< U > const &y)
Definition: optional.hpp:1439
bool equals(const Component &other) const
Check if this is the same component as other.
friend bool operator!=(const Component &lhs, const Component &rhs)
prefer 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.
static Component fromSegmentOffset(uint64_t offset)
bool operator<=(optional< T > const &x, optional< U > const &y)
Definition: optional.hpp:1451
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