All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
name-lite.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_NAME_LITE_HPP
23 #define NDN_NAME_LITE_HPP
24 
25 #include "../c/errors.h"
26 #include "util/blob-lite.hpp"
27 #include "../c/name-types.h"
28 
29 namespace ndn {
30 
31 class ExcludeLite;
32 
36 class NameLite : public ndn_Name {
37 public:
41  class Component : private ndn_NameComponent {
42  public:
46  Component();
47 
61  Component
62  (const uint8_t* value, size_t valueLength,
63  ndn_NameComponentType type = ndn_NameComponentType_GENERIC,
64  int otherTypeCode = -1);
65 
79  Component
80  (const BlobLite& value,
81  ndn_NameComponentType type = ndn_NameComponentType_GENERIC,
82  int otherTypeCode = -1);
83 
88  const BlobLite&
89  getValue() const { return BlobLite::downCast(value); }
90 
97  ndn_NameComponentType
98  getType() const { return type; }
99 
106  int
107  getOtherTypeCode() const { return otherTypeCode; }
108 
115  bool
116  isSegment() const;
117 
124  bool
125  isSegmentOffset() const;
126 
133  bool
134  isVersion() const;
135 
142  bool
143  isTimestamp() const;
144 
151  bool
152  isSequenceNumber() const;
153 
158  bool
159  isGeneric() const;
160 
165  bool
166  isImplicitSha256Digest() const;
167 
172  bool
173  isParametersSha256Digest() const;
174 
179  uint64_t
180  toNumber() const;
181 
190  ndn_Error
191  toNumberWithMarker(uint8_t marker, uint64_t& result) const;
192 
202  ndn_Error
204  (const uint8_t* prefix, size_t prefixLength, uint64_t& result) const;
205 
212  bool
213  hasPrefix(const uint8_t* prefix, size_t prefixLength) const;
214 
223  ndn_Error
224  toSegment(uint64_t& result) const;
225 
234  ndn_Error
235  toSegmentOffset(uint64_t& result) const;
236 
246  ndn_Error
247  toVersion(uint64_t& result) const;
248 
258  ndn_Error
259  toTimestamp(uint64_t& result) const;
260 
269  ndn_Error
270  toSequenceNumber(uint64_t& result) const;
271 
277  bool
278  equals(const Component& other) const;
279 
287  int
288  compare(const Component& other) const;
289 
311  ndn_Error
313  (uint64_t number, uint8_t* buffer, size_t bufferLength,
314  ndn_NameComponentType type = ndn_NameComponentType_GENERIC,
315  int otherTypeCode = -1);
316 
331  ndn_Error
333  (uint64_t number, uint8_t marker, uint8_t* buffer, size_t bufferLength);
334 
348  ndn_Error
349  setSegment(uint64_t segment, uint8_t* buffer, size_t bufferLength);
350 
364  ndn_Error
366  (uint64_t segmentOffset, uint8_t* buffer, size_t bufferLength);
367 
383  ndn_Error
384  setVersion(uint64_t version, uint8_t* buffer, size_t bufferLength);
385 
400  ndn_Error
401  setTimestamp(uint64_t timestamp, uint8_t* buffer, size_t bufferLength);
402 
416  ndn_Error
418  (uint64_t sequenceNumber, uint8_t* buffer, size_t bufferLength);
419 
429  ndn_Error
430  setImplicitSha256Digest(const uint8_t* digest, size_t digestLength);
431 
440  ndn_Error
442  {
443  return setImplicitSha256Digest(digest.buf(), digest.size());
444  }
445 
455  ndn_Error
456  setParametersSha256Digest(const uint8_t* digest, size_t digestLength);
457 
467  ndn_Error
469  {
470  return setParametersSha256Digest(digest.buf(), digest.size());
471  }
472 
478  static Component&
479  downCast(ndn_NameComponent& component) { return *(Component*)&component; }
480 
481  static const Component&
482  downCast(const ndn_NameComponent& component) { return *(Component*)&component; }
483 
484  private:
485  friend class NameLite;
486  friend class ExcludeLite;
487  };
488 
498 
505  const Component&
506  get(int i) const
507  {
508  // TODO: Range check.
509  if (i >= 0)
510  return Component::downCast(components[i]);
511  else
512  // Negative index.
514  }
515 
520  size_t
521  size() const { return nComponents; }
522 
530  bool
531  equals(const NameLite& name);
532 
540  bool
541  match(const NameLite& name) const;
542 
546  void
547  clear();
548 
565  ndn_Error
566  append
567  (const uint8_t* value, size_t valueLength,
568  ndn_NameComponentType type = ndn_NameComponentType_GENERIC,
569  int otherTypeCode = -1);
570 
586  ndn_Error
587  append
588  (const BlobLite& value,
589  ndn_NameComponentType type = ndn_NameComponentType_GENERIC,
590  int otherTypeCode = -1)
591  {
592  return append(value.buf(), value.size(), type, otherTypeCode);
593  }
594 
603  ndn_Error
604  append(const Component& component);
605 
613  ndn_Error
614  append(const NameLite& name);
615 
631  ndn_Error
632  append
633  (const char *value,
634  ndn_NameComponentType type = ndn_NameComponentType_GENERIC,
635  int otherTypeCode = -1);
636 
650  ndn_Error
651  appendSegment(uint64_t segment, uint8_t* buffer, size_t bufferLength);
652 
666  ndn_Error
668  (uint64_t segmentOffset, uint8_t* buffer, size_t bufferLength);
669 
684  ndn_Error
685  appendVersion(uint64_t version, uint8_t* buffer, size_t bufferLength);
686 
701  ndn_Error
702  appendTimestamp(uint64_t timestamp, uint8_t* buffer, size_t bufferLength);
703 
717  ndn_Error
719  (uint64_t sequenceNumber, uint8_t* buffer, size_t bufferLength);
720 
731  ndn_Error
732  appendImplicitSha256Digest(const uint8_t* digest, size_t digestLength);
733 
744  ndn_Error
746  {
747  return appendImplicitSha256Digest(digest.buf(), digest.size());
748  }
749 
760  ndn_Error
761  appendParametersSha256Digest(const uint8_t* digest, size_t digestLength);
762 
773  ndn_Error
775  {
776  return appendParametersSha256Digest(digest.buf(), digest.size());
777  }
778 
785  ndn_Error
786  set(const NameLite& other);
787 
793  const Component*
794  pop();
795 
801  static NameLite&
802  downCast(ndn_Name& name) { return *(NameLite*)&name; }
803 
804  static const NameLite&
805  downCast(const ndn_Name& name) { return *(NameLite*)&name; }
806 
807 private:
808  // Declare friends who can downcast to the private base.
809  friend class Tlv0_2WireFormatLite;
810 
815  NameLite(const NameLite& other);
816 
821  NameLite& operator=(const NameLite& other);
822 };
823 
824 }
825 
826 #endif
size_t size() const
Return size given to the constructor.
Definition: blob-lite.hpp:61
const Component * pop()
Remove and return the last name component.
Definition: name-lite.cpp:362
ndn_Error toTimestamp(uint64_t &result) const
Interpret this name component as a timestamp according to NDN naming conventions for "Timestamp" (mar...
Definition: name-lite.cpp:140
int compare(const Component &other) const
Compare this component to the other component using NDN component ordering.
Definition: name-lite.cpp:158
ndn_Error setImplicitSha256Digest(const uint8_t *digest, size_t digestLength)
Set this name component to have type ImplicitSha256DigestComponent with the given digest value...
Definition: name-lite.cpp:224
ndn_Error appendSegment(uint64_t segment, uint8_t *buffer, size_t bufferLength)
Append a component with the encoded segment number according to NDN naming conventions for "Segment n...
Definition: name-lite.cpp:295
uint64_t toNumber() const
Interpret the name component as a network-ordered number and return an integer.
Definition: name-lite.cpp:96
A NameLite holds an array of NameLite::Component.
Definition: name-lite.hpp:36
ndn_Error toNumberWithMarker(uint8_t marker, uint64_t &result) const
Interpret the name component as a network-ordered number with a marker and return an integer...
Definition: name-lite.cpp:102
bool match(const NameLite &name) const
Check if the N components of this name are the same as the first N components of the given name...
Definition: name-lite.cpp:248
ndn_Error appendSegmentOffset(uint64_t segmentOffset, uint8_t *buffer, size_t bufferLength)
Append a component with the encoded segment byte offset according to NDN naming conventions for segme...
Definition: name-lite.cpp:306
ndn_Error appendImplicitSha256Digest(const uint8_t *digest, size_t digestLength)
Append a component of type ImplicitSha256DigestComponent to this name with the given digest value...
Definition: name-lite.cpp:347
ndn_Error setParametersSha256Digest(const BlobLite &digest)
Set this name component to have type ParametersSha256DigestComponent with the given digest value...
Definition: name-lite.hpp:468
bool isVersion() const
Check if this component is a version number according to NDN naming conventions for "Versioning" (mar...
Definition: name-lite.cpp:60
bool isSegment() const
Check if this component is a segment number according to NDN naming conventions for "Segment number" ...
Definition: name-lite.cpp:48
ndn_Error setSequenceNumber(uint64_t sequenceNumber, uint8_t *buffer, size_t bufferLength)
Set this name component to have the encoded sequence number according to NDN naming conventions for "...
Definition: name-lite.cpp:216
static BlobLite & downCast(ndn_Blob &blob)
Downcast the reference to the ndn_Blob struct to a BlobLite.
Definition: blob-lite.hpp:86
struct ndn_NameComponent * components
pointer to the array of components.
Definition: name-types.h:57
ndn_Error append(const uint8_t *value, size_t valueLength, ndn_NameComponentType type=ndn_NameComponentType_GENERIC, int otherTypeCode=-1)
Append a component to this name with the bytes in the given buffer.
Definition: name-lite.cpp:258
const BlobLite & getValue() const
Get the component value.
Definition: name-lite.hpp:89
ndn_Error toNumberWithPrefix(const uint8_t *prefix, size_t prefixLength, uint64_t &result) const
Interpret the name component as a network-ordered number with a prefix and return an integer...
Definition: name-lite.cpp:109
ndn_Error set(const NameLite &other)
Set this name to have the values from the other name.
Definition: name-lite.cpp:359
ndn_Error setFromNumber(uint64_t number, uint8_t *buffer, size_t bufferLength, ndn_NameComponentType type=ndn_NameComponentType_GENERIC, int otherTypeCode=-1)
Set this name component to have a value which is the nonNegativeInteger encoding of the number...
Definition: name-lite.cpp:165
size_t nComponents
the number of components in the name
Definition: name-types.h:59
An ExcludeLite holds an array of ExcludeLite::Entry.
Definition: exclude-lite.hpp:33
An ndn_NameComponent holds a pointer to the component value.
Definition: name-types.h:47
bool isParametersSha256Digest() const
Check if this component is a ParametersSha256Digest component.
Definition: name-lite.cpp:90
struct ndn_Blob value
A Blob with a pointer to the pre-allocated buffer for the component value.
Definition: name-types.h:50
void clear()
Clear all the components.
Definition: name-lite.cpp:254
An ndn_Name holds an array of ndn_NameComponent.
Definition: name-types.h:56
A NameLite::Component holds a pointer to the component value.
Definition: name-lite.hpp:41
A BlobLite holds a pointer to an immutable pre-allocated buffer and its length This is like a JavaScr...
Definition: blob-lite.hpp:37
static Component & downCast(ndn_NameComponent &component)
Downcast the reference to the ndn_NameComponent struct to a NameLite::Component.
Definition: name-lite.hpp:479
bool hasPrefix(const uint8_t *prefix, size_t prefixLength) const
Check if this name component begins with the given prefix.
Definition: name-lite.cpp:116
bool isSequenceNumber() const
Check if this component is a sequence number according to NDN naming conventions for "Sequencing" (ma...
Definition: name-lite.cpp:72
ndn_Error appendParametersSha256Digest(const BlobLite &digest)
Append a component of type ParametersSha256DigestComponent to this name with the given digest value...
Definition: name-lite.hpp:774
bool equals(const Component &other) const
Check if this is the same component as other.
Definition: name-lite.cpp:152
const uint8_t * buf() const
Return buf given to the constructor.
Definition: blob-lite.hpp:55
bool isSegmentOffset() const
Check if this component is a segment byte offset according to NDN naming conventions for "Byte offset...
Definition: name-lite.cpp:54
size_t maxComponents
the number of elements in the allocated components array
Definition: name-types.h:58
ndn_Error setImplicitSha256Digest(const BlobLite &digest)
Set this name component to have type ImplicitSha256DigestComponent with the given digest value...
Definition: name-lite.hpp:441
ndn_Error setSegment(uint64_t segment, uint8_t *buffer, size_t bufferLength)
Set this name component to have the encoded segment number according to NDN naming conventions for "S...
Definition: name-lite.cpp:187
NameLite(ndn_NameComponent *components, size_t maxComponents)
Create a NameLite to use the components array.
Definition: name-lite.cpp:236
ndn_Error appendImplicitSha256Digest(const BlobLite &digest)
Append a component of type ImplicitSha256DigestComponent to this name with the given digest value...
Definition: name-lite.hpp:745
ndn_Error appendTimestamp(uint64_t timestamp, uint8_t *buffer, size_t bufferLength)
Append a component with the encoded timestamp according to NDN naming conventions for "Timestamp" (ma...
Definition: name-lite.cpp:326
static NameLite & downCast(ndn_Name &name)
Downcast the reference to the ndn_Name struct to a NameLite.
Definition: name-lite.hpp:802
ndn_NameComponentType getType() const
Get the name component type.
Definition: name-lite.hpp:98
ndn_Error toSegmentOffset(uint64_t &result) const
Interpret this name component as a segment byte offset according to NDN naming conventions for segmen...
Definition: name-lite.cpp:128
bool equals(const NameLite &name)
Check if this name has the same component count and components as the given name. ...
Definition: name-lite.cpp:242
bool isTimestamp() const
Check if this component is a timestamp according to NDN naming conventions for "Timestamp" (marker 0x...
Definition: name-lite.cpp:66
bool isGeneric() const
Check if this component is a generic component.
Definition: name-lite.cpp:78
ndn_Error appendParametersSha256Digest(const uint8_t *digest, size_t digestLength)
Append a component of type ParametersSha256DigestComponent to this name with the given digest value...
Definition: name-lite.cpp:353
int getOtherTypeCode() const
Get the component type code from the packet which is other than a recognized ndn_NameComponentType en...
Definition: name-lite.hpp:107
size_t size() const
Get the number of components.
Definition: name-lite.hpp:521
bool isImplicitSha256Digest() const
Check if this component is an ImplicitSha256Digest component.
Definition: name-lite.cpp:84
ndn_Error setSegmentOffset(uint64_t segmentOffset, uint8_t *buffer, size_t bufferLength)
Set this name component to have the encoded segment byte offset according to NDN naming conventions f...
Definition: name-lite.cpp:194
ndn_Error appendVersion(uint64_t version, uint8_t *buffer, size_t bufferLength)
Append a component with the encoded version number according to NDN naming conventions for "Versionin...
Definition: name-lite.cpp:316
ndn_Error toSequenceNumber(uint64_t &result) const
Interpret this name component as a sequence number according to NDN naming conventions for "Sequencin...
Definition: name-lite.cpp:146
ndn_Error setTimestamp(uint64_t timestamp, uint8_t *buffer, size_t bufferLength)
Set this name component to have the encoded timestamp according to NDN naming conventions for "Timest...
Definition: name-lite.cpp:209
ndn_Error appendSequenceNumber(uint64_t sequenceNumber, uint8_t *buffer, size_t bufferLength)
Append a component with the encoded sequence number according to NDN naming conventions for "Sequenci...
Definition: name-lite.cpp:337
ndn_Error setFromNumberWithMarker(uint64_t number, uint8_t marker, uint8_t *buffer, size_t bufferLength)
Set this name component to have a value which is the marker appended with the nonNegativeInteger enco...
Definition: name-lite.cpp:179
Component()
Create a GENERIC NameLite::Component of zero size.
Definition: name-lite.cpp:27
ndn_Error setVersion(uint64_t version, uint8_t *buffer, size_t bufferLength)
Set this name component to have the encoded version number according to NDN naming conventions for "V...
Definition: name-lite.cpp:202
ndn_Error toVersion(uint64_t &result) const
Interpret this name component as a version number according to NDN naming conventions for "Versioning...
Definition: name-lite.cpp:134
ndn_Error setParametersSha256Digest(const uint8_t *digest, size_t digestLength)
Set this name component to have type ParametersSha256DigestComponent with the given digest value...
Definition: name-lite.cpp:231
ndn_Error toSegment(uint64_t &result) const
Interpret this name component as a segment number according to NDN naming conventions for "Segment nu...
Definition: name-lite.cpp:122