name.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
24 #ifndef NDN_NAME_HPP
25 #define NDN_NAME_HPP
26 
27 #include <vector>
28 #include <string>
29 #include <string.h>
30 #include <sstream>
31 #include "util/blob.hpp"
32 #include "encoding/wire-format.hpp"
33 #include "lite/name-lite.hpp"
34 
35 namespace ndn {
36 
40 class Name {
41 public:
45  class Component {
46  public:
51  : value_((const uint8_t*)0, 0)
52  {
53  }
54 
59  Component(const std::vector<uint8_t>& value)
60  : value_(value)
61  {
62  }
63 
69  Component(const uint8_t *value, size_t valueLen)
70  : value_(value, valueLen)
71  {
72  }
73 
81  Component(const char* value)
82  : value_((const uint8_t*)value, ::strlen(value))
83  {
84  }
85 
93  Component(const std::string& value)
94  : value_((const uint8_t*)&value[0], value.size())
95  {
96  }
97 
102  Component(const Blob &value)
103  : value_(value)
104  {
105  }
106 
113  void
114  get(NameLite::Component& componentLite) const;
115 
116  const Blob&
117  getValue() const { return value_; }
118 
124  void
125  toEscapedString(std::ostringstream& result) const
126  {
127  Name::toEscapedString(*value_, result);
128  }
129 
135  std::string
137  {
138  return Name::toEscapedString(*value_);
139  }
140 
145  uint64_t
146  toNumber() const;
147 
154  uint64_t
155  toNumberWithMarker(uint8_t marker) const;
156 
164  uint64_t
165  toNumberWithPrefix(const uint8_t* prefix, size_t prefixLength) const;
166 
173  bool
174  hasPrefix(const uint8_t* prefix, size_t prefixLength) const;
175 
183  uint64_t
184  toSegment() const
185  {
186  return toNumberWithMarker(0x00);
187  }
188 
196  uint64_t
198  {
199  return toNumberWithMarker(0xFB);
200  }
201 
205  uint64_t
206  DEPRECATED_IN_NDN_CPP toSeqNum() const
207  {
208  return toSegment();
209  }
210 
214  bool
215  DEPRECATED_IN_NDN_CPP isFinalSegment() const { return hasPrefix(getFinalSegmentPrefix(), getFinalSegmentPrefixLength()); }
216 
220  uint64_t
221  DEPRECATED_IN_NDN_CPP toFinalSegment() const
222  {
224  }
225 
234  uint64_t
235  toVersion() const
236  {
237  return toNumberWithMarker(0xFD);
238  }
239 
248  uint64_t
249  toTimestamp() const
250  {
251  return toNumberWithMarker(0xFC);
252  }
253 
261  uint64_t
263  {
264  return toNumberWithMarker(0xFE);
265  }
266 
273  static Component
274  fromNumber(uint64_t number);
275 
283  static Component
284  fromNumberWithMarker(uint64_t number, uint8_t marker);
285 
294  static Component
295  fromNumberWithPrefix(uint64_t number, const uint8_t* prefix, size_t prefixLength);
296 
300  static const uint8_t*
301  getFinalSegmentPrefix() { return FINAL_SEGMENT_PREFIX; }
302 
306  static size_t
307  getFinalSegmentPrefixLength() { return FINAL_SEGMENT_PREFIX_LENGTH; }
308 
314  bool
315  equals(const Component& other) const
316  {
317  return *value_ == *other.value_;
318  }
319 
325  bool
326  operator == (const Component& other) const { return equals(other); }
327 
333  bool
334  operator != (const Component& other) const { return !equals(other); }
335 
344  int
345  compare(const Component& other) const;
346 
353  bool
354  operator <= (const Component& other) const { return compare(other) <= 0; }
355 
362  bool
363  operator < (const Component& other) const { return compare(other) < 0; }
364 
371  bool
372  operator >= (const Component& other) const { return compare(other) >= 0; }
373 
380  bool
381  operator > (const Component& other) const { return compare(other) > 0; }
382 
383  private:
387  static const uint8_t FINAL_SEGMENT_PREFIX[];
388  static size_t FINAL_SEGMENT_PREFIX_LENGTH;
389 
390  Blob value_;
391  };
392 
397  : changeCount_(0)
398  {
399  }
400 
405  Name(const std::vector<Component>& components)
406  : components_(components), changeCount_(0)
407  {
408  }
409 
414  Name(const char* uri)
415  : changeCount_(0)
416  {
417  set(uri);
418  }
419 
424  Name(const std::string& uri)
425  : changeCount_(0)
426  {
427  set(uri.c_str());
428  }
429 
437  void
438  get(NameLite& nameLite) const;
439 
444  void
445  set(const NameLite& nameLite);
446 
451  void
452  set(const char *uri);
453 
458  void
459  set(const std::string& uri) { set(uri.c_str()); }
460 
465  Name&
466  append(const uint8_t *value, size_t valueLength)
467  {
468  return append(Component(value, valueLength));
469  }
470 
475  Name&
476  append(const std::vector<uint8_t>& value)
477  {
478  return append(Component(value));
479  }
480 
481  Name&
482  append(const Blob &value)
483  {
484  return append(Component(value));
485  }
486 
487  Name&
488  append(const Component &value)
489  {
490  components_.push_back(value);
491  ++changeCount_;
492  return *this;
493  }
494 
503  Name&
504  append(const char* value)
505  {
506  return append(Component(value));
507  }
508 
517  Name&
518  append(const std::string& value)
519  {
520  return append(Component(value));
521  }
522 
528  Name&
529  append(const Name& name);
530 
534  Name&
535  DEPRECATED_IN_NDN_CPP appendComponent(const uint8_t *value, size_t valueLength)
536  {
537  return append(value, valueLength);
538  }
539 
543  Name&
544  DEPRECATED_IN_NDN_CPP appendComponent(const std::vector<uint8_t>& value)
545  {
546  return append(value);
547  }
548 
552  Name&
553  DEPRECATED_IN_NDN_CPP appendComponent(const Blob &value)
554  {
555  return append(value);
556  }
557 
561  Name&
562  DEPRECATED_IN_NDN_CPP addComponent(const uint8_t *value, size_t valueLength)
563  {
564  return append(value, valueLength);
565  }
566 
570  Name&
571  DEPRECATED_IN_NDN_CPP addComponent(const std::vector<uint8_t>& value)
572  {
573  return append(value);
574  }
575 
579  Name&
580  DEPRECATED_IN_NDN_CPP addComponent(const Blob &value)
581  {
582  return append(value);
583  }
584 
588  void
589  clear() {
590  components_.clear();
591  ++changeCount_;
592  }
593 
597  size_t
598  DEPRECATED_IN_NDN_CPP getComponentCount() const { return size(); }
599 
603  const Component&
604  DEPRECATED_IN_NDN_CPP getComponent(size_t i) const { return get(i); }
605 
614  Name
615  getSubName(int iStartComponent, size_t nComponents) const;
616 
624  Name
625  getSubName(int iStartComponent) const;
626 
633  Name
634  getPrefix(int nComponents) const
635  {
636  if (nComponents < 0)
637  return getSubName(0, components_.size() + nComponents);
638  else
639  return getSubName(0, nComponents);
640  }
641 
650  std::string
651  toUri(bool includeScheme = false) const;
652 
656  std::string
657  DEPRECATED_IN_NDN_CPP to_uri() const
658  {
659  return toUri();
660  }
661 
669  Name&
670  appendSegment(uint64_t segment)
671  {
672  return append(Component::fromNumberWithMarker(segment, 0x00));
673  }
674 
682  Name&
683  appendSegmentOffset(uint64_t segmentOffset)
684  {
685  return append(Component::fromNumberWithMarker(segmentOffset, 0xFB));
686  }
687 
691  Name&
692  DEPRECATED_IN_NDN_CPP appendFinalSegment(uint64_t segment)
693  {
696  }
697 
706  Name&
707  appendVersion(uint64_t version)
708  {
709  return append(Component::fromNumberWithMarker(version, 0xFD));
710  }
711 
720  Name&
721  appendTimestamp(uint64_t timestamp)
722  {
723  return append(Component::fromNumberWithMarker(timestamp, 0xFC));
724  }
725 
733  Name&
734  appendSequenceNumber(uint64_t sequenceNumber)
735  {
736  return append(Component::fromNumberWithMarker(sequenceNumber, 0xFE));
737  }
738 
744  bool
745  equals(const Name& name) const;
746 
752  bool
753  match(const Name& name) const;
754 
764  static Blob
765  fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset);
766 
774  static Blob
775  fromEscapedString(const char *escapedString);
776 
784  static Blob
785  fromEscapedString(const std::string& escapedString) { return fromEscapedString(escapedString.c_str()); }
786 
793  static void
794  toEscapedString(const std::vector<uint8_t>& value, std::ostringstream& result);
795 
802  static std::string
803  toEscapedString(const std::vector<uint8_t>& value);
804 
805  //
806  // vector equivalent interface.
807  //
808 
813  size_t
814  size() const { return components_.size(); }
815 
822  Blob
824  {
825  return wireFormat.encodeName(*this);
826  }
827 
835  void
836  wireDecode
837  (const uint8_t *input, size_t inputLength,
839  {
840  wireFormat.decodeName(*this, input, inputLength);
841  }
842 
849  void
850  wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
851  {
852  wireDecode(&input[0], input.size(), wireFormat);
853  }
854 
861  void
862  wireDecode
863  (const Blob& input,
865  {
866  wireDecode(input.buf(), input.size(), wireFormat);
867  }
868 
876  const Component&
877  get(int i) const;
878 
883  uint64_t
884  getChangeCount() const { return changeCount_; }
885 
900  int
901  compare(const Name& other) const;
902 
903  const Component&
904  operator [] (int i) const
905  {
906  return get(i);
907  }
908 
913  template<class T> void
914  push_back(const T &component)
915  {
916  append(component);
917  }
918 
924  bool
925  operator == (const Name &name) const { return equals(name); }
926 
932  bool
933  operator != (const Name &name) const { return !equals(name); }
934 
941  bool
942  operator <= (const Name& other) const { return compare(other) <= 0; }
943 
950  bool
951  operator < (const Name& other) const { return compare(other) < 0; }
952 
959  bool
960  operator >= (const Name& other) const { return compare(other) >= 0; }
961 
968  bool
969  operator > (const Name& other) const { return compare(other) > 0; }
970 
974  static bool
975  DEPRECATED_IN_NDN_CPP breadthFirstLess(const Name& name1, const Name& name2) { return name1 < name2; }
976 
981  bool operator() (const Name& name1, const Name& name2) const { return name1 < name2; }
982  };
983 
984  //
985  // Iterator interface to name components.
986  //
987  typedef std::vector<Component>::const_iterator const_iterator;
988  typedef std::vector<Component>::const_reverse_iterator const_reverse_iterator;
989 
990  typedef Component partial_type;
991 
995  const_iterator
996  begin() const { return components_.begin(); }
997 
1001  const_iterator
1002  end() const { return components_.end(); }
1003 
1007  const_reverse_iterator
1008  rbegin() const { return components_.rbegin(); }
1009 
1013  const_reverse_iterator
1014  rend() const { return components_.rend(); }
1015 
1016 private:
1017  std::vector<Component> components_;
1018  uint64_t changeCount_;
1019 };
1020 
1021 inline std::ostream&
1022 operator << (std::ostream& os, const Name& name)
1023 {
1024  os << name.toUri();
1025  return os;
1026 }
1027 
1028 }
1029 
1030 #endif
1031 
static Component fromNumberWithPrefix(uint64_t number, const uint8_t *prefix, size_t prefixLength)
Create a component whose value is the prefix appended with the network-ordered encoding of the number...
Definition: name.cpp:189
Name &DEPRECATED_IN_NDN_CPP appendComponent(const Blob &value)
Definition: name.hpp:553
Name & append(const std::string &value)
Append a new component, copying the bytes from the value string.
Definition: name.hpp:518
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
bool operator<=(const Component &other) const
Return true if this is less than or equal to the other Component in the NDN canonical ordering...
Definition: name.hpp:354
bool match(const Name &name) const
Check if the N components of this name are the same as the first N components of the given name...
Definition: name.cpp:385
Name & append(const std::vector< uint8_t > &value)
Append a new component, copying from value.
Definition: name.hpp:476
void set(const NameLite &nameLite)
Clear this name, and set the components by copying from nameLite.
Definition: name.cpp:303
uint64_t toTimestamp() const
Interpret this name component as a timestamp according to NDN naming conventions for "Timestamp" (mar...
Definition: name.hpp:249
Name & appendSegmentOffset(uint64_t segmentOffset)
Append a component with the encoded segment byte offset according to NDN naming conventions for segme...
Definition: name.hpp:683
Name &DEPRECATED_IN_NDN_CPP appendComponent(const std::vector< uint8_t > &value)
Definition: name.hpp:544
std::string toUri(bool includeScheme=false) const
Encode this name as a URI.
Definition: name.cpp:324
void set(const std::string &uri)
Parse the uri according to the NDN URI Scheme and set the name with the components.
Definition: name.hpp:459
void push_back(const T &component)
Append the component.
Definition: name.hpp:914
A NameLite holds an array of NameLite::Component.
Definition: name-lite.hpp:34
uint64_t toSequenceNumber() const
Interpret this name component as a sequence number according to NDN naming conventions for "Sequencin...
Definition: name.hpp:262
const Component &DEPRECATED_IN_NDN_CPP getComponent(size_t i) const
Definition: name.hpp:604
Name & append(const uint8_t *value, size_t valueLength)
Append a new component, copying from value of length valueLength.
Definition: name.hpp:466
Name getSubName(int iStartComponent, size_t nComponents) const
Get a new name, constructed as a subset of components.
Definition: name.cpp:341
void wireDecode(const std::vector< uint8_t > &input, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Decode the input using a particular wire format and update this Name.
Definition: name.hpp:850
const_reverse_iterator rbegin() const
Reverse begin iterator (const).
Definition: name.hpp:1008
static size_t getFinalSegmentPrefixLength()
Definition: name.hpp:307
bool operator==(const Name &name) const
Check if this name has the same component count and components as the given name. ...
Definition: name.hpp:925
A Name::Component holds a read-only name component value.
Definition: name.hpp:45
int compare(const Name &other) const
Compare this to the other Name using NDN canonical ordering.
Definition: name.cpp:496
int compare(const Component &other) const
Compare this to the other Component using NDN canonical ordering.
Definition: name.cpp:220
bool operator<(const Component &other) const
Return true if this is less than the other Component in the NDN canonical ordering.
Definition: name.hpp:363
Component(const std::vector< uint8_t > &value)
Create a new Name::Component, copying the given value.
Definition: name.hpp:59
Name & appendSequenceNumber(uint64_t sequenceNumber)
Append a component with the encoded sequence number according to NDN naming conventions for "Sequenci...
Definition: name.hpp:734
bool operator>=(const Name &other) const
Return true if this is less than or equal to the other Name in the NDN canonical ordering.
Definition: name.hpp:960
Component(const char *value)
Create a new Name::Component, copying the bytes from the value string.
Definition: name.hpp:81
bool operator>=(const Component &other) const
Return true if this is less than or equal to the other Component in the NDN canonical ordering...
Definition: name.hpp:372
uint64_t DEPRECATED_IN_NDN_CPP toFinalSegment() const
Definition: name.hpp:221
Name & appendVersion(uint64_t version)
Append a component with the encoded version number according to NDN naming conventions for "Versionin...
Definition: name.hpp:707
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
static Component fromNumberWithMarker(uint64_t number, uint8_t marker)
Create a component whose value is the marker appended with the nonNegativeInteger encoding of the num...
Definition: name.cpp:179
uint64_t toNumber() const
Interpret this name component as a network-ordered number and return an integer.
Definition: name.cpp:212
size_t size() const
Get the number of components.
Definition: name.hpp:814
bool DEPRECATED_IN_NDN_CPP isFinalSegment() const
Definition: name.hpp:215
static Blob fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset)
Make a Blob value by decoding the escapedString between beginOffset and endOffset according to the ND...
Definition: name.cpp:404
A Blob holds a pointer to an immutable byte array implemented as const std::vector.
Definition: blob.hpp:42
bool operator!=(const Name &name) const
Check if this name has the same component count and components as the given name. ...
Definition: name.hpp:933
const uint8_t * buf() const
Return a const pointer to the first byte of the immutable byte array, or 0 if the pointer is null...
Definition: blob.hpp:152
static bool DEPRECATED_IN_NDN_CPP breadthFirstLess(const Name &name1, const Name &name2)
Definition: name.hpp:975
Name &DEPRECATED_IN_NDN_CPP appendComponent(const uint8_t *value, size_t valueLength)
Definition: name.hpp:535
std::string toEscapedString() const
Convert this component value by escaping characters according to the NDN URI Scheme.
Definition: name.hpp:136
uint64_t toSegment() const
Interpret this name component as a segment number according to NDN naming conventions for "Segment nu...
Definition: name.hpp:184
bool operator>(const Component &other) const
Return true if this is greater than the other Component in the NDN canonical ordering.
Definition: name.hpp:381
uint64_t toNumberWithPrefix(const uint8_t *prefix, size_t prefixLength) const
Interpret this name component as a network-ordered number with a prefix and return an integer...
Definition: name.cpp:149
size_t size() const
Return the length of the immutable byte array.
Definition: blob.hpp:140
bool hasPrefix(const uint8_t *prefix, size_t prefixLength) const
Check if this name component begins with the given prefix.
Definition: name.cpp:163
A NameLite::Component holds a pointer to the component value.
Definition: name-lite.hpp:39
uint64_t DEPRECATED_IN_NDN_CPP toSeqNum() const
Definition: name.hpp:206
bool equals(const Component &other) const
Check if this is the same component as other.
Definition: name.hpp:315
uint64_t toNumberWithMarker(uint8_t marker) const
Interpret this name component as a network-ordered number with a marker and return an integer...
Definition: name.cpp:135
bool operator!=(const Component &other) const
Check if this is not the same component as other.
Definition: name.hpp:334
uint64_t toSegmentOffset() const
Interpret this name component as a segment byte offset according to NDN naming conventions for segmen...
Definition: name.hpp:197
static void toEscapedString(const std::vector< uint8_t > &value, std::ostringstream &result)
Write the value to result, escaping characters according to the NDN URI Scheme.
uint64_t getChangeCount() const
Get the change count, which is incremented each time this object is changed.
Definition: name.hpp:884
Component(const uint8_t *value, size_t valueLen)
Create a new Name::Component, copying the given value.
Definition: name.hpp:69
void toEscapedString(std::ostringstream &result) const
Write this component value to result, escaping characters according to the NDN URI Scheme...
Definition: name.hpp:125
Name &DEPRECATED_IN_NDN_CPP addComponent(const Blob &value)
Definition: name.hpp:580
Name & appendTimestamp(uint64_t timestamp)
Append a component with the encoded timestamp according to NDN naming conventions for "Timestamp" (ma...
Definition: name.hpp:721
const_iterator end() const
End iterator (const).
Definition: name.hpp:1002
static const uint8_t * getFinalSegmentPrefix()
Definition: name.hpp:301
Name(const char *uri)
Parse the uri according to the NDN URI Scheme and create the name with the components.
Definition: name.hpp:414
std::string DEPRECATED_IN_NDN_CPP to_uri() const
Definition: name.hpp:657
bool operator<=(const Name &other) const
Return true if this is less than or equal to the other Name in the NDN canonical ordering.
Definition: name.hpp:942
size_t DEPRECATED_IN_NDN_CPP getComponentCount() const
Definition: name.hpp:598
Component()
Create a new Name::Component with a zero-length value.
Definition: name.hpp:50
Name()
Create a new Name with no components.
Definition: name.hpp:396
bool operator==(const Component &other) const
Check if this is the same component as other.
Definition: name.hpp:326
static WireFormat * getDefaultWireFormat()
Return the default WireFormat used by default encoding and decoding methods which was set with setDef...
Definition: wire-format.cpp:34
Name & append(const char *value)
Append a new component, copying the bytes from the value string.
Definition: name.hpp:504
void wireDecode(const uint8_t *input, size_t inputLength, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Decode the input using a particular wire format and update this Name.
Definition: name.hpp:837
Name(const std::string &uri)
Parse the uri according to the NDN URI Scheme and create the name with the components.
Definition: name.hpp:424
Name &DEPRECATED_IN_NDN_CPP addComponent(const std::vector< uint8_t > &value)
Definition: name.hpp:571
Component(const std::string &value)
Create a new Name::Component, copying the bytes from the value string.
Definition: name.hpp:93
Component(const Blob &value)
Create a new Name::Component, taking another pointer to the Blob value.
Definition: name.hpp:102
Definition: wire-format.hpp:36
Name &DEPRECATED_IN_NDN_CPP addComponent(const uint8_t *value, size_t valueLength)
Definition: name.hpp:562
Definition: name.hpp:980
Name getPrefix(int nComponents) const
Return a new Name with the first nComponents components of this Name.
Definition: name.hpp:634
uint64_t toVersion() const
Interpret this name component as a version number according to NDN naming conventions for "Versioning...
Definition: name.hpp:235
const_reverse_iterator rend() const
Reverse end iterator (const).
Definition: name.hpp:1014
const_iterator begin() const
Begin iterator (const).
Definition: name.hpp:996
bool operator>(const Name &other) const
Return true if this is greater than the other Name in the NDN canonical ordering. ...
Definition: name.hpp:969
void clear()
Clear all the components.
Definition: name.hpp:589
Name &DEPRECATED_IN_NDN_CPP appendFinalSegment(uint64_t segment)
Definition: name.hpp:692
Name(const std::vector< Component > &components)
Create a new Name, copying the name components.
Definition: name.hpp:405
static Blob fromEscapedString(const std::string &escapedString)
Make a Blob value by decoding the escapedString according to the NDN URI Scheme.
Definition: name.hpp:785
bool operator<(const Name &other) const
Return true if this is less than the other Name in the NDN canonical ordering.
Definition: name.hpp:951
Blob wireEncode(WireFormat &wireFormat=*WireFormat::getDefaultWireFormat()) const
Encode this Name for a particular wire format.
Definition: name.hpp:823
bool equals(const Name &name) const
Check if this name has the same component count and components as the given name. ...
Definition: name.cpp:370
Name & appendSegment(uint64_t segment)
Append a component with the encoded segment number according to NDN naming conventions for "Segment n...
Definition: name.hpp:670
static Component fromNumber(uint64_t number)
Create a component whose value is the nonNegativeInteger encoding of the number.
Definition: name.cpp:171