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 
56  Blob(const uint8_t* value, size_t valueLength)
57  : ptr_lib::shared_ptr<const std::vector<uint8_t> >(new std::vector<uint8_t>(value, value + valueLength))
58  {
59  }
60 
67  Blob(const std::vector<uint8_t> &value)
68  : ptr_lib::shared_ptr<const std::vector<uint8_t> >(new std::vector<uint8_t>(value))
69  {
70  }
71 
76  Blob(const struct ndn_Blob& blobStruct)
77  : ptr_lib::shared_ptr<const std::vector<uint8_t> >
78  (new std::vector<uint8_t>(blobStruct.value, blobStruct.value + blobStruct.length))
79  {
80  }
81 
86  Blob(const BlobLite& blobLite)
87  : ptr_lib::shared_ptr<const std::vector<uint8_t> >
88  (new std::vector<uint8_t>(blobLite.buf(), blobLite.buf() + blobLite.size()))
89  {
90  }
91 
96  Blob(const Blob& blob)
97  : ptr_lib::shared_ptr<const std::vector<uint8_t> >(blob)
98  {
99  }
100 
109  Blob(const ptr_lib::shared_ptr<std::vector<uint8_t> > &value, bool copy)
110  : ptr_lib::shared_ptr<const std::vector<uint8_t> >
111  ((const ptr_lib::shared_ptr<const std::vector<uint8_t> > &)value)
112  {
113  if (copy)
114  this->reset(new std::vector<uint8_t>(*value));
115  }
116  Blob(const ptr_lib::shared_ptr<const std::vector<uint8_t> > &value, bool copy)
117  : ptr_lib::shared_ptr<const std::vector<uint8_t> >(value)
118  {
119  if (copy)
120  this->reset(new std::vector<uint8_t>(*value));
121  }
122 
127  DEPRECATED_IN_NDN_CPP Blob(const ptr_lib::shared_ptr<std::vector<uint8_t> > &value)
128  : ptr_lib::shared_ptr<const std::vector<uint8_t> >((const ptr_lib::shared_ptr<const std::vector<uint8_t> > &)value)
129  {
130  }
131  DEPRECATED_IN_NDN_CPP Blob(const ptr_lib::shared_ptr<const std::vector<uint8_t> > &value)
132  : ptr_lib::shared_ptr<const std::vector<uint8_t> >(value)
133  {
134  }
135 
139  size_t
140  size() const
141  {
142  if (*this)
143  return (*this)->size();
144  else
145  return 0;
146  }
147 
151  const uint8_t*
152  buf() const
153  {
154  if (*this)
155  return &(*this)->front();
156  else
157  return 0;
158  }
159 
164  bool
165  isNull() const { return !(*this); }
166 
172  std::string
173  toHex() const { return (*this) ? ndn::toHex(**this) : std::string(); }
174 
180  std::string
181  toRawStr() const
182  {
183  return (*this) ? std::string((const char*)(&(*this)->front()), (*this)->size())
184  : std::string();
185  }
186 
193  bool
194  equals(const Blob& other) const
195  {
196  if (isNull())
197  return other.isNull();
198  else if (other.isNull())
199  return false;
200  else
201  // Use the vector equals operator.
202  return (**this) == (*other);
203  }
204 
210  void
211  get(struct ndn_Blob& blobStruct) const
212  {
213  blobStruct.length = size();
214  blobStruct.value = (size() > 0 ? buf() : 0);
215  }
216 
217  operator const BlobLite() const
218  {
219  if (*this)
220  return BlobLite(&(*this)->front(), (*this)->size());
221  else
222  return BlobLite();
223  }
224 };
225 
226 inline std::ostream&
227 operator << (std::ostream& os, const Blob& blob)
228 {
229  for (size_t i = 0; i < blob.size(); ++i)
230  os << blob.buf()[i];
231  return os;
232 }
233 
234 }
235 
236 #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:127
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:86
Copyright (C) 2015-2016 Regents of the University of California.
bool isNull() const
Check if the array pointer is null.
Definition: blob.hpp:165
Blob(const Blob &blob)
Create a new Blob and take another pointer to the given blob's buffer.
Definition: blob.hpp:96
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:76
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:152
size_t size() const
Return the length of the immutable byte array.
Definition: blob.hpp:140
std::string toRawStr() const
Return the bytes of the byte array as a raw str of the same length.
Definition: blob.hpp:181
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:56
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:109
std::string toHex() const
Return the hex representation of the bytes in array.
Definition: blob.hpp:173
bool equals(const Blob &other) const
Check if the value of this Blob equals the other blob.
Definition: blob.hpp:194
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:67