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 Convention {
36  MARKER = 1 << 0,
37  TYPED = 1 << 1,
38  EITHER = MARKER | TYPED,
39 };
40 
43 enum : uint8_t {
49 };
50 
57 
61 void
63 
70 
75 void
77 
84 class Component : public Block
85 {
86 public:
87  class Error : public Block::Error
88  {
89  public:
90  using Block::Error::Error;
91  };
92 
93 public: // constructors
98  explicit
99  Component(uint32_t type = tlv::GenericNameComponent);
100 
107  Component(const Block& wire);
108 
116  Component(uint32_t type, ConstBufferPtr buffer);
117 
125  explicit
127  : Component(tlv::GenericNameComponent, std::move(buffer))
128  {
129  }
130 
134  Component(uint32_t type, const Buffer& buffer)
135  : Component(type, buffer.data(), buffer.size())
136  {
137  }
138 
142  explicit
143  Component(const Buffer& buffer)
144  : Component(tlv::GenericNameComponent, buffer)
145  {
146  }
147 
152  Component(uint32_t type, const uint8_t* value, size_t count);
153 
157  Component(const uint8_t* value, size_t count)
158  : Component(tlv::GenericNameComponent, value, count)
159  {
160  }
161 
170  template<class Iterator>
171  Component(uint32_t type, Iterator first, Iterator last)
172  : Block(makeBinaryBlock(type, first, last))
173  {
174  }
175 
179  template<class Iterator>
180  Component(Iterator first, Iterator last)
181  : Component(tlv::GenericNameComponent, first, last)
182  {
183  }
184 
190  explicit
191  Component(const char* str);
192 
198  explicit
199  Component(const std::string& str);
200 
201 public: // encoding and URI
205  template<encoding::Tag TAG>
206  size_t
207  wireEncode(EncodingImpl<TAG>& encoder) const;
208 
212  const Block&
213  wireEncode() const;
214 
218  void
219  wireDecode(const Block& wire);
220 
228  static Component
229  fromEscapedString(const char* input, size_t beginOffset, size_t endOffset)
230  {
231  return fromEscapedString(std::string(input + beginOffset, input + endOffset));
232  }
233 
238  static Component
239  fromEscapedString(const char* input)
240  {
241  return fromEscapedString(std::string(input));
242  }
243 
248  static Component
249  fromEscapedString(const std::string& input);
250 
258  void
259  toUri(std::ostream& os) const;
260 
268  std::string
269  toUri() const;
270 
271 public: // naming conventions
276  bool
277  isNumber() const;
278 
283  bool
284  isNumberWithMarker(uint8_t marker) const;
285 
290  bool
291  isVersion() const;
292 
297  bool
298  isSegment() const;
299 
304  bool
305  isByteOffset() const;
306 
308  bool
310  {
311  return isByteOffset();
312  }
313 
318  bool
319  isTimestamp() const;
320 
325  bool
326  isSequenceNumber() const;
327 
335  uint64_t
336  toNumber() const;
337 
348  uint64_t
349  toNumberWithMarker(uint8_t marker) const;
350 
358  uint64_t
359  toVersion() const;
360 
368  uint64_t
369  toSegment() const;
370 
378  uint64_t
379  toByteOffset() const;
380 
382  uint64_t
384  {
385  return toByteOffset();
386  }
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 
439  static Component
440  fromNumberWithMarker(uint8_t marker, uint64_t number);
441 
447  static Component
448  fromVersion(uint64_t version);
449 
455  static Component
456  fromSegment(uint64_t segmentNo);
457 
463  static Component
464  fromByteOffset(uint64_t offset);
465 
467  static Component
468  fromSegmentOffset(uint64_t offset)
469  {
470  return fromByteOffset(offset);
471  }
472 
478  static Component
479  fromTimestamp(const time::system_clock::TimePoint& timePoint);
480 
486  static Component
487  fromSequenceNumber(uint64_t seqNo);
488 
489 public: // commonly used TLV-TYPEs
493  bool
494  isGeneric() const;
495 
499  bool
500  isImplicitSha256Digest() const;
501 
505  static Component
506  fromImplicitSha256Digest(ConstBufferPtr digest);
507 
511  static Component
512  fromImplicitSha256Digest(const uint8_t* digest, size_t digestSize);
513 
517  bool
518  isParametersSha256Digest() const;
519 
523  static Component
524  fromParametersSha256Digest(ConstBufferPtr digest);
525 
529  static Component
530  fromParametersSha256Digest(const uint8_t* digest, size_t digestSize);
531 
532 public: // operators
533  bool
534  empty() const
535  {
536  return value_size() == 0;
537  }
538 
545  bool
546  equals(const Component& other) const;
547 
558  int
559  compare(const Component& other) const;
560 
567  bool
568  operator==(const Component& other) const
569  {
570  return equals(other);
571  }
572 
578  bool
579  operator!=(const Component& other) const
580  {
581  return !equals(other);
582  }
583 
590  bool
591  operator<=(const Component& other) const
592  {
593  return compare(other) <= 0;
594  }
595 
602  bool
603  operator<(const Component& other) const
604  {
605  return compare(other) < 0;
606  }
607 
614  bool
615  operator>=(const Component& other) const
616  {
617  return compare(other) >= 0;
618  }
619 
626  bool
627  operator>(const Component& other) const
628  {
629  return compare(other) > 0;
630  }
631 
657  Component
658  getSuccessor() const;
659 
660 private:
667  void
668  ensureValid() const;
669 
670  // !!! NOTE TO IMPLEMENTOR !!!
671  //
672  // This class MUST NOT contain any data fields.
673  // Block can be reinterpret_cast'ed as Component type.
674 };
675 
677 
678 inline std::ostream&
679 operator<<(std::ostream& os, const Component& component)
680 {
681  component.toUri(os);
682  return os;
683 }
684 
685 } // namespace name
686 } // namespace ndn
687 
688 #endif // NDN_NAME_COMPONENT_HPP
void setConventionDecoding(Convention convention)
Set which Naming Conventions style(s) to accept while decoding.
Definition: data.cpp:26
uint64_t toSegmentOffset() const
Component(uint32_t type, Iterator first, Iterator last)
Construct a NameComponent of TLV-TYPE type, copying TLV-VALUE from a range.
bool operator==(const Component &other) const
Check if this is the same component as other.
std::ostream & operator<<(std::ostream &os, const Component &component)
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
bool operator<=(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.cpp:43
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)
Convention getConventionDecoding()
Return which Naming Conventions style(s) to accept while decoding.
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.
void toUri(std::ostream &os) const
Write *this to the output stream, escaping characters according to the NDN URI Scheme.
import common constructs for ndn-cxx library internal use
bool operator!=(const Component &other) const
Check if this is not the same component as other.
Component(const Buffer &buffer)
Construct a GenericNameComponent, copying TLV-VALUE from buffer.
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.
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)
component markers (revision 1)
Convention getConventionEncoding()
Return which Naming Conventions style to use while encoding.
bool isSegmentOffset() const
Convention
Identify a style of NDN Naming Conventions.
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:40
bool operator>=(const Component &other) const
Check if the *this is greater or equal than the other in NDN canonical ordering.
bool operator<(const Delegation &lhs, const Delegation &rhs)
Definition: delegation.cpp:36
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
bool operator>(const Component &other) const
Check if the *this is greater than the other in NDN canonical ordering.
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126