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 
34 struct ndn_NameComponent;
35 struct ndn_Name;
36 
37 namespace ndn {
38 
42 class Name {
43 public:
47  class Component {
48  public:
53  : value_((const uint8_t*)0, 0)
54  {
55  }
56 
61  Component(const std::vector<uint8_t>& value)
62  : value_(value)
63  {
64  }
65 
71  Component(const uint8_t *value, size_t valueLen)
72  : value_(value, valueLen)
73  {
74  }
75 
83  Component(const char* value)
84  : value_((const uint8_t*)value, ::strlen(value))
85  {
86  }
87 
95  Component(const std::string& value)
96  : value_((const uint8_t*)&value[0], value.size())
97  {
98  }
99 
104  Component(const Blob &value)
105  : value_(value)
106  {
107  }
108 
114  void
115  get(struct ndn_NameComponent& componentStruct) const;
116 
117  const Blob&
118  getValue() const { return value_; }
119 
125  void
126  toEscapedString(std::ostringstream& result) const
127  {
128  Name::toEscapedString(*value_, result);
129  }
130 
136  std::string
138  {
139  return Name::toEscapedString(*value_);
140  }
141 
146  uint64_t
147  toNumber() const;
148 
155  uint64_t
156  toNumberWithMarker(uint8_t marker) const;
157 
165  uint64_t
166  toNumberWithPrefix(const uint8_t* prefix, size_t prefixLength) const;
167 
174  bool
175  hasPrefix(const uint8_t* prefix, size_t prefixLength) const;
176 
184  uint64_t
185  toSegment() const
186  {
187  return toNumberWithMarker(0x00);
188  }
189 
197  uint64_t
199  {
200  return toNumberWithMarker(0xFB);
201  }
202 
206  uint64_t
207  DEPRECATED_IN_NDN_CPP toSeqNum() const
208  {
209  return toSegment();
210  }
211 
215  bool
216  DEPRECATED_IN_NDN_CPP isFinalSegment() const { return hasPrefix(getFinalSegmentPrefix(), getFinalSegmentPrefixLength()); }
217 
221  uint64_t
222  DEPRECATED_IN_NDN_CPP toFinalSegment() const
223  {
225  }
226 
235  uint64_t
236  toVersion() const
237  {
238  return toNumberWithMarker(0xFD);
239  }
240 
249  uint64_t
250  toTimestamp() const
251  {
252  return toNumberWithMarker(0xFC);
253  }
254 
262  uint64_t
264  {
265  return toNumberWithMarker(0xFE);
266  }
267 
274  static Component
275  fromNumber(uint64_t number);
276 
284  static Component
285  fromNumberWithMarker(uint64_t number, uint8_t marker);
286 
295  static Component
296  fromNumberWithPrefix(uint64_t number, const uint8_t* prefix, size_t prefixLength);
297 
301  static const uint8_t*
302  getFinalSegmentPrefix() { return FINAL_SEGMENT_PREFIX; }
303 
307  static size_t
308  getFinalSegmentPrefixLength() { return FINAL_SEGMENT_PREFIX_LENGTH; }
309 
315  bool
316  equals(const Component& other) const
317  {
318  return *value_ == *other.value_;
319  }
320 
326  bool
327  operator == (const Component& other) const { return equals(other); }
328 
334  bool
335  operator != (const Component& other) const { return !equals(other); }
336 
345  int
346  compare(const Component& other) const;
347 
354  bool
355  operator <= (const Component& other) const { return compare(other) <= 0; }
356 
363  bool
364  operator < (const Component& other) const { return compare(other) < 0; }
365 
372  bool
373  operator >= (const Component& other) const { return compare(other) >= 0; }
374 
381  bool
382  operator > (const Component& other) const { return compare(other) > 0; }
383 
384  private:
388  static const uint8_t FINAL_SEGMENT_PREFIX[];
389  static size_t FINAL_SEGMENT_PREFIX_LENGTH;
390 
391  Blob value_;
392  };
393 
398  : changeCount_(0)
399  {
400  }
401 
406  Name(const std::vector<Component>& components)
407  : components_(components), changeCount_(0)
408  {
409  }
410 
415  Name(const char* uri)
416  : changeCount_(0)
417  {
418  set(uri);
419  }
420 
425  Name(const std::string& uri)
426  : changeCount_(0)
427  {
428  set(uri.c_str());
429  }
430 
436  void
437  get(struct ndn_Name& nameStruct) const;
438 
443  void
444  set(const struct ndn_Name& nameStruct);
445 
450  void
451  set(const char *uri);
452 
457  void
458  set(const std::string& uri) { set(uri.c_str()); }
459 
464  Name&
465  append(const uint8_t *value, size_t valueLength)
466  {
467  return append(Component(value, valueLength));
468  }
469 
474  Name&
475  append(const std::vector<uint8_t>& value)
476  {
477  return append(Component(value));
478  }
479 
480  Name&
481  append(const Blob &value)
482  {
483  return append(Component(value));
484  }
485 
486  Name&
487  append(const Component &value)
488  {
489  components_.push_back(value);
490  ++changeCount_;
491  return *this;
492  }
493 
502  Name&
503  append(const char* value)
504  {
505  return append(Component(value));
506  }
507 
516  Name&
517  append(const std::string& value)
518  {
519  return append(Component(value));
520  }
521 
527  Name&
528  append(const Name& name);
529 
533  Name&
534  DEPRECATED_IN_NDN_CPP appendComponent(const uint8_t *value, size_t valueLength)
535  {
536  return append(value, valueLength);
537  }
538 
542  Name&
543  DEPRECATED_IN_NDN_CPP appendComponent(const std::vector<uint8_t>& value)
544  {
545  return append(value);
546  }
547 
551  Name&
552  DEPRECATED_IN_NDN_CPP appendComponent(const Blob &value)
553  {
554  return append(value);
555  }
556 
560  Name&
561  DEPRECATED_IN_NDN_CPP addComponent(const uint8_t *value, size_t valueLength)
562  {
563  return append(value, valueLength);
564  }
565 
569  Name&
570  DEPRECATED_IN_NDN_CPP addComponent(const std::vector<uint8_t>& value)
571  {
572  return append(value);
573  }
574 
578  Name&
579  DEPRECATED_IN_NDN_CPP addComponent(const Blob &value)
580  {
581  return append(value);
582  }
583 
587  void
588  clear() {
589  components_.clear();
590  ++changeCount_;
591  }
592 
596  size_t
597  DEPRECATED_IN_NDN_CPP getComponentCount() const { return size(); }
598 
602  const Component&
603  DEPRECATED_IN_NDN_CPP getComponent(size_t i) const { return get(i); }
604 
613  Name
614  getSubName(int iStartComponent, size_t nComponents) const;
615 
623  Name
624  getSubName(int iStartComponent) const;
625 
632  Name
634  {
635  if (nComponents < 0)
636  return getSubName(0, components_.size() + nComponents);
637  else
638  return getSubName(0, nComponents);
639  }
640 
649  std::string
650  toUri(bool includeScheme = false) const;
651 
655  std::string
656  DEPRECATED_IN_NDN_CPP to_uri() const
657  {
658  return toUri();
659  }
660 
668  Name&
669  appendSegment(uint64_t segment)
670  {
671  return append(Component::fromNumberWithMarker(segment, 0x00));
672  }
673 
681  Name&
682  appendSegmentOffset(uint64_t segmentOffset)
683  {
684  return append(Component::fromNumberWithMarker(segmentOffset, 0xFB));
685  }
686 
690  Name&
691  DEPRECATED_IN_NDN_CPP appendFinalSegment(uint64_t segment)
692  {
695  }
696 
705  Name&
706  appendVersion(uint64_t version)
707  {
708  return append(Component::fromNumberWithMarker(version, 0xFD));
709  }
710 
719  Name&
720  appendTimestamp(uint64_t timestamp)
721  {
722  return append(Component::fromNumberWithMarker(timestamp, 0xFC));
723  }
724 
732  Name&
733  appendSequenceNumber(uint64_t sequenceNumber)
734  {
735  return append(Component::fromNumberWithMarker(sequenceNumber, 0xFE));
736  }
737 
743  bool
744  equals(const Name& name) const;
745 
751  bool
752  match(const Name& name) const;
753 
763  static Blob
764  fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset);
765 
773  static Blob
774  fromEscapedString(const char *escapedString);
775 
783  static Blob
784  fromEscapedString(const std::string& escapedString) { return fromEscapedString(escapedString.c_str()); }
785 
792  static void
793  toEscapedString(const std::vector<uint8_t>& value, std::ostringstream& result);
794 
801  static std::string
802  toEscapedString(const std::vector<uint8_t>& value);
803 
804  //
805  // vector equivalent interface.
806  //
807 
812  size_t
813  size() const { return components_.size(); }
814 
821  Blob
823  {
824  return wireFormat.encodeName(*this);
825  }
826 
834  void
835  wireDecode
836  (const uint8_t *input, size_t inputLength,
838  {
839  wireFormat.decodeName(*this, input, inputLength);
840  }
841 
848  void
849  wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
850  {
851  wireDecode(&input[0], input.size(), wireFormat);
852  }
853 
860  void
861  wireDecode
862  (const Blob& input,
864  {
865  wireDecode(input.buf(), input.size(), wireFormat);
866  }
867 
875  const Component&
876  get(int i) const;
877 
882  uint64_t
883  getChangeCount() const { return changeCount_; }
884 
899  int
900  compare(const Name& other) const;
901 
902  const Component&
903  operator [] (int i) const
904  {
905  return get(i);
906  }
907 
912  template<class T> void
913  push_back(const T &component)
914  {
915  append(component);
916  }
917 
923  bool
924  operator == (const Name &name) const { return equals(name); }
925 
931  bool
932  operator != (const Name &name) const { return !equals(name); }
933 
940  bool
941  operator <= (const Name& other) const { return compare(other) <= 0; }
942 
949  bool
950  operator < (const Name& other) const { return compare(other) < 0; }
951 
958  bool
959  operator >= (const Name& other) const { return compare(other) >= 0; }
960 
967  bool
968  operator > (const Name& other) const { return compare(other) > 0; }
969 
973  static bool
974  DEPRECATED_IN_NDN_CPP breadthFirstLess(const Name& name1, const Name& name2) { return name1 < name2; }
975 
980  bool operator() (const Name& name1, const Name& name2) const { return name1 < name2; }
981  };
982 
983  //
984  // Iterator interface to name components.
985  //
986  typedef std::vector<Component>::const_iterator const_iterator;
987  typedef std::vector<Component>::const_reverse_iterator const_reverse_iterator;
988 
989  typedef Component partial_type;
990 
994  const_iterator
995  begin() const { return components_.begin(); }
996 
1000  const_iterator
1001  end() const { return components_.end(); }
1002 
1006  const_reverse_iterator
1007  rbegin() const { return components_.rbegin(); }
1008 
1012  const_reverse_iterator
1013  rend() const { return components_.rend(); }
1014 
1015 private:
1016  std::vector<Component> components_;
1017  uint64_t changeCount_;
1018 };
1019 
1020 inline std::ostream&
1021 operator << (std::ostream& os, const Name& name)
1022 {
1023  os << name.toUri();
1024  return os;
1025 }
1026 
1027 }
1028 
1029 #endif
1030 
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:190
Name &DEPRECATED_IN_NDN_CPP appendComponent(const Blob &value)
Definition: name.hpp:552
Name & append(const std::string &value)
Append a new component, copying the bytes from the value string.
Definition: name.hpp:517
Copyright (C) 2013-2015 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:355
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:475
uint64_t toTimestamp() const
Interpret this name component as a timestamp according to NDN naming conventions for "Timestamp" (mar...
Definition: name.hpp:250
Name & appendSegmentOffset(uint64_t segmentOffset)
Append a component with the encoded segment byte offset according to NDN naming conventions for segme...
Definition: name.hpp:682
Name &DEPRECATED_IN_NDN_CPP appendComponent(const std::vector< uint8_t > &value)
Definition: name.hpp:543
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:458
void push_back(const T &component)
Append the component.
Definition: name.hpp:913
uint64_t toSequenceNumber() const
Interpret this name component as a sequence number according to NDN naming conventions for "Sequencin...
Definition: name.hpp:263
const Component &DEPRECATED_IN_NDN_CPP getComponent(size_t i) const
Definition: name.hpp:603
Name & append(const uint8_t *value, size_t valueLength)
Append a new component, copying from value of length valueLength.
Definition: name.hpp:465
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:849
const_reverse_iterator rbegin() const
Reverse begin iterator (const).
Definition: name.hpp:1007
static size_t getFinalSegmentPrefixLength()
Definition: name.hpp:308
bool operator==(const Name &name) const
Check if this name has the same component count and components as the given name. ...
Definition: name.hpp:924
A Name::Component holds a read-only name component value.
Definition: name.hpp:47
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:221
bool operator<(const Component &other) const
Return true if this is less than the other Component in the NDN canonical ordering.
Definition: name.hpp:364
Component(const std::vector< uint8_t > &value)
Create a new Name::Component, copying the given value.
Definition: name.hpp:61
Name & appendSequenceNumber(uint64_t sequenceNumber)
Append a component with the encoded sequence number according to NDN naming conventions for "Sequenci...
Definition: name.hpp:733
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:959
Component(const char *value)
Create a new Name::Component, copying the bytes from the value string.
Definition: name.hpp:83
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:373
uint64_t DEPRECATED_IN_NDN_CPP toFinalSegment() const
Definition: name.hpp:222
Name & appendVersion(uint64_t version)
Append a component with the encoded version number according to NDN naming conventions for "Versionin...
Definition: name.hpp:706
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:42
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:180
uint64_t toNumber() const
Interpret this name component as a network-ordered number and return an integer.
Definition: name.cpp:213
size_t size() const
Get the number of components.
Definition: name.hpp:813
bool DEPRECATED_IN_NDN_CPP isFinalSegment() const
Definition: name.hpp:216
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
size_t nComponents
the number of components in the name
Definition: name-types.h:43
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:932
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:138
static bool DEPRECATED_IN_NDN_CPP breadthFirstLess(const Name &name1, const Name &name2)
Definition: name.hpp:974
Copyright (C) 2015 Regents of the University of California.
Definition: name-types.h:33
Name &DEPRECATED_IN_NDN_CPP appendComponent(const uint8_t *value, size_t valueLength)
Definition: name.hpp:534
std::string toEscapedString() const
Convert this component value by escaping characters according to the NDN URI Scheme.
Definition: name.hpp:137
uint64_t toSegment() const
Interpret this name component as a segment number according to NDN naming conventions for "Segment nu...
Definition: name.hpp:185
bool operator>(const Component &other) const
Return true if this is greater than the other Component in the NDN canonical ordering.
Definition: name.hpp:382
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:150
size_t size() const
Return the length of the immutable byte array.
Definition: blob.hpp:126
bool hasPrefix(const uint8_t *prefix, size_t prefixLength) const
Check if this name component begins with the given prefix.
Definition: name.cpp:164
An ndn_Name holds an array of ndn_NameComponent.
Definition: name-types.h:40
uint64_t DEPRECATED_IN_NDN_CPP toSeqNum() const
Definition: name.hpp:207
bool equals(const Component &other) const
Check if this is the same component as other.
Definition: name.hpp:316
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:136
bool operator!=(const Component &other) const
Check if this is not the same component as other.
Definition: name.hpp:335
uint64_t toSegmentOffset() const
Interpret this name component as a segment byte offset according to NDN naming conventions for segmen...
Definition: name.hpp:198
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:883
Component(const uint8_t *value, size_t valueLen)
Create a new Name::Component, copying the given value.
Definition: name.hpp:71
void toEscapedString(std::ostringstream &result) const
Write this component value to result, escaping characters according to the NDN URI Scheme...
Definition: name.hpp:126
Name &DEPRECATED_IN_NDN_CPP addComponent(const Blob &value)
Definition: name.hpp:579
Name & appendTimestamp(uint64_t timestamp)
Append a component with the encoded timestamp according to NDN naming conventions for "Timestamp" (ma...
Definition: name.hpp:720
const_iterator end() const
End iterator (const).
Definition: name.hpp:1001
static const uint8_t * getFinalSegmentPrefix()
Definition: name.hpp:302
Name(const char *uri)
Parse the uri according to the NDN URI Scheme and create the name with the components.
Definition: name.hpp:415
std::string DEPRECATED_IN_NDN_CPP to_uri() const
Definition: name.hpp:656
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:941
size_t DEPRECATED_IN_NDN_CPP getComponentCount() const
Definition: name.hpp:597
Component()
Create a new Name::Component with a zero-length value.
Definition: name.hpp:52
Name()
Create a new Name with no components.
Definition: name.hpp:397
bool operator==(const Component &other) const
Check if this is the same component as other.
Definition: name.hpp:327
static WireFormat * getDefaultWireFormat()
Return the default WireFormat used by default encoding and decoding methods which was set with setDef...
Definition: wire-format.cpp:36
Name & append(const char *value)
Append a new component, copying the bytes from the value string.
Definition: name.hpp:503
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:836
Name(const std::string &uri)
Parse the uri according to the NDN URI Scheme and create the name with the components.
Definition: name.hpp:425
Name &DEPRECATED_IN_NDN_CPP addComponent(const std::vector< uint8_t > &value)
Definition: name.hpp:570
Component(const std::string &value)
Create a new Name::Component, copying the bytes from the value string.
Definition: name.hpp:95
Component(const Blob &value)
Create a new Name::Component, taking another pointer to the Blob value.
Definition: name.hpp:104
Definition: wire-format.hpp:37
Name &DEPRECATED_IN_NDN_CPP addComponent(const uint8_t *value, size_t valueLength)
Definition: name.hpp:561
Definition: name.hpp:979
Name getPrefix(int nComponents) const
Return a new Name with the first nComponents components of this Name.
Definition: name.hpp:633
uint64_t toVersion() const
Interpret this name component as a version number according to NDN naming conventions for "Versioning...
Definition: name.hpp:236
const_reverse_iterator rend() const
Reverse end iterator (const).
Definition: name.hpp:1013
const_iterator begin() const
Begin iterator (const).
Definition: name.hpp:995
bool operator>(const Name &other) const
Return true if this is greater than the other Name in the NDN canonical ordering. ...
Definition: name.hpp:968
void clear()
Clear all the components.
Definition: name.hpp:588
Name &DEPRECATED_IN_NDN_CPP appendFinalSegment(uint64_t segment)
Definition: name.hpp:691
void set(const struct ndn_Name &nameStruct)
Clear this name, and set the components by copying from the name struct.
Definition: name.cpp:303
Name(const std::vector< Component > &components)
Create a new Name, copying the name components.
Definition: name.hpp:406
static Blob fromEscapedString(const std::string &escapedString)
Make a Blob value by decoding the escapedString according to the NDN URI Scheme.
Definition: name.hpp:784
bool operator<(const Name &other) const
Return true if this is less than the other Name in the NDN canonical ordering.
Definition: name.hpp:950
Blob wireEncode(WireFormat &wireFormat=*WireFormat::getDefaultWireFormat()) const
Encode this Name for a particular wire format.
Definition: name.hpp:822
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:669
static Component fromNumber(uint64_t number)
Create a component whose value is the nonNegativeInteger encoding of the number.
Definition: name.cpp:172