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 "../c/util/dynamic-uint8-array.h"
29 
30 namespace ndn {
31 
37 public:
42  DynamicUInt8Vector(size_t initialLength);
43 
50  void
52  {
53  ndn_Error error;
54  if ((error = ndn_DynamicUInt8Array_ensureLength(this, length)))
55  throw std::runtime_error(ndn_getErrorString(error));
56  }
57 
66  size_t
67  copy(const uint8_t *value, size_t valueLength, size_t offset)
68  {
69  ndn_Error error;
70  if ((error = ndn_DynamicUInt8Array_copy(this, value, valueLength, offset)))
71  throw std::runtime_error(ndn_getErrorString(error));
72 
73  return offset + valueLength;
74  }
75 
83  size_t
84  copy(const std::vector<uint8_t>& value, size_t offset)
85  {
86  return copy(&value[0], value.size(), offset);
87  }
88 
95  void
97  {
98  ndn_Error error;
99  if ((error = ndn_DynamicUInt8Array_ensureLengthFromBack(this, length)))
100  throw std::runtime_error(ndn_getErrorString(error));
101  }
102 
112  void
113  copyFromBack(const uint8_t *value, size_t valueLength, size_t offsetFromBack)
114  {
115  ndn_Error error;
116  if ((error = ndn_DynamicUInt8Array_copyFromBack
117  (this, value, valueLength, offsetFromBack)))
118  throw std::runtime_error(ndn_getErrorString(error));
119  }
120 
125  ptr_lib::shared_ptr<std::vector<uint8_t> >&
126  get() { return vector_; }
127 
128  uint8_t&
129  operator [] (size_t i) { return (*vector_)[i]; }
130 
131  const uint8_t&
132  operator [] (size_t i) const { return (*vector_)[i]; }
133 
134 private:
142  static uint8_t*
143  realloc(struct ndn_DynamicUInt8Array *self, uint8_t *array, size_t length);
144 
145  ptr_lib::shared_ptr<std::vector<uint8_t> > vector_;
146 };
147 
148 }
149 
150 #endif
Copyright (C) 2013-2015 Regents of the University of California.
Definition: common.hpp:35
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:84
void ensureLengthFromBack(size_t length)
Ensure that the vector size is greater than or equal to length.
Definition: dynamic-uint8-vector.hpp:96
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:67
void ensureLength(size_t length)
Ensure that the vector size is greater than or equal to length.
Definition: dynamic-uint8-vector.hpp:51
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:113
A DynamicUInt8Vector extends ndn_DynamicUInt8Array to hold a shared_ptr > for use wit...
Definition: dynamic-uint8-vector.hpp:36
uint8_t * array
the allocated array buffer
Definition: dynamic-uint8-array-types.h:41