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 
28 struct ndn_Blob;
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 
82  Blob(const Blob& blob)
83  : ptr_lib::shared_ptr<const std::vector<uint8_t> >(blob)
84  {
85  }
86 
95  Blob(const ptr_lib::shared_ptr<std::vector<uint8_t> > &value, bool copy)
96  : ptr_lib::shared_ptr<const std::vector<uint8_t> >
97  ((const ptr_lib::shared_ptr<const std::vector<uint8_t> > &)value)
98  {
99  if (copy)
100  this->reset(new std::vector<uint8_t>(*value));
101  }
102  Blob(const ptr_lib::shared_ptr<const std::vector<uint8_t> > &value, bool copy)
103  : ptr_lib::shared_ptr<const std::vector<uint8_t> >(value)
104  {
105  if (copy)
106  this->reset(new std::vector<uint8_t>(*value));
107  }
108 
113  DEPRECATED_IN_NDN_CPP Blob(const ptr_lib::shared_ptr<std::vector<uint8_t> > &value)
114  : ptr_lib::shared_ptr<const std::vector<uint8_t> >((const ptr_lib::shared_ptr<const std::vector<uint8_t> > &)value)
115  {
116  }
117  DEPRECATED_IN_NDN_CPP Blob(const ptr_lib::shared_ptr<const std::vector<uint8_t> > &value)
118  : ptr_lib::shared_ptr<const std::vector<uint8_t> >(value)
119  {
120  }
121 
125  size_t
126  size() const
127  {
128  if (*this)
129  return (*this)->size();
130  else
131  return 0;
132  }
133 
137  const uint8_t*
138  buf() const
139  {
140  if (*this)
141  return &(*this)->front();
142  else
143  return 0;
144  }
145 
150  bool
151  isNull() const { return !(*this); }
152 
158  std::string
159  toHex() const { return (*this) ? ndn::toHex(**this) : std::string(); }
160 
166  std::string
167  toRawStr() const
168  {
169  return (*this) ? std::string((const char*)(&(*this)->front()), (*this)->size())
170  : std::string();
171  }
172 
179  bool
180  equals(const Blob& other) const
181  {
182  if (isNull())
183  return other.isNull();
184  else if (other.isNull())
185  return false;
186  else
187  // Use the vector equals operator.
188  return (**this) == (*other);
189  }
190 
196  void
197  get(struct ndn_Blob& blobStruct) const;
198 };
199 
200 inline std::ostream&
201 operator << (std::ostream& os, const Blob& blob)
202 {
203  for (size_t i = 0; i < blob.size(); ++i)
204  os << blob.buf()[i];
205  return os;
206 }
207 
208 }
209 
210 #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:113
Copyright (C) 2013-2015 Regents of the University of California.
Definition: common.hpp:35
Copyright (C) 2015 Regents of the University of California.
bool isNull() const
Check if the array pointer is null.
Definition: blob.hpp:151
Blob(const Blob &blob)
Create a new Blob and take another pointer to the given blob's buffer.
Definition: blob.hpp:82
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:138
size_t size() const
Return the length of the immutable byte array.
Definition: blob.hpp:126
std::string toRawStr() const
Return the bytes of the byte array as a raw str of the same length.
Definition: blob.hpp:167
Blob()
Create a new Blob with a null pointer.
Definition: blob.hpp:47
Copyright (C) 2015 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:95
std::string toHex() const
Return the hex representation of the bytes in array.
Definition: blob.hpp:159
bool equals(const Blob &other) const
Check if the value of this Blob equals the other blob.
Definition: blob.hpp:180
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