Public Member Functions | List of all members
ndn::Blob Class Reference

A Blob holds a pointer to an immutable byte array implemented as const std::vector<uint8_t>. More...

#include <blob.hpp>

Inheritance diagram for ndn::Blob:
ndn::SignedBlob

Public Member Functions

 Blob ()
 Create a new Blob with a null pointer.
 
 Blob (const uint8_t *value, size_t valueLength)
 Create a new Blob with an immutable copy of the given array. More...
 
 Blob (const std::vector< uint8_t > &value)
 Create a new Blob with an immutable copy of the array in the given vector. More...
 
 Blob (const struct ndn_Blob &blobStruct)
 Create a new Blob with an immutable copy of the array in the given Blob struct. More...
 
 Blob (const BlobLite &blobLite)
 Create a new Blob with an immutable copy of the array in blobLite. More...
 
 Blob (const Blob &blob)
 Create a new Blob and take another pointer to the given blob's buffer. More...
 
 Blob (const ptr_lib::shared_ptr< std::vector< uint8_t > > &value, bool copy)
 Create a new Blob to point to an existing byte array. More...
 
 Blob (const ptr_lib::shared_ptr< const std::vector< uint8_t > > &value, bool copy)
 
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. More...
 
DEPRECATED_IN_NDN_CPP Blob (const ptr_lib::shared_ptr< const std::vector< uint8_t > > &value)
 
size_t size () const
 Return the length of the immutable byte array.
 
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.
 
bool isNull () const
 Check if the array pointer is null. More...
 
std::string toHex () const
 Return the hex representation of the bytes in array. More...
 
std::string toRawStr () const
 Return the bytes of the byte array as a raw str of the same length. More...
 
bool equals (const Blob &other) const
 Check if the value of this Blob equals the other blob. More...
 
void get (struct ndn_Blob &blobStruct) const
 Set the blobStruct to point to this Blob's byte array, without copying any memory. More...
 
 operator const BlobLite () const
 

Detailed Description

A Blob holds a pointer to an immutable byte array implemented as const std::vector<uint8_t>.

This is like a JavaScript string which is a pointer to an immutable string. It is OK to pass a pointer to the string because the new owner can't change the bytes of the string. However, like a JavaScript string, it is possible to change the pointer, and so this does allow the copy constructor and assignment to change the pointer. Also remember that the pointer can be null. (Note that we could have made Blob derive directly from vector<uint8_t> and then explicitly use a pointer to it like Blob, but this does not enforce immutability because we can't declare Blob as derived from const vector<uint8_t>.)

Constructor & Destructor Documentation

ndn::Blob::Blob ( const uint8_t *  value,
size_t  valueLength 
)
inline

Create a new Blob with an immutable copy of the given array.

Parameters
valueA pointer to the byte array which is copied. However, if value is 0 then create a Blob where isNull() is true.
valueLengthThe length of value.
ndn::Blob::Blob ( const std::vector< uint8_t > &  value)
inline

Create a new Blob with an immutable copy of the array in the given vector.

If you want to transfer the array without copying, the the vector has to start as a ptr_lib::shared_ptr<std::vector<uint8_t> > and you can use the Blob constructor with this type.

Parameters
valueA reference to a vector which is copied.
ndn::Blob::Blob ( const struct ndn_Blob & blobStruct  )
inline

Create a new Blob with an immutable copy of the array in the given Blob struct.

Parameters
blobStructThe C ndn_Blob struct to receive the pointer. However, if blobStruct.value is 0 then create a Blob where isNull() is true.
ndn::Blob::Blob ( const BlobLite blobLite)
inline

Create a new Blob with an immutable copy of the array in blobLite.

Parameters
blobStructThe C ndn_Blob struct to receive the pointer. However, if blobLite.isNull() then create a Blob where isNull() is true.
ndn::Blob::Blob ( const Blob blob)
inline

Create a new Blob and take another pointer to the given blob's buffer.

Parameters
blobThe Blob from which we take another pointer to the same buffer.
ndn::Blob::Blob ( const ptr_lib::shared_ptr< std::vector< uint8_t > > &  value,
bool  copy 
)
inline

Create a new Blob to point to an existing byte array.

IMPORTANT: If copy is false, after calling this constructor, if you keep a pointer to the array then you must treat the array as immutable and promise not to change it.

Parameters
valueA pointer to a vector with the byte array.
copyIf true, copy the value into a new vector. Otherwise, take another reference and do not copy the bytes.
DEPRECATED_IN_NDN_CPP ndn::Blob::Blob ( const ptr_lib::shared_ptr< std::vector< uint8_t > > &  value)
inline

Create a new Blob to point to an existing byte array.

Deprecated:
Use the constructor with an explicit copy parameter.

Member Function Documentation

bool ndn::Blob::equals ( const Blob other) const
inline

Check if the value of this Blob equals the other blob.

Parameters
otherThe other Blob to check.
Returns
True if this isNull and other isNull or if the bytes of this blob equal the bytes of the other.
void ndn::Blob::get ( struct ndn_Blob & blobStruct  ) const
inline

Set the blobStruct to point to this Blob's byte array, without copying any memory.

WARNING: The resulting pointer in blobStruct is invalid after a further use of this object which could reallocate memory.

Parameters
blobStructThe C ndn_Blob struct to receive the pointer.
bool ndn::Blob::isNull ( ) const
inline

Check if the array pointer is null.

Returns
true if the buffer pointer is null, otherwise false.
std::string ndn::Blob::toHex ( ) const
inline

Return the hex representation of the bytes in array.

Returns
The hex bytes as a string, or an empty string if the pointer is null.
std::string ndn::Blob::toRawStr ( ) const
inline

Return the bytes of the byte array as a raw str of the same length.

This does not do any character encoding such as UTF-8.

Returns
The buffer as a string, or "" if isNull().

The documentation for this class was generated from the following file: