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-2017 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 
25 #include "common.hpp"
26 #include "encoding/block.hpp"
28 #include "util/time.hpp"
29 
30 namespace ndn {
31 namespace name {
32 
34 static const uint8_t SEGMENT_MARKER = 0x00;
36 static const uint8_t SEGMENT_OFFSET_MARKER = 0xFB;
38 static const uint8_t VERSION_MARKER = 0xFD;
40 static const uint8_t TIMESTAMP_MARKER = 0xFC;
42 static const uint8_t SEQUENCE_NUMBER_MARKER = 0xFE;
43 
47 class Component : public Block
48 {
49 public:
53  class Error : public Block::Error
54  {
55  public:
56  explicit
57  Error(const std::string& what)
58  : Block::Error(what)
59  {
60  }
61  };
62 
66  Component();
67 
76  Component(const Block& wire);
77 
87  explicit
88  Component(const ConstBufferPtr& buffer);
89 
97  explicit
98  Component(const Buffer& buffer);
99 
108  Component(const uint8_t* buffer, size_t bufferSize);
109 
121  template<class Iterator>
122  Component(Iterator first, Iterator last);
123 
133  explicit
134  Component(const char* str);
135 
145  explicit
146  Component(const std::string& str);
147 
151  template<encoding::Tag TAG>
152  size_t
153  wireEncode(EncodingImpl<TAG>& encoder) const;
154 
158  const Block&
159  wireEncode() const;
160 
164  void
165  wireDecode(const Block& wire);
166 
181  static Component
182  fromEscapedString(const char* escapedString, size_t beginOffset, size_t endOffset);
183 
189  static Component
190  fromEscapedString(const char* escapedString)
191  {
192  return fromEscapedString(escapedString, 0, std::char_traits<char>::length(escapedString));
193  }
194 
200  static Component
201  fromEscapedString(const std::string& escapedString)
202  {
203  return fromEscapedString(escapedString.c_str(), 0, escapedString.size());
204  }
205 
213  void
214  toUri(std::ostream& os) const;
215 
223  std::string
224  toUri() const;
225 
227 
232  bool
233  isNumber() const;
234 
239  bool
240  isNumberWithMarker(uint8_t marker) const;
241 
246  bool
247  isVersion() const;
248 
253  bool
254  isSegment() const;
255 
260  bool
261  isSegmentOffset() const;
262 
267  bool
268  isTimestamp() const;
269 
274  bool
275  isSequenceNumber() const;
276 
278 
286  uint64_t
287  toNumber() const;
288 
299  uint64_t
300  toNumberWithMarker(uint8_t marker) const;
301 
310  uint64_t
311  toVersion() const;
312 
321  uint64_t
322  toSegment() const;
323 
332  uint64_t
333  toSegmentOffset() const;
334 
344  toTimestamp() const;
345 
354  uint64_t
355  toSequenceNumber() const;
356 
358 
367  static Component
368  fromNumber(uint64_t number);
369 
391  static Component
392  fromNumberWithMarker(uint8_t marker, uint64_t number);
393 
399  static Component
400  fromVersion(uint64_t version);
401 
407  static Component
408  fromSegment(uint64_t segmentNo);
409 
415  static Component
416  fromSegmentOffset(uint64_t offset);
417 
423  static Component
425 
431  static Component
432  fromSequenceNumber(uint64_t seqNo);
433 
435 
439  bool
440  isGeneric() const;
441 
445  bool
446  isImplicitSha256Digest() const;
447 
451  static Component
453 
457  static Component
458  fromImplicitSha256Digest(const uint8_t* digest, size_t digestSize);
459 
461 
462  bool
463  empty() const
464  {
465  return value_size() == 0;
466  }
467 
474  bool
475  equals(const Component& other) const;
476 
487  int
488  compare(const Component& other) const;
489 
496  bool
497  operator==(const Component& other) const
498  {
499  return equals(other);
500  }
501 
507  bool
508  operator!=(const Component& other) const
509  {
510  return !equals(other);
511  }
512 
519  bool
520  operator<=(const Component& other) const
521  {
522  return compare(other) <= 0;
523  }
524 
531  bool
532  operator<(const Component& other) const
533  {
534  return compare(other) < 0;
535  }
536 
543  bool
544  operator>=(const Component& other) const
545  {
546  return compare(other) >= 0;
547  }
548 
555  bool
556  operator>(const Component& other) const
557  {
558  return compare(other) > 0;
559  }
560 
561  Component
562  getSuccessor() const;
563 
564  // !!! NOTE TO IMPLEMENTOR !!!
565  //
566  // This class MUST NOT contain any data fields.
567  // Block can be reinterpret_cast'ed as Component type.
568 };
569 
571 
572 inline std::ostream&
573 operator<<(std::ostream& os, const Component& component)
574 {
575  component.toUri(os);
576  return os;
577 }
578 
579 template<class Iterator>
580 inline
581 Component::Component(Iterator first, Iterator last)
582  : Block(makeBinaryBlock(tlv::NameComponent, first, last))
583 {
584 }
585 
586 } // namespace name
587 } // namespace ndn
588 
589 #endif // NDN_NAME_COMPONENT_HPP
static Component fromNumber(uint64_t number)
Create a component encoded as nonNegativeInteger.
static Component fromSequenceNumber(uint64_t seqNo)
Create sequence number component using NDN naming conventions.
bool isGeneric() const
Check if the component is GenericComponent.
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
uint64_t toSegmentOffset() const
Interpret as segment offset component using NDN naming conventions.
bool isTimestamp() const
Check if the component is timestamp per NDN naming conventions.
static Component fromNumberWithMarker(uint8_t marker, uint64_t number)
Create a component encoded as NameComponentWithMarker.
int compare(const Component &other) const
Compare this to the other Component using NDN canonical ordering.
static Component fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset)
Create name::Component by decoding the escapedString between beginOffset and endOffset according to t...
const Block & wireEncode() const
Encode to a wire format.
std::string toUri() const
Convert *this by escaping characters according to the NDN URI Scheme.
bool operator==(const Component &other) const
Check if this is the same component as other.
std::ostream & operator<<(std::ostream &os, const Component &component)
time::system_clock::TimePoint toTimestamp() const
Interpret as timestamp component using NDN naming conventions.
bool isNumberWithMarker(uint8_t marker) const
Check if the component is NameComponentWithMarker per NDN naming conventions.
bool isSequenceNumber() const
Check if the component is sequence number per NDN naming conventions.
static Component fromTimestamp(const time::system_clock::TimePoint &timePoint)
Create sequence number component using NDN naming conventions.
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
bool operator<(const Component &other) const
Check if the *this is less than the other in NDN canonical ordering.
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
static Component fromSegmentOffset(uint64_t offset)
Create segment offset component using NDN naming conventions.
static const uint8_t SEGMENT_OFFSET_MARKER
Segment offset marker for NDN naming conventions.
import common constructs for ndn-cxx library internal use
uint64_t toNumberWithMarker(uint8_t marker) const
Interpret this name component as NameComponentWithMarker.
static Component fromSegment(uint64_t segmentNo)
Create segment number component using NDN naming conventions.
bool isSegment() const
Check if the component is segment number per NDN naming conventions.
bool isVersion() const
Check if the component is version per NDN naming conventions.
uint64_t toVersion() const
Interpret as version component using NDN naming conventions.
static Component fromEscapedString(const char *escapedString)
Create name::Component by decoding the escapedString according to the NDN URI Scheme.
uint64_t toSegment() const
Interpret as segment number component using NDN naming conventions.
static Component fromEscapedString(const std::string &escapedString)
Create name::Component by decoding the escapedString according to the NDN URI Scheme.
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block copying TLV-VALUE from raw buffer.
Component()
Create a new name::Component with an empty value.
bool operator<=(const Component &other) const
Check if the *this is less than or equal to the other in NDN canonical ordering.
void toUri(std::ostream &os) const
Write *this to the output stream, escaping characters according to the NDN URI Scheme.
bool operator!=(const Component &other) const
Check if this is not the same component as other.
size_t value_size() const
Get size of TLV-VALUE aka TLV-LENGTH.
Definition: block.cpp:317
Component getSuccessor() const
static const uint8_t VERSION_MARKER
Version marker for NDN naming conventions.
time_point TimePoint
Definition: time.hpp:90
Component holds a read-only name component value.
static Component fromImplicitSha256Digest(const ConstBufferPtr &digest)
Create ImplicitSha256DigestComponent component.
void wireDecode(const Block &wire)
Decode from the wire format.
static const uint8_t SEGMENT_MARKER
Segment marker for NDN naming conventions.
bool isNumber() const
Check if the component is nonNegativeInteger.
const uint8_t * wire() const
Get pointer to encoded wire.
Definition: block.cpp:290
static Component fromVersion(uint64_t version)
Create version component using NDN naming conventions.
bool isSegmentOffset() const
Check if the component is segment offset per NDN naming conventions.
static const uint8_t SEQUENCE_NUMBER_MARKER
Sequence number marker for NDN naming conventions.
uint64_t toSequenceNumber() const
Interpret as sequence number component using NDN naming conventions.
bool equals(const Component &other) const
Check if this is the same component as other.
uint64_t toNumber() const
Interpret this name component as nonNegativeInteger.
static const uint8_t TIMESTAMP_MARKER
Timestamp marker for 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 Component &other) const
Check if the *this is greater than the other in NDN canonical ordering.
bool isImplicitSha256Digest() const
Check if the component is ImplicitSha256DigestComponent.
Error that can be thrown from name::Component.
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:89
Error(const std::string &what)