blob.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_BLOB_HPP
23 #define NDN_BLOB_HPP
24 
25 #include <iostream>
26 #include "../common.hpp"
27 #include "../c/util/blob-types.h"
28 #include "../lite/util/blob-lite.hpp"
29 
30 namespace ndn {
31 
42 class Blob : public ptr_lib::shared_ptr<const std::vector<uint8_t> > {
43 public:
47  Blob()
48  {
49  }
50 
57  Blob(const uint8_t* value, size_t valueLength)
58  : ptr_lib::shared_ptr<const std::vector<uint8_t> >
59  (value ? new std::vector<uint8_t>(value, value + valueLength) : 0)
60  {
61  }
62 
69  Blob(const std::vector<uint8_t> &value)
70  : ptr_lib::shared_ptr<const std::vector<uint8_t> >(new std::vector<uint8_t>(value))
71  {
72  }
73 
79  Blob(const struct ndn_Blob& blobStruct)
80  : ptr_lib::shared_ptr<const std::vector<uint8_t> >
81  (blobStruct.value ?
82  new std::vector<uint8_t>(blobStruct.value, blobStruct.value + blobStruct.length)
83  : 0)
84  {
85  }
86 
92  Blob(const BlobLite& blobLite)
93  : ptr_lib::shared_ptr<const std::vector<uint8_t> >
94  (blobLite.isNull() ? 0
95  : new std::vector<uint8_t>(blobLite.buf(), blobLite.buf() + blobLite.size()))
96  {
97  }
98 
103  Blob(const Blob& blob)
104  : ptr_lib::shared_ptr<const std::vector<uint8_t> >(blob)
105  {
106  }
107 
116  Blob(const ptr_lib::shared_ptr<std::vector<uint8_t> > &value, bool copy)
117  : ptr_lib::shared_ptr<const std::vector<uint8_t> >
118  ((const ptr_lib::shared_ptr<const std::vector<uint8_t> > &)value)
119  {
120  if (copy)
121  this->reset(new std::vector<uint8_t>(*value));
122  }
123  Blob(const ptr_lib::shared_ptr<const std::vector<uint8_t> > &value, bool copy)
124  : ptr_lib::shared_ptr<const std::vector<uint8_t> >(value)
125  {
126  if (copy)
127  this->reset(new std::vector<uint8_t>(*value));
128  }
129 
134  DEPRECATED_IN_NDN_CPP Blob(const ptr_lib::shared_ptr<std::vector<uint8_t> > &value)
135  : ptr_lib::shared_ptr<const std::vector<uint8_t> >((const ptr_lib::shared_ptr<const std::vector<uint8_t> > &)value)
136  {
137  }
138  DEPRECATED_IN_NDN_CPP Blob(const ptr_lib::shared_ptr<const std::vector<uint8_t> > &value)
139  : ptr_lib::shared_ptr<const std::vector<uint8_t> >(value)
140  {
141  }
142 
146  size_t
147  size() const
148  {
149  if (*this)
150  return (*this)->size();
151  else
152  return 0;
153  }
154 
158  const uint8_t*
159  buf() const
160  {
161  if (*this)
162  return &(*this)->front();
163  else
164  return 0;
165  }
166 
171  bool
172  isNull() const { return !(*this); }
173 
179  std::string
180  toHex() const { return (*this) ? ndn::toHex(**this) : std::string(); }
181 
187  std::string
188  toRawStr() const
189  {
190  return (*this) ? std::string((const char*)(&(*this)->front()), (*this)->size())
191  : std::string();
192  }
193 
200  bool
201  equals(const Blob& other) const
202  {
203  if (isNull())
204  return other.isNull();
205  else if (other.isNull())
206  return false;
207  else
208  // Use the vector equals operator.
209  return (**this) == (*other);
210  }
211 
217  void
218  get(struct ndn_Blob& blobStruct) const
219  {
220  blobStruct.length = size();
221  blobStruct.value = (size() > 0 ? buf() : 0);
222  }
223 
224  operator const BlobLite() const
225  {
226  if (*this)
227  return BlobLite(&(*this)->front(), (*this)->size());
228  else
229  return BlobLite();
230  }
231 };
232 
233 inline std::ostream&
234 operator << (std::ostream& os, const Blob& blob)
235 {
236  for (size_t i = 0; i < blob.size(); ++i)
237  os << blob.buf()[i];
238  return os;
239 }
240 
241 }
242 
243 #endif
DEPRECATED_IN_NDN_CPP Blob(const ptr_lib::shared_ptr< std::vector< uint8_t > > &value)
Create a new Blob to point to an existing byte array.
Definition: blob.hpp:134
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
Blob(const BlobLite &blobLite)
Create a new Blob with an immutable copy of the array in blobLite.
Definition: blob.hpp:92
Copyright (C) 2015-2016 Regents of the University of California.
bool isNull() const
Check if the array pointer is null.
Definition: blob.hpp:172
Blob(const Blob &blob)
Create a new Blob and take another pointer to the given blob's buffer.
Definition: blob.hpp:103
Blob(const struct ndn_Blob &blobStruct)
Create a new Blob with an immutable copy of the array in the given Blob struct.
Definition: blob.hpp:79
A Blob holds a pointer to an immutable byte array implemented as const std::vector.
Definition: blob.hpp:42
const uint8_t * buf() const
Return a const pointer to the first byte of the immutable byte array, or 0 if the pointer is null...
Definition: blob.hpp:159
size_t size() const
Return the length of the immutable byte array.
Definition: blob.hpp:147
std::string toRawStr() const
Return the bytes of the byte array as a raw str of the same length.
Definition: blob.hpp:188
Blob()
Create a new Blob with a null pointer.
Definition: blob.hpp:47
A BlobLite holds a pointer to an immutable pre-allocated buffer and its length This is like a JavaScr...
Definition: blob-lite.hpp:37
Copyright (C) 2015-2016 Regents of the University of California.
Definition: blob-types.h:33
std::string toHex(const uint8_t *array, size_t arrayLength)
Return the hex representation of the bytes in array.
Definition: common.cpp:30
Blob(const uint8_t *value, size_t valueLength)
Create a new Blob with an immutable copy of the given array.
Definition: blob.hpp:57
Blob(const ptr_lib::shared_ptr< std::vector< uint8_t > > &value, bool copy)
Create a new Blob to point to an existing byte array.
Definition: blob.hpp:116
std::string toHex() const
Return the hex representation of the bytes in array.
Definition: blob.hpp:180
bool equals(const Blob &other) const
Check if the value of this Blob equals the other blob.
Definition: blob.hpp:201
Blob(const std::vector< uint8_t > &value)
Create a new Blob with an immutable copy of the array in the given vector.
Definition: blob.hpp:69