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 : private ndn_Name {
37 public:
41  class Component : private ndn_NameComponent {
42  public:
46  Component();
47 
53  Component(const uint8_t* value, size_t valueLength);
54 
60  Component(const BlobLite& value);
61 
62  const BlobLite&
63  getValue() const { return BlobLite::downCast(value); }
64 
71  bool
72  isSegment() const;
73 
80  bool
81  isSegmentOffset() const;
82 
89  bool
90  isVersion() const;
91 
98  bool
99  isTimestamp() const;
100 
107  bool
108  isSequenceNumber() const;
109 
114  bool
115  isGeneric() const;
116 
121  bool
122  isImplicitSha256Digest() const;
123 
128  uint64_t
129  toNumber() const;
130 
139  ndn_Error
140  toNumberWithMarker(uint8_t marker, uint64_t& result) const;
141 
151  ndn_Error
153  (const uint8_t* prefix, size_t prefixLength, uint64_t& result) const;
154 
161  bool
162  hasPrefix(const uint8_t* prefix, size_t prefixLength) const;
163 
172  ndn_Error
173  toSegment(uint64_t& result) const;
174 
183  ndn_Error
184  toSegmentOffset(uint64_t& result) const;
185 
195  ndn_Error
196  toVersion(uint64_t& result) const;
197 
207  ndn_Error
208  toTimestamp(uint64_t& result) const;
209 
218  ndn_Error
219  toSequenceNumber(uint64_t& result) const;
220 
226  bool
227  equals(const Component& other) const;
228 
236  int
237  compare(const Component& other) const;
238 
252  ndn_Error
253  setFromNumber(uint64_t number, uint8_t* buffer, size_t bufferLength);
254 
269  ndn_Error
271  (uint64_t number, uint8_t marker, uint8_t* buffer, size_t bufferLength);
272 
286  ndn_Error
287  setSegment(uint64_t segment, uint8_t* buffer, size_t bufferLength);
288 
302  ndn_Error
304  (uint64_t segmentOffset, uint8_t* buffer, size_t bufferLength);
305 
321  ndn_Error
322  setVersion(uint64_t version, uint8_t* buffer, size_t bufferLength);
323 
338  ndn_Error
339  setTimestamp(uint64_t timestamp, uint8_t* buffer, size_t bufferLength);
340 
354  ndn_Error
356  (uint64_t sequenceNumber, uint8_t* buffer, size_t bufferLength);
357 
367  ndn_Error
368  setImplicitSha256Digest(const uint8_t* digest, size_t digestLength);
369 
378  ndn_Error
380  {
381  return setImplicitSha256Digest(digest.buf(), digest.size());
382  }
383 
389  static Component&
390  downCast(ndn_NameComponent& component) { return *(Component*)&component; }
391 
392  static const Component&
393  downCast(const ndn_NameComponent& component) { return *(Component*)&component; }
394 
395  private:
396  friend NameLite;
397  friend ExcludeLite;
398  };
399 
409 
416  const Component&
417  get(int i) const
418  {
419  // TODO: Range check.
420  if (i >= 0)
421  return Component::downCast(components[i]);
422  else
423  // Negative index.
425  }
426 
431  size_t
432  size() const { return nComponents; }
433 
441  bool
442  equals(const NameLite& name);
443 
451  bool
452  match(const NameLite& name) const;
453 
457  void
458  clear();
459 
468  ndn_Error
469  append(const uint8_t* value, size_t valueLength);
470 
478  ndn_Error
479  append(const BlobLite& value) { return append(value.buf(), value.size()); }
480 
489  ndn_Error
490  append(const NameLite::Component& component);
491 
499  ndn_Error
500  append(const char *value);
501 
515  ndn_Error
516  appendSegment(uint64_t segment, uint8_t* buffer, size_t bufferLength);
517 
531  ndn_Error
533  (uint64_t segmentOffset, uint8_t* buffer, size_t bufferLength);
534 
549  ndn_Error
550  appendVersion(uint64_t version, uint8_t* buffer, size_t bufferLength);
551 
566  ndn_Error
567  appendTimestamp(uint64_t timestamp, uint8_t* buffer, size_t bufferLength);
568 
582  ndn_Error
584  (uint64_t sequenceNumber, uint8_t* buffer, size_t bufferLength);
585 
596  ndn_Error
597  appendImplicitSha256Digest(const uint8_t* digest, size_t digestLength);
598 
609  ndn_Error
611  {
612  return appendImplicitSha256Digest(digest.buf(), digest.size());
613  }
614 
621  ndn_Error
622  set(const NameLite& other);
623 
629  static NameLite&
630  downCast(ndn_Name& name) { return *(NameLite*)&name; }
631 
632  static const NameLite&
633  downCast(const ndn_Name& name) { return *(NameLite*)&name; }
634 
635 private:
636  // Declare friends who can downcast to the private base.
637  friend class Tlv0_2WireFormatLite;
638 
643  NameLite(const NameLite& other);
644 
649  NameLite& operator=(const NameLite& other);
650 };
651 
652 }
653 
654 #endif
ndn_Error append(const uint8_t *value, size_t valueLength)
Append a GENERIC component to this name with the bytes in the given buffer.
Definition: name-lite.cpp:233
size_t size() const
Return size given to the constructor.
Definition: blob-lite.hpp:61
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:129
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:36
int compare(const Component &other) const
Compare this component to the other component using NDN component ordering.
Definition: name-lite.cpp:147
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:207
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:254
uint64_t toNumber() const
Interpret the name component as a network-ordered number and return an integer.
Definition: name-lite.cpp:85
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:91
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:224
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:265
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:306
bool isVersion() const
Check if this component is a version number according to NDN naming conventions for "Versioning" (mar...
Definition: name-lite.cpp:55
bool isSegment() const
Check if this component is a segment number according to NDN naming conventions for "Segment number" ...
Definition: name-lite.cpp:43
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:199
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:50
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:98
ndn_Error set(const NameLite &other)
Set this name to have the values from the other name.
Definition: name-lite.cpp:312
size_t nComponents
the number of components in the name
Definition: name-types.h:52
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:41
struct ndn_Blob value
A Blob with a pointer to the pre-allocated buffer for the component value.
Definition: name-types.h:43
void clear()
Clear all the components.
Definition: name-lite.cpp:230
An ndn_Name holds an array of ndn_NameComponent.
Definition: name-types.h:49
ndn_Error setFromNumber(uint64_t number, uint8_t *buffer, size_t bufferLength)
Set this name component to have a value which is the nonNegativeInteger encoding of the number...
Definition: name-lite.cpp:154
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:390
bool hasPrefix(const uint8_t *prefix, size_t prefixLength) const
Check if this name component begins with the given prefix.
Definition: name-lite.cpp:105
ndn_Error append(const BlobLite &value)
Append a GENERIC component to this name with the bytes in the given blob.
Definition: name-lite.hpp:479
bool isSequenceNumber() const
Check if this component is a sequence number according to NDN naming conventions for "Sequencing" (ma...
Definition: name-lite.cpp:67
bool equals(const Component &other) const
Check if this is the same component as other.
Definition: name-lite.cpp:141
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:49
size_t maxComponents
the number of elements in the allocated components array
Definition: name-types.h:51
ndn_Error setImplicitSha256Digest(const BlobLite &digest)
Set this name component to have type ImplicitSha256DigestComponent with the given digest value...
Definition: name-lite.hpp:379
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:170
NameLite(ndn_NameComponent *components, size_t maxComponents)
Create a NameLite to use the components array.
Definition: name-lite.cpp:212
ndn_Error appendImplicitSha256Digest(const BlobLite &digest)
Append a component of type ImplicitSha256DigestComponent to this name with the given digest value...
Definition: name-lite.hpp:610
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:285
static NameLite & downCast(ndn_Name &name)
Downcast the reference to the ndn_Name struct to a NameLite.
Definition: name-lite.hpp:630
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:117
bool equals(const NameLite &name)
Check if this name has the same component count and components as the given name. ...
Definition: name-lite.cpp:218
bool isTimestamp() const
Check if this component is a timestamp according to NDN naming conventions for "Timestamp" (marker 0x...
Definition: name-lite.cpp:61
bool isGeneric() const
Check if this component is a generic component.
Definition: name-lite.cpp:73
size_t size() const
Get the number of components.
Definition: name-lite.hpp:432
bool isImplicitSha256Digest() const
Check if this component is an ImplicitSha256Digest component.
Definition: name-lite.cpp:79
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:177
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:275
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:135
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:192
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:296
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:162
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:185
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:123
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:111