dynamic-uint8-vector.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_DYNAMIC_UCHAR_VECTOR_HPP
23 #define NDN_DYNAMIC_UCHAR_VECTOR_HPP
24 
25 #include <vector>
26 #include <stdexcept>
27 #include <ndn-cpp/common.hpp>
28 #include <ndn-cpp/util/blob.hpp>
29 #include "../c/util/dynamic-uint8-array.h"
30 
31 namespace ndn {
32 
38 public:
43  DynamicUInt8Vector(size_t initialLength);
44 
51  void
53  {
54  ndn_Error error;
55  if ((error = ndn_DynamicUInt8Array_ensureLength(this, length)))
56  throw std::runtime_error(ndn_getErrorString(error));
57  }
58 
67  size_t
68  copy(const uint8_t *value, size_t valueLength, size_t offset)
69  {
70  ndn_Error error;
71  if ((error = ndn_DynamicUInt8Array_copy(this, value, valueLength, offset)))
72  throw std::runtime_error(ndn_getErrorString(error));
73 
74  return offset + valueLength;
75  }
76 
84  size_t
85  copy(const std::vector<uint8_t>& value, size_t offset)
86  {
87  return copy(&value[0], value.size(), offset);
88  }
89 
96  void
98  {
99  ndn_Error error;
100  if ((error = ndn_DynamicUInt8Array_ensureLengthFromBack(this, length)))
101  throw std::runtime_error(ndn_getErrorString(error));
102  }
103 
113  void
114  copyFromBack(const uint8_t *value, size_t valueLength, size_t offsetFromBack)
115  {
116  ndn_Error error;
117  if ((error = ndn_DynamicUInt8Array_copyFromBack
118  (this, value, valueLength, offsetFromBack)))
119  throw std::runtime_error(ndn_getErrorString(error));
120  }
121 
126  ptr_lib::shared_ptr<std::vector<uint8_t> >&
127  get() { return vector_; }
128 
135  Blob
136  finish(size_t size)
137  {
138  vector_->resize(size);
139  Blob result(vector_, false);
140  vector_.reset();
141  return result;
142  }
143 
144  uint8_t&
145  operator [] (size_t i) { return (*vector_)[i]; }
146 
147  const uint8_t&
148  operator [] (size_t i) const { return (*vector_)[i]; }
149 
150 private:
158  static uint8_t*
159  realloc(struct ndn_DynamicUInt8Array *self, uint8_t *array, size_t length);
160 
161  ptr_lib::shared_ptr<std::vector<uint8_t> > vector_;
162 };
163 
164 }
165 
166 #endif
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
Blob finish(size_t size)
Resize the allocated vector to the given size, transfer the bytes to a Blob and return the Blob...
Definition: dynamic-uint8-vector.hpp:136
size_t copy(const std::vector< uint8_t > &value, size_t offset)
Copy value into the vector at offset, using ensureLength to make sure the vector has enough size...
Definition: dynamic-uint8-vector.hpp:85
A Blob holds a pointer to an immutable byte array implemented as const std::vector.
Definition: blob.hpp:42
void ensureLengthFromBack(size_t length)
Ensure that the vector size is greater than or equal to length.
Definition: dynamic-uint8-vector.hpp:97
A struct ndn_DynamicUInt8Array holds a pointer to an allocated array, the length of the allocated arr...
Definition: dynamic-uint8-array-types.h:40
size_t length
the length of the allocated array buffer
Definition: dynamic-uint8-array-types.h:42
DynamicUInt8Vector(size_t initialLength)
Create a new DynamicUInt8Vector with an initial length.
Definition: dynamic-uint8-vector.cpp:28
size_t copy(const uint8_t *value, size_t valueLength, size_t offset)
Copy value into the vector at offset, using ensureLength to make sure the vector has enough size...
Definition: dynamic-uint8-vector.hpp:68
void ensureLength(size_t length)
Ensure that the vector size is greater than or equal to length.
Definition: dynamic-uint8-vector.hpp:52
void copyFromBack(const uint8_t *value, size_t valueLength, size_t offsetFromBack)
First call ensureLengthFromBack to make sure the vector has offsetFromBack bytes. ...
Definition: dynamic-uint8-vector.hpp:114
A DynamicUInt8Vector extends ndn_DynamicUInt8Array to hold a shared_ptr > for use wit...
Definition: dynamic-uint8-vector.hpp:37
uint8_t * array
the allocated array buffer
Definition: dynamic-uint8-array-types.h:41