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 
34 class NameLite : private ndn_Name {
35 public:
39  class Component : private ndn_NameComponent {
40  public:
44  Component();
45 
51  Component(const uint8_t* value, size_t valueLength);
52 
58  Component(const BlobLite& value);
59 
60  const BlobLite&
61  getValue() const { return BlobLite::downCast(value); }
62 
69  bool
70  isSegment() const;
71 
78  bool
79  isSegmentOffset() const;
80 
87  bool
88  isVersion() const;
89 
96  bool
97  isTimestamp() const;
98 
105  bool
106  isSequenceNumber() const;
107 
112  uint64_t
113  toNumber() const;
114 
123  ndn_Error
124  toNumberWithMarker(uint8_t marker, uint64_t& result) const;
125 
135  ndn_Error
137  (const uint8_t* prefix, size_t prefixLength, uint64_t& result) const;
138 
145  bool
146  hasPrefix(const uint8_t* prefix, size_t prefixLength) const;
147 
156  ndn_Error
157  toSegment(uint64_t& result) const;
158 
167  ndn_Error
168  toSegmentOffset(uint64_t& result) const;
169 
179  ndn_Error
180  toVersion(uint64_t& result) const;
181 
191  ndn_Error
192  toTimestamp(uint64_t& result) const;
193 
202  ndn_Error
203  toSequenceNumber(uint64_t& result) const;
204 
212  int compare(const Component& other) const;
213 
219  static Component&
220  downCast(ndn_NameComponent& component) { return *(Component*)&component; }
221 
222  static const Component&
223  downCast(const ndn_NameComponent& component) { return *(Component*)&component; }
224  };
225 
235 
242  const Component&
243  get(int i) const
244  {
245  // TODO: Range check.
246  if (i >= 0)
247  return Component::downCast(components[i]);
248  else
249  // Negative index.
251  }
252 
257  size_t
258  size() const { return nComponents; }
259 
267  bool
268  equals(const NameLite& name);
269 
277  bool
278  match(const NameLite& name) const;
279 
283  void
284  clear();
285 
294  ndn_Error
295  append(const uint8_t* value, size_t valueLength);
296 
304  ndn_Error
305  append(const BlobLite& value) { return append(value.buf(), value.size()); }
306 
314  ndn_Error
315  append(const NameLite::Component& component)
316  {
317  return append(component.getValue().buf(), component.getValue().size());
318  }
319 
327  ndn_Error
328  append(const char *value);
329 
343  ndn_Error
344  appendSegment(uint64_t segment, uint8_t* buffer, size_t bufferLength);
345 
359  ndn_Error
361  (uint64_t segmentOffset, uint8_t* buffer, size_t bufferLength);
362 
377  ndn_Error
378  appendVersion(uint64_t version, uint8_t* buffer, size_t bufferLength);
379 
394  ndn_Error
395  appendTimestamp(uint64_t timestamp, uint8_t* buffer, size_t bufferLength);
396 
410  ndn_Error
412  (uint64_t sequenceNumber, uint8_t* buffer, size_t bufferLength);
413 
420  ndn_Error
421  set(const NameLite& other);
422 
428  static NameLite&
429  downCast(ndn_Name& name) { return *(NameLite*)&name; }
430 
431  static const NameLite&
432  downCast(const ndn_Name& name) { return *(NameLite*)&name; }
433 
434 private:
435  // Declare friends who can downcast to the private base.
436  friend class Tlv0_1_1WireFormatLite;
437 
442  NameLite(const NameLite& other);
443 
448  NameLite& operator=(const NameLite& other);
449 };
450 
451 }
452 
453 #endif
ndn_Error append(const uint8_t *value, size_t valueLength)
Append a component to this name with the bytes in the given buffer.
Definition: name-lite.cpp:155
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:117
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
int compare(const Component &other) const
Compare this component to the other component using NDN component ordering.
Definition: name-lite.cpp:129
ndn_Error append(const NameLite::Component &component)
Append a component to this name with the bytes in the given component.
Definition: name-lite.hpp:315
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:164
uint64_t toNumber() const
Interpret the name component as a network-ordered number and return an integer.
Definition: name-lite.cpp:73
A NameLite holds an array of NameLite::Component.
Definition: name-lite.hpp:34
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:79
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:146
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:175
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
static BlobLite & downCast(ndn_Blob &blob)
Downcast the reference to the ndn_Blob struct to a BlobLite.
Definition: blob-lite.hpp:76
struct ndn_NameComponent * components
pointer to the array of components.
Definition: name-types.h:41
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:86
ndn_Error set(const NameLite &other)
Set this name to have the values from the other name.
Definition: name-lite.cpp:216
size_t nComponents
the number of components in the name
Definition: name-types.h:43
Copyright (C) 2015-2016 Regents of the University of California.
Definition: name-types.h:33
struct ndn_Blob value
A Blob with a pointer to the pre-allocated buffer for the component value.
Definition: name-types.h:34
void clear()
Clear all the components.
Definition: name-lite.cpp:152
An ndn_Name holds an array of ndn_NameComponent.
Definition: name-types.h:40
A NameLite::Component holds a pointer to the component value.
Definition: name-lite.hpp:39
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:220
bool hasPrefix(const uint8_t *prefix, size_t prefixLength) const
Check if this name component begins with the given prefix.
Definition: name-lite.cpp:93
ndn_Error append(const BlobLite &value)
Append a component to this name with the bytes in the given blob.
Definition: name-lite.hpp:305
bool isSequenceNumber() const
Check if this component is a sequence number according to NDN naming conventions for "Sequencing" (ma...
Definition: name-lite.cpp:67
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:42
NameLite(ndn_NameComponent *components, size_t maxComponents)
Create a NameLite to use the components array.
Definition: name-lite.cpp:134
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:195
static NameLite & downCast(ndn_Name &name)
Downcast the reference to the ndn_Name struct to a NameLite.
Definition: name-lite.hpp:429
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:105
bool equals(const NameLite &name)
Check if this name has the same component count and components as the given name. ...
Definition: name-lite.cpp:140
bool isTimestamp() const
Check if this component is a timestamp according to NDN naming conventions for "Timestamp" (marker 0x...
Definition: name-lite.cpp:61
size_t size() const
Get the number of components.
Definition: name-lite.hpp:258
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:185
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:123
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:206
Component()
Create a NameLite::Component of zero size.
Definition: name-lite.cpp:27
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:111
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:99